openapi-ts
openapi-ts copied to clipboard
Generated client has conflicting request when OpenApi spec has "Request" tag
Description
When the OpenApi Spec has "Request" tag, the generated client.ts contains both of these: public readonly request: RequestService; public readonly request: BaseHttpRequest;
These cause compile error in the client.ts.
My workaround is to rename this and all references to it: public readonly httpRequest: BaseHttpRequest;
OpenAPI specification (optional)
"/api/v1/request/summary/{requestId}": {
"get": {
"tags": [
"Request",
],
....
Configuration
No response
System information (optional)
No response
Hey @schehlmj, are you able to share your config? It looks like you're using a custom client instance? Can I ask why?
I am generating with this:
npx @hey-api/openapi-ts --input ./app.json --output ./src/services/app-client-lib --client axios --name AppClient
Why do you use --name?
I now see that it is deprecated in @next. I am using it as a carryover from before, but I am not tied to it. I am not sure what I should do instead. As a simple fix, I can just not use --name, and then just keep using the old generated AppClient.ts as my own src code. I don't see a way to just use the Services directly because I am not clear how I would authenticate.
My current code has an app-client.ts:
import { AppClient } from './app-client-lib';
import { useSessionStore } from '../stores/session'; // pinia
const sessionStore = useSessionStore();
export const appClient = new AppClient({
TOKEN: async () => {
if (sessionStore.bypassAuth === true) {
console.log('Bypassing adding Authorization header Bearer token');
return '';
} else if (sessionStore.isAuthenticated !== true) {
throw 'Not authenticated';
} else {
return sessionStore.token || '';
}
},
});
Then I would just import appClient from that and use it like appClient.request.summary(requestId);
If you're using it only for authorisation, you can set that function directly on OpenAPI config object https://github.com/ferdikoomen/openapi-typescript-codegen/wiki/Authorization
We plan to improve this in the clients release too
@mrlubos and @jordanshatford Thanks for you attention and help with this.
One other nice thing about --name generated clients is that each instance of them can point to a different base URL. That isn't so easy to do with setting the OpenAPI singleton. Hopefully the clients release will handle that if --name generate clients are removed.
Yep they will!
Hey @schehlmj, the new Axios client is available now https://www.npmjs.com/package/@hey-api/client-axios
Docs: https://heyapi.vercel.app/openapi-ts/clients.html#axios
Let me know your thoughts if you do try it out
@mrlubos It looks good. Thanks for your hard work on all this!