@kubb/plugin-vue-query
Version:
Vue Query hooks generator plugin for Kubb, creating type-safe API client hooks from OpenAPI specifications for Vue.js applications.
863 lines (848 loc) • 30.8 kB
JavaScript
const require_generators = require('./generators-BSkCG7hV.cjs');
let _kubb_core_utils = require("@kubb/core/utils");
let _kubb_oas = require("@kubb/oas");
let _kubb_plugin_oas_utils = require("@kubb/plugin-oas/utils");
let _kubb_react_fabric = require("@kubb/react-fabric");
let _kubb_react_fabric_jsx_runtime = require("@kubb/react-fabric/jsx-runtime");
let _kubb_plugin_client_components = require("@kubb/plugin-client/components");
//#region src/components/QueryKey.tsx
function getParams$6({ pathParamsType, paramsCasing, typeSchemas }) {
return _kubb_react_fabric.FunctionParams.factory({
pathParams: {
mode: pathParamsType === "object" ? "object" : "inlineSpread",
children: (0, _kubb_plugin_oas_utils.getPathParams)(typeSchemas.pathParams, {
typed: true,
casing: paramsCasing,
override(item) {
return {
...item,
type: `MaybeRefOrGetter<${item.type}>`
};
}
})
},
data: typeSchemas.request?.name ? {
type: `MaybeRefOrGetter<${typeSchemas.request?.name}>`,
optional: (0, _kubb_oas.isOptional)(typeSchemas.request?.schema)
} : void 0,
params: typeSchemas.queryParams?.name ? {
type: `MaybeRefOrGetter<${typeSchemas.queryParams?.name}>`,
optional: (0, _kubb_oas.isOptional)(typeSchemas.queryParams?.schema)
} : void 0
});
}
const getTransformer$1 = ({ operation, schemas, casing }) => {
return [
new _kubb_core_utils.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$6({
pathParamsType,
typeSchemas,
paramsCasing
});
const keys = transformer({
operation,
schemas: typeSchemas,
casing: paramsCasing
});
return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Source, {
name,
isExportable: true,
isIndexable: true,
children: /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.Function.Arrow, {
name,
export: true,
params: params.toConstructor(),
singleLine: true,
children: `[${keys.join(", ")}] as const`
})
}), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Source, {
name: typeName,
isExportable: true,
isIndexable: true,
isTypeOnly: true,
children: /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.Type, {
name: typeName,
export: true,
children: `ReturnType<typeof ${name}>`
})
})] });
}
QueryKey.getParams = getParams$6;
QueryKey.getTransformer = getTransformer$1;
//#endregion
//#region src/components/QueryOptions.tsx
function getParams$5({ paramsType, paramsCasing, pathParamsType, typeSchemas }) {
if (paramsType === "object") return _kubb_react_fabric.FunctionParams.factory({
data: {
mode: "object",
children: {
...(0, _kubb_plugin_oas_utils.getPathParams)(typeSchemas.pathParams, {
typed: true,
casing: paramsCasing,
override(item) {
return {
...item,
type: `MaybeRefOrGetter<${item.type}>`
};
}
}),
data: typeSchemas.request?.name ? {
type: `MaybeRefOrGetter<${typeSchemas.request?.name}>`,
optional: (0, _kubb_oas.isOptional)(typeSchemas.request?.schema)
} : void 0,
params: typeSchemas.queryParams?.name ? {
type: `MaybeRefOrGetter<${typeSchemas.queryParams?.name}>`,
optional: (0, _kubb_oas.isOptional)(typeSchemas.queryParams?.schema)
} : void 0,
headers: typeSchemas.headerParams?.name ? {
type: `MaybeRefOrGetter<${typeSchemas.queryParams?.name}>`,
optional: (0, _kubb_oas.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 _kubb_react_fabric.FunctionParams.factory({
pathParams: {
mode: pathParamsType === "object" ? "object" : "inlineSpread",
optional: (0, _kubb_oas.isOptional)(typeSchemas.pathParams?.schema),
children: (0, _kubb_plugin_oas_utils.getPathParams)(typeSchemas.pathParams, {
typed: true,
casing: paramsCasing,
override(item) {
return {
...item,
type: `MaybeRefOrGetter<${item.type}>`
};
}
})
},
data: typeSchemas.request?.name ? {
type: `MaybeRefOrGetter<${typeSchemas.request?.name}>`,
optional: (0, _kubb_oas.isOptional)(typeSchemas.request?.schema)
} : void 0,
params: typeSchemas.queryParams?.name ? {
type: `MaybeRefOrGetter<${typeSchemas.queryParams?.name}>`,
optional: (0, _kubb_oas.isOptional)(typeSchemas.queryParams?.schema)
} : void 0,
headers: typeSchemas.headerParams?.name ? {
type: `MaybeRefOrGetter<${typeSchemas.queryParams?.name}>`,
optional: (0, _kubb_oas.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 TData = dataReturnType === "data" ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`;
const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(" | ") || "Error"}>`;
const params = getParams$5({
paramsType,
paramsCasing,
pathParamsType,
typeSchemas
});
const clientParams = _kubb_plugin_client_components.Client.getParams({
paramsType,
paramsCasing,
typeSchemas,
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__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Source, {
name,
isExportable: true,
isIndexable: true,
children: /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.Function, {
name,
export: true,
params: params.toConstructor(),
children: `
const queryKey = ${queryKeyName}(${queryKeyParams.toCall()})
return queryOptions<${TData}, ${TError}, ${TData}, typeof queryKey>({
${enabledText}
queryKey,
queryFn: async ({ signal }) => {
config.signal = signal
return ${clientName}(${clientParams.toCall({ transformName(name$1) {
return `toValue(${name$1})`;
} })})
},
})
`
})
});
}
QueryOptions.getParams = getParams$5;
//#endregion
//#region src/components/InfiniteQuery.tsx
function getParams$4({ 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 _kubb_react_fabric.FunctionParams.factory({
data: {
mode: "object",
children: {
...(0, _kubb_plugin_oas_utils.getPathParams)(typeSchemas.pathParams, {
typed: true,
casing: paramsCasing,
override(item) {
return {
...item,
type: `MaybeRefOrGetter<${item.type}>`
};
}
}),
data: typeSchemas.request?.name ? {
type: `MaybeRefOrGetter<${typeSchemas.request?.name}>`,
optional: (0, _kubb_oas.isOptional)(typeSchemas.request?.schema)
} : void 0,
params: typeSchemas.queryParams?.name ? {
type: `MaybeRefOrGetter<${typeSchemas.queryParams?.name}>`,
optional: (0, _kubb_oas.isOptional)(typeSchemas.queryParams?.schema)
} : void 0,
headers: typeSchemas.headerParams?.name ? {
type: `MaybeRefOrGetter<${typeSchemas.headerParams?.name}>`,
optional: (0, _kubb_oas.isOptional)(typeSchemas.headerParams?.schema)
} : void 0
}
},
options: {
type: `
{
query?: Partial<UseInfiniteQueryOptions<${[
TData,
TError,
"TQueryData",
"TQueryKey",
"TQueryData"
].join(", ")}>> & { client?: QueryClient },
client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof fetch }` : "Partial<RequestConfig> & { client?: typeof fetch }"}
}
`,
default: "{}"
}
});
return _kubb_react_fabric.FunctionParams.factory({
pathParams: {
mode: pathParamsType === "object" ? "object" : "inlineSpread",
optional: (0, _kubb_oas.isOptional)(typeSchemas.pathParams?.schema),
children: (0, _kubb_plugin_oas_utils.getPathParams)(typeSchemas.pathParams, {
typed: true,
casing: paramsCasing,
override(item) {
return {
...item,
type: `MaybeRefOrGetter<${item.type}>`
};
}
})
},
data: typeSchemas.request?.name ? {
type: `MaybeRefOrGetter<${typeSchemas.request?.name}>`,
optional: (0, _kubb_oas.isOptional)(typeSchemas.request?.schema)
} : void 0,
params: typeSchemas.queryParams?.name ? {
type: `MaybeRefOrGetter<${typeSchemas.queryParams?.name}>`,
optional: (0, _kubb_oas.isOptional)(typeSchemas.queryParams?.schema)
} : void 0,
headers: typeSchemas.headerParams?.name ? {
type: `MaybeRefOrGetter<${typeSchemas.headerParams?.name}>`,
optional: (0, _kubb_oas.isOptional)(typeSchemas.headerParams?.schema)
} : void 0,
options: {
type: `
{
query?: Partial<UseInfiniteQueryOptions<${[
TData,
TError,
"TQueryData",
"TQueryKey",
"TQueryData"
].join(", ")}>> & { 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, pathParamsType, paramsCasing, 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 = `UseInfiniteQueryReturnType<${["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 = getParams$4({
paramsCasing,
paramsType,
pathParamsType,
dataReturnType,
typeSchemas
});
const queryOptions = `${queryOptionsName}(${queryOptionsParams.toCall()})`;
return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Source, {
name,
isExportable: true,
isIndexable: true,
children: /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.Function, {
name,
export: true,
generics: generics.join(", "),
params: params.toConstructor(),
JSDoc: { comments: (0, _kubb_plugin_oas_utils.getComments)(operation) },
children: `
const { query: queryConfig = {}, client: config = {} } = options ?? {}
const { client: queryClient, ...queryOptions } = queryConfig
const queryKey = (queryOptions && 'queryKey' in queryOptions ? toValue(queryOptions.queryKey) : undefined) ?? ${queryKeyName}(${queryKeyParams.toCall()})
const query = useInfiniteQuery({
...${queryOptions},
...queryOptions,
queryKey
} as unknown as UseInfiniteQueryOptions<${TData}, ${TError}, ${TData}, TQueryKey, ${TData}>, toValue(queryClient)) as ${returnType}
query.queryKey = queryKey as TQueryKey
return query
`
})
});
}
InfiniteQuery.getParams = getParams$4;
//#endregion
//#region src/components/InfiniteQueryOptions.tsx
function getParams$3({ paramsType, paramsCasing, pathParamsType, typeSchemas }) {
if (paramsType === "object") return _kubb_react_fabric.FunctionParams.factory({
data: {
mode: "object",
children: {
...(0, _kubb_plugin_oas_utils.getPathParams)(typeSchemas.pathParams, {
typed: true,
casing: paramsCasing,
override(item) {
return {
...item,
type: `MaybeRefOrGetter<${item.type}>`
};
}
}),
data: typeSchemas.request?.name ? {
type: `MaybeRefOrGetter<${typeSchemas.request?.name}>`,
optional: (0, _kubb_oas.isOptional)(typeSchemas.request?.schema)
} : void 0,
params: typeSchemas.queryParams?.name ? {
type: `MaybeRefOrGetter<${typeSchemas.queryParams?.name}>`,
optional: (0, _kubb_oas.isOptional)(typeSchemas.queryParams?.schema)
} : void 0,
headers: typeSchemas.headerParams?.name ? {
type: `MaybeRefOrGetter<${typeSchemas.queryParams?.name}>`,
optional: (0, _kubb_oas.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 _kubb_react_fabric.FunctionParams.factory({
pathParams: {
mode: pathParamsType === "object" ? "object" : "inlineSpread",
optional: (0, _kubb_oas.isOptional)(typeSchemas.pathParams?.schema),
children: (0, _kubb_plugin_oas_utils.getPathParams)(typeSchemas.pathParams, {
typed: true,
casing: paramsCasing,
override(item) {
return {
...item,
type: `MaybeRefOrGetter<${item.type}>`
};
}
})
},
data: typeSchemas.request?.name ? {
type: `MaybeRefOrGetter<${typeSchemas.request?.name}>`,
optional: (0, _kubb_oas.isOptional)(typeSchemas.request?.schema)
} : void 0,
params: typeSchemas.queryParams?.name ? {
type: `MaybeRefOrGetter<${typeSchemas.queryParams?.name}>`,
optional: (0, _kubb_oas.isOptional)(typeSchemas.queryParams?.schema)
} : void 0,
headers: typeSchemas.headerParams?.name ? {
type: `MaybeRefOrGetter<${typeSchemas.headerParams?.name}>`,
optional: (0, _kubb_oas.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, paramsType, paramsCasing, 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 = getParams$3({
paramsType,
paramsCasing,
pathParamsType,
typeSchemas
});
const clientParams = _kubb_plugin_client_components.Client.getParams({
paramsType,
paramsCasing,
typeSchemas,
pathParamsType,
isConfigurable: true
});
const queryKeyParams = QueryKey.getParams({
paramsCasing,
pathParamsType,
typeSchemas
});
const hasNewParams = nextParam !== void 0 || previousParam !== void 0;
let getNextPageParamExpr;
let getPreviousPageParamExpr;
if (hasNewParams) {
if (nextParam) {
const accessor = (0, _kubb_core_utils.getNestedAccessor)(nextParam, "lastPage");
if (accessor) getNextPageParamExpr = `getNextPageParam: (lastPage) => ${accessor}`;
}
if (previousParam) {
const accessor = (0, _kubb_core_utils.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 ? `
if (!params) {
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}),` : "";
return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Source, {
name,
isExportable: true,
isIndexable: true,
children: /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.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({ transformName(name$1) {
return `toValue(${name$1})`;
} })})
},
${queryOptions.join(",\n")}
})
`
})
});
}
InfiniteQueryOptions.getParams = getParams$3;
//#endregion
//#region src/components/MutationKey.tsx
function getParams$2({}) {
return _kubb_react_fabric.FunctionParams.factory({});
}
const getTransformer = ({ operation, casing }) => {
return [`{ url: '${new _kubb_core_utils.URLPath(operation.path, { casing }).toURLPath()}' }`];
};
function MutationKey({ name, typeSchemas, paramsCasing, pathParamsType, operation, typeName, transformer = getTransformer }) {
const params = getParams$2({
pathParamsType,
typeSchemas
});
const keys = transformer({
operation,
schemas: typeSchemas,
casing: paramsCasing
});
return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Source, {
name,
isExportable: true,
isIndexable: true,
children: /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.Function.Arrow, {
name,
export: true,
params: params.toConstructor(),
singleLine: true,
children: `[${keys.join(", ")}] as const`
})
}), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Source, {
name: typeName,
isExportable: true,
isIndexable: true,
isTypeOnly: true,
children: /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.Type, {
name: typeName,
export: true,
children: `ReturnType<typeof ${name}>`
})
})] });
}
MutationKey.getParams = getParams$2;
MutationKey.getTransformer = getTransformer;
//#endregion
//#region src/components/Mutation.tsx
function getParams$1({ paramsCasing, dataReturnType, typeSchemas }) {
const TData = dataReturnType === "data" ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`;
const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(" | ") || "Error"}>`;
const TRequest = _kubb_react_fabric.FunctionParams.factory({
...(0, _kubb_plugin_oas_utils.getPathParams)(typeSchemas.pathParams, {
typed: true,
casing: paramsCasing,
override(item) {
return {
...item,
type: `MaybeRefOrGetter<${item.type}>`
};
}
}),
data: typeSchemas.request?.name ? {
type: `MaybeRefOrGetter<${typeSchemas.request?.name}>`,
optional: (0, _kubb_oas.isOptional)(typeSchemas.request?.schema)
} : void 0,
params: typeSchemas.queryParams?.name ? {
type: `MaybeRefOrGetter<${typeSchemas.queryParams?.name}>`,
optional: (0, _kubb_oas.isOptional)(typeSchemas.queryParams?.schema)
} : void 0,
headers: typeSchemas.headerParams?.name ? {
type: `MaybeRefOrGetter<${typeSchemas.headerParams?.name}>`,
optional: (0, _kubb_oas.isOptional)(typeSchemas.headerParams?.schema)
} : void 0
}).toConstructor();
return _kubb_react_fabric.FunctionParams.factory({ options: {
type: `
{
mutation?: MutationObserverOptions<${[
TData,
TError,
TRequest ? `{${TRequest}}` : "void",
"TContext"
].join(", ")}> & { client?: QueryClient },
client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof fetch }` : "Partial<RequestConfig> & { client?: typeof fetch }"},
}
`,
default: "{}"
} });
}
function Mutation({ name, clientName, paramsCasing, paramsType, pathParamsType, dataReturnType, typeSchemas, operation, mutationKeyName }) {
const mutationKeyParams = MutationKey.getParams({
pathParamsType,
typeSchemas
});
const params = getParams$1({
paramsCasing,
pathParamsType,
dataReturnType,
typeSchemas
});
const clientParams = _kubb_plugin_client_components.Client.getParams({
paramsCasing,
paramsType,
typeSchemas,
pathParamsType,
isConfigurable: true
});
const mutationParams = _kubb_react_fabric.FunctionParams.factory({
...(0, _kubb_plugin_oas_utils.getPathParams)(typeSchemas.pathParams, {
typed: true,
casing: paramsCasing
}),
data: typeSchemas.request?.name ? {
type: typeSchemas.request?.name,
optional: (0, _kubb_oas.isOptional)(typeSchemas.request?.schema)
} : void 0,
params: typeSchemas.queryParams?.name ? {
type: typeSchemas.queryParams?.name,
optional: (0, _kubb_oas.isOptional)(typeSchemas.queryParams?.schema)
} : void 0,
headers: typeSchemas.headerParams?.name ? {
type: typeSchemas.headerParams?.name,
optional: (0, _kubb_oas.isOptional)(typeSchemas.headerParams?.schema)
} : void 0
});
const dataParams = _kubb_react_fabric.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();
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(", ");
return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Source, {
name,
isExportable: true,
isIndexable: true,
children: /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.Function, {
name,
export: true,
params: params.toConstructor(),
JSDoc: { comments: (0, _kubb_plugin_oas_utils.getComments)(operation) },
generics: ["TContext"],
children: `
const { mutation = {}, client: config = {} } = options ?? {}
const { client: queryClient, ...mutationOptions } = mutation;
const mutationKey = mutationOptions?.mutationKey ?? ${mutationKeyName}(${mutationKeyParams.toCall()})
return useMutation<${generics}>({
mutationFn: async(${dataParams.toConstructor()}) => {
return ${clientName}(${clientParams.toCall()})
},
mutationKey,
...mutationOptions
}, queryClient)
`
})
});
}
//#endregion
//#region src/components/Query.tsx
function getParams({ paramsCasing, paramsType, 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 _kubb_react_fabric.FunctionParams.factory({
data: {
mode: "object",
children: {
...(0, _kubb_plugin_oas_utils.getPathParams)(typeSchemas.pathParams, {
typed: true,
casing: paramsCasing,
override(item) {
return {
...item,
type: `MaybeRefOrGetter<${item.type}>`
};
}
}),
data: typeSchemas.request?.name ? {
type: `MaybeRefOrGetter<${typeSchemas.request?.name}>`,
optional: (0, _kubb_oas.isOptional)(typeSchemas.request?.schema)
} : void 0,
params: typeSchemas.queryParams?.name ? {
type: `MaybeRefOrGetter<${typeSchemas.queryParams?.name}>`,
optional: (0, _kubb_oas.isOptional)(typeSchemas.queryParams?.schema)
} : void 0,
headers: typeSchemas.headerParams?.name ? {
type: `MaybeRefOrGetter<${typeSchemas.headerParams?.name}>`,
optional: (0, _kubb_oas.isOptional)(typeSchemas.headerParams?.schema)
} : void 0
}
},
options: {
type: `
{
query?: Partial<UseQueryOptions<${[
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 _kubb_react_fabric.FunctionParams.factory({
pathParams: {
mode: pathParamsType === "object" ? "object" : "inlineSpread",
optional: (0, _kubb_oas.isOptional)(typeSchemas.pathParams?.schema),
children: (0, _kubb_plugin_oas_utils.getPathParams)(typeSchemas.pathParams, {
typed: true,
casing: paramsCasing,
override(item) {
return {
...item,
type: `MaybeRefOrGetter<${item.type}>`
};
}
})
},
data: typeSchemas.request?.name ? {
type: `MaybeRefOrGetter<${typeSchemas.request?.name}>`,
optional: (0, _kubb_oas.isOptional)(typeSchemas.request?.schema)
} : void 0,
params: typeSchemas.queryParams?.name ? {
type: `MaybeRefOrGetter<${typeSchemas.queryParams?.name}>`,
optional: (0, _kubb_oas.isOptional)(typeSchemas.queryParams?.schema)
} : void 0,
headers: typeSchemas.headerParams?.name ? {
type: `MaybeRefOrGetter<${typeSchemas.headerParams?.name}>`,
optional: (0, _kubb_oas.isOptional)(typeSchemas.headerParams?.schema)
} : void 0,
options: {
type: `
{
query?: Partial<UseQueryOptions<${[
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 TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(" | ") || "Error"}>`;
const returnType = `UseQueryReturnType<${["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 = getParams({
paramsCasing,
paramsType,
pathParamsType,
dataReturnType,
typeSchemas
});
const queryOptions = `${queryOptionsName}(${queryOptionsParams.toCall()})`;
return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Source, {
name,
isExportable: true,
isIndexable: true,
children: /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.Function, {
name,
export: true,
generics: generics.join(", "),
params: params.toConstructor(),
JSDoc: { comments: (0, _kubb_plugin_oas_utils.getComments)(operation) },
children: `
const { query: queryConfig = {}, client: config = {} } = options ?? {}
const { client: queryClient, ...queryOptions } = queryConfig
const queryKey = (queryOptions && 'queryKey' in queryOptions ? toValue(queryOptions.queryKey) : undefined) ?? ${queryKeyName}(${queryKeyParams.toCall()})
const query = useQuery({
...${queryOptions},
...queryOptions,
queryKey
} as unknown as UseQueryOptions<${TData}, ${TError}, TData, ${TData}, TQueryKey>, toValue(queryClient)) as ${returnType}
query.queryKey = queryKey as TQueryKey
return query
`
})
});
}
Query.getParams = getParams;
//#endregion
Object.defineProperty(exports, 'InfiniteQuery', {
enumerable: true,
get: function () {
return InfiniteQuery;
}
});
Object.defineProperty(exports, 'InfiniteQueryOptions', {
enumerable: true,
get: function () {
return InfiniteQueryOptions;
}
});
Object.defineProperty(exports, 'Mutation', {
enumerable: true,
get: function () {
return Mutation;
}
});
Object.defineProperty(exports, 'MutationKey', {
enumerable: true,
get: function () {
return MutationKey;
}
});
Object.defineProperty(exports, 'Query', {
enumerable: true,
get: function () {
return Query;
}
});
Object.defineProperty(exports, 'QueryKey', {
enumerable: true,
get: function () {
return QueryKey;
}
});
Object.defineProperty(exports, 'QueryOptions', {
enumerable: true,
get: function () {
return QueryOptions;
}
});
//# sourceMappingURL=components-CpE5cSDx.cjs.map