openapi-ts icon indicating copy to clipboard operation
openapi-ts copied to clipboard

Generated client has conflicting request when OpenApi spec has "Request" tag

Open schehlmj opened this issue 1 year ago • 7 comments

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

schehlmj avatar Apr 23 '24 17:04 schehlmj

Hey @schehlmj, are you able to share your config? It looks like you're using a custom client instance? Can I ask why?

mrlubos avatar Apr 23 '24 17:04 mrlubos

I am generating with this: npx @hey-api/openapi-ts --input ./app.json --output ./src/services/app-client-lib --client axios --name AppClient

schehlmj avatar Apr 23 '24 18:04 schehlmj

Why do you use --name?

mrlubos avatar Apr 23 '24 19:04 mrlubos

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);

schehlmj avatar Apr 23 '24 19:04 schehlmj

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 avatar Apr 23 '24 19:04 mrlubos

@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.

schehlmj avatar Apr 23 '24 23:04 schehlmj

Yep they will!

mrlubos avatar Apr 24 '24 04:04 mrlubos

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 avatar Jul 21 '24 21:07 mrlubos

@mrlubos It looks good. Thanks for your hard work on all this!

schehlmj avatar Jul 28 '24 01:07 schehlmj