UNPKG

@kubb/plugin-svelte-query

Version:

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

343 lines (340 loc) 10.8 kB
import { Mutation, MutationKey, Query, QueryKey, QueryOptions } from "./components-Cu5NltDE.js"; import { createReactGenerator } from "@kubb/plugin-oas"; import { pluginTsName } from "@kubb/plugin-ts"; import { pluginZodName } from "@kubb/plugin-zod"; import { File, useApp } from "@kubb/react"; import { Client } from "@kubb/plugin-client/components"; import { getBanner, getFooter } from "@kubb/plugin-oas/utils"; import { Fragment, jsx, jsxs } from "@kubb/react/jsx-runtime"; import { pluginClientName } from "@kubb/plugin-client"; import { useOas, useOperationManager } from "@kubb/plugin-oas/hooks"; import { difference } from "remeda"; //#region src/generators/queryGenerator.tsx const queryGenerator = createReactGenerator({ name: "svelte-query", Operation({ options, operation }) { const { plugin: { options: { output } }, pluginManager } = useApp(); const oas = useOas(); const { getSchemas, getName, getFile } = useOperationManager(); const isQuery = typeof options.query === "boolean" ? true : options.query?.methods.some((method) => operation.method === method); const isMutation = difference(options.mutation ? options.mutation.methods : [], options.query ? options.query.methods : []).some((method) => operation.method === method); const importPath = options.query ? options.query.importPath : "@tanstack/svelte-query"; const query = { name: getName(operation, { type: "function", prefix: "create" }), typeName: getName(operation, { type: "type" }), file: getFile(operation, { prefix: "create" }) }; const hasClientPlugin = !!pluginManager.getPluginByKey([pluginClientName]); const client = { name: hasClientPlugin ? getName(operation, { type: "function", pluginKey: [pluginClientName] }) : getName(operation, { type: "function" }), file: getFile(operation, { pluginKey: [pluginClientName] }) }; const queryOptions = { name: getName(operation, { type: "function", suffix: "QueryOptions" }) }; const queryKey = { name: getName(operation, { type: "const", suffix: "QueryKey" }), typeName: getName(operation, { type: "type", suffix: "QueryKey" }) }; const type = { file: getFile(operation, { pluginKey: [pluginTsName] }), schemas: getSchemas(operation, { pluginKey: [pluginTsName], type: "type" }) }; const zod = { file: getFile(operation, { pluginKey: [pluginZodName] }), schemas: getSchemas(operation, { pluginKey: [pluginZodName], type: "function" }) }; if (!isQuery || isMutation) return null; return /* @__PURE__ */ jsxs(File, { baseName: query.file.baseName, path: query.file.path, meta: query.file.meta, banner: getBanner({ oas, output, config: pluginManager.config }), footer: getFooter({ oas, output }), children: [ options.parser === "zod" && /* @__PURE__ */ jsx(File.Import, { name: [zod.schemas.response.name, zod.schemas.request?.name].filter(Boolean), root: query.file.path, path: zod.file.path }), /* @__PURE__ */ jsx(File.Import, { name: "fetch", path: options.client.importPath }), !!hasClientPlugin && /* @__PURE__ */ jsx(File.Import, { name: [client.name], root: query.file.path, path: client.file.path }), /* @__PURE__ */ jsx(File.Import, { name: ["RequestConfig", "ResponseErrorConfig"], path: options.client.importPath, isTypeOnly: true }), options.client.dataReturnType === "full" && /* @__PURE__ */ jsx(File.Import, { name: ["ResponseConfig"], path: options.client.importPath, isTypeOnly: true }), /* @__PURE__ */ jsx(File.Import, { name: [ type.schemas.request?.name, type.schemas.response.name, type.schemas.pathParams?.name, type.schemas.queryParams?.name, type.schemas.headerParams?.name, ...type.schemas.statusCodes?.map((item) => item.name) || [] ].filter(Boolean), root: query.file.path, path: type.file.path, isTypeOnly: true }), /* @__PURE__ */ jsx(QueryKey, { name: queryKey.name, typeName: queryKey.typeName, operation, pathParamsType: options.pathParamsType, typeSchemas: type.schemas, paramsCasing: options.paramsCasing, transformer: options.queryKey }), !hasClientPlugin && /* @__PURE__ */ jsx(Client, { name: client.name, baseURL: options.client.baseURL, operation, typeSchemas: type.schemas, zodSchemas: zod.schemas, dataReturnType: options.client.dataReturnType, paramsCasing: options.paramsCasing, paramsType: options.paramsType, pathParamsType: options.pathParamsType, parser: options.parser }), /* @__PURE__ */ jsx(File.Import, { name: ["queryOptions"], path: importPath }), /* @__PURE__ */ jsx(QueryOptions, { name: queryOptions.name, clientName: client.name, queryKeyName: queryKey.name, typeSchemas: type.schemas, paramsCasing: options.paramsCasing, paramsType: options.paramsType, pathParamsType: options.pathParamsType, dataReturnType: options.client.dataReturnType }), options.query && /* @__PURE__ */ jsxs(Fragment, { children: [ /* @__PURE__ */ jsx(File.Import, { name: ["createQuery"], path: importPath }), /* @__PURE__ */ jsx(File.Import, { name: [ "QueryKey", "QueryClient", "CreateBaseQueryOptions", "CreateQueryResult" ], path: importPath, isTypeOnly: true }), /* @__PURE__ */ jsx(Query, { name: query.name, queryOptionsName: queryOptions.name, typeSchemas: type.schemas, pathParamsType: options.pathParamsType, operation, paramsCasing: options.paramsCasing, paramsType: options.paramsType, dataReturnType: options.client.dataReturnType, queryKeyName: queryKey.name, queryKeyTypeName: queryKey.typeName }) ] }) ] }); } }); //#endregion //#region src/generators/mutationGenerator.tsx const mutationGenerator = createReactGenerator({ name: "svelte-query", Operation({ options, operation }) { const { plugin: { options: { output } }, pluginManager } = useApp(); const oas = useOas(); const { getSchemas, getName, getFile } = useOperationManager(); const isMutation = !(!!options.query && options.query?.methods.some((method) => operation.method === method)) && difference(options.mutation ? options.mutation.methods : [], options.query ? options.query.methods : []).some((method) => operation.method === method); const importPath = options.mutation ? options.mutation.importPath : "@tanstack/svelte-query"; const mutation = { name: getName(operation, { type: "function", prefix: "create" }), typeName: getName(operation, { type: "type" }), file: getFile(operation, { prefix: "create" }) }; const type = { file: getFile(operation, { pluginKey: [pluginTsName] }), schemas: getSchemas(operation, { pluginKey: [pluginTsName], type: "type" }) }; const zod = { file: getFile(operation, { pluginKey: [pluginZodName] }), schemas: getSchemas(operation, { pluginKey: [pluginZodName], type: "function" }) }; const hasClientPlugin = !!pluginManager.getPluginByKey([pluginClientName]); const client = { name: hasClientPlugin ? getName(operation, { type: "function", pluginKey: [pluginClientName] }) : getName(operation, { type: "function" }), file: getFile(operation, { pluginKey: [pluginClientName] }) }; const mutationKey = { name: getName(operation, { type: "const", suffix: "MutationKey" }), typeName: getName(operation, { type: "type", suffix: "MutationKey" }) }; if (!isMutation) return null; return /* @__PURE__ */ jsxs(File, { baseName: mutation.file.baseName, path: mutation.file.path, meta: mutation.file.meta, banner: getBanner({ oas, output, config: pluginManager.config }), footer: getFooter({ oas, output }), children: [ options.parser === "zod" && /* @__PURE__ */ jsx(File.Import, { name: [zod.schemas.response.name, zod.schemas.request?.name].filter(Boolean), root: mutation.file.path, path: zod.file.path }), /* @__PURE__ */ jsx(File.Import, { name: "fetch", path: options.client.importPath }), !!hasClientPlugin && /* @__PURE__ */ jsx(File.Import, { name: [client.name], root: mutation.file.path, path: client.file.path }), /* @__PURE__ */ jsx(File.Import, { name: [ "RequestConfig", "ResponseConfig", "ResponseErrorConfig" ], path: options.client.importPath, isTypeOnly: true }), /* @__PURE__ */ jsx(File.Import, { name: [ type.schemas.request?.name, type.schemas.response.name, type.schemas.pathParams?.name, type.schemas.queryParams?.name, type.schemas.headerParams?.name, ...type.schemas.statusCodes?.map((item) => item.name) || [] ].filter(Boolean), root: mutation.file.path, path: type.file.path, isTypeOnly: true }), /* @__PURE__ */ jsx(MutationKey, { name: mutationKey.name, typeName: mutationKey.typeName, operation, paramsCasing: options.paramsCasing, pathParamsType: options.pathParamsType, typeSchemas: type.schemas, transformer: options.mutationKey }), !hasClientPlugin && /* @__PURE__ */ jsx(Client, { name: client.name, baseURL: options.client.baseURL, operation, typeSchemas: type.schemas, zodSchemas: zod.schemas, dataReturnType: options.client.dataReturnType, paramsCasing: options.paramsCasing, paramsType: options.paramsType, pathParamsType: options.pathParamsType, parser: options.parser }), options.mutation && /* @__PURE__ */ jsxs(Fragment, { children: [ /* @__PURE__ */ jsx(File.Import, { name: ["createMutation"], path: importPath }), /* @__PURE__ */ jsx(File.Import, { name: [ "CreateMutationOptions", "CreateMutationResult", "QueryClient" ], path: importPath, isTypeOnly: true }), /* @__PURE__ */ jsx(Mutation, { name: mutation.name, clientName: client.name, typeName: mutation.typeName, typeSchemas: type.schemas, operation, paramsCasing: options.paramsCasing, dataReturnType: options.client.dataReturnType, paramsType: options.paramsType, pathParamsType: options.pathParamsType, mutationKeyName: mutationKey.name }) ] }) ] }); } }); //#endregion export { mutationGenerator, queryGenerator }; //# sourceMappingURL=generators-CYShRFGm.js.map