@kubb/plugin-svelte-query
Version:
Svelte Query hooks generator plugin for Kubb, creating type-safe API client hooks from OpenAPI specifications for Svelte applications.
483 lines (473 loc) • 15.5 kB
JavaScript
import { File, Function, FunctionParams, Type } from "@kubb/react";
import { isOptional } from "@kubb/oas";
import { Client } from "@kubb/plugin-client/components";
import { getComments, getPathParams } from "@kubb/plugin-oas/utils";
import { URLPath } from "@kubb/core/utils";
import { Fragment, jsx, jsxs } from "@kubb/react/jsx-runtime";
//#region src/components/MutationKey.tsx
function getParams$4({}) {
return FunctionParams.factory({});
}
const getTransformer$1 = ({ operation, casing }) => {
return [`{ url: '${new URLPath(operation.path, { casing }).toURLPath()}' }`];
};
function MutationKey({ name, typeSchemas, paramsCasing, pathParamsType, operation, typeName, transformer = getTransformer$1 }) {
const params = getParams$4({
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$4;
MutationKey.getTransformer = getTransformer$1;
//#endregion
//#region src/components/Mutation.tsx
function getParams$3({ 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 = 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();
return FunctionParams.factory({ options: {
type: `
{
mutation?: CreateMutationOptions<${[
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$3({
paramsCasing,
pathParamsType,
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: {
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: config = {} } = options ?? {}
const { client: queryClient, ...mutationOptions } = mutation;
const mutationKey = mutationOptions?.mutationKey ?? ${mutationKeyName}(${mutationKeyParams.toCall()})
return createMutation<${generics}>({
mutationFn: async(${dataParams.toConstructor()}) => {
return ${clientName}(${clientParams.toCall()})
},
mutationKey,
...mutationOptions
}, queryClient)
`
})
});
}
//#endregion
//#region src/components/QueryKey.tsx
function getParams$2({ 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 = ({ 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 }) {
const params = getParams$2({
pathParamsType,
paramsCasing,
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}>`
})
})] });
}
QueryKey.getParams = getParams$2;
QueryKey.getTransformer = getTransformer;
//#endregion
//#region src/components/QueryOptions.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 QueryOptions({ name, clientName, typeSchemas, paramsCasing, paramsType, dataReturnType, 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$1({
paramsType,
paramsCasing,
pathParamsType,
typeSchemas
});
const clientParams = Client.getParams({
paramsType,
paramsCasing,
typeSchemas,
pathParamsType,
isConfigurable: true
});
const queryKeyParams = QueryKey.getParams({
pathParamsType,
paramsCasing,
typeSchemas
});
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}, ${TError}, ${TData}, typeof queryKey>({
${enabledText}
queryKey,
queryFn: async ({ signal }) => {
config.signal = signal
return ${clientName}(${clientParams.toCall()})
},
})
`
})
});
}
QueryOptions.getParams = getParams$1;
//#endregion
//#region src/components/Query.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<CreateBaseQueryOptions<${[
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<CreateBaseQueryOptions<${[
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 = `CreateQueryResult<${["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,
paramsCasing,
typeSchemas
});
const queryOptionsParams = QueryOptions.getParams({
paramsType,
paramsCasing,
pathParamsType,
typeSchemas
});
const params = getParams({
paramsType,
paramsCasing,
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 = createQuery({
...${queryOptions},
queryKey,
...queryOptions
} as unknown as CreateBaseQueryOptions, queryClient) as ${returnType}
query.queryKey = queryKey as TQueryKey
return query
`
})
});
}
Query.getParams = getParams;
//#endregion
export { Mutation, MutationKey, Query, QueryKey, QueryOptions };
//# sourceMappingURL=components-Cu5NltDE.js.map