Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

base url handling issue #2

Closed
gaohoward opened this issue Sep 3, 2024 · 3 comments
Closed

base url handling issue #2

gaohoward opened this issue Sep 3, 2024 · 3 comments

Comments

@gaohoward
Copy link

gaohoward commented Sep 3, 2024

Hi I'm trying out apigen-ts to generate the client for an api-server but I got problem running it.
The openapi def is here
https://github.com/artemiscloud/activemq-artemis-jolokia-api-server/blob/main/src/config/openapi.yml

After generation, in my code I do

const api = new ApiClient({
  baseUrl: apiServerUrl + "/api/v1/",
})

Then I invoke one of the api method

api.development.apiInfo().then((response)=>{
    console.log('response: ', response);
  }).catch((ex) => {
    console.log('error', ex);
  });

I got 404 error. The reason seems to be in the generated client code the fetch url is not correctly formed
For example if my api-server listens on https://localhost:9443, the baseUrl I passed in ApiClient is https://localhost:9443/api/v1/ , inside the client code when handling the method call, it uses URL constructor

const url = new URL('/api-info', 'https://localhost:9443/api/v1/')

It turns out the new url removed the path from base url before appending the path (/api-info), like this

(new url) https://localhost:9443/api-info

but the correct url should be https://localhost:9443/api/v1/api-info.

@gaohoward
Copy link
Author

The version is ^0.1.2

@vladkens
Copy link
Owner

vladkens commented Sep 3, 2024

@gaohoward Hi. Thanks for the issues. URL resolving depends on the behavior of the new URL constructor, so I can't change at the level of current interfaces (might break backwards compatibility somewhere).

Instead I added the ability to override endpoint build logic (kind of hook).

class MyClient extends ApiClient {
  PrepareFetchUrl(path: string) {
    return new URL(`${this.Config.baseUrl}/${path}`.replace(/\/{2,}/g, "/"))
  }
}

const api = new MyClient({ baseUrl: "https://example.com/v1" })
// will call: https://example.com/v1/pet/ instead of https://example.com/pet/
const pet = await api.pet.getPetById(404)

I hope this helps you. Released as version 0.2.0

@gaohoward
Copy link
Author

Thank you @vladkens !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants