@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.
361 lines (356 loc) • 13.8 kB
JavaScript
import { File, Function, Type, FunctionParams } 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 { jsxs, Fragment, jsx } from '@kubb/react/jsx-runtime';
// src/components/Mutation.tsx
function getParams({}) {
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, paramsCasing, pathParamsType, operation, typeName, transformer = getTransformer }) {
const params = getParams({ });
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;
MutationKey.getTransformer = getTransformer;
function getParams2({ 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"}>`;
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?: Parameters<typeof useSWRMutation<${[TData, TError, mutationKeyTypeName, typeSchemas.request?.name].join(", ")}>>[2],
client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof fetch }` : "Partial<RequestConfig> & { client?: typeof fetch }"},
shouldFetch?: boolean,
}
`,
default: "{}"
}
});
}
function Mutation({
name,
clientName,
mutationKeyName,
mutationKeyTypeName,
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 generics = [
TData,
TError,
`${mutationKeyTypeName} | null`,
typeSchemas.request?.name
//arg: data
].filter(Boolean);
const mutationKeyParams = MutationKey.getParams({
pathParamsType,
typeSchemas
});
const params = getParams2({
paramsCasing,
pathParamsType,
dataReturnType,
typeSchemas,
mutationKeyTypeName
});
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(),
JSDoc: {
comments: getComments(operation)
},
children: `
const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {}
const mutationKey = ${mutationKeyName}(${mutationKeyParams.toCall()})
return useSWRMutation<${generics}>(
shouldFetch ? mutationKey : null,
async (_url${typeSchemas.request?.name ? ", { arg: data }" : ""}) => {
return ${clientName}(${clientParams.toCall()})
},
mutationOptions
)
`
}
) });
}
function getParams3({ 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 = getParams3({ 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 = getParams3;
QueryKey.getTransformer = getTransformer2;
function getParams4({ 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, pathParamsType }) {
const params = getParams4({ 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 = getParams4;
function getParams5({ 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?: Parameters<typeof useSWR<${[TData, TError].join(", ")}>>[2],
client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof fetch }` : "Partial<RequestConfig> & { client?: typeof fetch }"},
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 }),
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?: Parameters<typeof useSWR<${[TData, TError].join(", ")}>>[2],
client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof fetch }` : "Partial<RequestConfig> & { client?: typeof fetch }"},
shouldFetch?: boolean,
immutable?: boolean
}
`,
default: "{}"
}
});
}
function Query({
name,
typeSchemas,
queryKeyName,
queryKeyTypeName,
queryOptionsName,
operation,
dataReturnType,
paramsType,
paramsCasing,
pathParamsType
}) {
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, `${queryKeyTypeName} | null`];
const queryKeyParams = QueryKey.getParams({
pathParamsType,
typeSchemas,
paramsCasing
});
const params = getParams5({
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
}
)
`
}
) });
}
export { Mutation, MutationKey, Query, QueryKey, QueryOptions };
//# sourceMappingURL=chunk-RZTUH4QW.js.map
//# sourceMappingURL=chunk-RZTUH4QW.js.map