ecyrbe / zodios

typescript http client and server with zod validation
https://www.zodios.org/
MIT License
1.71k stars 46 forks source link

Is there any possible way to override the baseUrl to one specific endpoint ? #279

Closed lucas-santosP closed 1 year ago

lucas-santosP commented 1 year ago

The use case for that is that sometimes you need to add an API call to external APIs with completely different base URL but its not complex enough to be worthy make another ZodiosInstance just for that one endpoint. So would be really neat if was possible to override the baseUrl in the endpoint definition, for example:

const apiClient = new Zodios("https://my-app-api.com", [
  {
    method: "get",
    baseUrl: "https://external-api.com", // override the default baseUrl. It will call the URL: https://external-api.com/address/:zipcode
    path: "/address/:zipcode",
    response: z.object({ street: z.string(), city: z.string() }),
  },
  // others endpoints following the default baseUrl
  {
    method: "get",
    path: "/profile",
    response: z.object({ id: z.string(), name: z.string(), email: z.string() }),
  },
]);

Reading the issue #184 I saw that using plugins is possible to change the baseURL, but with my understanding it seems that would be really annoying to do that for each specific endpoint in this use case.

Thanks for your good work here, outstanding library!

ecyrbe commented 1 year ago

You can add a plugin that listen to only one endpoint and override the url only for that endpoint :

client.use('get','/address/:zipcode', urlPlugin('https:://external-api.com')); //you still need to create the plugin

cf docs: http://www.zodios.org/docs/client/plugins#use-a-plugin-only-for-some-endpoints would this answer your use case ? and if you make the plugin, i would be happy to include it in @zodios/plugins

lucas-santosP commented 1 year ago

Great, I would be glad to do that. Should I open the PR on this repo or in zodios-plugins?

ecyrbe commented 1 year ago

If you want to have it fast merged, it's better to create it for zodios-plugins. If not, you can create a PR here for v11 branch and it will be released as v11 beta release. Be carrefull, v11 branch is a monorepo with turbo repo and pnpm. You might have difficulties to set it up, if you are not familliar with monorepos.

ecyrbe commented 1 year ago

thank you, it's now available on @zodios/plugins v10.4.0