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.

608 lines (597 loc) 21.1 kB
//#region \0rolldown/runtime.js var __create = Object.create; var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) { key = keys[i]; if (!__hasOwnProp.call(to, key) && key !== except) { __defProp(to, key, { get: ((k) => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } } } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); //#endregion let _kubb_oas = require("@kubb/oas"); let _kubb_plugin_client_components = require("@kubb/plugin-client/components"); let _kubb_plugin_oas_utils = require("@kubb/plugin-oas/utils"); let _kubb_react_fabric = require("@kubb/react-fabric"); let _kubb_core_utils = require("@kubb/core/utils"); let _kubb_react_fabric_jsx_runtime = require("@kubb/react-fabric/jsx-runtime"); //#region src/components/MutationKey.tsx function getParams$4({}) { return _kubb_react_fabric.FunctionParams.factory({}); } __name(getParams$4, "getParams"); const getTransformer$1 = /* @__PURE__ */ __name(({ operation, casing }) => { return [`{ url: '${new _kubb_core_utils.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__ */ (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$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 _kubb_react_fabric.FunctionParams.factory({ pathParams: { mode: pathParamsType === "object" ? "object" : "inlineSpread", children: (0, _kubb_plugin_oas_utils.getPathParams)(typeSchemas.pathParams, { typed: true, casing: paramsCasing }) }, 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, 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 _kubb_react_fabric.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 _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 }); } 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 = _kubb_plugin_client_components.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 = _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 params = getTriggerParams({ dataReturnType, typeSchemas, mutationKeyTypeName, mutationArgTypeName }); const generics = [ TData, TError, `${mutationKeyTypeName} | null`, mutationArgTypeName ].filter(Boolean); const mutationArg = mutationParams.toConstructor(); 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: mutationArgTypeName, isExportable: true, isIndexable: true, isTypeOnly: true, children: /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.Type, { name: mutationArgTypeName, export: true, children: hasMutationParams ? `{${mutationArg}}` : "never" }) }), /* @__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) }, 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__ */ (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) }, 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 _kubb_react_fabric.FunctionParams.factory({ pathParams: { mode: pathParamsType === "object" ? "object" : "inlineSpread", children: (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 }); } __name(getParams$2, "getParams"); const getTransformer = ({ 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 }) { const params = getParams$2({ pathParamsType, paramsCasing, 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}>` }) })] }); } 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 = (0, _kubb_plugin_oas_utils.getPathParams)(typeSchemas.pathParams, { typed: true, casing: paramsCasing }); return _kubb_react_fabric.FunctionParams.factory({ data: { mode: "object", children: { ...pathParams, 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 } }, config: { type: typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }` : "Partial<RequestConfig> & { client?: Client }", default: "{}" } }); } return _kubb_react_fabric.FunctionParams.factory({ pathParams: typeSchemas.pathParams?.name ? { mode: pathParamsType === "object" ? "object" : "inlineSpread", children: (0, _kubb_plugin_oas_utils.getPathParams)(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), default: (0, _kubb_oas.getDefaultValue)(typeSchemas.pathParams?.schema) } : void 0, 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, 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 = _kubb_plugin_client_components.Client.getParams({ paramsCasing, paramsType, typeSchemas, pathParamsType, isConfigurable: true }); 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: ` 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 = { ...(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 allChildrenAreOptional = Object.values(children).every((child) => !child || child.optional); return _kubb_react_fabric.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 _kubb_react_fabric.FunctionParams.factory({ pathParams: typeSchemas.pathParams?.name ? { mode: pathParamsType === "object" ? "object" : "inlineSpread", children: (0, _kubb_plugin_oas_utils.getPathParams)(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), default: (0, _kubb_oas.getDefaultValue)(typeSchemas.pathParams?.schema) } : void 0, 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, 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__ */ (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) }, 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 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; } }); Object.defineProperty(exports, '__name', { enumerable: true, get: function () { return __name; } }); Object.defineProperty(exports, '__toESM', { enumerable: true, get: function () { return __toESM; } }); //# sourceMappingURL=Query-CbVZ99IO.cjs.map