UNPKG

@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.

367 lines (361 loc) 14.3 kB
'use strict'; var react = require('@kubb/react'); var oas = require('@kubb/oas'); var components = require('@kubb/plugin-client/components'); var utils = require('@kubb/plugin-oas/utils'); var utils$1 = require('@kubb/core/utils'); var jsxRuntime = require('@kubb/react/jsx-runtime'); // src/components/Mutation.tsx function getParams({}) { return react.FunctionParams.factory({}); } var getTransformer = ({ operation, casing }) => { const path = new utils$1.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__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [ /* @__PURE__ */ jsxRuntime.jsx(react.File.Source, { name, isExportable: true, isIndexable: true, children: /* @__PURE__ */ jsxRuntime.jsx(react.Function.Arrow, { name, export: true, params: params.toConstructor(), singleLine: true, children: `[${keys.join(", ")}] as const` }) }), /* @__PURE__ */ jsxRuntime.jsx(react.File.Source, { name: typeName, isExportable: true, isIndexable: true, isTypeOnly: true, children: /* @__PURE__ */ jsxRuntime.jsx(react.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 react.FunctionParams.factory({ pathParams: { mode: pathParamsType === "object" ? "object" : "inlineSpread", children: utils.getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }) }, params: typeSchemas.queryParams?.name ? { type: typeSchemas.queryParams?.name, optional: oas.isOptional(typeSchemas.queryParams?.schema) } : void 0, headers: typeSchemas.headerParams?.name ? { type: typeSchemas.headerParams?.name, optional: oas.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 = components.Client.getParams({ paramsCasing, paramsType, typeSchemas, pathParamsType, isConfigurable: true }); return /* @__PURE__ */ jsxRuntime.jsx(react.File.Source, { name, isExportable: true, isIndexable: true, children: /* @__PURE__ */ jsxRuntime.jsx( react.Function, { name, export: true, params: params.toConstructor(), JSDoc: { comments: utils.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 react.FunctionParams.factory({ pathParams: { mode: pathParamsType === "object" ? "object" : "inlineSpread", children: utils.getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }) }, data: typeSchemas.request?.name ? { type: typeSchemas.request?.name, optional: oas.isOptional(typeSchemas.request?.schema) } : void 0, params: typeSchemas.queryParams?.name ? { type: typeSchemas.queryParams?.name, optional: oas.isOptional(typeSchemas.queryParams?.schema) } : void 0 }); } var getTransformer2 = ({ operation, schemas, casing }) => { const path = new utils$1.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__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [ /* @__PURE__ */ jsxRuntime.jsx(react.File.Source, { name, isExportable: true, isIndexable: true, children: /* @__PURE__ */ jsxRuntime.jsx(react.Function.Arrow, { name, export: true, params: params.toConstructor(), singleLine: true, children: `[${keys.join(", ")}] as const` }) }), /* @__PURE__ */ jsxRuntime.jsx(react.File.Source, { name: typeName, isExportable: true, isIndexable: true, isTypeOnly: true, children: /* @__PURE__ */ jsxRuntime.jsx(react.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 react.FunctionParams.factory({ data: { mode: "object", children: { ...utils.getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), data: typeSchemas.request?.name ? { type: typeSchemas.request?.name, optional: oas.isOptional(typeSchemas.request?.schema) } : void 0, params: typeSchemas.queryParams?.name ? { type: typeSchemas.queryParams?.name, optional: oas.isOptional(typeSchemas.queryParams?.schema) } : void 0, headers: typeSchemas.headerParams?.name ? { type: typeSchemas.headerParams?.name, optional: 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 react.FunctionParams.factory({ pathParams: typeSchemas.pathParams?.name ? { mode: pathParamsType === "object" ? "object" : "inlineSpread", children: utils.getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), optional: oas.isOptional(typeSchemas.pathParams?.schema) } : void 0, data: typeSchemas.request?.name ? { type: typeSchemas.request?.name, optional: oas.isOptional(typeSchemas.request?.schema) } : void 0, params: typeSchemas.queryParams?.name ? { type: typeSchemas.queryParams?.name, optional: oas.isOptional(typeSchemas.queryParams?.schema) } : void 0, headers: typeSchemas.headerParams?.name ? { type: typeSchemas.headerParams?.name, optional: 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, typeSchemas, paramsCasing, paramsType, pathParamsType }) { const params = getParams4({ paramsType, paramsCasing, pathParamsType, typeSchemas }); const clientParams = components.Client.getParams({ paramsCasing, paramsType, typeSchemas, pathParamsType, isConfigurable: true }); return /* @__PURE__ */ jsxRuntime.jsx(react.File.Source, { name, isExportable: true, isIndexable: true, children: /* @__PURE__ */ jsxRuntime.jsx(react.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 react.FunctionParams.factory({ data: { mode: "object", children: { ...utils.getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), data: typeSchemas.request?.name ? { type: typeSchemas.request?.name, optional: oas.isOptional(typeSchemas.request?.schema) } : void 0, params: typeSchemas.queryParams?.name ? { type: typeSchemas.queryParams?.name, optional: oas.isOptional(typeSchemas.queryParams?.schema) } : void 0, headers: typeSchemas.headerParams?.name ? { type: typeSchemas.headerParams?.name, optional: oas.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 react.FunctionParams.factory({ pathParams: typeSchemas.pathParams?.name ? { mode: pathParamsType === "object" ? "object" : "inlineSpread", children: utils.getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), optional: oas.isOptional(typeSchemas.pathParams?.schema) } : void 0, data: typeSchemas.request?.name ? { type: typeSchemas.request?.name, optional: oas.isOptional(typeSchemas.request?.schema) } : void 0, params: typeSchemas.queryParams?.name ? { type: typeSchemas.queryParams?.name, optional: oas.isOptional(typeSchemas.queryParams?.schema) } : void 0, headers: typeSchemas.headerParams?.name ? { type: typeSchemas.headerParams?.name, optional: oas.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__ */ jsxRuntime.jsx(react.File.Source, { name, isExportable: true, isIndexable: true, children: /* @__PURE__ */ jsxRuntime.jsx( react.Function, { name, export: true, params: params.toConstructor(), JSDoc: { comments: utils.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 } ) ` } ) }); } exports.Mutation = Mutation; exports.MutationKey = MutationKey; exports.Query = Query; exports.QueryKey = QueryKey; exports.QueryOptions = QueryOptions; //# sourceMappingURL=chunk-MNOTKHSE.cjs.map //# sourceMappingURL=chunk-MNOTKHSE.cjs.map