brillout / telefunc

Remote Functions. Instead of API.
https://telefunc.com
MIT License
690 stars 31 forks source link

Consume telefunc end point from another telefunc apps ? #63

Closed fortezhuo closed 9 months ago

fortezhuo commented 1 year ago

Hi, I just dig into telefunc documentation. I am curious how to share telefunc end point between two apps.

Let's say we have 2 apps that developed using telefunc, eg : Master Data and Attendance. How the Attendance app get employee from Master Data without rewrite / copy paste Master Data's telefunc ?

Same question I had asked to trpc's discussion too https://github.com/trpc/trpc/discussions/2496

brillout commented 1 year ago

You can do that by setting config.telefuncUrl to an absolute URL, see https://telefunc.com/telefuncUrl.

brillout commented 1 year ago

Closing, but let me know if you have further questions.

Irrelon commented 9 months ago

If you set it to an absolute URL, there are complaints from validateUserConfig() in /node/server/serverConfig

image

I have this exact specific use case so I tried to set the telefuncUrl to http://localhost:3000/_telefunc where I have a telefunc server running but it doesn't let you.

If you comment out that assertion line the app will start but then attempting to use the telefunc function fails with:

Error: [telefunc@0.1.70][Wrong Usage] telefunc({ url }): The pathname of `url` is `/_telefunc` but it's expected to be `http://localhost:3000/_telefunc`. Either make sure that `url` is the HTTP request URL, or set `config.telefuncUrl` to `/_telefunc`.
    at assertUrl 

Commenting a final line (below) fixes the issue but I hate patching node modules unless I have to... is there are reason they urls were asserted like this that I'm not aware of and would break something?

image
brillout commented 9 months ago

@Irrelon Let me fix those assertions.

Irrelon commented 9 months ago

Actually I spoke too soon. On the client-side the telefunc call seems not to respect the telefuncUrl config setting. I'm 3-hours new to vike and telefunc so I could entirely be being a numpty though, but when the page loads in the browser and I trigger an event that calls a telefunc action, the post request is going to the browser's current domain, not the one I set in the telefuncUrl config entry.

brillout commented 9 months ago

Did you apply it twice? Once for the server-side import { config } from 'telefunc' and a second time for the client-side import { config } from 'telefunc/client'.

Irrelon commented 9 months ago

Did you apply it twice? Once for the server-side import { config } from 'telefunc' and a second time for the client-side import { config } from 'telefunc/client'.

I set up vike via Bati and modified the vite.config.ts file to add the telefuncUrl like this:

image

When I read the telefunc docs for this config option https://telefunc.com/telefuncUrl, the location of the config files that should be modified was unclear. Do I just create two new files? Where in the tree should they go? What should they be named? I'm sure I'm missing something obvious or likely something auto-magically abstracted by the vike-react plugin.

brillout commented 9 months ago

Actually, I think the assertion is correct. On the server-side set config.telefuncUrl to the URL pathname (i.e. starting with /), and on the client-side set config.telefuncUrl to the absolute URL (i.e. starting with http).

I'll improve the assertion error message and the docs tomorrow.