UNPKG

@kubb/plugin-solid-query

Version:

Solid Query hooks generator plugin for Kubb, creating type-safe API client hooks from OpenAPI specifications for Solid.js applications.

367 lines (360 loc) 12.9 kB
//#region rolldown:runtime var __create = Object.create; var __defProp = Object.defineProperty; 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 const __kubb_oas = __toESM(require("@kubb/oas")); const __kubb_plugin_oas_utils = __toESM(require("@kubb/plugin-oas/utils")); const __kubb_react = __toESM(require("@kubb/react")); const __kubb_core_utils = __toESM(require("@kubb/core/utils")); const __kubb_react_jsx_runtime = __toESM(require("@kubb/react/jsx-runtime")); const __kubb_plugin_client_components = __toESM(require("@kubb/plugin-client/components")); //#region src/components/QueryKey.tsx function getParams$2({ pathParamsType, paramsCasing, typeSchemas }) { return __kubb_react.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 }); } const getTransformer = ({ operation, schemas, casing }) => { const path = new __kubb_core_utils.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 = getTransformer }) { const params = getParams$2({ pathParamsType, paramsCasing, typeSchemas }); const keys = transformer({ operation, schemas: typeSchemas, casing: paramsCasing }); return /* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsxs)(__kubb_react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsx)(__kubb_react.File.Source, { name, isExportable: true, isIndexable: true, children: /* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsx)(__kubb_react.Function.Arrow, { name, export: true, params: params.toConstructor(), singleLine: true, children: `[${keys.join(", ")}] as const` }) }), /* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsx)(__kubb_react.File.Source, { name: typeName, isExportable: true, isIndexable: true, isTypeOnly: true, children: /* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsx)(__kubb_react.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 __kubb_react.FunctionParams.factory({ data: { mode: "object", 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 } }, config: { type: typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof fetch }` : "Partial<RequestConfig> & { client?: typeof fetch }", default: "{}" } }); return __kubb_react.FunctionParams.factory({ pathParams: typeSchemas.pathParams?.name ? { mode: pathParamsType === "object" ? "object" : "inlineSpread", children: (0, __kubb_plugin_oas_utils.getPathParams)(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), optional: (0, __kubb_oas.isOptional)(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?: 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 = __kubb_plugin_client_components.Client.getParams({ paramsCasing, typeSchemas, paramsType, 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__ */ (0, __kubb_react_jsx_runtime.jsx)(__kubb_react.File.Source, { name, isExportable: true, isIndexable: true, children: /* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsx)(__kubb_react.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 __kubb_react.FunctionParams.factory({ data: { mode: "object", 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 } }, options: { type: ` { query?: Partial<UseBaseQueryOptions<${[ 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 __kubb_react.FunctionParams.factory({ pathParams: typeSchemas.pathParams?.name ? { mode: pathParamsType === "object" ? "object" : "inlineSpread", children: (0, __kubb_plugin_oas_utils.getPathParams)(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), optional: (0, __kubb_oas.isOptional)(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?: Partial<UseBaseQueryOptions<${[ 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, paramsCasing, paramsType, 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 returnType = `UseQueryResult<${["TData", TError].join(", ")}> & { queryKey: TQueryKey }`; const generics = [ `TData = ${TData}`, `TQueryData = ${TData}`, `TQueryKey extends QueryKey = ${queryKeyTypeName}` ]; const queryKeyParams = QueryKey.getParams({ pathParamsType, typeSchemas, paramsCasing }); const queryOptionsParams = QueryOptions.getParams({ paramsType, paramsCasing, pathParamsType, typeSchemas }); const params = getParams({ paramsCasing, paramsType, pathParamsType, dataReturnType, typeSchemas }); const queryOptions = `${queryOptionsName}(${queryOptionsParams.toCall()}) as unknown as UseBaseQueryOptions`; return /* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsx)(__kubb_react.File.Source, { name, isExportable: true, isIndexable: true, children: /* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsx)(__kubb_react.Function, { name, export: true, generics: generics.join(", "), params: params.toConstructor(), JSDoc: { comments: (0, __kubb_plugin_oas_utils.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, initialData: null, ...queryOptions as unknown as Omit<UseBaseQueryOptions, "queryKey"> }), queryClient? () => queryClient: undefined) as ${returnType} return query ` }) }); } Query.getParams = getParams; //#endregion 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, '__toESM', { enumerable: true, get: function () { return __toESM; } }); //# sourceMappingURL=components-CCkbce4B.cjs.map