@kubb/plugin-swr
Version:
SWR hooks generator plugin for Kubb, creating type-safe data fetching hooks from OpenAPI specifications for React and Next.js applications.
537 lines (527 loc) • 17.3 kB
JavaScript
import { t as __name } from "./chunk-DKWOrOAv.js";
import { getDefaultValue, isOptional } from "@kubb/oas";
import { Client } from "@kubb/plugin-client/components";
import { getComments, getPathParams } from "@kubb/plugin-oas/utils";
import { File, Function, FunctionParams, Type } from "@kubb/react-fabric";
import { URLPath } from "@kubb/core/utils";
import { Fragment, jsx, jsxs } from "@kubb/react-fabric/jsx-runtime";
//#region src/components/MutationKey.tsx
function getParams$4({}) {
return FunctionParams.factory({});
}
__name(getParams$4, "getParams");
const getTransformer$1 = /* @__PURE__ */ __name(({ operation, casing }) => {
return [`{ url: '${new URLPath(operation.path, { casing }).toURLPath()}' }`];
}, "getTransformer");
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({ pathParamsType, paramsCasing, dataReturnType, typeSchemas, mutationKeyTypeName }) {
const TData = dataReturnType === "data" ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`;
const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(" | ") || "Error"}>`;
const TExtraArg = typeSchemas.request?.name || "never";
return FunctionParams.factory({
pathParams: {
mode: pathParamsType === "object" ? "object" : "inlineSpread",
children: getPathParams(typeSchemas.pathParams, {
typed: true,
casing: paramsCasing
})
},
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: `
{
mutation?: SWRMutationConfiguration<${TData}, ${TError}, ${mutationKeyTypeName} | null, ${TExtraArg}> & { throwOnError?: boolean },
client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }` : "Partial<RequestConfig> & { client?: Client }"},
shouldFetch?: boolean,
}
`,
default: "{}"
}
});
}
__name(getParams$3, "getParams");
function getTriggerParams({ dataReturnType, typeSchemas, mutationKeyTypeName, mutationArgTypeName }) {
const TData = dataReturnType === "data" ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`;
const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(" | ") || "Error"}>`;
return FunctionParams.factory({ options: {
type: `
{
mutation?: SWRMutationConfiguration<${TData}, ${TError}, ${mutationKeyTypeName} | null, ${mutationArgTypeName}> & { throwOnError?: boolean },
client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }` : "Partial<RequestConfig> & { client?: Client }"},
shouldFetch?: boolean,
}
`,
default: "{}"
} });
}
function getMutationParams({ paramsCasing, typeSchemas }) {
return 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
});
}
function Mutation({ name, clientName, mutationKeyName, mutationKeyTypeName, paramsType, paramsCasing, pathParamsType, dataReturnType, typeSchemas, operation, paramsToTrigger = false }) {
const TData = dataReturnType === "data" ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`;
const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(" | ") || "Error"}>`;
const mutationKeyParams = MutationKey.getParams({
pathParamsType,
typeSchemas
});
const clientParams = Client.getParams({
paramsCasing,
paramsType,
typeSchemas,
pathParamsType,
isConfigurable: true
});
if (paramsToTrigger) {
const mutationParams = getMutationParams({
paramsCasing,
typeSchemas
});
const mutationArgTypeName = `${mutationKeyTypeName.replace("MutationKey", "")}MutationArg`;
const hasMutationParams = Object.keys(mutationParams.params).length > 0;
const argParams = 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 params = getTriggerParams({
dataReturnType,
typeSchemas,
mutationKeyTypeName,
mutationArgTypeName
});
const generics = [
TData,
TError,
`${mutationKeyTypeName} | null`,
mutationArgTypeName
].filter(Boolean);
const mutationArg = mutationParams.toConstructor();
return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(File.Source, {
name: mutationArgTypeName,
isExportable: true,
isIndexable: true,
isTypeOnly: true,
children: /* @__PURE__ */ jsx(Type, {
name: mutationArgTypeName,
export: true,
children: hasMutationParams ? `{${mutationArg}}` : "never"
})
}), /* @__PURE__ */ jsx(File.Source, {
name,
isExportable: true,
isIndexable: true,
children: /* @__PURE__ */ jsx(Function, {
name,
export: true,
params: params.toConstructor(),
JSDoc: { comments: getComments(operation) },
children: `
const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {}
const mutationKey = ${mutationKeyName}(${mutationKeyParams.toCall()})
return useSWRMutation<${generics.join(", ")}>(
shouldFetch ? mutationKey : null,
async (_url${hasMutationParams ? `, { arg: ${argParams.toCall()} }` : ""}) => {
return ${clientName}(${clientParams.toCall()})
},
mutationOptions
)
`
})
})] });
}
const generics = [
TData,
TError,
`${mutationKeyTypeName} | null`,
typeSchemas.request?.name
].filter(Boolean);
const params = getParams$3({
paramsCasing,
pathParamsType,
dataReturnType,
typeSchemas,
mutationKeyTypeName
});
return /* @__PURE__ */ jsx(File.Source, {
name,
isExportable: true,
isIndexable: true,
children: /* @__PURE__ */ jsx(Function, {
name,
export: true,
params: params.toConstructor(),
JSDoc: { comments: getComments(operation) },
children: `
const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {}
const mutationKey = ${mutationKeyName}(${mutationKeyParams.toCall()})
return useSWRMutation<${generics.join(", ")}>(
shouldFetch ? mutationKey : null,
async (_url${typeSchemas.request?.name ? ", { arg: data }" : ""}) => {
return ${clientName}(${clientParams.toCall()})
},
mutationOptions
)
`
})
});
}
//#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
});
}
__name(getParams$2, "getParams");
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") {
const pathParams = getPathParams(typeSchemas.pathParams, {
typed: true,
casing: paramsCasing
});
return FunctionParams.factory({
data: {
mode: "object",
children: {
...pathParams,
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?: Client }` : "Partial<RequestConfig> & { client?: Client }",
default: "{}"
}
});
}
return FunctionParams.factory({
pathParams: typeSchemas.pathParams?.name ? {
mode: pathParamsType === "object" ? "object" : "inlineSpread",
children: getPathParams(typeSchemas.pathParams, {
typed: true,
casing: paramsCasing
}),
default: getDefaultValue(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?: Client }` : "Partial<RequestConfig> & { client?: Client }",
default: "{}"
}
});
}
__name(getParams$1, "getParams");
function QueryOptions({ name, clientName, typeSchemas, paramsCasing, paramsType, pathParamsType }) {
const params = getParams$1({
paramsType,
paramsCasing,
pathParamsType,
typeSchemas
});
const clientParams = Client.getParams({
paramsCasing,
paramsType,
typeSchemas,
pathParamsType,
isConfigurable: true
});
return /* @__PURE__ */ jsx(File.Source, {
name,
isExportable: true,
isIndexable: true,
children: /* @__PURE__ */ jsx(Function, {
name,
export: true,
params: params.toConstructor(),
children: `
return {
fetcher: async () => {
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") {
const 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
};
const allChildrenAreOptional = Object.values(children).every((child) => !child || child.optional);
return FunctionParams.factory({
data: {
mode: "object",
children,
default: allChildrenAreOptional ? "{}" : void 0
},
options: {
type: `
{
query?: Parameters<typeof useSWR<${[TData, TError].join(", ")}>>[2],
client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }` : "Partial<RequestConfig> & { client?: Client }"},
shouldFetch?: boolean,
immutable?: boolean
}
`,
default: "{}"
}
});
}
return FunctionParams.factory({
pathParams: typeSchemas.pathParams?.name ? {
mode: pathParamsType === "object" ? "object" : "inlineSpread",
children: getPathParams(typeSchemas.pathParams, {
typed: true,
casing: paramsCasing
}),
default: getDefaultValue(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?: Parameters<typeof useSWR<${[TData, TError].join(", ")}>>[2],
client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }` : "Partial<RequestConfig> & { client?: Client }"},
shouldFetch?: boolean,
immutable?: boolean
}
`,
default: "{}"
}
});
}
function Query({ name, typeSchemas, queryKeyName, queryKeyTypeName, queryOptionsName, operation, dataReturnType, paramsType, paramsCasing, pathParamsType }) {
const generics = [
dataReturnType === "data" ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`,
`ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(" | ") || "Error"}>`,
`${queryKeyTypeName} | null`
];
const queryKeyParams = QueryKey.getParams({
pathParamsType,
typeSchemas,
paramsCasing
});
const params = getParams({
paramsCasing,
paramsType,
pathParamsType,
dataReturnType,
typeSchemas
});
const queryOptionsParams = QueryOptions.getParams({
paramsCasing,
paramsType,
pathParamsType,
typeSchemas
});
return /* @__PURE__ */ jsx(File.Source, {
name,
isExportable: true,
isIndexable: true,
children: /* @__PURE__ */ jsx(Function, {
name,
export: true,
params: params.toConstructor(),
JSDoc: { comments: getComments(operation) },
children: `
const { query: queryOptions, client: config = {}, shouldFetch = true, immutable } = options ?? {}
const queryKey = ${queryKeyName}(${queryKeyParams.toCall()})
return useSWR<${generics.join(", ")}>(
shouldFetch ? queryKey : null,
{
...${queryOptionsName}(${queryOptionsParams.toCall()}),
...(immutable ? {
revalidateIfStale: false,
revalidateOnFocus: false,
revalidateOnReconnect: false
} : { }),
...queryOptions
}
)
`
})
});
}
//#endregion
export { MutationKey as a, Mutation as i, QueryOptions as n, QueryKey as r, Query as t };
//# sourceMappingURL=Query-DDIFmxNc.js.map