@kubb/plugin-react-query
Version:
Generator react-query hooks
958 lines (946 loc) • 38.8 kB
JavaScript
import { File, Function, FunctionParams, Const, Type } from '@kubb/react';
import { isOptional } from '@kubb/oas';
import { URLPath } from '@kubb/core/utils';
import { getComments, getPathParams } from '@kubb/plugin-oas/utils';
import { jsx, jsxs, Fragment } from '@kubb/react/jsx-runtime';
// src/components/Mutation.tsx
function getParams({ paramsType, paramsCasing, pathParamsType, typeSchemas }) {
if (paramsType === "object") {
return FunctionParams.factory({
data: {
mode: "object",
children: {
...getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing })
}
}
});
}
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
});
}
function Url({ name, isExportable = true, isIndexable = true, typeSchemas, baseURL, paramsType, paramsCasing, pathParamsType, operation }) {
const path = new URLPath(operation.path, { casing: paramsCasing });
const params = getParams({ paramsType, paramsCasing, pathParamsType, typeSchemas });
return /* @__PURE__ */ jsx(File.Source, { name, isExportable, isIndexable, children: /* @__PURE__ */ jsx(Function, { name, export: isExportable, params: params.toConstructor(), children: `return ${path.toTemplateString({ prefix: baseURL })} as const` }) });
}
Url.getParams = getParams;
function getParams2({ paramsType, paramsCasing, pathParamsType, typeSchemas, isConfigurable }) {
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: isConfigurable ? {
type: typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof client }` : "Partial<RequestConfig> & { client?: typeof client }",
default: "{}"
} : void 0
});
}
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: isConfigurable ? {
type: typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof client }` : "Partial<RequestConfig> & { client?: typeof client }",
default: "{}"
} : void 0
});
}
function Client({
name,
isExportable = true,
isIndexable = true,
returnType,
typeSchemas,
baseURL,
dataReturnType,
parser,
zodSchemas,
paramsType,
paramsCasing,
pathParamsType,
operation,
urlName,
children,
isConfigurable = true
}) {
const path = new URLPath(operation.path, { casing: paramsCasing });
const contentType = operation.getContentType();
const isFormData = contentType === "multipart/form-data";
const headers = [
contentType !== "application/json" ? `'Content-Type': '${contentType}'` : void 0,
typeSchemas.headerParams?.name ? "...headers" : void 0
].filter(Boolean);
const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(" | ") || "Error"}>`;
const generics = [typeSchemas.response.name, TError, typeSchemas.request?.name || "unknown"].filter(Boolean);
const params = getParams2({ paramsType, paramsCasing, pathParamsType, typeSchemas, isConfigurable });
const urlParams = Url.getParams({
paramsType,
paramsCasing,
pathParamsType,
typeSchemas
});
const clientParams = FunctionParams.factory({
config: {
mode: "object",
children: {
method: {
value: JSON.stringify(operation.method.toUpperCase())
},
url: {
value: urlName ? `${urlName}(${urlParams.toCall()}).toString()` : path.template
},
baseURL: baseURL && !urlName ? {
value: JSON.stringify(baseURL)
} : void 0,
params: typeSchemas.queryParams?.name ? {} : void 0,
data: typeSchemas.request?.name ? {
value: parser === "zod" && zodSchemas ? `${zodSchemas.request?.name}.parse(${isFormData ? "formData" : "data"})` : isFormData ? "formData" : void 0
} : void 0,
requestConfig: isConfigurable ? {
mode: "inlineSpread"
} : void 0,
headers: headers.length ? {
value: isConfigurable ? `{ ${headers.join(", ")}, ...requestConfig.headers }` : `{ ${headers.join(", ")} }`
} : void 0
}
}
});
const formData = isFormData ? `
const formData = new FormData()
if(data) {
Object.keys(data).forEach((key) => {
const value = data[key as keyof typeof data];
if (typeof key === "string" && (typeof value === "string" || (value as Blob) instanceof Blob)) {
formData.append(key, value as unknown as string);
}
})
}
` : "";
const childrenElement = children ? children : /* @__PURE__ */ jsxs(Fragment, { children: [
dataReturnType === "full" && parser === "zod" && zodSchemas && `return {...res, data: ${zodSchemas.response.name}.parse(res.data)}`,
dataReturnType === "data" && parser === "zod" && zodSchemas && `return ${zodSchemas.response.name}.parse(res.data)`,
dataReturnType === "full" && parser === "client" && "return res",
dataReturnType === "data" && parser === "client" && "return res.data"
] });
return /* @__PURE__ */ jsx(File.Source, { name, isExportable, isIndexable, children: /* @__PURE__ */ jsxs(
Function,
{
name,
async: true,
export: isExportable,
params: params.toConstructor(),
JSDoc: {
comments: getComments(operation)
},
returnType,
children: [
isConfigurable ? "const { client:request = client, ...requestConfig } = config" : "",
/* @__PURE__ */ jsx("br", {}),
/* @__PURE__ */ jsx("br", {}),
formData,
isConfigurable ? `const res = await request<${generics.join(", ")}>(${clientParams.toCall()})` : `const res = await client<${generics.join(", ")}>(${clientParams.toCall()})`,
/* @__PURE__ */ jsx("br", {}),
childrenElement
]
}
) });
}
Client.getParams = getParams2;
function Operations({ name, operations }) {
const operationsObject = {};
operations.forEach((operation) => {
operationsObject[operation.getOperationId()] = {
path: new URLPath(operation.path).URL,
method: operation.method
};
});
return /* @__PURE__ */ jsx(File.Source, { name, isExportable: true, isIndexable: true, children: /* @__PURE__ */ jsx(Const, { name, export: true, asConst: true, children: JSON.stringify(operationsObject, void 0, 2) }) });
}
function getParams3({}) {
return FunctionParams.factory({});
}
var getTransformer = ({ operation, casing }) => {
const path = new URLPath(operation.path, { casing });
return [JSON.stringify({ url: path.path })].filter(Boolean);
};
function MutationKey({ name, typeSchemas, pathParamsType, paramsCasing, operation, typeName, transformer = getTransformer }) {
const params = getParams3({ });
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 = getParams3;
MutationKey.getTransformer = getTransformer;
function getParams4({ paramsCasing, dataReturnType, typeSchemas }) {
const TData = dataReturnType === "data" ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`;
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 TRequest = mutationParams.toConstructor();
const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(" | ") || "Error"}>`;
const generics = [TData, TError, 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 client }` : "Partial<RequestConfig> & { client?: typeof client }"},
}
`,
default: "{}"
}
});
}
function Mutation({
name,
clientName,
paramsCasing,
paramsType,
pathParamsType,
dataReturnType,
typeSchemas,
operation,
mutationKeyName
}) {
const mutationKeyParams = MutationKey.getParams({
pathParamsType,
typeSchemas
});
const params = getParams4({
paramsCasing,
dataReturnType,
typeSchemas
});
const clientParams = Client.getParams({
paramsCasing,
paramsType,
typeSchemas,
pathParamsType,
isConfigurable: true
});
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: {
// No use of pathParams because useMutation can only take one argument in object form,
// see https://tanstack.com/query/latest/docs/framework/react/reference/useMutation#usemutation
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();
const TData = dataReturnType === "data" ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`;
const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(" | ") || "Error"}>`;
const generics = [TData, TError, TRequest ? `{${TRequest}}` : "void", "TContext"].join(", ");
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: queryClient, ...mutationOptions } = {}, client: config = {} } = options ?? {}
const mutationKey = mutationOptions?.mutationKey ?? ${mutationKeyName}(${mutationKeyParams.toCall()})
return useMutation<${generics}>({
mutationFn: async(${dataParams.toConstructor()}) => {
return ${clientName}(${clientParams.toCall()})
},
mutationKey,
...mutationOptions
}, queryClient)
`
}
) });
}
function getParams5({ 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
});
}
var getTransformer2 = ({ operation, schemas, casing }) => {
const path = new URLPath(operation.path, { casing });
const keys = [
path.toObject({
type: "path",
stringify: true
}),
schemas.queryParams?.name ? "...(params ? [params] : [])" : void 0,
schemas.request?.name ? "...(data ? [data] : [])" : void 0
].filter(Boolean);
return keys;
};
function QueryKey({ name, typeSchemas, paramsCasing, pathParamsType, operation, typeName, transformer = getTransformer2 }) {
const params = getParams5({ 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 = getParams5;
QueryKey.getTransformer = getTransformer2;
function getParams6({ 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 client }` : "Partial<RequestConfig> & { client?: typeof client }",
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 client }` : "Partial<RequestConfig> & { client?: typeof client }",
default: "{}"
}
});
}
function QueryOptions({ name, clientName, dataReturnType, typeSchemas, paramsCasing, paramsType, pathParamsType, queryKeyName }) {
const params = getParams6({ 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 = getParams6;
function getParams7({ 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 client }` : "Partial<RequestConfig> & { client?: typeof client }"}
}
`,
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 client }` : "Partial<RequestConfig> & { client?: typeof client }"}
}
`,
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 TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(" | ") || "Error"}>`;
const returnType = `UseQueryResult<${["TData", TError].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 = getParams7({
paramsCasing,
paramsType,
pathParamsType,
dataReturnType,
typeSchemas
});
const queryOptions = `${queryOptionsName}(${queryOptionsParams.toCall()}) as unknown as QueryObserverOptions`;
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: { client: queryClient, ...queryOptions } = {}, client: config = {} } = options ?? {}
const queryKey = queryOptions?.queryKey ?? ${queryKeyName}(${queryKeyParams.toCall()})
const query = useQuery({
...${queryOptions},
queryKey,
...queryOptions as unknown as Omit<QueryObserverOptions, "queryKey">
}, queryClient) as ${returnType}
query.queryKey = queryKey as TQueryKey
return query
`
}
) });
}
Query.getParams = getParams7;
function getParams8({ 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 client }` : "Partial<RequestConfig> & { client?: typeof client }",
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 client }` : "Partial<RequestConfig> & { client?: typeof client }",
default: "{}"
}
});
}
function InfiniteQueryOptions({
name,
clientName,
initialPageParam,
cursorParam,
typeSchemas,
paramsCasing,
paramsType,
dataReturnType,
pathParamsType,
queryParam,
queryKeyName
}) {
const TData = dataReturnType === "data" ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`;
const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(" | ") || "Error"}>`;
const params = getParams8({ paramsType, paramsCasing, pathParamsType, typeSchemas });
const clientParams = Client.getParams({
paramsCasing,
typeSchemas,
paramsType,
pathParamsType,
isConfigurable: true
});
const queryKeyParams = QueryKey.getParams({
pathParamsType,
typeSchemas,
paramsCasing
});
const queryOptions = [
`initialPageParam: ${typeof initialPageParam === "string" ? JSON.stringify(initialPageParam) : initialPageParam}`,
cursorParam ? `getNextPageParam: (lastPage) => lastPage['${cursorParam}']` : void 0,
cursorParam ? `getPreviousPageParam: (firstPage) => firstPage['${cursorParam}']` : void 0,
!cursorParam && dataReturnType === "full" ? "getNextPageParam: (lastPage, _allPages, lastPageParam) => Array.isArray(lastPage.data) && lastPage.data.length === 0 ? undefined : lastPageParam + 1" : void 0,
!cursorParam && dataReturnType === "data" ? "getNextPageParam: (lastPage, _allPages, lastPageParam) => Array.isArray(lastPage) && lastPage.length === 0 ? undefined : lastPageParam + 1" : void 0,
!cursorParam ? "getPreviousPageParam: (_firstPage, _allPages, firstPageParam) => firstPageParam <= 1 ? undefined : firstPageParam - 1" : void 0
].filter(Boolean);
const infiniteOverrideParams = queryParam && typeSchemas.queryParams?.name ? `
if(params) {
params['${queryParam}'] = pageParam as unknown as ${typeSchemas.queryParams?.name}['${queryParam}']
}` : "";
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<${TData}, ${TError}, ${TData}, typeof queryKey, number>({
${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<${TData}, ${TError}, ${TData}, typeof queryKey>({
${enabledText}
queryKey,
queryFn: async ({ signal }) => {
config.signal = signal
return ${clientName}(${clientParams.toCall()})
},
${queryOptions.join(",\n")}
})
` }) });
}
InfiniteQueryOptions.getParams = getParams8;
function getParams9({ 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<InfiniteQueryObserverOptions<${[TData, TError, "TData", "TQueryData", "TQueryKey"].join(", ")}>> & { client?: QueryClient },
client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof client }` : "Partial<RequestConfig> & { client?: typeof client }"}
}
`,
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<${[TData, TError, "TData", "TQueryData", "TQueryKey"].join(", ")}>> & { client?: QueryClient },
client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof client }` : "Partial<RequestConfig> & { client?: typeof client }"}
}
`,
default: "{}"
}
});
}
function InfiniteQuery({
name,
queryKeyTypeName,
queryOptionsName,
queryKeyName,
paramsType,
paramsCasing,
pathParamsType,
dataReturnType,
typeSchemas,
operation
}) {
const TData = dataReturnType === "data" ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`;
const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(" | ") || "Error"}>`;
const returnType = `UseInfiniteQueryResult<${["TData", TError].join(", ")}> & { queryKey: TQueryKey }`;
const generics = [`TData = InfiniteData<${TData}>`, `TQueryData = ${TData}`, `TQueryKey extends QueryKey = ${queryKeyTypeName}`];
const queryKeyParams = QueryKey.getParams({
pathParamsType,
typeSchemas,
paramsCasing
});
const queryOptionsParams = QueryOptions.getParams({
paramsType,
pathParamsType,
typeSchemas,
paramsCasing
});
const params = getParams9({
paramsCasing,
paramsType,
pathParamsType,
dataReturnType,
typeSchemas
});
const queryOptions = `${queryOptionsName}(${queryOptionsParams.toCall()}) as unknown as InfiniteQueryObserverOptions`;
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: { client: queryClient, ...queryOptions } = {}, client: config = {} } = options ?? {}
const queryKey = queryOptions?.queryKey ?? ${queryKeyName}(${queryKeyParams.toCall()})
const query = useInfiniteQuery({
...${queryOptions},
queryKey,
...queryOptions as unknown as Omit<InfiniteQueryObserverOptions, "queryKey">
}, queryClient) as ${returnType}
query.queryKey = queryKey as TQueryKey
return query
`
}
) });
}
InfiniteQuery.getParams = getParams9;
function getParams10({ 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 client }` : "Partial<RequestConfig> & { client?: typeof client }"}
}
`,
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 client }` : "Partial<RequestConfig> & { client?: typeof client }"}
}
`,
default: "{}"
}
});
}
function SuspenseQuery({
name,
queryKeyTypeName,
queryOptionsName,
queryKeyName,
paramsType,
paramsCasing,
pathParamsType,
dataReturnType,
typeSchemas,
operation
}) {
const TData = dataReturnType === "data" ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`;
const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(" | ") || "Error"}>`;
const returnType = `UseSuspenseQueryResult<${["TData", TError].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 = getParams10({
paramsCasing,
paramsType,
pathParamsType,
dataReturnType,
typeSchemas
});
const queryOptions = `${queryOptionsName}(${queryOptionsParams.toCall()}) as unknown as UseSuspenseQueryOptions`;
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: { client: queryClient, ...queryOptions } = {}, client: config = {} } = options ?? {}
const queryKey = queryOptions?.queryKey ?? ${queryKeyName}(${queryKeyParams.toCall()})
const query = useSuspenseQuery({
...${queryOptions},
queryKey,
...queryOptions as unknown as Omit<UseSuspenseQueryOptions, "queryKey">
}, queryClient) as ${returnType}
query.queryKey = queryKey as TQueryKey
return query
`
}
) });
}
SuspenseQuery.getParams = getParams10;
export { Client, InfiniteQuery, InfiniteQueryOptions, Mutation, MutationKey, Operations, Query, QueryKey, QueryOptions, SuspenseQuery, Url };
//# sourceMappingURL=chunk-B5HX4HGF.js.map
//# sourceMappingURL=chunk-B5HX4HGF.js.map