orval icon indicating copy to clipboard operation
orval copied to clipboard

No import statements for HTTP exceptions (react-query)

Open ANTOSzbk opened this issue 11 months ago • 5 comments

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 avatar Mar 17 '24 05:03 ANTOSzbk

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;

ANTOSzbk avatar Mar 18 '24 00:03 ANTOSzbk

Please submit a PR!

melloware avatar Mar 18 '24 00:03 melloware

@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

ANTOSzbk avatar Mar 18 '24 00:03 ANTOSzbk

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

melloware avatar Mar 18 '24 00:03 melloware

Adding @soartec-lab for thoughts

melloware avatar Mar 18 '24 00:03 melloware