orval icon indicating copy to clipboard operation
orval copied to clipboard

Missing param in mutationOptions

Open jiwanovski87 opened this issue 7 months ago • 2 comments

Introduction

Within the orval.config.ts I have added a transformer for the input to add the api version number. For the output I have also added a mutationOption. When generating the files we have the result that the url with the version param is added everywhere. But within the hooks we are missing the parameter.

How can I add the missing param to the hooks?

orval.config.ts

{
	"test-file": {
		input: {
			target: "../openapi/v3/openapi.yaml",
			override: {
				transformer: "./src/api/transformer/add-version.cjs"
			}
		},
		output: {
			mode: "tags-split",
			target: "./src/api/endpoints/test.ts",
			schemas: "./src/api/model",
			client: "react-query",
			override: {
				query: {
					useQuery: true,
					signal: true,
					mutationOptions: {
						path: "./src/api/mutator/custom-query-mutator-options.ts",
						name: "useCustomQueryMutatorOptions"
					},
					shouldSplitQueryKey: true
				},
				mutator: {
					path: "./src/api/mutator/custom-client.ts",
					name: "customClient"
				}
			}
		}
	}
}

add-version.cjs

/**
 * Transformer function for orval.
 *
 * @param {OpenAPIObject} schema
 * @return {OpenAPIObject}
 */
module.exports = (inputSchema) => ({
    ...inputSchema,
    paths: Object.entries(inputSchema.paths || {}).reduce(
      (acc, [path, pathItem]) => Object.assign({}, acc, {
        [`api/v{version}${path}`]: Object.entries(pathItem).reduce(
          (pathItemAcc, [verb, operation]) => Object.assign({}, pathItemAcc, {
            [verb]: {
              ...operation,
              parameters: [
                ...(operation.parameters || []),
                {
                  name: 'version',
                  in: 'path',
                  required: true,
                  schema: {
                    type: 'number',
                    default: 1,
                  },
                },
              ],
            },
          }),
          {},
        ),
      }),
      {},
    ),
  });

Generated result with issue

export const useCreateEntityMutationOptions = <
	TError = ErrorType<unknown>,
	TContext = unknown,
>(options?: {
	mutation?: UseMutationOptions<
		Awaited<ReturnType<typeof createEntity>>,
		TError,
		{
			entityId: string | undefined | null;
			data: BodyType<EntitiesEntityRequestBody>;
			version?: number | undefined | null;
		},
		TContext
	>;
	request?: SecondParameter<typeof customClient>;
}): UseMutationOptions<
	Awaited<ReturnType<typeof createEntity>>,
	TError,
	{
		entityId: string | undefined | null;
		data: BodyType<EntitysEntityRequestBody>;
		version?: number | undefined | null;
	},
	TContext
> => {
	const mutationKey = ["createEntity"];
	const { mutation: mutationOptions, request: requestOptions } = options
		? options.mutation &&
			"mutationKey" in options.mutation &&
			options.mutation.mutationKey
			? options
			: { ...options, mutation: { ...options.mutation, mutationKey } }
		: { mutation: { mutationKey }, request: undefined };

	const mutationFn: MutationFunction<
		Awaited<ReturnType<typeof createEntity>>,
		{
			entityId: string | undefined | null;
			data: BodyType<EntitysEntityRequestBody>;
			version?: number | undefined | null;
		}
	> = (props) => {
		const { entityId, data, version } = props ?? {};

		return createEntity(entityId, data, version, requestOptions);
	};

	const customOptions = useCustomQueryMutatorOptions(
		{ ...mutationOptions, mutationFn },
                // Here is the issue
                // version is unknown
		{ url: `/api/v${version}/entity/` },
		{ operationId: "CreateEntity", operationName: "createEntity" },
	);

	return customOptions;
};

jiwanovski87 avatar Apr 17 '25 11:04 jiwanovski87

Still having this issue... Would be happy if someone could help

jiwanovski87 avatar May 06 '25 18:05 jiwanovski87

Probably a bug in the generator somewhere. PR is welcome!

melloware avatar May 06 '25 20:05 melloware