@kubb/plugin-swr 5.0.0-beta.36 → 5.0.0-beta.56
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -11
- package/dist/{components-CD7ZoS3B.cjs → components-C8FJ3-Cb.cjs} +175 -196
- package/dist/components-C8FJ3-Cb.cjs.map +1 -0
- package/dist/{components-BuLagnaM.js → components-CR9NLFcO.js} +176 -197
- package/dist/components-CR9NLFcO.js.map +1 -0
- package/dist/components.cjs +1 -1
- package/dist/components.d.ts +1 -1
- package/dist/components.js +1 -1
- package/dist/{generators-bnwHiUqJ.js → generators-BB6csINY.js} +71 -94
- package/dist/generators-BB6csINY.js.map +1 -0
- package/dist/{generators-DM18y_Qk.cjs → generators-mw3FvFJD.cjs} +70 -93
- package/dist/generators-mw3FvFJD.cjs.map +1 -0
- package/dist/generators.cjs +1 -1
- package/dist/generators.d.ts +6 -1
- package/dist/generators.js +1 -1
- package/dist/index.cjs +62 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +63 -13
- package/dist/index.js.map +1 -1
- package/dist/{types-BoANASs2.d.ts → types-Dz_j0Bqv.d.ts} +11 -14
- package/package.json +9 -18
- package/src/components/Mutation.tsx +3 -3
- package/src/components/Query.tsx +3 -3
- package/src/components/QueryOptions.tsx +3 -21
- package/src/generators/mutationGenerator.tsx +13 -9
- package/src/generators/queryGenerator.tsx +5 -9
- package/src/plugin.ts +6 -18
- package/src/resolvers/resolverSwr.ts +2 -2
- package/src/types.ts +9 -12
- package/src/utils.ts +8 -1
- package/dist/components-BuLagnaM.js.map +0 -1
- package/dist/components-CD7ZoS3B.cjs.map +0 -1
- package/dist/generators-DM18y_Qk.cjs.map +0 -1
- package/dist/generators-bnwHiUqJ.js.map +0 -1
- package/extension.yaml +0 -199
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["fetchClientSource","axiosClientSource","configSource"],"sources":["../src/resolvers/resolverSwr.ts","../src/plugin.ts"],"sourcesContent":["import { camelCase } from '@internals/utils'\nimport { defineResolver } from '@kubb/core'\nimport type { PluginSwr } from '../types.ts'\n\nfunction capitalize(name: string): string {\n return `${name.charAt(0).toUpperCase()}${name.slice(1)}`\n}\n\n/**\n * Naming convention resolver for the SWR plugin.\n *\n * Provides default naming helpers using camelCase for functions and file paths.\n *\n * @example\n * `resolverSwr.default('list pets', 'function') // → 'listPets'`\n */\nexport const resolverSwr = defineResolver<PluginSwr>(() => ({\n name: 'default',\n pluginName: 'plugin-swr',\n default(name, type) {\n return camelCase(name, { isFile: type === 'file' })\n },\n resolveName(name) {\n return this.default(name, 'function')\n },\n resolvePathName(name, type) {\n return this.default(name, type)\n },\n resolveQueryName(node) {\n return `use${capitalize(this.resolveName(node.operationId))}`\n },\n resolveMutationName(node) {\n return `use${capitalize(this.resolveName(node.operationId))}`\n },\n resolveQueryOptionsName(node) {\n return `${this.resolveName(node.operationId)}QueryOptions`\n },\n resolveQueryKeyName(node) {\n return `${this.resolveName(node.operationId)}QueryKey`\n },\n resolveMutationKeyName(node) {\n return `${this.resolveName(node.operationId)}MutationKey`\n },\n resolveQueryKeyTypeName(node) {\n return `${capitalize(this.resolveName(node.operationId))}QueryKey`\n },\n resolveMutationKeyTypeName(node) {\n return `${capitalize(this.resolveName(node.operationId))}MutationKey`\n },\n resolveMutationArgTypeName(node) {\n return `${capitalize(this.resolveName(node.operationId))}MutationArg`\n },\n resolveClientName(node) {\n return this.resolveName(node.operationId)\n },\n}))\n","import path from 'node:path'\nimport { camelCase } from '@internals/utils'\nimport { ast, definePlugin, type Group } from '@kubb/core'\nimport { pluginClientName } from '@kubb/plugin-client'\nimport { source as axiosClientSource } from '@kubb/plugin-client/templates/clients/axios.source'\nimport { source as fetchClientSource } from '@kubb/plugin-client/templates/clients/fetch.source'\nimport { source as configSource } from '@kubb/plugin-client/templates/config.source'\nimport { pluginTsName } from '@kubb/plugin-ts'\nimport { pluginZodName } from '@kubb/plugin-zod'\nimport { mutationKeyTransformer, queryKeyTransformer } from '@internals/tanstack-query'\nimport { mutationGenerator, queryGenerator } from './generators'\nimport { resolverSwr } from './resolvers/resolverSwr.ts'\nimport type { PluginSwr } from './types.ts'\n\nexport const pluginSwrName = 'plugin-swr' satisfies PluginSwr['name']\n\nexport const pluginSwr = definePlugin<PluginSwr>((options) => {\n const {\n output = { path: 'hooks', barrelType: 'named' },\n group,\n exclude = [],\n include,\n override = [],\n parser = false,\n paramsType = 'inline',\n pathParamsType = paramsType === 'object' ? 'object' : options.pathParamsType || 'inline',\n mutation = {},\n query = {},\n mutationKey = mutationKeyTransformer,\n queryKey = queryKeyTransformer,\n paramsCasing,\n client,\n resolver: userResolver,\n transformer: userTransformer,\n generators: userGenerators = [],\n } = options\n\n const clientName = client?.client ?? 'axios'\n const clientImportPath = client?.importPath ?? (!client?.bundle ? `@kubb/plugin-client/clients/${clientName}` : undefined)\n\n const selectedGenerators =\n options.generators ?? [queryGenerator, mutationGenerator].filter((generator): generator is NonNullable<typeof generator> => Boolean(generator))\n\n const groupConfig = group\n ? ({\n ...group,\n name: group.name\n ? group.name\n : (ctx: { group: string }) => {\n if (group.type === 'path') {\n return `${ctx.group.split('/')[1]}`\n }\n return `${camelCase(ctx.group)}Controller`\n },\n } satisfies Group)\n : undefined\n\n return {\n name: pluginSwrName,\n options,\n dependencies: [pluginTsName, parser === 'zod' ? pluginZodName : undefined].filter((dependency): dependency is string => Boolean(dependency)),\n hooks: {\n 'kubb:plugin:setup'(ctx) {\n const resolver = userResolver ? { ...resolverSwr, ...userResolver } : resolverSwr\n\n ctx.setOptions({\n output,\n client: {\n bundle: client?.bundle,\n baseURL: client?.baseURL,\n client: clientName,\n clientType: client?.clientType ?? 'function',\n importPath: clientImportPath,\n dataReturnType: client?.dataReturnType ?? 'data',\n paramsCasing,\n },\n queryKey,\n query:\n query === false\n ? false\n : {\n importPath: 'swr',\n methods: ['get'],\n ...query,\n },\n mutationKey,\n mutation:\n mutation === false\n ? false\n : {\n importPath: 'swr/mutation',\n methods: ['post', 'put', 'patch', 'delete'],\n ...mutation,\n },\n parser,\n paramsType,\n pathParamsType,\n paramsCasing,\n group: groupConfig,\n exclude,\n include,\n override,\n resolver,\n })\n ctx.setResolver(resolver)\n if (userTransformer) {\n ctx.setTransformer(userTransformer)\n }\n\n for (const gen of selectedGenerators) {\n ctx.addGenerator(gen)\n }\n for (const gen of userGenerators) {\n ctx.addGenerator(gen)\n }\n\n const root = path.resolve(ctx.config.root, ctx.config.output.path)\n const hasClientPlugin = !!ctx.config.plugins?.some((p) => (p as { name?: string }).name === pluginClientName)\n\n if (client?.bundle && !hasClientPlugin && !clientImportPath) {\n ctx.injectFile({\n baseName: 'client.ts',\n path: path.resolve(root, '.kubb/client.ts'),\n sources: [\n ast.createSource({\n name: 'client',\n nodes: [ast.createText(clientName === 'fetch' ? fetchClientSource : axiosClientSource)],\n isExportable: true,\n isIndexable: true,\n }),\n ],\n })\n }\n\n if (!hasClientPlugin) {\n ctx.injectFile({\n baseName: 'config.ts',\n path: path.resolve(root, '.kubb/config.ts'),\n sources: [\n ast.createSource({\n name: 'config',\n nodes: [ast.createText(configSource)],\n isExportable: false,\n isIndexable: false,\n }),\n ],\n })\n }\n },\n },\n }\n})\n\nexport default pluginSwr\n"],"mappings":";;;;;;;;;;;;AAIA,SAAS,WAAW,MAAsB;CACxC,OAAO,GAAG,KAAK,OAAO,CAAC,EAAE,YAAY,IAAI,KAAK,MAAM,CAAC;AACvD;;;;;;;;;AAUA,MAAa,cAAc,sBAAiC;CAC1D,MAAM;CACN,YAAY;CACZ,QAAQ,MAAM,MAAM;EAClB,OAAO,UAAU,MAAM,EAAE,QAAQ,SAAS,OAAO,CAAC;CACpD;CACA,YAAY,MAAM;EAChB,OAAO,KAAK,QAAQ,MAAM,UAAU;CACtC;CACA,gBAAgB,MAAM,MAAM;EAC1B,OAAO,KAAK,QAAQ,MAAM,IAAI;CAChC;CACA,iB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AAC,mBAAmB,CAAC,kBACzC,IAAI,WAAW;IACb,UAAU;IACV,MAAM,KAAK,QAAQ,MAAM,iBAAiB;IAC1C,SAAS,CACP,IAAI,aAAa;KACf,MAAM;KACN,OAAO,CAAC,IAAI,WAAW,eAAe,UAAUA,WAAoBC,MAAiB,CAAC;KACtF,cAAc;KACd,aAAa;IACf,CAAC,CACH;GACF,CAAC;GAGH,IAAI,CAAC,iBACH,IAAI,WAAW;IACb,UAAU;IACV,MAAM,KAAK,QAAQ,MAAM,iBAAiB;IAC1C,SAAS,CACP,IAAI,aAAa;KACf,MAAM;KACN,OAAO,CAAC,IAAI,WAAWC,QAAY,CAAC;KACpC,cAAc;KACd,aAAa;IACf,CAAC,CACH;GACF,CAAC;EAEL,EACF;CACF;AACF,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["fetchClientSource","axiosClientSource","configSource"],"sources":["../../../internals/utils/src/fs.ts","../../../internals/shared/src/group.ts","../src/resolvers/resolverSwr.ts","../src/plugin.ts"],"sourcesContent":["import { posix } from 'node:path'\nimport { camelCase } from './casing.ts'\n\nfunction toSlash(p: string): string {\n if (p.startsWith('\\\\\\\\?\\\\')) return p\n return p.replaceAll('\\\\', '/')\n}\n\n/**\n * Returns the relative path from `rootDir` to `filePath`, always using forward slashes\n * and prefixed with `./` when not already traversing upward.\n *\n * @example\n * ```ts\n * getRelativePath('/src/components', '/src/components/Button.tsx') // './Button.tsx'\n * getRelativePath('/src/components', '/src/utils/helpers.ts') // '../utils/helpers.ts'\n * ```\n */\nexport function getRelativePath(rootDir?: string | null, filePath?: string | null): string {\n if (!rootDir || !filePath) {\n throw new Error(`Root and file should be filled in when retrieving the relativePath, ${rootDir || ''} ${filePath || ''}`)\n }\n\n const relativePath = posix.relative(toSlash(rootDir), toSlash(filePath))\n\n return relativePath.startsWith('../') ? relativePath : `./${relativePath}`\n}\n\n/**\n * Builds a nested file path from a dotted name. Splits on dots that precede a letter\n * (so version numbers embedded in operationIds like `v2025.0` stay intact), camelCases\n * every earlier segment, applies `caseLast` to the final segment, and joins with `/`.\n *\n * Empty segments are dropped before joining. They arise when the name starts with a dot\n * followed by a letter (e.g. `..Schema` splits into `['..', 'Schema']` and `'..'` cases to\n * an empty string). Without this a leading `/` would form, which `path.resolve` reads as an\n * absolute path, letting generated files escape the configured output directory.\n *\n * @example Nested path from a dotted name\n * `toFilePath('pet.petId') // 'pet/petId'`\n *\n * @example PascalCase the final segment\n * `toFilePath('pet.Pet', pascalCase) // 'pet/Pet'`\n *\n * @example Suffix applied to the final segment only\n * `toFilePath('tag.tag', (part) => camelCase(part, { suffix: 'schema' })) // 'tag/tagSchema'`\n */\nexport function toFilePath(name: string, caseLast: (part: string) => string = camelCase): string {\n const parts = name.split(/\\.(?=[a-zA-Z])/)\n return parts\n .map((part, i) => (i === parts.length - 1 ? caseLast(part) : camelCase(part)))\n .filter(Boolean)\n .join('/')\n}\n","import { camelCase } from '@internals/utils'\nimport type { Group } from '@kubb/core'\n\n/**\n * Builds the `group` config a Kubb plugin passes to `ctx.setOptions`, applying the\n * shared default naming so every plugin groups output consistently:\n *\n * - `path` groups use the second path segment (`/pet/findByStatus` → `pet`).\n * - other groups use the camelCased group (`pet store` → `petStore`).\n *\n * A user-provided `group.name` always wins over the default namer, so callers stay in\n * control of their output folders. Returns `null` when grouping is disabled, matching the\n * per-plugin convention.\n *\n * @param group - The user-supplied group option, or `undefined` to disable grouping.\n *\n * @example\n * ```ts\n * createGroupConfig(group) // shared across every plugin\n * ```\n */\nexport function createGroupConfig(group: Group | undefined): Group | null {\n if (!group) {\n return null\n }\n\n const defaultName = (ctx: { group: string }): string => {\n if (group.type === 'path') {\n return `${ctx.group.split('/')[1]}`\n }\n\n return camelCase(ctx.group)\n }\n\n return {\n ...group,\n name: group.name ? group.name : defaultName,\n } satisfies Group\n}\n","import { camelCase, toFilePath } from '@internals/utils'\nimport { defineResolver } from '@kubb/core'\nimport type { PluginSwr } from '../types.ts'\n\nfunction capitalize(name: string): string {\n return `${name.charAt(0).toUpperCase()}${name.slice(1)}`\n}\n\n/**\n * Naming convention resolver for the SWR plugin.\n *\n * Provides default naming helpers using camelCase for functions and file paths.\n *\n * @example\n * `resolverSwr.default('list pets', 'function') // → 'listPets'`\n */\nexport const resolverSwr = defineResolver<PluginSwr>(() => ({\n name: 'default',\n pluginName: 'plugin-swr',\n default(name, type) {\n return type === 'file' ? toFilePath(name) : camelCase(name)\n },\n resolveName(name) {\n return this.default(name, 'function')\n },\n resolvePathName(name, type) {\n return this.default(name, type)\n },\n resolveQueryName(node) {\n return `use${capitalize(this.resolveName(node.operationId))}`\n },\n resolveMutationName(node) {\n return `use${capitalize(this.resolveName(node.operationId))}`\n },\n resolveQueryOptionsName(node) {\n return `${this.resolveName(node.operationId)}QueryOptions`\n },\n resolveQueryKeyName(node) {\n return `${this.resolveName(node.operationId)}QueryKey`\n },\n resolveMutationKeyName(node) {\n return `${this.resolveName(node.operationId)}MutationKey`\n },\n resolveQueryKeyTypeName(node) {\n return `${capitalize(this.resolveName(node.operationId))}QueryKey`\n },\n resolveMutationKeyTypeName(node) {\n return `${capitalize(this.resolveName(node.operationId))}MutationKey`\n },\n resolveMutationArgTypeName(node) {\n return `${capitalize(this.resolveName(node.operationId))}MutationArg`\n },\n resolveClientName(node) {\n return this.resolveName(node.operationId)\n },\n}))\n","import path from 'node:path'\nimport { createGroupConfig } from '@internals/shared'\nimport { ast, definePlugin } from '@kubb/core'\nimport { isParserEnabled, pluginClientName } from '@kubb/plugin-client'\nimport { source as axiosClientSource } from '@kubb/plugin-client/templates/clients/axios.source'\nimport { source as fetchClientSource } from '@kubb/plugin-client/templates/clients/fetch.source'\nimport { source as configSource } from '@kubb/plugin-client/templates/config.source'\nimport { pluginTsName } from '@kubb/plugin-ts'\nimport { pluginZodName } from '@kubb/plugin-zod'\nimport { mutationKeyTransformer, queryKeyTransformer } from '@internals/tanstack-query'\nimport { mutationGenerator, queryGenerator } from './generators'\nimport { resolverSwr } from './resolvers/resolverSwr.ts'\nimport type { PluginSwr } from './types.ts'\n\nexport const pluginSwrName = 'plugin-swr' satisfies PluginSwr['name']\n\nexport const pluginSwr = definePlugin<PluginSwr>((options) => {\n const {\n output = { path: 'hooks', barrel: { type: 'named' } },\n group,\n exclude = [],\n include,\n override = [],\n parser = false,\n paramsType = 'inline',\n pathParamsType = paramsType === 'object' ? 'object' : options.pathParamsType || 'inline',\n mutation = {},\n query = {},\n mutationKey = mutationKeyTransformer,\n queryKey = queryKeyTransformer,\n paramsCasing,\n client,\n resolver: userResolver,\n transformer: userTransformer,\n generators: userGenerators = [],\n } = options\n\n const clientName = client?.client ?? 'axios'\n const clientImportPath = client?.importPath ?? (!client?.bundle ? `@kubb/plugin-client/clients/${clientName}` : undefined)\n\n const selectedGenerators =\n options.generators ?? [queryGenerator, mutationGenerator].filter((generator): generator is NonNullable<typeof generator> => Boolean(generator))\n\n const groupConfig = createGroupConfig(group) ?? undefined\n\n return {\n name: pluginSwrName,\n options,\n dependencies: [pluginTsName, isParserEnabled(parser) ? pluginZodName : undefined].filter((dependency): dependency is string => Boolean(dependency)),\n hooks: {\n 'kubb:plugin:setup'(ctx) {\n const resolver = userResolver ? { ...resolverSwr, ...userResolver } : resolverSwr\n\n ctx.setOptions({\n output,\n client: {\n bundle: client?.bundle,\n baseURL: client?.baseURL,\n client: clientName,\n clientType: client?.clientType ?? 'function',\n importPath: clientImportPath,\n dataReturnType: client?.dataReturnType ?? 'data',\n paramsCasing,\n },\n queryKey,\n query:\n query === false\n ? false\n : {\n importPath: 'swr',\n methods: ['get'],\n ...query,\n },\n mutationKey,\n mutation:\n mutation === false\n ? false\n : {\n importPath: 'swr/mutation',\n methods: ['post', 'put', 'patch', 'delete'],\n ...mutation,\n },\n parser,\n paramsType,\n pathParamsType,\n paramsCasing,\n group: groupConfig,\n exclude,\n include,\n override,\n resolver,\n })\n ctx.setResolver(resolver)\n if (userTransformer) {\n ctx.setTransformer(userTransformer)\n }\n\n for (const gen of selectedGenerators) {\n ctx.addGenerator(gen)\n }\n for (const gen of userGenerators) {\n ctx.addGenerator(gen)\n }\n\n const root = path.resolve(ctx.config.root, ctx.config.output.path)\n const hasClientPlugin = !!ctx.config.plugins?.some((p) => (p as { name?: string }).name === pluginClientName)\n\n if (client?.bundle && !hasClientPlugin && !clientImportPath) {\n ctx.injectFile({\n baseName: 'client.ts',\n path: path.resolve(root, '.kubb/client.ts'),\n sources: [\n ast.createSource({\n name: 'client',\n nodes: [ast.createText(clientName === 'fetch' ? fetchClientSource : axiosClientSource)],\n isExportable: true,\n isIndexable: true,\n }),\n ],\n })\n }\n\n if (!hasClientPlugin) {\n ctx.injectFile({\n baseName: 'config.ts',\n path: path.resolve(root, '.kubb/config.ts'),\n sources: [\n ast.createSource({\n name: 'config',\n nodes: [ast.createText(configSource)],\n isExportable: false,\n isIndexable: false,\n }),\n ],\n })\n }\n },\n },\n }\n})\n\nexport default pluginSwr\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+CA,SAAgB,WAAW,MAAc,WAAqC,WAAmB;CAC/F,MAAM,QAAQ,KAAK,MAAM,gBAAgB;CACzC,OAAO,MACJ,KAAK,MAAM,MAAO,MAAM,MAAM,SAAS,IAAI,SAAS,IAAI,IAAI,UAAU,IAAI,CAAE,CAAC,CAC7E,OAAO,OAAO,CAAC,CACf,KAAK,GAAG;AACb;;;;;;;;;;;;;;;;;;;;;AChCA,SAAgB,kBAAkB,OAAwC;CACxE,IAAI,CAAC,OACH,OAAO;CAGT,MAAM,eAAe,QAAmC;EACtD,IAAI,MAAM,SAAS,QACjB,OAAO,GAAG,IAAI,MAAM,MAAM,GAAG,CAAC,CAAC;EAGjC,OAAO,UAAU,IAAI,KAAK;CAC5B;CAEA,OAAO;EACL,GAAG;EACH,MAAM,MAAM,OAAO,MAAM,OAAO;CAClC;AACF;;;AClCA,SAAS,WAAW,MAAsB;CACxC,OAAO,GAAG,KAAK,OAAO,CAAC,CAAC,CAAC,YAAY,IAAI,KAAK,MAAM,CA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K,QAAQ,IAAI,OAAO,MAAM,IAAI,OAAO,OAAO,IAAI;GACjE,MAAM,kBAAkB,CAAC,CAAC,IAAI,OAAO,SAAS,MAAM,MAAO,EAAwB,SAAS,gBAAgB;GAE5G,IAAI,QAAQ,UAAU,CAAC,mBAAmB,CAAC,kBACzC,IAAI,WAAW;IACb,UAAU;IACV,MAAM,KAAK,QAAQ,MAAM,iBAAiB;IAC1C,SAAS,CACP,IAAI,aAAa;KACf,MAAM;KACN,OAAO,CAAC,IAAI,WAAW,eAAe,UAAUA,WAAoBC,MAAiB,CAAC;KACtF,cAAc;KACd,aAAa;IACf,CAAC,CACH;GACF,CAAC;GAGH,IAAI,CAAC,iBACH,IAAI,WAAW;IACb,UAAU;IACV,MAAM,KAAK,QAAQ,MAAM,iBAAiB;IAC1C,SAAS,CACP,IAAI,aAAa;KACf,MAAM;KACN,OAAO,CAAC,IAAI,WAAWC,QAAY,CAAC;KACpC,cAAc;KACd,aAAa;IACf,CAAC,CACH;GACF,CAAC;EAEL,EACF;CACF;AACF,CAAC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { t as __name } from "./chunk-C0LytTxp.js";
|
|
2
|
-
import { Exclude, Generator, Group, Include, Output, Override, PluginFactoryOptions, Resolver, ast } from "@kubb/core";
|
|
2
|
+
import { Exclude, Generator, Group, Include, Output, OutputOptions, Override, PluginFactoryOptions, Resolver, ast } from "@kubb/core";
|
|
3
3
|
import { ClientImportPath, PluginClient } from "@kubb/plugin-client";
|
|
4
|
-
import { PluginTs } from "@kubb/plugin-ts";
|
|
4
|
+
import { PluginTs, ResolverTs } from "@kubb/plugin-ts";
|
|
5
5
|
import { KubbReactNode } from "@kubb/renderer-jsx/types";
|
|
6
6
|
|
|
7
7
|
//#region ../../internals/tanstack-query/src/types.d.ts
|
|
@@ -124,16 +124,13 @@ type Mutation = {
|
|
|
124
124
|
*/
|
|
125
125
|
importPath?: string;
|
|
126
126
|
};
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
* Group the SWR hooks based on the provided name.
|
|
135
|
-
*/
|
|
136
|
-
group?: Group;
|
|
127
|
+
/**
|
|
128
|
+
* Where the generated SWR hooks are written and how they are exported, plus the optional `group`
|
|
129
|
+
* strategy. The `group` option organizes `output.mode: 'directory'` output into per-tag or per-path subdirectories.
|
|
130
|
+
*
|
|
131
|
+
* @default { path: 'hooks', barrel: { type: 'named' } }
|
|
132
|
+
*/
|
|
133
|
+
type Options = OutputOptions & {
|
|
137
134
|
client?: ClientImportPath & Pick<PluginClient['options'], 'clientType' | 'dataReturnType' | 'baseURL' | 'bundle' | 'paramsCasing'>;
|
|
138
135
|
/**
|
|
139
136
|
* Tags, operations, or paths to exclude from generation.
|
|
@@ -197,7 +194,7 @@ type ResolvedOptions = {
|
|
|
197
194
|
include: Options['include'];
|
|
198
195
|
override: NonNullable<Options['override']>;
|
|
199
196
|
client: Pick<PluginClient['options'], 'client' | 'clientType' | 'dataReturnType' | 'importPath' | 'baseURL' | 'bundle' | 'paramsCasing'>;
|
|
200
|
-
parser:
|
|
197
|
+
parser: NonNullable<Options['parser']>;
|
|
201
198
|
pathParamsType: NonNullable<Options['pathParamsType']>;
|
|
202
199
|
paramsCasing: Options['paramsCasing'];
|
|
203
200
|
paramsType: NonNullable<Options['paramsType']>;
|
|
@@ -217,4 +214,4 @@ declare global {
|
|
|
217
214
|
}
|
|
218
215
|
//#endregion
|
|
219
216
|
export { Transformer as i, PluginSwr as n, QueryKey$1 as r, Options as t };
|
|
220
|
-
//# sourceMappingURL=types-
|
|
217
|
+
//# sourceMappingURL=types-Dz_j0Bqv.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/plugin-swr",
|
|
3
|
-
"version": "5.0.0-beta.
|
|
3
|
+
"version": "5.0.0-beta.56",
|
|
4
4
|
"description": "Generate type-safe SWR hooks from your OpenAPI specification. Covers useSWR and useSWRMutation with full TypeScript support.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"code-generation",
|
|
@@ -26,7 +26,6 @@
|
|
|
26
26
|
"files": [
|
|
27
27
|
"src",
|
|
28
28
|
"dist",
|
|
29
|
-
"extension.yaml",
|
|
30
29
|
"!/**/**.test.**",
|
|
31
30
|
"!/**/__tests__/**",
|
|
32
31
|
"!/**/__snapshots__/**"
|
|
@@ -66,12 +65,11 @@
|
|
|
66
65
|
"registry": "https://registry.npmjs.org/"
|
|
67
66
|
},
|
|
68
67
|
"dependencies": {
|
|
69
|
-
"@kubb/core": "5.0.0-beta.
|
|
70
|
-
"@kubb/renderer-jsx": "5.0.0-beta.
|
|
71
|
-
"
|
|
72
|
-
"@kubb/plugin-
|
|
73
|
-
"@kubb/plugin-
|
|
74
|
-
"@kubb/plugin-zod": "5.0.0-beta.36"
|
|
68
|
+
"@kubb/core": "5.0.0-beta.55",
|
|
69
|
+
"@kubb/renderer-jsx": "5.0.0-beta.55",
|
|
70
|
+
"@kubb/plugin-client": "5.0.0-beta.56",
|
|
71
|
+
"@kubb/plugin-ts": "5.0.0-beta.56",
|
|
72
|
+
"@kubb/plugin-zod": "5.0.0-beta.56"
|
|
75
73
|
},
|
|
76
74
|
"devDependencies": {
|
|
77
75
|
"@internals/shared": "0.0.0",
|
|
@@ -79,21 +77,14 @@
|
|
|
79
77
|
"@internals/utils": "0.0.0"
|
|
80
78
|
},
|
|
81
79
|
"peerDependencies": {
|
|
82
|
-
"@kubb/renderer-jsx": "5.0.0-beta.
|
|
80
|
+
"@kubb/renderer-jsx": "5.0.0-beta.55"
|
|
83
81
|
},
|
|
84
|
-
"size-limit": [
|
|
85
|
-
{
|
|
86
|
-
"path": "./dist/*.js",
|
|
87
|
-
"limit": "510 KiB",
|
|
88
|
-
"gzip": true
|
|
89
|
-
}
|
|
90
|
-
],
|
|
91
82
|
"engines": {
|
|
92
83
|
"node": ">=22"
|
|
93
84
|
},
|
|
94
85
|
"scripts": {
|
|
95
|
-
"build": "tsdown
|
|
96
|
-
"clean": "
|
|
86
|
+
"build": "tsdown",
|
|
87
|
+
"clean": "node -e \"require('node:fs').rmSync('./dist', {recursive:true,force:true})\"",
|
|
97
88
|
"lint": "oxlint .",
|
|
98
89
|
"lint:fix": "oxlint --fix .",
|
|
99
90
|
"release": "pnpm publish --no-git-check",
|
|
@@ -4,7 +4,7 @@ import { functionPrinter } from '@kubb/plugin-ts'
|
|
|
4
4
|
import { File, Function, Type } from '@kubb/renderer-jsx'
|
|
5
5
|
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
6
6
|
import type { PluginSwr } from '../types.ts'
|
|
7
|
-
import { buildRequestConfigType, getComments, resolveErrorNames } from '../utils.ts'
|
|
7
|
+
import { buildRequestConfigType, buildStatusUnionType, getComments, resolveErrorNames } from '../utils.ts'
|
|
8
8
|
|
|
9
9
|
type Props = {
|
|
10
10
|
name: string
|
|
@@ -53,7 +53,7 @@ function buildMutationParamsNode(
|
|
|
53
53
|
const responseName = resolver.resolveResponseName(node)
|
|
54
54
|
const errorNames = resolveErrorNames(node, resolver)
|
|
55
55
|
|
|
56
|
-
const TData = dataReturnType === 'data' ? responseName :
|
|
56
|
+
const TData = dataReturnType === 'data' ? responseName : buildStatusUnionType(node, resolver)
|
|
57
57
|
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
|
|
58
58
|
|
|
59
59
|
return ast.createFunctionParameters({
|
|
@@ -90,7 +90,7 @@ export function Mutation({
|
|
|
90
90
|
const responseName = tsResolver.resolveResponseName(node)
|
|
91
91
|
const errorNames = resolveErrorNames(node, tsResolver)
|
|
92
92
|
|
|
93
|
-
const TData = dataReturnType === 'data' ? responseName :
|
|
93
|
+
const TData = dataReturnType === 'data' ? responseName : buildStatusUnionType(node, tsResolver)
|
|
94
94
|
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
|
|
95
95
|
|
|
96
96
|
const mutationArgParamsNode = createMutationArgParams(node, { paramsCasing, resolver: tsResolver })
|
package/src/components/Query.tsx
CHANGED
|
@@ -5,7 +5,7 @@ import { File, Function } from '@kubb/renderer-jsx'
|
|
|
5
5
|
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
6
6
|
import { getEnabledParamNames, markParamsOptional } from '@internals/tanstack-query'
|
|
7
7
|
import type { PluginSwr } from '../types.ts'
|
|
8
|
-
import { buildQueryKeyParams, getComments, resolveErrorNames } from '../utils.ts'
|
|
8
|
+
import { buildQueryKeyParams, buildStatusUnionType, getComments, resolveErrorNames } from '../utils.ts'
|
|
9
9
|
import { getQueryOptionsParams } from './QueryOptions.tsx'
|
|
10
10
|
|
|
11
11
|
type Props = {
|
|
@@ -39,7 +39,7 @@ function buildQueryParamsNode(
|
|
|
39
39
|
const requestName = node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : undefined
|
|
40
40
|
const errorNames = resolveErrorNames(node, resolver)
|
|
41
41
|
|
|
42
|
-
const TData = dataReturnType === 'data' ? responseName :
|
|
42
|
+
const TData = dataReturnType === 'data' ? responseName : buildStatusUnionType(node, resolver)
|
|
43
43
|
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
|
|
44
44
|
|
|
45
45
|
const optionsParam = ast.createFunctionParameter({
|
|
@@ -80,7 +80,7 @@ export function Query({
|
|
|
80
80
|
const responseName = tsResolver.resolveResponseName(node)
|
|
81
81
|
const errorNames = resolveErrorNames(node, tsResolver)
|
|
82
82
|
|
|
83
|
-
const TData = dataReturnType === 'data' ? responseName :
|
|
83
|
+
const TData = dataReturnType === 'data' ? responseName : buildStatusUnionType(node, tsResolver)
|
|
84
84
|
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
|
|
85
85
|
const generics = [TData, TError, `${queryKeyTypeName} | null`]
|
|
86
86
|
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { ast } from '@kubb/core'
|
|
1
|
+
import type { ast } from '@kubb/core'
|
|
2
2
|
import type { ResolverTs } from '@kubb/plugin-ts'
|
|
3
3
|
import { functionPrinter } from '@kubb/plugin-ts'
|
|
4
4
|
import { File, Function } from '@kubb/renderer-jsx'
|
|
5
5
|
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
6
|
-
import { getEnabledParamNames, injectNonNullAssertions, markParamsOptional } from '@internals/tanstack-query'
|
|
6
|
+
import { buildQueryOptionsParams, getEnabledParamNames, injectNonNullAssertions, markParamsOptional } from '@internals/tanstack-query'
|
|
7
7
|
import type { PluginSwr } from '../types.ts'
|
|
8
8
|
import { buildQueryKeyParams } from '../utils.ts'
|
|
9
9
|
|
|
@@ -29,25 +29,7 @@ export function getQueryOptionsParams(
|
|
|
29
29
|
resolver: ResolverTs
|
|
30
30
|
},
|
|
31
31
|
): ast.FunctionParametersNode {
|
|
32
|
-
|
|
33
|
-
const requestName = node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : undefined
|
|
34
|
-
|
|
35
|
-
return ast.createOperationParams(node, {
|
|
36
|
-
paramsType,
|
|
37
|
-
pathParamsType: paramsType === 'object' ? 'object' : pathParamsType === 'object' ? 'object' : 'inline',
|
|
38
|
-
paramsCasing,
|
|
39
|
-
resolver,
|
|
40
|
-
extraParams: [
|
|
41
|
-
ast.createFunctionParameter({
|
|
42
|
-
name: 'config',
|
|
43
|
-
type: ast.createParamsType({
|
|
44
|
-
variant: 'reference',
|
|
45
|
-
name: requestName ? `Partial<RequestConfig<${requestName}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }',
|
|
46
|
-
}),
|
|
47
|
-
default: '{}',
|
|
48
|
-
}),
|
|
49
|
-
],
|
|
50
|
-
})
|
|
32
|
+
return buildQueryOptionsParams(node, options)
|
|
51
33
|
}
|
|
52
34
|
|
|
53
35
|
export function QueryOptions({ name, clientName, node, tsResolver, paramsCasing, paramsType, pathParamsType }: Props): KubbReactNode {
|
|
@@ -2,14 +2,18 @@ import path from 'node:path'
|
|
|
2
2
|
import { resolveOperationTypeNames } from '@internals/shared'
|
|
3
3
|
import { resolveZodSchemaNames } from '@internals/tanstack-query'
|
|
4
4
|
import { ast, defineGenerator } from '@kubb/core'
|
|
5
|
-
import { Client, pluginClientName } from '@kubb/plugin-client'
|
|
5
|
+
import { Client, isParserEnabled, pluginClientName } from '@kubb/plugin-client'
|
|
6
6
|
import { pluginTsName } from '@kubb/plugin-ts'
|
|
7
7
|
import { pluginZodName } from '@kubb/plugin-zod'
|
|
8
8
|
import { File, jsxRenderer } from '@kubb/renderer-jsx'
|
|
9
|
-
import { difference } from 'remeda'
|
|
10
9
|
import { Mutation, MutationKey } from '../components'
|
|
11
10
|
import type { PluginSwr } from '../types'
|
|
12
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Built-in generator for `useSWRMutation` hooks. Emits one `useFooMutation` hook
|
|
14
|
+
* per POST/PUT/DELETE operation (configurable via `mutation.methods`) plus
|
|
15
|
+
* the matching `fooMutationKey` helper.
|
|
16
|
+
*/
|
|
13
17
|
export const mutationGenerator = defineGenerator<PluginSwr>({
|
|
14
18
|
name: 'swr-mutation',
|
|
15
19
|
renderer: jsxRenderer,
|
|
@@ -23,10 +27,11 @@ export const mutationGenerator = defineGenerator<PluginSwr>({
|
|
|
23
27
|
const tsResolver = driver.getResolver(pluginTsName)
|
|
24
28
|
|
|
25
29
|
const isQuery = query === false || (!!query && query.methods.some((method) => node.method.toLowerCase() === method.toLowerCase()))
|
|
30
|
+
const queryMethods = new Set(query ? query.methods : [])
|
|
26
31
|
const isMutation =
|
|
27
32
|
mutation !== false &&
|
|
28
33
|
!isQuery &&
|
|
29
|
-
|
|
34
|
+
(mutation ? mutation.methods : []).some((method) => !queryMethods.has(method) && node.method.toLowerCase() === method.toLowerCase())
|
|
30
35
|
|
|
31
36
|
if (!isMutation) return null
|
|
32
37
|
|
|
@@ -48,7 +53,7 @@ export const mutationGenerator = defineGenerator<PluginSwr>({
|
|
|
48
53
|
|
|
49
54
|
const importedTypeNames = resolveOperationTypeNames(node, tsResolver, { paramsCasing, order: 'body-response-first' })
|
|
50
55
|
|
|
51
|
-
const pluginZod = parser
|
|
56
|
+
const pluginZod = isParserEnabled(parser) ? driver.getPlugin(pluginZodName) : undefined
|
|
52
57
|
const zodResolver = pluginZod ? driver.getResolver(pluginZodName) : undefined
|
|
53
58
|
const fileZod = zodResolver
|
|
54
59
|
? zodResolver.resolveFile(
|
|
@@ -56,7 +61,7 @@ export const mutationGenerator = defineGenerator<PluginSwr>({
|
|
|
56
61
|
{ root, output: pluginZod?.options?.output ?? output, group: pluginZod?.options?.group },
|
|
57
62
|
)
|
|
58
63
|
: undefined
|
|
59
|
-
const zodSchemaNames = resolveZodSchemaNames(node, zodResolver)
|
|
64
|
+
const zodSchemaNames = resolveZodSchemaNames(node, zodResolver, parser)
|
|
60
65
|
|
|
61
66
|
const clientPlugin = driver.getPlugin(pluginClientName)
|
|
62
67
|
const hasClientPlugin = clientPlugin?.name === pluginClientName
|
|
@@ -89,7 +94,6 @@ export const mutationGenerator = defineGenerator<PluginSwr>({
|
|
|
89
94
|
<>
|
|
90
95
|
{!shouldUseClientPlugin && <File.Import name={'client'} path={clientOptions.importPath} />}
|
|
91
96
|
<File.Import name={['Client', 'RequestConfig', 'ResponseErrorConfig']} path={clientOptions.importPath} isTypeOnly />
|
|
92
|
-
{clientOptions.dataReturnType === 'full' && <File.Import name={['ResponseConfig']} path={clientOptions.importPath} isTypeOnly />}
|
|
93
97
|
</>
|
|
94
98
|
) : (
|
|
95
99
|
<>
|
|
@@ -100,15 +104,15 @@ export const mutationGenerator = defineGenerator<PluginSwr>({
|
|
|
100
104
|
path={path.resolve(root, '.kubb/client.ts')}
|
|
101
105
|
isTypeOnly
|
|
102
106
|
/>
|
|
103
|
-
{clientOptions.dataReturnType === 'full' && (
|
|
104
|
-
<File.Import name={['ResponseConfig']} root={meta.file.path} path={path.resolve(root, '.kubb/client.ts')} isTypeOnly />
|
|
105
|
-
)}
|
|
106
107
|
</>
|
|
107
108
|
)}
|
|
108
109
|
{shouldUseClientPlugin && clientFile && <File.Import name={[resolvedClientName]} root={meta.file.path} path={clientFile.path} />}
|
|
109
110
|
{!shouldUseClientPlugin && node.requestBody?.content?.some((e) => e.contentType === 'multipart/form-data') && (
|
|
110
111
|
<File.Import name={['buildFormData']} root={meta.file.path} path={path.resolve(root, '.kubb/config.ts')} />
|
|
111
112
|
)}
|
|
113
|
+
{!shouldUseClientPlugin && parser === 'zod' && zodResolver && node.requestBody?.content?.[0]?.schema && (
|
|
114
|
+
<File.Import name={['z']} path="zod" isTypeOnly />
|
|
115
|
+
)}
|
|
112
116
|
{meta.fileTs && importedTypeNames.length > 0 && (
|
|
113
117
|
<File.Import name={Array.from(new Set(importedTypeNames))} root={meta.file.path} path={meta.fileTs.path} isTypeOnly />
|
|
114
118
|
)}
|
|
@@ -2,11 +2,10 @@ import path from 'node:path'
|
|
|
2
2
|
import { resolveOperationTypeNames } from '@internals/shared'
|
|
3
3
|
import { resolveZodSchemaNames } from '@internals/tanstack-query'
|
|
4
4
|
import { ast, defineGenerator } from '@kubb/core'
|
|
5
|
-
import { Client, pluginClientName } from '@kubb/plugin-client'
|
|
5
|
+
import { Client, isParserEnabled, pluginClientName } from '@kubb/plugin-client'
|
|
6
6
|
import { pluginTsName } from '@kubb/plugin-ts'
|
|
7
7
|
import { pluginZodName } from '@kubb/plugin-zod'
|
|
8
8
|
import { File, jsxRenderer } from '@kubb/renderer-jsx'
|
|
9
|
-
import { difference } from 'remeda'
|
|
10
9
|
import { Query, QueryKey, QueryOptions } from '../components'
|
|
11
10
|
import type { PluginSwr } from '../types'
|
|
12
11
|
|
|
@@ -23,10 +22,11 @@ export const queryGenerator = defineGenerator<PluginSwr>({
|
|
|
23
22
|
const tsResolver = driver.getResolver(pluginTsName)
|
|
24
23
|
|
|
25
24
|
const isQuery = query === false || (!!query && query.methods.some((method) => node.method.toLowerCase() === method.toLowerCase()))
|
|
25
|
+
const queryMethods = new Set(query ? query.methods : [])
|
|
26
26
|
const isMutation =
|
|
27
27
|
mutation !== false &&
|
|
28
28
|
!isQuery &&
|
|
29
|
-
|
|
29
|
+
(mutation ? mutation.methods : []).some((method) => !queryMethods.has(method) && node.method.toLowerCase() === method.toLowerCase())
|
|
30
30
|
|
|
31
31
|
if (!isQuery || isMutation) return null
|
|
32
32
|
|
|
@@ -52,7 +52,7 @@ export const queryGenerator = defineGenerator<PluginSwr>({
|
|
|
52
52
|
order: 'body-response-first',
|
|
53
53
|
})
|
|
54
54
|
|
|
55
|
-
const pluginZod = parser
|
|
55
|
+
const pluginZod = isParserEnabled(parser) ? driver.getPlugin(pluginZodName) : undefined
|
|
56
56
|
const zodResolver = pluginZod ? driver.getResolver(pluginZodName) : undefined
|
|
57
57
|
const fileZod = zodResolver
|
|
58
58
|
? zodResolver.resolveFile(
|
|
@@ -60,7 +60,7 @@ export const queryGenerator = defineGenerator<PluginSwr>({
|
|
|
60
60
|
{ root, output: pluginZod?.options?.output ?? output, group: pluginZod?.options?.group },
|
|
61
61
|
)
|
|
62
62
|
: undefined
|
|
63
|
-
const zodSchemaNames = resolveZodSchemaNames(node, zodResolver)
|
|
63
|
+
const zodSchemaNames = resolveZodSchemaNames(node, zodResolver, parser)
|
|
64
64
|
|
|
65
65
|
const clientPlugin = driver.getPlugin(pluginClientName)
|
|
66
66
|
const hasClientPlugin = clientPlugin?.name === pluginClientName
|
|
@@ -93,7 +93,6 @@ export const queryGenerator = defineGenerator<PluginSwr>({
|
|
|
93
93
|
<>
|
|
94
94
|
{!shouldUseClientPlugin && <File.Import name={'client'} path={clientOptions.importPath} />}
|
|
95
95
|
<File.Import name={['Client', 'RequestConfig', 'ResponseErrorConfig']} path={clientOptions.importPath} isTypeOnly />
|
|
96
|
-
{clientOptions.dataReturnType === 'full' && <File.Import name={['ResponseConfig']} path={clientOptions.importPath} isTypeOnly />}
|
|
97
96
|
</>
|
|
98
97
|
) : (
|
|
99
98
|
<>
|
|
@@ -104,9 +103,6 @@ export const queryGenerator = defineGenerator<PluginSwr>({
|
|
|
104
103
|
path={path.resolve(root, '.kubb/client.ts')}
|
|
105
104
|
isTypeOnly
|
|
106
105
|
/>
|
|
107
|
-
{clientOptions.dataReturnType === 'full' && (
|
|
108
|
-
<File.Import name={['ResponseConfig']} root={meta.file.path} path={path.resolve(root, '.kubb/client.ts')} isTypeOnly />
|
|
109
|
-
)}
|
|
110
106
|
</>
|
|
111
107
|
)}
|
|
112
108
|
{shouldUseClientPlugin && clientFile && <File.Import name={[resolvedClientName]} root={meta.file.path} path={clientFile.path} />}
|
package/src/plugin.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import path from 'node:path'
|
|
2
|
-
import {
|
|
3
|
-
import { ast, definePlugin
|
|
4
|
-
import { pluginClientName } from '@kubb/plugin-client'
|
|
2
|
+
import { createGroupConfig } from '@internals/shared'
|
|
3
|
+
import { ast, definePlugin } from '@kubb/core'
|
|
4
|
+
import { isParserEnabled, pluginClientName } from '@kubb/plugin-client'
|
|
5
5
|
import { source as axiosClientSource } from '@kubb/plugin-client/templates/clients/axios.source'
|
|
6
6
|
import { source as fetchClientSource } from '@kubb/plugin-client/templates/clients/fetch.source'
|
|
7
7
|
import { source as configSource } from '@kubb/plugin-client/templates/config.source'
|
|
@@ -16,7 +16,7 @@ export const pluginSwrName = 'plugin-swr' satisfies PluginSwr['name']
|
|
|
16
16
|
|
|
17
17
|
export const pluginSwr = definePlugin<PluginSwr>((options) => {
|
|
18
18
|
const {
|
|
19
|
-
output = { path: 'hooks',
|
|
19
|
+
output = { path: 'hooks', barrel: { type: 'named' } },
|
|
20
20
|
group,
|
|
21
21
|
exclude = [],
|
|
22
22
|
include,
|
|
@@ -41,24 +41,12 @@ export const pluginSwr = definePlugin<PluginSwr>((options) => {
|
|
|
41
41
|
const selectedGenerators =
|
|
42
42
|
options.generators ?? [queryGenerator, mutationGenerator].filter((generator): generator is NonNullable<typeof generator> => Boolean(generator))
|
|
43
43
|
|
|
44
|
-
const groupConfig = group
|
|
45
|
-
? ({
|
|
46
|
-
...group,
|
|
47
|
-
name: group.name
|
|
48
|
-
? group.name
|
|
49
|
-
: (ctx: { group: string }) => {
|
|
50
|
-
if (group.type === 'path') {
|
|
51
|
-
return `${ctx.group.split('/')[1]}`
|
|
52
|
-
}
|
|
53
|
-
return `${camelCase(ctx.group)}Controller`
|
|
54
|
-
},
|
|
55
|
-
} satisfies Group)
|
|
56
|
-
: undefined
|
|
44
|
+
const groupConfig = createGroupConfig(group) ?? undefined
|
|
57
45
|
|
|
58
46
|
return {
|
|
59
47
|
name: pluginSwrName,
|
|
60
48
|
options,
|
|
61
|
-
dependencies: [pluginTsName, parser
|
|
49
|
+
dependencies: [pluginTsName, isParserEnabled(parser) ? pluginZodName : undefined].filter((dependency): dependency is string => Boolean(dependency)),
|
|
62
50
|
hooks: {
|
|
63
51
|
'kubb:plugin:setup'(ctx) {
|
|
64
52
|
const resolver = userResolver ? { ...resolverSwr, ...userResolver } : resolverSwr
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { camelCase } from '@internals/utils'
|
|
1
|
+
import { camelCase, toFilePath } from '@internals/utils'
|
|
2
2
|
import { defineResolver } from '@kubb/core'
|
|
3
3
|
import type { PluginSwr } from '../types.ts'
|
|
4
4
|
|
|
@@ -18,7 +18,7 @@ export const resolverSwr = defineResolver<PluginSwr>(() => ({
|
|
|
18
18
|
name: 'default',
|
|
19
19
|
pluginName: 'plugin-swr',
|
|
20
20
|
default(name, type) {
|
|
21
|
-
return
|
|
21
|
+
return type === 'file' ? toFilePath(name) : camelCase(name)
|
|
22
22
|
},
|
|
23
23
|
resolveName(name) {
|
|
24
24
|
return this.default(name, 'function')
|
package/src/types.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Transformer } from '@internals/tanstack-query'
|
|
2
|
-
import type { ast, Exclude, Generator, Group, Include, Output, Override, PluginFactoryOptions, Resolver } from '@kubb/core'
|
|
2
|
+
import type { ast, Exclude, Generator, Group, Include, Output, OutputOptions, Override, PluginFactoryOptions, Resolver } from '@kubb/core'
|
|
3
3
|
import type { ClientImportPath, PluginClient } from '@kubb/plugin-client'
|
|
4
4
|
|
|
5
5
|
export type { Transformer } from '@internals/tanstack-query'
|
|
@@ -101,16 +101,13 @@ type Mutation = {
|
|
|
101
101
|
importPath?: string
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
* Group the SWR hooks based on the provided name.
|
|
112
|
-
*/
|
|
113
|
-
group?: Group
|
|
104
|
+
/**
|
|
105
|
+
* Where the generated SWR hooks are written and how they are exported, plus the optional `group`
|
|
106
|
+
* strategy. The `group` option organizes `output.mode: 'directory'` output into per-tag or per-path subdirectories.
|
|
107
|
+
*
|
|
108
|
+
* @default { path: 'hooks', barrel: { type: 'named' } }
|
|
109
|
+
*/
|
|
110
|
+
export type Options = OutputOptions & {
|
|
114
111
|
client?: ClientImportPath & Pick<PluginClient['options'], 'clientType' | 'dataReturnType' | 'baseURL' | 'bundle' | 'paramsCasing'>
|
|
115
112
|
/**
|
|
116
113
|
* Tags, operations, or paths to exclude from generation.
|
|
@@ -175,7 +172,7 @@ type ResolvedOptions = {
|
|
|
175
172
|
include: Options['include']
|
|
176
173
|
override: NonNullable<Options['override']>
|
|
177
174
|
client: Pick<PluginClient['options'], 'client' | 'clientType' | 'dataReturnType' | 'importPath' | 'baseURL' | 'bundle' | 'paramsCasing'>
|
|
178
|
-
parser:
|
|
175
|
+
parser: NonNullable<Options['parser']>
|
|
179
176
|
pathParamsType: NonNullable<Options['pathParamsType']>
|
|
180
177
|
paramsCasing: Options['paramsCasing']
|
|
181
178
|
paramsType: NonNullable<Options['paramsType']>
|
package/src/utils.ts
CHANGED
|
@@ -7,4 +7,11 @@ export {
|
|
|
7
7
|
resolveQueryGroupType,
|
|
8
8
|
resolveZodSchemaNames,
|
|
9
9
|
} from '@internals/tanstack-query'
|
|
10
|
-
export {
|
|
10
|
+
export {
|
|
11
|
+
buildOperationComments as getComments,
|
|
12
|
+
buildRequestConfigType,
|
|
13
|
+
buildStatusUnionType,
|
|
14
|
+
getContentTypeInfo,
|
|
15
|
+
resolveErrorNames,
|
|
16
|
+
resolveStatusCodeNames,
|
|
17
|
+
} from '@internals/shared'
|