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

Vue Query: It generates undefined function "useCallback" #1872

Open
wiz-tajima opened this issue Feb 3, 2025 · 3 comments · May be fixed by #1902
Open

Vue Query: It generates undefined function "useCallback" #1872

wiz-tajima opened this issue Feb 3, 2025 · 3 comments · May be fixed by #1902
Assignees
Labels
tanstack-query TanStack Query related issue
Milestone

Comments

@wiz-tajima
Copy link

What are the steps to reproduce this issue?

  1. …generate on Vue3 project with config below:
import { config } from "dotenv";
import { defineConfig } from "orval";

export default () => {
  config();
  return defineConfig({
    "standard-frontend": {
      output: {
        mode: "tags",
        headers: true,
        workspace: "./openapi/client",
        target: "./endpoints",
        schemas: "./models",
        indexFiles: true,
        client: 'vue-query',
        urlEncodeParameters: true, // https://zenn.dev/hsato_workman/books/5f41b770d9221b/viewer/0bccda#urlencodeparameters
        override: {
          useDates: true,
          mutator: {
            path: "./mutator/use-custom-instance.ts",
            name: "useCustomInstance",
          },
          formData: {
            path: "./mutator/custom-form-data.ts",
            name: "customFormData",
          },
          query: {
            queryOptions: {
              path: "./openapi/client/mutator/custom-query-options.ts",
              name: "customTranQueryOptions",
            },
          },
          mock: {},
          operations: {},
        },
      },
      input: {
        target: "./openapi/openapi-docs.yml",
      },
    },
  });
};

What happens?

Orval generates code below:

export const usePostAuthLoginHook = () => {
        const postAuthLogin = useCustomInstance<void>();

        return useCallback((
    authLoginPostRequest: MaybeRef<AuthLoginPostRequest>,
 signal?: AbortSignal
) => {
        return postAuthLogin(
          {url: `/auths/login`, method: 'POST',
      headers: {'Content-Type': 'application/json', },
      data: authLoginPostRequest, signal
    },
          );
        }, [postAuthLogin])
      }

This code has critical problem: useCallback() doesn't exist on Vue.
So, this makes unavoidable error.

What were you expecting to happen?

Before orval version 6.29.0, it generates correct code.
Please make back.

Any logs, error output, etc?

Any other comments?

Probably, useCallback is made with setting mutator on orval.config.ts.

What versions are you using?

Operating System: … Linux (WSL)
Package Version: …6.29.1 ~ 7.4.1
Browser Version: N/A

@wiz-tajima
Copy link
Author

wiz-tajima commented Feb 3, 2025

Maybe I found code makes matter:
function generateAxiosRequestFunction on packages/query/src/client.ts row 134

if (mutator.isHook) {
      return `${
        override.query.shouldExportMutatorHooks ? 'export ' : ''
      }const use${pascal(operationName)}Hook = () => {
        const ${operationName} = ${mutator.name}<${
          response.definition.success || 'unknown'
        }>();

        return useCallback((\n    ${propsImplementation}\n ${
          isRequestOptions && mutator.hasSecondArg
            ? `options${context.output.optionsParamRequired ? '' : '?'}: SecondParameter<ReturnType<typeof ${mutator.name}>>,`
            : ''
        }${hasSignal ? 'signal?: AbortSignal\n' : ''}) => {${bodyForm}
        return ${operationName}(
          ${mutatorConfig},
          ${requestOptions});
        }, [${operationName}])
      }
    `;
    }

I think it have to have a branch for "vue or not vue", and make making output without "useCallback" when it uses vue-query.

I can solve the problem by adding an simple if branch, but would you be okay with it solving?
If it'll be okay, I'd try to make pull-request.
(I don't know Svelte, Angular and React, so I'll fix only adding pattern for vue.)

============================

問題点が分かったと思います。
packages/query/src/client.ts 134行目 、generateAxiosRequestFunction メソッド内

if (mutator.isHook) {
      return `${
        override.query.shouldExportMutatorHooks ? 'export ' : ''
      }const use${pascal(operationName)}Hook = () => {
        const ${operationName} = ${mutator.name}<${
          response.definition.success || 'unknown'
        }>();

        return useCallback((\n    ${propsImplementation}\n ${
          isRequestOptions && mutator.hasSecondArg
            ? `options${context.output.optionsParamRequired ? '' : '?'}: SecondParameter<ReturnType<typeof ${mutator.name}>>,`
            : ''
        }${hasSignal ? 'signal?: AbortSignal\n' : ''}) => {${bodyForm}
        return ${operationName}(
          ${mutatorConfig},
          ${requestOptions});
        }, [${operationName}])
      }
    `;
    }

ここに、「vue か vue でないかの分岐」をおき、vue の場合は useCallback を使わない形で出力すべきではないでしょうか。

ただ単純に if分岐を足す形の修正であれば、私でも可能かと思います。
(Svelte や Angular、React の方はあまり分からないため、あくまで vue 用の分岐を作るだけです。)
もしよければ、pull-request を出してみますが、いかがでしょうか。

@melloware melloware added the tanstack-query TanStack Query related issue label Feb 3, 2025
@melloware
Copy link
Collaborator

Ahhh good debugging do you want to open a PR?

@wiz-tajima
Copy link
Author

@melloware
Sorry to reply so late.
I'm OK. I'll open a PR in this week.

@melloware melloware added this to the 7.6.0 milestone Feb 16, 2025
@melloware melloware modified the milestones: 7.6.0, 7.7.0 Feb 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tanstack-query TanStack Query related issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants