orval-labs / orval

orval is able to generate client with appropriate type-signatures (TypeScript) from any valid OpenAPI v3 or Swagger v2 specification, either in yaml or json formats. 🍺
https://orval.dev
MIT License
2.85k stars 318 forks source link

No import statements for HTTP exceptions (react-query) #1267

Open ANTOSzbk opened 6 months ago

ANTOSzbk commented 6 months ago

What are the steps to reproduce this issue?

  1. Have Exception defined in your Swagger API like this: image
  2. Generate react-query client code from Swagger docs using npx orval and config:
    export default defineConfig({
    myproject: {
    input: './api/docs.json',
    output: {
      mode: 'tags',
      target: './src/queries',
      schemas: './api/schemas',
      client: 'react-query',
      mock: true,
      override: {
        mutator: {
          path: './src/lib/api/client.ts',
          name: 'axiosClient',
        },
        query: {
          queryOptions: {
            path: './src/lib/api/queries.ts',
            name: 'paginationQueryOptions',
          },
          signal: true,
          useQuery: true,
          useInfinite: true,
          useInfiniteQueryParam: 'page',
        },
      },
      tsconfig: './tsconfig.json',
      clean: true,
    },
    },
    });

What happens?

Orval understands there are exceptions schemas and creates them createGroupChannel401.ts, createGroupChannel409.ts, uses them within react-query code but does not import them from schemas directory.

export type CreateGroupChannelMutationResult = NonNullable<Awaited<ReturnType<typeof createGroupChannel>>>
export type CreateGroupChannelMutationBody = BodyType<CreateGroupChannelDto>
/* no import statements for exceptions below */
export type CreateGroupChannelMutationError = ErrorType<CreateGroupChannel400 | CreateGroupChannel401 | CreateGroupChannel409>

What were you expecting to happen?

Create import statements for exceptions.

Any logs, error output, etc?

No specific errors.

What versions are you using?

Operating System: MacOS Sonoma 14.3.1 (M3 Pro) Package Version: 6.25.0 Browser Version: Chrome 122.0.6261.129

ANTOSzbk commented 6 months ago

I managed to create temporary workaround using npm library patch-package on @orval/query, but I suppose correct patch should be applied to @orval/core package.

diff --git a/node_modules/@orval/query/dist/index.js b/node_modules/@orval/query/dist/index.js
index c8621a2..66842f9 100644
--- a/node_modules/@orval/query/dist/index.js
+++ b/node_modules/@orval/query/dist/index.js
@@ -2520,6 +2520,10 @@ var generateQueryHook = async ({
       ...queryKeyMutator ? [queryKeyMutator] : []
     ] : void 0;
   }
+  const exceptionImports = response.definition.errors.split('|').map(value => value.trim()).filter(value => value !== 'void' && value !== 'unknown' && value !== 'null' && value !== 'undefined');
+  if(exceptionImports.length > 0) {
+    implementation += `import { ${exceptionImports.join(', ') } } from '../../api/schemas';\n`;
+  }
   let isMutation = verb !== import_core2.Verbs.GET && override.query.useMutation;
   if ((operationQueryOptions == null ? void 0 : operationQueryOptions.useMutation) !== void 0) {
     isMutation = operationQueryOptions.useMutation;
melloware commented 6 months ago

Please submit a PR!

ANTOSzbk commented 6 months ago

@melloware Wouldn't that be an issue on other clients? I cannot find code responsible for generating imports inside @orval/query and compiled @orval/core is too hard to read

melloware commented 6 months ago

If I had to guess yes it's a problem for all clients if the code is in core.

melloware commented 6 months ago

Adding @soartec-lab for thoughts