diff --git a/packages/openapi-ts/src/plugins/@tanstack/query-core/infiniteQueryOptions.ts b/packages/openapi-ts/src/plugins/@tanstack/query-core/infiniteQueryOptions.ts index b6e248a17..00fc5617f 100644 --- a/packages/openapi-ts/src/plugins/@tanstack/query-core/infiniteQueryOptions.ts +++ b/packages/openapi-ts/src/plugins/@tanstack/query-core/infiniteQueryOptions.ts @@ -32,6 +32,17 @@ const createInfiniteParamsFunction = ({ }) => { const file = context.file({ id: plugin.name })!; + if (plugin.runtimeConfigPath) { + file.import({ + module: file.relativePathToFile({ + context, + id: plugin.runtimeConfigPath, + }), + name: createInfiniteParamsFn, + }); + return; + } + const fn = compiler.constVariable({ expression: compiler.arrowFunction({ multiLine: true, diff --git a/packages/openapi-ts/src/plugins/@tanstack/query-core/queryKey.ts b/packages/openapi-ts/src/plugins/@tanstack/query-core/queryKey.ts index ab3e7e526..2e2c6500f 100644 --- a/packages/openapi-ts/src/plugins/@tanstack/query-core/queryKey.ts +++ b/packages/openapi-ts/src/plugins/@tanstack/query-core/queryKey.ts @@ -30,6 +30,17 @@ export const createQueryKeyFunction = ({ }); if (identifierCreateQueryKey.name) { + if (plugin.runtimeConfigPath) { + file.import({ + module: file.relativePathToFile({ + context, + id: plugin.runtimeConfigPath, + }), + name: identifierCreateQueryKey.name, + }); + return; + } + const returnType = compiler.indexedAccessTypeNode({ indexType: compiler.literalTypeNode({ literal: compiler.ots.number(0), @@ -250,6 +261,18 @@ export const createQueryKeyType = ({ }) => { const file = context.file({ id: plugin.name })!; + if (plugin.runtimeConfigPath) { + file.import({ + asType: true, + module: file.relativePathToFile({ + context, + id: plugin.runtimeConfigPath, + }), + name: queryKeyName, + }); + return; + } + const properties: Property[] = [ { name: '_id', diff --git a/packages/openapi-ts/src/plugins/@tanstack/query-core/types.d.ts b/packages/openapi-ts/src/plugins/@tanstack/query-core/types.d.ts index 89a09df10..9ee819f05 100644 --- a/packages/openapi-ts/src/plugins/@tanstack/query-core/types.d.ts +++ b/packages/openapi-ts/src/plugins/@tanstack/query-core/types.d.ts @@ -44,5 +44,9 @@ export namespace TanStackQuery { * @default false */ exportFromIndex?: boolean; + /** + * TODO + */ + runtimeConfigPath?: string; }; } diff --git a/packages/openapi-ts/test/hey-api.ts b/packages/openapi-ts/test/hey-api.ts new file mode 100644 index 000000000..2af13839c --- /dev/null +++ b/packages/openapi-ts/test/hey-api.ts @@ -0,0 +1,70 @@ +import { client as _heyApiClient } from './generated/sample/client.gen'; +import type { Options } from './generated/sample/sdk.gen'; + +export type QueryKey = [ + Pick & { + _id: string; + _infinite?: boolean; + }, +]; + +export const createQueryKey = ( + id: string, + options?: TOptions, + infinite?: boolean, +): [QueryKey[0]] => { + const params: QueryKey[0] = { + _id: id, + baseUrl: (options?.client ?? _heyApiClient).getConfig().baseUrl, + } as QueryKey[0]; + if (infinite) { + params._infinite = infinite; + } + if (options?.body) { + params.body = options.body; + } + if (options?.headers) { + params.headers = options.headers; + } + if (options?.path) { + params.path = options.path; + } + if (options?.query) { + params.query = options.query; + } + return [params]; +}; + +export const createInfiniteParams = < + K extends Pick[0], 'body' | 'headers' | 'path' | 'query'>, +>( + queryKey: QueryKey, + page: K, +) => { + const params = queryKey[0]; + if (page.body) { + params.body = { + ...(queryKey[0].body as any), + ...(page.body as any), + }; + } + if (page.headers) { + params.headers = { + ...queryKey[0].headers, + ...page.headers, + }; + } + if (page.path) { + params.path = { + ...(queryKey[0].path as any), + ...(page.path as any), + }; + } + if (page.query) { + params.query = { + ...(queryKey[0].query as any), + ...(page.query as any), + }; + } + return params as unknown as typeof page; +}; diff --git a/packages/openapi-ts/test/openapi-ts.config.ts b/packages/openapi-ts/test/openapi-ts.config.ts index 096e06f39..9dd08ba3f 100644 --- a/packages/openapi-ts/test/openapi-ts.config.ts +++ b/packages/openapi-ts/test/openapi-ts.config.ts @@ -78,6 +78,7 @@ export default defineConfig({ // @ts-ignore { name: '@tanstack/react-query', + runtimeConfigPath: './hey-api.ts', }, // @ts-ignore {