UNPKG

@kubb/plugin-react-query

Version:

React Query hooks generator plugin for Kubb, creating type-safe API client hooks from OpenAPI specifications for React applications.

1,278 lines (1,252 loc) 47 kB
import { URLPath, getNestedAccessor } from "@kubb/core/utils"; import { isOptional } from "@kubb/oas"; import { getComments, getPathParams } from "@kubb/plugin-oas/utils"; import { File, Function, FunctionParams, Type } from "@kubb/react-fabric"; import { Fragment, jsx, jsxs } from "@kubb/react-fabric/jsx-runtime"; import { Client } from "@kubb/plugin-client/components"; //#region src/components/QueryKey.tsx function getParams$10({ pathParamsType, paramsCasing, typeSchemas }) { return FunctionParams.factory({ pathParams: { mode: pathParamsType === "object" ? "object" : "inlineSpread", children: getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }) }, data: typeSchemas.request?.name ? { type: typeSchemas.request?.name, optional: isOptional(typeSchemas.request?.schema) } : void 0, params: typeSchemas.queryParams?.name ? { type: typeSchemas.queryParams?.name, optional: isOptional(typeSchemas.queryParams?.schema) } : void 0 }); } const getTransformer$1 = ({ operation, schemas, casing }) => { return [ new URLPath(operation.path, { casing }).toObject({ type: "path", stringify: true }), schemas.queryParams?.name ? "...(params ? [params] : [])" : void 0, schemas.request?.name ? "...(data ? [data] : [])" : void 0 ].filter(Boolean); }; function QueryKey({ name, typeSchemas, paramsCasing, pathParamsType, operation, typeName, transformer = getTransformer$1 }) { const params = getParams$10({ pathParamsType, typeSchemas, paramsCasing }); const keys = transformer({ operation, schemas: typeSchemas, casing: paramsCasing }); return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(File.Source, { name, isExportable: true, isIndexable: true, children: /* @__PURE__ */ jsx(Function.Arrow, { name, export: true, params: params.toConstructor(), singleLine: true, children: `[${keys.join(", ")}] as const` }) }), /* @__PURE__ */ jsx(File.Source, { name: typeName, isExportable: true, isIndexable: true, isTypeOnly: true, children: /* @__PURE__ */ jsx(Type, { name: typeName, export: true, children: `ReturnType<typeof ${name}>` }) })] }); } QueryKey.getParams = getParams$10; QueryKey.getTransformer = getTransformer$1; //#endregion //#region src/components/QueryOptions.tsx function getParams$9({ paramsType, paramsCasing, pathParamsType, typeSchemas }) { if (paramsType === "object") return FunctionParams.factory({ data: { mode: "object", children: { ...getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), data: typeSchemas.request?.name ? { type: typeSchemas.request?.name, optional: isOptional(typeSchemas.request?.schema) } : void 0, params: typeSchemas.queryParams?.name ? { type: typeSchemas.queryParams?.name, optional: isOptional(typeSchemas.queryParams?.schema) } : void 0, headers: typeSchemas.headerParams?.name ? { type: typeSchemas.headerParams?.name, optional: isOptional(typeSchemas.headerParams?.schema) } : void 0 } }, config: { type: typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof fetch }` : "Partial<RequestConfig> & { client?: typeof fetch }", default: "{}" } }); return FunctionParams.factory({ pathParams: typeSchemas.pathParams?.name ? { mode: pathParamsType === "object" ? "object" : "inlineSpread", children: getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), optional: isOptional(typeSchemas.pathParams?.schema) } : void 0, data: typeSchemas.request?.name ? { type: typeSchemas.request?.name, optional: isOptional(typeSchemas.request?.schema) } : void 0, params: typeSchemas.queryParams?.name ? { type: typeSchemas.queryParams?.name, optional: isOptional(typeSchemas.queryParams?.schema) } : void 0, headers: typeSchemas.headerParams?.name ? { type: typeSchemas.headerParams?.name, optional: isOptional(typeSchemas.headerParams?.schema) } : void 0, config: { type: typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof fetch }` : "Partial<RequestConfig> & { client?: typeof fetch }", default: "{}" } }); } function QueryOptions({ name, clientName, dataReturnType, typeSchemas, paramsCasing, paramsType, pathParamsType, queryKeyName }) { const params = getParams$9({ paramsType, paramsCasing, pathParamsType, typeSchemas }); const TData = dataReturnType === "data" ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`; const TError = typeSchemas.errors?.map((item) => item.name).join(" | ") || "Error"; const clientParams = Client.getParams({ typeSchemas, paramsCasing, paramsType, pathParamsType, isConfigurable: true }); const queryKeyParams = QueryKey.getParams({ pathParamsType, typeSchemas, paramsCasing }); const enabled = Object.entries(queryKeyParams.flatParams).map(([key, item]) => item && !item.optional ? key : void 0).filter(Boolean).join("&& "); const enabledText = enabled ? `enabled: !!(${enabled}),` : ""; return /* @__PURE__ */ jsx(File.Source, { name, isExportable: true, isIndexable: true, children: /* @__PURE__ */ jsx(Function, { name, export: true, params: params.toConstructor(), children: ` const queryKey = ${queryKeyName}(${queryKeyParams.toCall()}) return queryOptions<${TData}, ResponseErrorConfig<${TError}>, ${TData}, typeof queryKey>({ ${enabledText} queryKey, queryFn: async ({ signal }) => { config.signal = signal return ${clientName}(${clientParams.toCall({})}) }, }) ` }) }); } QueryOptions.getParams = getParams$9; //#endregion //#region src/components/InfiniteQuery.tsx function getParams$8({ paramsType, paramsCasing, pathParamsType, typeSchemas, pageParamGeneric }) { if (paramsType === "object") return FunctionParams.factory({ data: { mode: "object", children: { ...getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), data: typeSchemas.request?.name ? { type: typeSchemas.request?.name, optional: isOptional(typeSchemas.request?.schema) } : void 0, params: typeSchemas.queryParams?.name ? { type: typeSchemas.queryParams?.name, optional: isOptional(typeSchemas.queryParams?.schema) } : void 0, headers: typeSchemas.headerParams?.name ? { type: typeSchemas.headerParams?.name, optional: isOptional(typeSchemas.headerParams?.schema) } : void 0 } }, options: { type: ` { query?: Partial<InfiniteQueryObserverOptions<TQueryFnData, TError, TData, TQueryKey, ${pageParamGeneric}>> & { client?: QueryClient }, client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof fetch }` : "Partial<RequestConfig> & { client?: typeof fetch }"} } `, default: "{}" } }); return FunctionParams.factory({ pathParams: typeSchemas.pathParams?.name ? { mode: pathParamsType === "object" ? "object" : "inlineSpread", children: getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), optional: isOptional(typeSchemas.pathParams?.schema) } : void 0, data: typeSchemas.request?.name ? { type: typeSchemas.request?.name, optional: isOptional(typeSchemas.request?.schema) } : void 0, params: typeSchemas.queryParams?.name ? { type: typeSchemas.queryParams?.name, optional: isOptional(typeSchemas.queryParams?.schema) } : void 0, headers: typeSchemas.headerParams?.name ? { type: typeSchemas.headerParams?.name, optional: isOptional(typeSchemas.headerParams?.schema) } : void 0, options: { type: ` { query?: Partial<InfiniteQueryObserverOptions<TQueryFnData, TError, TData, TQueryKey, ${pageParamGeneric}>> & { client?: QueryClient }, client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof fetch }` : "Partial<RequestConfig> & { client?: typeof fetch }"} } `, default: "{}" } }); } function InfiniteQuery({ name, queryKeyTypeName, queryOptionsName, queryKeyName, paramsType, paramsCasing, pathParamsType, dataReturnType, typeSchemas, operation, initialPageParam, queryParam }) { const responseType = dataReturnType === "data" ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`; const errorType = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(" | ") || "Error"}>`; const isInitialPageParamDefined = initialPageParam !== void 0 && initialPageParam !== null; const fallbackPageParamType = typeof initialPageParam === "number" ? "number" : typeof initialPageParam === "string" ? initialPageParam.includes(" as ") ? (() => { const parts = initialPageParam.split(" as "); return parts[parts.length - 1] ?? "unknown"; })() : "string" : typeof initialPageParam === "boolean" ? "boolean" : "unknown"; const queryParamType = queryParam && typeSchemas.queryParams?.name ? `${typeSchemas.queryParams?.name}['${queryParam}']` : void 0; const pageParamType = queryParamType ? isInitialPageParamDefined ? `NonNullable<${queryParamType}>` : queryParamType : fallbackPageParamType; const returnType = "UseInfiniteQueryResult<TData, TError> & { queryKey: TQueryKey }"; const generics = [ `TQueryFnData = ${responseType}`, `TError = ${errorType}`, "TData = InfiniteData<TQueryFnData>", `TQueryKey extends QueryKey = ${queryKeyTypeName}`, `TPageParam = ${pageParamType}` ]; const queryKeyParams = QueryKey.getParams({ pathParamsType, typeSchemas, paramsCasing }); const queryOptionsParams = QueryOptions.getParams({ paramsType, pathParamsType, typeSchemas, paramsCasing }); const params = getParams$8({ paramsCasing, paramsType, pathParamsType, typeSchemas, pageParamGeneric: "TPageParam" }); const queryOptions = `${queryOptionsName}(${queryOptionsParams.toCall()})`; return /* @__PURE__ */ jsx(File.Source, { name, isExportable: true, isIndexable: true, children: /* @__PURE__ */ jsx(Function, { name, export: true, generics: generics.join(", "), params: params.toConstructor(), JSDoc: { comments: getComments(operation) }, children: ` const { query: queryConfig = {}, client: config = {} } = options ?? {} const { client: queryClient, ...queryOptions } = queryConfig const queryKey = queryOptions?.queryKey ?? ${queryKeyName}(${queryKeyParams.toCall()}) const query = useInfiniteQuery({ ...${queryOptions}, queryKey, ...queryOptions } as unknown as InfiniteQueryObserverOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, queryClient) as ${returnType} query.queryKey = queryKey as TQueryKey return query ` }) }); } InfiniteQuery.getParams = getParams$8; //#endregion //#region src/components/InfiniteQueryOptions.tsx function getParams$7({ paramsType, paramsCasing, pathParamsType, typeSchemas }) { if (paramsType === "object") return FunctionParams.factory({ data: { mode: "object", children: { ...getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), data: typeSchemas.request?.name ? { type: typeSchemas.request?.name, optional: isOptional(typeSchemas.request?.schema) } : void 0, params: typeSchemas.queryParams?.name ? { type: typeSchemas.queryParams?.name, optional: isOptional(typeSchemas.queryParams?.schema) } : void 0, headers: typeSchemas.headerParams?.name ? { type: typeSchemas.headerParams?.name, optional: isOptional(typeSchemas.headerParams?.schema) } : void 0 } }, config: { type: typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof fetch }` : "Partial<RequestConfig> & { client?: typeof fetch }", default: "{}" } }); return FunctionParams.factory({ pathParams: typeSchemas.pathParams?.name ? { mode: pathParamsType === "object" ? "object" : "inlineSpread", children: getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), optional: isOptional(typeSchemas.pathParams?.schema) } : void 0, data: typeSchemas.request?.name ? { type: typeSchemas.request?.name, optional: isOptional(typeSchemas.request?.schema) } : void 0, params: typeSchemas.queryParams?.name ? { type: typeSchemas.queryParams?.name, optional: isOptional(typeSchemas.queryParams?.schema) } : void 0, headers: typeSchemas.headerParams?.name ? { type: typeSchemas.headerParams?.name, optional: isOptional(typeSchemas.headerParams?.schema) } : void 0, config: { type: typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof fetch }` : "Partial<RequestConfig> & { client?: typeof fetch }", default: "{}" } }); } function InfiniteQueryOptions({ name, clientName, initialPageParam, cursorParam, nextParam, previousParam, typeSchemas, paramsCasing, paramsType, dataReturnType, pathParamsType, queryParam, queryKeyName }) { const queryFnDataType = dataReturnType === "data" ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`; const errorType = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(" | ") || "Error"}>`; const isInitialPageParamDefined = initialPageParam !== void 0 && initialPageParam !== null; const fallbackPageParamType = typeof initialPageParam === "number" ? "number" : typeof initialPageParam === "string" ? initialPageParam.includes(" as ") ? (() => { const parts = initialPageParam.split(" as "); return parts[parts.length - 1] ?? "unknown"; })() : "string" : typeof initialPageParam === "boolean" ? "boolean" : "unknown"; const queryParamType = queryParam && typeSchemas.queryParams?.name ? `${typeSchemas.queryParams?.name}['${queryParam}']` : void 0; const pageParamType = queryParamType ? isInitialPageParamDefined ? `NonNullable<${queryParamType}>` : queryParamType : fallbackPageParamType; const params = getParams$7({ paramsType, paramsCasing, pathParamsType, typeSchemas }); const clientParams = Client.getParams({ paramsCasing, typeSchemas, paramsType, pathParamsType, isConfigurable: true }); const queryKeyParams = QueryKey.getParams({ pathParamsType, typeSchemas, paramsCasing }); const hasNewParams = nextParam !== void 0 || previousParam !== void 0; let getNextPageParamExpr; let getPreviousPageParamExpr; if (hasNewParams) { if (nextParam) { const accessor = getNestedAccessor(nextParam, "lastPage"); if (accessor) getNextPageParamExpr = `getNextPageParam: (lastPage) => ${accessor}`; } if (previousParam) { const accessor = getNestedAccessor(previousParam, "firstPage"); if (accessor) getPreviousPageParamExpr = `getPreviousPageParam: (firstPage) => ${accessor}`; } } else if (cursorParam) { getNextPageParamExpr = `getNextPageParam: (lastPage) => lastPage['${cursorParam}']`; getPreviousPageParamExpr = `getPreviousPageParam: (firstPage) => firstPage['${cursorParam}']`; } else { if (dataReturnType === "full") getNextPageParamExpr = "getNextPageParam: (lastPage, _allPages, lastPageParam) => Array.isArray(lastPage.data) && lastPage.data.length === 0 ? undefined : lastPageParam + 1"; else getNextPageParamExpr = "getNextPageParam: (lastPage, _allPages, lastPageParam) => Array.isArray(lastPage) && lastPage.length === 0 ? undefined : lastPageParam + 1"; getPreviousPageParamExpr = "getPreviousPageParam: (_firstPage, _allPages, firstPageParam) => firstPageParam <= 1 ? undefined : firstPageParam - 1"; } const queryOptions = [ `initialPageParam: ${typeof initialPageParam === "string" ? JSON.stringify(initialPageParam) : initialPageParam}`, getNextPageParamExpr, getPreviousPageParamExpr ].filter(Boolean); const infiniteOverrideParams = queryParam && typeSchemas.queryParams?.name ? ` params = { ...(params ?? {}), ['${queryParam}']: pageParam as unknown as ${typeSchemas.queryParams?.name}['${queryParam}'], } as ${typeSchemas.queryParams?.name}` : ""; const enabled = Object.entries(queryKeyParams.flatParams).map(([key, item]) => item && !item.optional ? key : void 0).filter(Boolean).join("&& "); const enabledText = enabled ? `enabled: !!(${enabled}),` : ""; if (infiniteOverrideParams) return /* @__PURE__ */ jsx(File.Source, { name, isExportable: true, isIndexable: true, children: /* @__PURE__ */ jsx(Function, { name, export: true, params: params.toConstructor(), children: ` const queryKey = ${queryKeyName}(${queryKeyParams.toCall()}) return infiniteQueryOptions<${queryFnDataType}, ${errorType}, InfiniteData<${queryFnDataType}>, typeof queryKey, ${pageParamType}>({ ${enabledText} queryKey, queryFn: async ({ signal, pageParam }) => { config.signal = signal ${infiniteOverrideParams} return ${clientName}(${clientParams.toCall()}) }, ${queryOptions.join(",\n")} }) ` }) }); return /* @__PURE__ */ jsx(File.Source, { name, isExportable: true, isIndexable: true, children: /* @__PURE__ */ jsx(Function, { name, export: true, params: params.toConstructor(), children: ` const queryKey = ${queryKeyName}(${queryKeyParams.toCall()}) return infiniteQueryOptions<${queryFnDataType}, ${errorType}, InfiniteData<${queryFnDataType}>, typeof queryKey, ${pageParamType}>({ ${enabledText} queryKey, queryFn: async ({ signal }) => { config.signal = signal return ${clientName}(${clientParams.toCall()}) }, ${queryOptions.join(",\n")} }) ` }) }); } InfiniteQueryOptions.getParams = getParams$7; //#endregion //#region src/components/MutationKey.tsx function getParams$6({}) { return FunctionParams.factory({}); } const getTransformer = ({ operation, casing }) => { return [`{ url: '${new URLPath(operation.path, { casing }).toURLPath()}' }`]; }; function MutationKey({ name, typeSchemas, pathParamsType, paramsCasing, operation, typeName, transformer = getTransformer }) { const params = getParams$6({ pathParamsType, typeSchemas }); const keys = transformer({ operation, schemas: typeSchemas, casing: paramsCasing }); return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(File.Source, { name, isExportable: true, isIndexable: true, children: /* @__PURE__ */ jsx(Function.Arrow, { name, export: true, params: params.toConstructor(), singleLine: true, children: `[${keys.join(", ")}] as const` }) }), /* @__PURE__ */ jsx(File.Source, { name: typeName, isExportable: true, isIndexable: true, isTypeOnly: true, children: /* @__PURE__ */ jsx(Type, { name: typeName, export: true, children: `ReturnType<typeof ${name}>` }) })] }); } MutationKey.getParams = getParams$6; MutationKey.getTransformer = getTransformer; //#endregion //#region src/components/MutationOptions.tsx function getParams$5({ typeSchemas }) { return FunctionParams.factory({ config: { type: typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof fetch }` : "Partial<RequestConfig> & { client?: typeof fetch }", default: "{}" } }); } function MutationOptions({ name, clientName, dataReturnType, typeSchemas, paramsCasing, paramsType, pathParamsType, mutationKeyName }) { const params = getParams$5({ typeSchemas }); const TData = dataReturnType === "data" ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`; const TError = typeSchemas.errors?.map((item) => item.name).join(" | ") || "Error"; const clientParams = Client.getParams({ typeSchemas, paramsCasing, paramsType, pathParamsType, isConfigurable: true }); const mutationKeyParams = MutationKey.getParams({ pathParamsType, typeSchemas }); const mutationParams = FunctionParams.factory({ ...getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), data: typeSchemas.request?.name ? { type: typeSchemas.request?.name, optional: isOptional(typeSchemas.request?.schema) } : void 0, params: typeSchemas.queryParams?.name ? { type: typeSchemas.queryParams?.name, optional: isOptional(typeSchemas.queryParams?.schema) } : void 0, headers: typeSchemas.headerParams?.name ? { type: typeSchemas.headerParams?.name, optional: isOptional(typeSchemas.headerParams?.schema) } : void 0 }); const dataParams = FunctionParams.factory({ data: { mode: "object", children: Object.entries(mutationParams.params).reduce((acc, [key, value]) => { if (value) acc[key] = { ...value, type: void 0 }; return acc; }, {}) } }); const TRequest = mutationParams.toConstructor(); return /* @__PURE__ */ jsx(File.Source, { name, isExportable: true, isIndexable: true, children: /* @__PURE__ */ jsx(Function, { name, export: true, params: params.toConstructor(), children: ` const mutationKey = ${mutationKeyName}(${mutationKeyParams.toCall()}) return mutationOptions<${TData}, ResponseErrorConfig<${TError}>, ${TRequest ? `{${TRequest}}` : "void"}, typeof mutationKey>({ mutationKey, mutationFn: async(${dataParams.toConstructor()}) => { return ${clientName}(${clientParams.toCall()}) }, }) ` }) }); } MutationOptions.getParams = getParams$5; //#endregion //#region src/components/Mutation.tsx function getParams$4({ paramsCasing, dataReturnType, typeSchemas }) { const TData = dataReturnType === "data" ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`; const TRequest = FunctionParams.factory({ ...getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), data: typeSchemas.request?.name ? { type: typeSchemas.request?.name, optional: isOptional(typeSchemas.request?.schema) } : void 0, params: typeSchemas.queryParams?.name ? { type: typeSchemas.queryParams?.name, optional: isOptional(typeSchemas.queryParams?.schema) } : void 0, headers: typeSchemas.headerParams?.name ? { type: typeSchemas.headerParams?.name, optional: isOptional(typeSchemas.headerParams?.schema) } : void 0 }).toConstructor(); const generics = [ TData, `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(" | ") || "Error"}>`, TRequest ? `{${TRequest}}` : "void", "TContext" ].join(", "); return FunctionParams.factory({ options: { type: ` { mutation?: UseMutationOptions<${generics}> & { client?: QueryClient }, client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof fetch }` : "Partial<RequestConfig> & { client?: typeof fetch }"}, } `, default: "{}" } }); } function Mutation({ name, mutationOptionsName, paramsCasing, pathParamsType, dataReturnType, typeSchemas, operation, mutationKeyName }) { const mutationKeyParams = MutationKey.getParams({ pathParamsType, typeSchemas }); const params = getParams$4({ paramsCasing, pathParamsType, dataReturnType, typeSchemas }); const mutationParams = FunctionParams.factory({ ...getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), data: typeSchemas.request?.name ? { type: typeSchemas.request?.name, optional: isOptional(typeSchemas.request?.schema) } : void 0, params: typeSchemas.queryParams?.name ? { type: typeSchemas.queryParams?.name, optional: isOptional(typeSchemas.queryParams?.schema) } : void 0, headers: typeSchemas.headerParams?.name ? { type: typeSchemas.headerParams?.name, optional: isOptional(typeSchemas.headerParams?.schema) } : void 0 }); const mutationOptionsParams = MutationOptions.getParams({ typeSchemas }); const TRequest = mutationParams.toConstructor(); const generics = [ dataReturnType === "data" ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`, `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(" | ") || "Error"}>`, TRequest ? `{${TRequest}}` : "void", "TContext" ].join(", "); const returnType = `UseMutationResult<${generics}>`; const mutationOptions = `${mutationOptionsName}(${mutationOptionsParams.toCall()})`; return /* @__PURE__ */ jsx(File.Source, { name, isExportable: true, isIndexable: true, children: /* @__PURE__ */ jsx(Function, { name, export: true, params: params.toConstructor(), JSDoc: { comments: getComments(operation) }, generics: ["TContext"], children: ` const { mutation = {}, client: config = {} } = options ?? {} const { client: queryClient, ...mutationOptions } = mutation; const mutationKey = mutationOptions.mutationKey ?? ${mutationKeyName}(${mutationKeyParams.toCall()}) const baseOptions = ${mutationOptions} as UseMutationOptions<${generics}> return useMutation<${generics}>({ ...baseOptions, mutationKey, ...mutationOptions, }, queryClient) as ${returnType} ` }) }); } //#endregion //#region src/components/Query.tsx function getParams$3({ paramsType, paramsCasing, pathParamsType, dataReturnType, typeSchemas }) { const TData = dataReturnType === "data" ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`; const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(" | ") || "Error"}>`; if (paramsType === "object") return FunctionParams.factory({ data: { mode: "object", children: { ...getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), data: typeSchemas.request?.name ? { type: typeSchemas.request?.name, optional: isOptional(typeSchemas.request?.schema) } : void 0, params: typeSchemas.queryParams?.name ? { type: typeSchemas.queryParams?.name, optional: isOptional(typeSchemas.queryParams?.schema) } : void 0, headers: typeSchemas.headerParams?.name ? { type: typeSchemas.headerParams?.name, optional: isOptional(typeSchemas.headerParams?.schema) } : void 0 } }, options: { type: ` { query?: Partial<QueryObserverOptions<${[ TData, TError, "TData", "TQueryData", "TQueryKey" ].join(", ")}>> & { client?: QueryClient }, client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof fetch }` : "Partial<RequestConfig> & { client?: typeof fetch }"} } `, default: "{}" } }); return FunctionParams.factory({ pathParams: typeSchemas.pathParams?.name ? { mode: pathParamsType === "object" ? "object" : "inlineSpread", children: getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), optional: isOptional(typeSchemas.pathParams?.schema) } : void 0, data: typeSchemas.request?.name ? { type: typeSchemas.request?.name, optional: isOptional(typeSchemas.request?.schema) } : void 0, params: typeSchemas.queryParams?.name ? { type: typeSchemas.queryParams?.name, optional: isOptional(typeSchemas.queryParams?.schema) } : void 0, headers: typeSchemas.headerParams?.name ? { type: typeSchemas.headerParams?.name, optional: isOptional(typeSchemas.headerParams?.schema) } : void 0, options: { type: ` { query?: Partial<QueryObserverOptions<${[ TData, TError, "TData", "TQueryData", "TQueryKey" ].join(", ")}>> & { client?: QueryClient }, client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof fetch }` : "Partial<RequestConfig> & { client?: typeof fetch }"} } `, default: "{}" } }); } function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName, paramsType, paramsCasing, pathParamsType, dataReturnType, typeSchemas, operation }) { const TData = dataReturnType === "data" ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`; const returnType = `UseQueryResult<${["TData", `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(" | ") || "Error"}>`].join(", ")}> & { queryKey: TQueryKey }`; const generics = [ `TData = ${TData}`, `TQueryData = ${TData}`, `TQueryKey extends QueryKey = ${queryKeyTypeName}` ]; const queryKeyParams = QueryKey.getParams({ pathParamsType, typeSchemas, paramsCasing }); const queryOptionsParams = QueryOptions.getParams({ paramsType, pathParamsType, typeSchemas, paramsCasing }); const params = getParams$3({ paramsCasing, paramsType, pathParamsType, dataReturnType, typeSchemas }); const queryOptions = `${queryOptionsName}(${queryOptionsParams.toCall()})`; return /* @__PURE__ */ jsx(File.Source, { name, isExportable: true, isIndexable: true, children: /* @__PURE__ */ jsx(Function, { name, export: true, generics: generics.join(", "), params: params.toConstructor(), JSDoc: { comments: getComments(operation) }, children: ` const { query: queryConfig = {}, client: config = {} } = options ?? {} const { client: queryClient, ...queryOptions } = queryConfig const queryKey = queryOptions?.queryKey ?? ${queryKeyName}(${queryKeyParams.toCall()}) const query = useQuery({ ...${queryOptions}, queryKey, ...queryOptions } as unknown as QueryObserverOptions, queryClient) as ${returnType} query.queryKey = queryKey as TQueryKey return query ` }) }); } Query.getParams = getParams$3; //#endregion //#region src/components/SuspenseInfiniteQuery.tsx function getParams$2({ paramsType, paramsCasing, pathParamsType, typeSchemas, pageParamGeneric }) { if (paramsType === "object") return FunctionParams.factory({ data: { mode: "object", children: { ...getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), data: typeSchemas.request?.name ? { type: typeSchemas.request?.name, optional: isOptional(typeSchemas.request?.schema) } : void 0, params: typeSchemas.queryParams?.name ? { type: typeSchemas.queryParams?.name, optional: isOptional(typeSchemas.queryParams?.schema) } : void 0, headers: typeSchemas.headerParams?.name ? { type: typeSchemas.headerParams?.name, optional: isOptional(typeSchemas.headerParams?.schema) } : void 0 } }, options: { type: ` { query?: Partial<UseSuspenseInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, ${pageParamGeneric}>> & { client?: QueryClient }, client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof fetch }` : "Partial<RequestConfig> & { client?: typeof fetch }"} } `, default: "{}" } }); return FunctionParams.factory({ pathParams: typeSchemas.pathParams?.name ? { mode: pathParamsType === "object" ? "object" : "inlineSpread", children: getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), optional: isOptional(typeSchemas.pathParams?.schema) } : void 0, data: typeSchemas.request?.name ? { type: typeSchemas.request?.name, optional: isOptional(typeSchemas.request?.schema) } : void 0, params: typeSchemas.queryParams?.name ? { type: typeSchemas.queryParams?.name, optional: isOptional(typeSchemas.queryParams?.schema) } : void 0, headers: typeSchemas.headerParams?.name ? { type: typeSchemas.headerParams?.name, optional: isOptional(typeSchemas.headerParams?.schema) } : void 0, options: { type: ` { query?: Partial<UseSuspenseInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, ${pageParamGeneric}>> & { client?: QueryClient }, client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof fetch }` : "Partial<RequestConfig> & { client?: typeof fetch }"} } `, default: "{}" } }); } function SuspenseInfiniteQuery({ name, queryKeyTypeName, queryOptionsName, queryKeyName, paramsType, paramsCasing, pathParamsType, dataReturnType, typeSchemas, operation, initialPageParam, queryParam }) { const responseType = dataReturnType === "data" ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`; const errorType = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(" | ") || "Error"}>`; const isInitialPageParamDefined = initialPageParam !== void 0 && initialPageParam !== null; const fallbackPageParamType = typeof initialPageParam === "number" ? "number" : typeof initialPageParam === "string" ? initialPageParam.includes(" as ") ? (() => { const parts = initialPageParam.split(" as "); return parts[parts.length - 1] ?? "unknown"; })() : "string" : typeof initialPageParam === "boolean" ? "boolean" : "unknown"; const queryParamType = queryParam && typeSchemas.queryParams?.name ? `${typeSchemas.queryParams?.name}['${queryParam}']` : void 0; const pageParamType = queryParamType ? isInitialPageParamDefined ? `NonNullable<${queryParamType}>` : queryParamType : fallbackPageParamType; const returnType = "UseSuspenseInfiniteQueryResult<TData, TError> & { queryKey: TQueryKey }"; const generics = [ `TQueryFnData = ${responseType}`, `TError = ${errorType}`, "TData = InfiniteData<TQueryFnData>", `TQueryKey extends QueryKey = ${queryKeyTypeName}`, `TPageParam = ${pageParamType}` ]; const queryKeyParams = QueryKey.getParams({ pathParamsType, typeSchemas, paramsCasing }); const queryOptionsParams = QueryOptions.getParams({ paramsType, pathParamsType, typeSchemas, paramsCasing }); const params = getParams$2({ paramsCasing, paramsType, pathParamsType, typeSchemas, pageParamGeneric: "TPageParam" }); const queryOptions = `${queryOptionsName}(${queryOptionsParams.toCall()})`; return /* @__PURE__ */ jsx(File.Source, { name, isExportable: true, isIndexable: true, children: /* @__PURE__ */ jsx(Function, { name, export: true, generics: generics.join(", "), params: params.toConstructor(), JSDoc: { comments: getComments(operation) }, children: ` const { query: queryConfig = {}, client: config = {} } = options ?? {} const { client: queryClient, ...queryOptions } = queryConfig const queryKey = queryOptions?.queryKey ?? ${queryKeyName}(${queryKeyParams.toCall()}) const query = useSuspenseInfiniteQuery({ ...${queryOptions}, queryKey, ...queryOptions } as unknown as UseSuspenseInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, queryClient) as ${returnType} query.queryKey = queryKey as TQueryKey return query ` }) }); } SuspenseInfiniteQuery.getParams = getParams$2; //#endregion //#region src/components/SuspenseInfiniteQueryOptions.tsx function getParams$1({ paramsType, paramsCasing, pathParamsType, typeSchemas }) { if (paramsType === "object") return FunctionParams.factory({ data: { mode: "object", children: { ...getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), data: typeSchemas.request?.name ? { type: typeSchemas.request?.name, optional: isOptional(typeSchemas.request?.schema) } : void 0, params: typeSchemas.queryParams?.name ? { type: typeSchemas.queryParams?.name, optional: isOptional(typeSchemas.queryParams?.schema) } : void 0, headers: typeSchemas.headerParams?.name ? { type: typeSchemas.headerParams?.name, optional: isOptional(typeSchemas.headerParams?.schema) } : void 0 } }, config: { type: typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof fetch }` : "Partial<RequestConfig> & { client?: typeof fetch }", default: "{}" } }); return FunctionParams.factory({ pathParams: typeSchemas.pathParams?.name ? { mode: pathParamsType === "object" ? "object" : "inlineSpread", children: getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), optional: isOptional(typeSchemas.pathParams?.schema) } : void 0, data: typeSchemas.request?.name ? { type: typeSchemas.request?.name, optional: isOptional(typeSchemas.request?.schema) } : void 0, params: typeSchemas.queryParams?.name ? { type: typeSchemas.queryParams?.name, optional: isOptional(typeSchemas.queryParams?.schema) } : void 0, headers: typeSchemas.headerParams?.name ? { type: typeSchemas.headerParams?.name, optional: isOptional(typeSchemas.headerParams?.schema) } : void 0, config: { type: typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof fetch }` : "Partial<RequestConfig> & { client?: typeof fetch }", default: "{}" } }); } function SuspenseInfiniteQueryOptions({ name, clientName, initialPageParam, cursorParam, nextParam, previousParam, typeSchemas, paramsCasing, paramsType, dataReturnType, pathParamsType, queryParam, queryKeyName }) { const queryFnDataType = dataReturnType === "data" ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`; const errorType = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(" | ") || "Error"}>`; const isInitialPageParamDefined = initialPageParam !== void 0 && initialPageParam !== null; const fallbackPageParamType = typeof initialPageParam === "number" ? "number" : typeof initialPageParam === "string" ? initialPageParam.includes(" as ") ? (() => { const parts = initialPageParam.split(" as "); return parts[parts.length - 1] ?? "unknown"; })() : "string" : typeof initialPageParam === "boolean" ? "boolean" : "unknown"; const queryParamType = queryParam && typeSchemas.queryParams?.name ? `${typeSchemas.queryParams?.name}['${queryParam}']` : void 0; const pageParamType = queryParamType ? isInitialPageParamDefined ? `NonNullable<${queryParamType}>` : queryParamType : fallbackPageParamType; const params = getParams$1({ paramsType, paramsCasing, pathParamsType, typeSchemas }); const clientParams = Client.getParams({ paramsCasing, typeSchemas, paramsType, pathParamsType, isConfigurable: true }); const queryKeyParams = QueryKey.getParams({ pathParamsType, typeSchemas, paramsCasing }); const hasNewParams = nextParam !== void 0 || previousParam !== void 0; let getNextPageParamExpr; let getPreviousPageParamExpr; if (hasNewParams) { if (nextParam) { const accessor = getNestedAccessor(nextParam, "lastPage"); if (accessor) getNextPageParamExpr = `getNextPageParam: (lastPage) => ${accessor}`; } if (previousParam) { const accessor = getNestedAccessor(previousParam, "firstPage"); if (accessor) getPreviousPageParamExpr = `getPreviousPageParam: (firstPage) => ${accessor}`; } } else if (cursorParam) { getNextPageParamExpr = `getNextPageParam: (lastPage) => lastPage['${cursorParam}']`; getPreviousPageParamExpr = `getPreviousPageParam: (firstPage) => firstPage['${cursorParam}']`; } else { if (dataReturnType === "full") getNextPageParamExpr = "getNextPageParam: (lastPage, _allPages, lastPageParam) => Array.isArray(lastPage.data) && lastPage.data.length === 0 ? undefined : lastPageParam + 1"; else getNextPageParamExpr = "getNextPageParam: (lastPage, _allPages, lastPageParam) => Array.isArray(lastPage) && lastPage.length === 0 ? undefined : lastPageParam + 1"; getPreviousPageParamExpr = "getPreviousPageParam: (_firstPage, _allPages, firstPageParam) => firstPageParam <= 1 ? undefined : firstPageParam - 1"; } const queryOptions = [ `initialPageParam: ${typeof initialPageParam === "string" ? JSON.stringify(initialPageParam) : initialPageParam}`, getNextPageParamExpr, getPreviousPageParamExpr ].filter(Boolean); const infiniteOverrideParams = queryParam && typeSchemas.queryParams?.name ? ` params = { ...(params ?? {}), ['${queryParam}']: pageParam as unknown as ${typeSchemas.queryParams?.name}['${queryParam}'], } as ${typeSchemas.queryParams?.name}` : ""; const enabled = Object.entries(queryKeyParams.flatParams).map(([key, item]) => item && !item.optional ? key : void 0).filter(Boolean).join("&& "); const enabledText = enabled ? `enabled: !!(${enabled}),` : ""; if (infiniteOverrideParams) return /* @__PURE__ */ jsx(File.Source, { name, isExportable: true, isIndexable: true, children: /* @__PURE__ */ jsx(Function, { name, export: true, params: params.toConstructor(), children: ` const queryKey = ${queryKeyName}(${queryKeyParams.toCall()}) return infiniteQueryOptions<${queryFnDataType}, ${errorType}, InfiniteData<${queryFnDataType}>, typeof queryKey, ${pageParamType}>({ ${enabledText} queryKey, queryFn: async ({ signal, pageParam }) => { config.signal = signal ${infiniteOverrideParams} return ${clientName}(${clientParams.toCall()}) }, ${queryOptions.join(",\n")} }) ` }) }); return /* @__PURE__ */ jsx(File.Source, { name, isExportable: true, isIndexable: true, children: /* @__PURE__ */ jsx(Function, { name, export: true, params: params.toConstructor(), children: ` const queryKey = ${queryKeyName}(${queryKeyParams.toCall()}) return infiniteQueryOptions<${queryFnDataType}, ${errorType}, InfiniteData<${queryFnDataType}>, typeof queryKey, ${pageParamType}>({ ${enabledText} queryKey, queryFn: async ({ signal }) => { config.signal = signal return ${clientName}(${clientParams.toCall()}) }, ${queryOptions.join(",\n")} }) ` }) }); } SuspenseInfiniteQueryOptions.getParams = getParams$1; //#endregion //#region src/components/SuspenseQuery.tsx function getParams({ paramsType, paramsCasing, pathParamsType, dataReturnType, typeSchemas }) { const TData = dataReturnType === "data" ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`; const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(" | ") || "Error"}>`; if (paramsType === "object") return FunctionParams.factory({ data: { mode: "object", children: { ...getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), data: typeSchemas.request?.name ? { type: typeSchemas.request?.name, optional: isOptional(typeSchemas.request?.schema) } : void 0, params: typeSchemas.queryParams?.name ? { type: typeSchemas.queryParams?.name, optional: isOptional(typeSchemas.queryParams?.schema) } : void 0, headers: typeSchemas.headerParams?.name ? { type: typeSchemas.headerParams?.name, optional: isOptional(typeSchemas.headerParams?.schema) } : void 0 } }, options: { type: ` { query?: Partial<UseSuspenseQueryOptions<${[ TData, TError, "TData", "TQueryKey" ].join(", ")}>> & { client?: QueryClient }, client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof fetch }` : "Partial<RequestConfig> & { client?: typeof fetch }"} } `, default: "{}" } }); return FunctionParams.factory({ pathParams: typeSchemas.pathParams?.name ? { mode: pathParamsType === "object" ? "object" : "inlineSpread", children: getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), optional: isOptional(typeSchemas.pathParams?.schema) } : void 0, data: typeSchemas.request?.name ? { type: typeSchemas.request?.name, optional: isOptional(typeSchemas.request?.schema) } : void 0, params: typeSchemas.queryParams?.name ? { type: typeSchemas.queryParams?.name, optional: isOptional(typeSchemas.queryParams?.schema) } : void 0, headers: typeSchemas.headerParams?.name ? { type: typeSchemas.headerParams?.name, optional: isOptional(typeSchemas.headerParams?.schema) } : void 0, options: { type: ` { query?: Partial<UseSuspenseQueryOptions<${[ TData, TError, "TData", "TQueryKey" ].join(", ")}>> & { client?: QueryClient }, client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof fetch }` : "Partial<RequestConfig> & { client?: typeof fetch }"} } `, default: "{}" } }); } /** * Generates a strongly-typed React Query Suspense hook function for an OpenAPI operation. * * The generated function wraps `useSuspenseQuery`, providing type-safe parameters and return types based on the supplied OpenAPI schemas and configuration. * * @returns A React component source node containing the generated query function. */ function SuspenseQuery({ name, queryKeyTypeName, queryOptionsName, queryKeyName, paramsType, paramsCasing, pathParamsType, dataReturnType, typeSchemas, operation }) { const TData = dataReturnType === "data" ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`; const returnType = `UseSuspenseQueryResult<${["TData", `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(" | ") || "Error"}>`].join(", ")}> & { queryKey: TQueryKey }`; const generics = [`TData = ${TData}`, `TQueryKey extends QueryKey = ${queryKeyTypeName}`]; const queryKeyParams = QueryKey.getParams({ pathParamsType, typeSchemas, paramsCasing }); const queryOptionsParams = QueryOptions.getParams({ paramsCasing, paramsType, pathParamsType, typeSchemas }); const params = getParams({ paramsCasing, paramsType, pathParamsType, dataReturnType, typeSchemas }); const queryOptions = `${queryOptionsName}(${queryOptionsParams.toCall()})`; return /* @__PURE__ */ jsx(File.Source, { name, isExportable: true, isIndexable: true, children: /* @__PURE__ */ jsx(Function, { name, export: true, generics: generics.join(", "), params: params.toConstructor(), JSDoc: { comments: getComments(operation) }, children: ` const { query: queryConfig = {}, client: config = {} } = options ?? {} const { client: queryClient, ...queryOptions } = queryConfig const queryKey = queryOptions?.queryKey ?? ${queryKeyName}(${queryKeyParams.toCall()}) const query = useSuspenseQuery({ ...${queryOptions}, queryKey, ...queryOptions } as unknown as UseSuspenseQueryOptions, queryClient) as ${returnType} query.queryKey = queryKey as TQueryKey return query ` }) }); } SuspenseQuery.getParams = getParams; //#endregion export { Mutation as a, InfiniteQueryOptions as c, QueryKey as d, Query as i, InfiniteQuery as l, SuspenseInfiniteQueryOptions as n, MutationOptions as o, SuspenseInfiniteQuery as r, MutationKey as s, SuspenseQuery as t, QueryOptions as u }; //# sourceMappingURL=components-DQHnoHX_.js.map