@kubb/plugin-faker 5.0.0-alpha.9 → 5.0.0-beta.3
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/LICENSE +17 -10
- package/README.md +1 -4
- package/dist/Faker-BgleOzVN.cjs +486 -0
- package/dist/Faker-BgleOzVN.cjs.map +1 -0
- package/dist/Faker-CdyPfOPg.d.ts +27 -0
- package/dist/Faker-fcQEB9i5.js +384 -0
- package/dist/Faker-fcQEB9i5.js.map +1 -0
- package/dist/components.cjs +2 -2
- package/dist/components.d.ts +2 -31
- package/dist/components.js +1 -1
- package/dist/fakerGenerator-C3Ho3BaI.d.ts +9 -0
- package/dist/fakerGenerator-D7daHCh6.js +516 -0
- package/dist/fakerGenerator-D7daHCh6.js.map +1 -0
- package/dist/fakerGenerator-VJEVzLjc.cjs +526 -0
- package/dist/fakerGenerator-VJEVzLjc.cjs.map +1 -0
- package/dist/generators.cjs +1 -1
- package/dist/generators.d.ts +2 -505
- package/dist/generators.js +1 -1
- package/dist/index.cjs +136 -84
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +28 -4
- package/dist/index.js +128 -83
- package/dist/index.js.map +1 -1
- package/dist/printerFaker-CJiwzoto.d.ts +206 -0
- package/package.json +52 -50
- package/src/components/Faker.tsx +124 -78
- package/src/generators/fakerGenerator.tsx +235 -134
- package/src/index.ts +7 -2
- package/src/plugin.ts +60 -121
- package/src/printers/printerFaker.ts +341 -0
- package/src/resolvers/resolverFaker.ts +92 -0
- package/src/types.ts +127 -81
- package/src/utils.ts +356 -0
- package/dist/components-BkBIov4R.js +0 -419
- package/dist/components-BkBIov4R.js.map +0 -1
- package/dist/components-IdP8GXXX.cjs +0 -461
- package/dist/components-IdP8GXXX.cjs.map +0 -1
- package/dist/fakerGenerator-CYUCNH3Q.cjs +0 -204
- package/dist/fakerGenerator-CYUCNH3Q.cjs.map +0 -1
- package/dist/fakerGenerator-M5oCrPmy.js +0 -200
- package/dist/fakerGenerator-M5oCrPmy.js.map +0 -1
- package/dist/types-r7BubMLO.d.ts +0 -132
- package/src/parser.ts +0 -453
package/dist/index.cjs
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
Object.
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
Object.defineProperties(exports, {
|
|
2
|
+
__esModule: { value: true },
|
|
3
|
+
[Symbol.toStringTag]: { value: "Module" }
|
|
4
|
+
});
|
|
5
|
+
const require_Faker = require("./Faker-BgleOzVN.cjs");
|
|
6
|
+
const require_fakerGenerator = require("./fakerGenerator-VJEVzLjc.cjs");
|
|
4
7
|
let node_path = require("node:path");
|
|
5
|
-
node_path =
|
|
8
|
+
node_path = require_Faker.__toESM(node_path, 1);
|
|
6
9
|
let _kubb_core = require("@kubb/core");
|
|
7
|
-
let _kubb_plugin_oas = require("@kubb/plugin-oas");
|
|
8
10
|
let _kubb_plugin_ts = require("@kubb/plugin-ts");
|
|
11
|
+
let node_crypto = require("node:crypto");
|
|
9
12
|
//#region ../../internals/utils/src/casing.ts
|
|
10
13
|
/**
|
|
11
14
|
* Shared implementation for camelCase and PascalCase conversion.
|
|
@@ -25,9 +28,12 @@ function toCamelOrPascal(text, pascal) {
|
|
|
25
28
|
* Splits `text` on `.` and applies `transformPart` to each segment.
|
|
26
29
|
* The last segment receives `isLast = true`, all earlier segments receive `false`.
|
|
27
30
|
* Segments are joined with `/` to form a file path.
|
|
31
|
+
*
|
|
32
|
+
* Only splits on dots followed by a letter so that version numbers
|
|
33
|
+
* embedded in operationIds (e.g. `v2025.0`) are kept intact.
|
|
28
34
|
*/
|
|
29
35
|
function applyToFileParts(text, transformPart) {
|
|
30
|
-
const parts = text.split(
|
|
36
|
+
const parts = text.split(/\.(?=[a-zA-Z])/);
|
|
31
37
|
return parts.map((part, i) => transformPart(part, i === parts.length - 1)).join("/");
|
|
32
38
|
}
|
|
33
39
|
/**
|
|
@@ -46,99 +52,145 @@ function camelCase(text, { isFile, prefix = "", suffix = "" } = {}) {
|
|
|
46
52
|
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
|
|
47
53
|
}
|
|
48
54
|
//#endregion
|
|
55
|
+
//#region src/resolvers/resolverFaker.ts
|
|
56
|
+
function isValidStrictIdentifier(name) {
|
|
57
|
+
try {
|
|
58
|
+
new Function(`"use strict"; const ${name} = 1;`);
|
|
59
|
+
} catch {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Naming convention resolver for Faker plugin.
|
|
66
|
+
*
|
|
67
|
+
* Provides default naming helpers using camelCase. Prefixes invalid identifiers with `_`.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* `resolverFaker.default('list pets', 'function') // → 'listPets'`
|
|
71
|
+
*/
|
|
72
|
+
const resolverFaker = (0, _kubb_core.defineResolver)((ctx) => {
|
|
73
|
+
return {
|
|
74
|
+
name: "default",
|
|
75
|
+
pluginName: "plugin-faker",
|
|
76
|
+
default(name, type) {
|
|
77
|
+
const resolvedName = camelCase(name, { isFile: type === "file" });
|
|
78
|
+
if (type === "file" || isValidStrictIdentifier(resolvedName)) return resolvedName;
|
|
79
|
+
return `_${resolvedName}`;
|
|
80
|
+
},
|
|
81
|
+
resolveName(name, type) {
|
|
82
|
+
return ctx.default(name, type);
|
|
83
|
+
},
|
|
84
|
+
resolvePathName(name, type) {
|
|
85
|
+
return ctx.default(name, type);
|
|
86
|
+
},
|
|
87
|
+
resolveFile({ name, extname, tag, path: groupPath }, context) {
|
|
88
|
+
const pathMode = _kubb_core.PluginDriver.getMode(node_path.default.resolve(context.root, context.output.path));
|
|
89
|
+
const baseName = `${pathMode === "single" ? "" : ctx.resolveName(name, "file")}${extname}`;
|
|
90
|
+
const filePath = ctx.resolvePath({
|
|
91
|
+
baseName,
|
|
92
|
+
pathMode,
|
|
93
|
+
tag,
|
|
94
|
+
path: groupPath
|
|
95
|
+
}, context);
|
|
96
|
+
return {
|
|
97
|
+
kind: "File",
|
|
98
|
+
id: (0, node_crypto.createHash)("sha256").update(filePath).digest("hex"),
|
|
99
|
+
name: node_path.default.basename(filePath, extname),
|
|
100
|
+
path: filePath,
|
|
101
|
+
baseName,
|
|
102
|
+
extname,
|
|
103
|
+
meta: { pluginName: ctx.pluginName },
|
|
104
|
+
sources: [],
|
|
105
|
+
imports: [],
|
|
106
|
+
exports: []
|
|
107
|
+
};
|
|
108
|
+
},
|
|
109
|
+
resolveParamName(node, param) {
|
|
110
|
+
return ctx.resolveName(`${node.operationId} ${param.in} ${param.name}`);
|
|
111
|
+
},
|
|
112
|
+
resolveDataName(node) {
|
|
113
|
+
return ctx.resolveName(`${node.operationId} Data`);
|
|
114
|
+
},
|
|
115
|
+
resolveResponseStatusName(node, statusCode) {
|
|
116
|
+
return ctx.resolveName(`${node.operationId} Status ${statusCode}`);
|
|
117
|
+
},
|
|
118
|
+
resolveResponseName(node) {
|
|
119
|
+
return ctx.resolveName(`${node.operationId} Response`);
|
|
120
|
+
},
|
|
121
|
+
resolvePathParamsName(node, param) {
|
|
122
|
+
return ctx.resolveParamName(node, param);
|
|
123
|
+
},
|
|
124
|
+
resolveQueryParamsName(node, param) {
|
|
125
|
+
return ctx.resolveParamName(node, param);
|
|
126
|
+
},
|
|
127
|
+
resolveHeaderParamsName(node, param) {
|
|
128
|
+
return ctx.resolveParamName(node, param);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
});
|
|
132
|
+
//#endregion
|
|
49
133
|
//#region src/plugin.ts
|
|
134
|
+
/**
|
|
135
|
+
* Canonical plugin name for `@kubb/plugin-faker`, used in driver lookups and warnings.
|
|
136
|
+
*/
|
|
50
137
|
const pluginFakerName = "plugin-faker";
|
|
51
|
-
|
|
138
|
+
/**
|
|
139
|
+
* Generates Faker mock data factories from OpenAPI/AST specification.
|
|
140
|
+
*
|
|
141
|
+
* Creates randomized test data and mock helpers from schema definitions.
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* `import pluginFaker from '@kubb/plugin-faker'; export default defineConfig({ plugins: [pluginFaker({ output: { path: 'mocks' } })], })`
|
|
145
|
+
*/
|
|
146
|
+
const pluginFaker = (0, _kubb_core.definePlugin)((options) => {
|
|
52
147
|
const { output = {
|
|
53
148
|
path: "mocks",
|
|
54
149
|
barrelType: "named"
|
|
55
|
-
}, seed, group, exclude = [], include, override = [],
|
|
150
|
+
}, seed, locale, group, exclude = [], include, override = [], mapper = {}, dateParser = "faker", generators: userGenerators = [], regexGenerator = "faker", paramsCasing, printer, resolver: userResolver, transformer: userTransformer } = options;
|
|
151
|
+
const groupConfig = group ? {
|
|
152
|
+
...group,
|
|
153
|
+
name: group.name ? group.name : (ctx) => {
|
|
154
|
+
if (group.type === "path") return `${ctx.group.split("/")[1]}`;
|
|
155
|
+
return `${camelCase(ctx.group)}Controller`;
|
|
156
|
+
}
|
|
157
|
+
} : void 0;
|
|
56
158
|
return {
|
|
57
159
|
name: pluginFakerName,
|
|
58
|
-
options
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
emptySchemaType,
|
|
66
|
-
dateParser,
|
|
67
|
-
mapper,
|
|
68
|
-
override,
|
|
69
|
-
regexGenerator,
|
|
70
|
-
paramsCasing,
|
|
71
|
-
group,
|
|
72
|
-
usedEnumNames: {}
|
|
73
|
-
},
|
|
74
|
-
pre: [_kubb_plugin_oas.pluginOasName, _kubb_plugin_ts.pluginTsName],
|
|
75
|
-
resolvePath(baseName, pathMode, options) {
|
|
76
|
-
const root = node_path.default.resolve(this.config.root, this.config.output.path);
|
|
77
|
-
if ((pathMode ?? (0, _kubb_core.getMode)(node_path.default.resolve(root, output.path))) === "single")
|
|
78
|
-
/**
|
|
79
|
-
* when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
|
|
80
|
-
* Other plugins then need to call addOrAppend instead of just add from the fileManager class
|
|
81
|
-
*/
|
|
82
|
-
return node_path.default.resolve(root, output.path);
|
|
83
|
-
if (group && (options?.group?.path || options?.group?.tag)) {
|
|
84
|
-
const groupName = group?.name ? group.name : (ctx) => {
|
|
85
|
-
if (group?.type === "path") return `${ctx.group.split("/")[1]}`;
|
|
86
|
-
return `${camelCase(ctx.group)}Controller`;
|
|
87
|
-
};
|
|
88
|
-
return node_path.default.resolve(root, output.path, groupName({ group: group.type === "path" ? options.group.path : options.group.tag }), baseName);
|
|
89
|
-
}
|
|
90
|
-
return node_path.default.resolve(root, output.path, baseName);
|
|
91
|
-
},
|
|
92
|
-
resolveName(name, type) {
|
|
93
|
-
const resolvedName = camelCase(name, {
|
|
94
|
-
prefix: type ? "create" : void 0,
|
|
95
|
-
isFile: type === "file"
|
|
96
|
-
});
|
|
97
|
-
if (type) return transformers?.name?.(resolvedName, type) || resolvedName;
|
|
98
|
-
return resolvedName;
|
|
99
|
-
},
|
|
100
|
-
async install() {
|
|
101
|
-
const root = node_path.default.resolve(this.config.root, this.config.output.path);
|
|
102
|
-
const mode = (0, _kubb_core.getMode)(node_path.default.resolve(root, output.path));
|
|
103
|
-
const oas = await this.getOas();
|
|
104
|
-
const schemaFiles = await new _kubb_plugin_oas.SchemaGenerator(this.plugin.options, {
|
|
105
|
-
fabric: this.fabric,
|
|
106
|
-
oas,
|
|
107
|
-
driver: this.driver,
|
|
108
|
-
events: this.events,
|
|
109
|
-
plugin: this.plugin,
|
|
110
|
-
contentType,
|
|
111
|
-
include: void 0,
|
|
112
|
-
override,
|
|
113
|
-
mode,
|
|
114
|
-
output: output.path
|
|
115
|
-
}).build(...generators);
|
|
116
|
-
await this.upsertFile(...schemaFiles);
|
|
117
|
-
const operationFiles = await new _kubb_plugin_oas.OperationGenerator(this.plugin.options, {
|
|
118
|
-
fabric: this.fabric,
|
|
119
|
-
oas,
|
|
120
|
-
driver: this.driver,
|
|
121
|
-
events: this.events,
|
|
122
|
-
plugin: this.plugin,
|
|
123
|
-
contentType,
|
|
160
|
+
options,
|
|
161
|
+
dependencies: [_kubb_plugin_ts.pluginTsName],
|
|
162
|
+
hooks: { "kubb:plugin:setup"(ctx) {
|
|
163
|
+
ctx.setOptions({
|
|
164
|
+
output,
|
|
165
|
+
seed,
|
|
166
|
+
locale,
|
|
124
167
|
exclude,
|
|
125
168
|
include,
|
|
126
169
|
override,
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
output,
|
|
134
|
-
meta: { pluginName: this.plugin.name }
|
|
170
|
+
group: groupConfig,
|
|
171
|
+
mapper,
|
|
172
|
+
dateParser,
|
|
173
|
+
regexGenerator,
|
|
174
|
+
paramsCasing,
|
|
175
|
+
printer
|
|
135
176
|
});
|
|
136
|
-
|
|
137
|
-
|
|
177
|
+
ctx.setResolver(userResolver ? {
|
|
178
|
+
...resolverFaker,
|
|
179
|
+
...userResolver
|
|
180
|
+
} : resolverFaker);
|
|
181
|
+
if (userTransformer) ctx.setTransformer(userTransformer);
|
|
182
|
+
ctx.addGenerator(require_fakerGenerator.fakerGenerator);
|
|
183
|
+
for (const generator of userGenerators) ctx.addGenerator(generator);
|
|
184
|
+
} }
|
|
138
185
|
};
|
|
139
186
|
});
|
|
140
187
|
//#endregion
|
|
188
|
+
exports.Faker = require_Faker.Faker;
|
|
189
|
+
exports.default = pluginFaker;
|
|
190
|
+
exports.fakerGenerator = require_fakerGenerator.fakerGenerator;
|
|
141
191
|
exports.pluginFaker = pluginFaker;
|
|
142
192
|
exports.pluginFakerName = pluginFakerName;
|
|
193
|
+
exports.printerFaker = require_fakerGenerator.printerFaker;
|
|
194
|
+
exports.resolverFaker = resolverFaker;
|
|
143
195
|
|
|
144
196
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["fakerGenerator","pluginOasName","pluginTsName","path","SchemaGenerator","OperationGenerator"],"sources":["../../../internals/utils/src/casing.ts","../src/plugin.ts"],"sourcesContent":["type Options = {\n /** When `true`, dot-separated segments are split on `.` and joined with `/` after casing. */\n isFile?: boolean\n /** Text prepended before casing is applied. */\n prefix?: string\n /** Text appended before casing is applied. */\n suffix?: string\n}\n\n/**\n * Shared implementation for camelCase and PascalCase conversion.\n * Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)\n * and capitalizes each word according to `pascal`.\n *\n * When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.\n */\nfunction toCamelOrPascal(text: string, pascal: boolean): string {\n const normalized = text\n .trim()\n .replace(/([a-z\\d])([A-Z])/g, '$1 $2')\n .replace(/([A-Z]+)([A-Z][a-z])/g, '$1 $2')\n .replace(/(\\d)([a-z])/g, '$1 $2')\n\n const words = normalized.split(/[\\s\\-_./\\\\:]+/).filter(Boolean)\n\n return words\n .map((word, i) => {\n const allUpper = word.length > 1 && word === word.toUpperCase()\n if (allUpper) return word\n if (i === 0 && !pascal) return word.charAt(0).toLowerCase() + word.slice(1)\n return word.charAt(0).toUpperCase() + word.slice(1)\n })\n .join('')\n .replace(/[^a-zA-Z0-9]/g, '')\n}\n\n/**\n * Splits `text` on `.` and applies `transformPart` to each segment.\n * The last segment receives `isLast = true`, all earlier segments receive `false`.\n * Segments are joined with `/` to form a file path.\n */\nfunction applyToFileParts(text: string, transformPart: (part: string, isLast: boolean) => string): string {\n const parts = text.split('.')\n return parts.map((part, i) => transformPart(part, i === parts.length - 1)).join('/')\n}\n\n/**\n * Converts `text` to camelCase.\n * When `isFile` is `true`, dot-separated segments are each cased independently and joined with `/`.\n *\n * @example\n * camelCase('hello-world') // 'helloWorld'\n * camelCase('pet.petId', { isFile: true }) // 'pet/petId'\n */\nexport function camelCase(text: string, { isFile, prefix = '', suffix = '' }: Options = {}): string {\n if (isFile) {\n return applyToFileParts(text, (part, isLast) => camelCase(part, isLast ? { prefix, suffix } : {}))\n }\n\n return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false)\n}\n\n/**\n * Converts `text` to PascalCase.\n * When `isFile` is `true`, the last dot-separated segment is PascalCased and earlier segments are camelCased.\n *\n * @example\n * pascalCase('hello-world') // 'HelloWorld'\n * pascalCase('pet.petId', { isFile: true }) // 'pet/PetId'\n */\nexport function pascalCase(text: string, { isFile, prefix = '', suffix = '' }: Options = {}): string {\n if (isFile) {\n return applyToFileParts(text, (part, isLast) => (isLast ? pascalCase(part, { prefix, suffix }) : camelCase(part)))\n }\n\n return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true)\n}\n\n/**\n * Converts `text` to snake_case.\n *\n * @example\n * snakeCase('helloWorld') // 'hello_world'\n * snakeCase('Hello-World') // 'hello_world'\n */\nexport function snakeCase(text: string, { prefix = '', suffix = '' }: Omit<Options, 'isFile'> = {}): string {\n const processed = `${prefix} ${text} ${suffix}`.trim()\n return processed\n .replace(/([a-z])([A-Z])/g, '$1_$2')\n .replace(/[\\s\\-.]+/g, '_')\n .replace(/[^a-zA-Z0-9_]/g, '')\n .toLowerCase()\n .split('_')\n .filter(Boolean)\n .join('_')\n}\n\n/**\n * Converts `text` to SCREAMING_SNAKE_CASE.\n *\n * @example\n * screamingSnakeCase('helloWorld') // 'HELLO_WORLD'\n */\nexport function screamingSnakeCase(text: string, { prefix = '', suffix = '' }: Omit<Options, 'isFile'> = {}): string {\n return snakeCase(text, { prefix, suffix }).toUpperCase()\n}\n","import path from 'node:path'\nimport { camelCase } from '@internals/utils'\nimport { createPlugin, type Group, getBarrelFiles, getMode } from '@kubb/core'\nimport { OperationGenerator, pluginOasName, SchemaGenerator } from '@kubb/plugin-oas'\nimport { pluginTsName } from '@kubb/plugin-ts'\nimport { fakerGenerator } from './generators/fakerGenerator.tsx'\nimport type { PluginFaker } from './types.ts'\n\nexport const pluginFakerName = 'plugin-faker' satisfies PluginFaker['name']\n\nexport const pluginFaker = createPlugin<PluginFaker>((options) => {\n const {\n output = { path: 'mocks', barrelType: 'named' },\n seed,\n group,\n exclude = [],\n include,\n override = [],\n transformers = {},\n mapper = {},\n unknownType = 'any',\n emptySchemaType = unknownType,\n dateType = 'string',\n integerType = 'number',\n dateParser = 'faker',\n generators = [fakerGenerator].filter(Boolean),\n regexGenerator = 'faker',\n paramsCasing,\n contentType,\n } = options\n\n // @deprecated Will be removed in v5 when collisionDetection defaults to true\n const usedEnumNames = {}\n\n return {\n name: pluginFakerName,\n options: {\n output,\n transformers,\n seed,\n dateType,\n integerType,\n unknownType,\n emptySchemaType,\n dateParser,\n mapper,\n override,\n regexGenerator,\n paramsCasing,\n group,\n usedEnumNames,\n },\n pre: [pluginOasName, pluginTsName],\n resolvePath(baseName, pathMode, options) {\n const root = path.resolve(this.config.root, this.config.output.path)\n const mode = pathMode ?? getMode(path.resolve(root, output.path))\n\n if (mode === 'single') {\n /**\n * when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend\n * Other plugins then need to call addOrAppend instead of just add from the fileManager class\n */\n return path.resolve(root, output.path)\n }\n\n if (group && (options?.group?.path || options?.group?.tag)) {\n const groupName: Group['name'] = group?.name\n ? group.name\n : (ctx) => {\n if (group?.type === 'path') {\n return `${ctx.group.split('/')[1]}`\n }\n return `${camelCase(ctx.group)}Controller`\n }\n\n return path.resolve(\n root,\n output.path,\n groupName({\n group: group.type === 'path' ? options.group.path! : options.group.tag!,\n }),\n baseName,\n )\n }\n\n return path.resolve(root, output.path, baseName)\n },\n resolveName(name, type) {\n const resolvedName = camelCase(name, {\n prefix: type ? 'create' : undefined,\n isFile: type === 'file',\n })\n\n if (type) {\n return transformers?.name?.(resolvedName, type) || resolvedName\n }\n\n return resolvedName\n },\n async install() {\n const root = path.resolve(this.config.root, this.config.output.path)\n const mode = getMode(path.resolve(root, output.path))\n const oas = await this.getOas()\n\n const schemaGenerator = new SchemaGenerator(this.plugin.options, {\n fabric: this.fabric,\n oas,\n driver: this.driver,\n events: this.events,\n plugin: this.plugin,\n contentType,\n include: undefined,\n override,\n mode,\n output: output.path,\n })\n\n const schemaFiles = await schemaGenerator.build(...generators)\n await this.upsertFile(...schemaFiles)\n\n const operationGenerator = new OperationGenerator(this.plugin.options, {\n fabric: this.fabric,\n oas,\n driver: this.driver,\n events: this.events,\n plugin: this.plugin,\n contentType,\n exclude,\n include,\n override,\n mode,\n })\n\n const operationFiles = await operationGenerator.build(...generators)\n await this.upsertFile(...operationFiles)\n\n const barrelFiles = await getBarrelFiles(this.fabric.files, {\n type: output.barrelType ?? 'named',\n root,\n output,\n meta: {\n pluginName: this.plugin.name,\n },\n })\n\n await this.upsertFile(...barrelFiles)\n },\n }\n})\n"],"mappings":";;;;;;;;;;;;;;;;AAgBA,SAAS,gBAAgB,MAAc,QAAyB;AAS9D,QARmB,KAChB,MAAM,CACN,QAAQ,qBAAqB,QAAQ,CACrC,QAAQ,yBAAyB,QAAQ,CACzC,QAAQ,gBAAgB,QAAQ,CAEV,MAAM,gBAAgB,CAAC,OAAO,QAAQ,CAG5D,KAAK,MAAM,MAAM;AAEhB,MADiB,KAAK,SAAS,KAAK,SAAS,KAAK,aAAa,CACjD,QAAO;AACrB,MAAI,MAAM,KAAK,CAAC,OAAQ,QAAO,KAAK,OAAO,EAAE,CAAC,aAAa,GAAG,KAAK,MAAM,EAAE;AAC3E,SAAO,KAAK,OAAO,EAAE,CAAC,aAAa,GAAG,KAAK,MAAM,EAAE;GACnD,CACD,KAAK,GAAG,CACR,QAAQ,iBAAiB,GAAG;;;;;;;AAQjC,SAAS,iBAAiB,MAAc,eAAkE;CACxG,MAAM,QAAQ,KAAK,MAAM,IAAI;AAC7B,QAAO,MAAM,KAAK,MAAM,MAAM,cAAc,MAAM,MAAM,MAAM,SAAS,EAAE,CAAC,CAAC,KAAK,IAAI;;;;;;;;;;AAWtF,SAAgB,UAAU,MAAc,EAAE,QAAQ,SAAS,IAAI,SAAS,OAAgB,EAAE,EAAU;AAClG,KAAI,OACF,QAAO,iBAAiB,OAAO,MAAM,WAAW,UAAU,MAAM,SAAS;EAAE;EAAQ;EAAQ,GAAG,EAAE,CAAC,CAAC;AAGpG,QAAO,gBA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,OAAO,MAAM,SAAS;;EAElD,YAAY,MAAM,MAAM;GACtB,MAAM,eAAe,UAAU,MAAM;IACnC,QAAQ,OAAO,WAAW,KAAA;IAC1B,QAAQ,SAAS;IAClB,CAAC;AAEF,OAAI,KACF,QAAO,cAAc,OAAO,cAAc,KAAK,IAAI;AAGrD,UAAO;;EAET,MAAM,UAAU;GACd,MAAM,OAAOA,UAAAA,QAAK,QAAQ,KAAK,OAAO,MAAM,KAAK,OAAO,OAAO,KAAK;GACpE,MAAM,QAAA,GAAA,WAAA,SAAeA,UAAAA,QAAK,QAAQ,MAAM,OAAO,KAAK,CAAC;GACrD,MAAM,MAAM,MAAM,KAAK,QAAQ;GAe/B,MAAM,cAAc,MAbI,IAAIC,iBAAAA,gBAAgB,KAAK,OAAO,SAAS;IAC/D,QAAQ,KAAK;IACb;IACA,QAAQ,KAAK;IACb,QAAQ,KAAK;IACb,QAAQ,KAAK;IACb;IACA,SAAS,KAAA;IACT;IACA;IACA,QAAQ,OAAO;IAChB,CAAC,CAEwC,MAAM,GAAG,WAAW;AAC9D,SAAM,KAAK,WAAW,GAAG,YAAY;GAerC,MAAM,iBAAiB,MAbI,IAAIC,iBAAAA,mBAAmB,KAAK,OAAO,SAAS;IACrE,QAAQ,KAAK;IACb;IACA,QAAQ,KAAK;IACb,QAAQ,KAAK;IACb,QAAQ,KAAK;IACb;IACA;IACA;IACA;IACA;IACD,CAAC,CAE8C,MAAM,GAAG,WAAW;AACpE,SAAM,KAAK,WAAW,GAAG,eAAe;GAExC,MAAM,cAAc,OAAA,GAAA,WAAA,gBAAqB,KAAK,OAAO,OAAO;IAC1D,MAAM,OAAO,cAAc;IAC3B;IACA;IACA,MAAM,EACJ,YAAY,KAAK,OAAO,MACzB;IACF,CAAC;AAEF,SAAM,KAAK,WAAW,GAAG,YAAY;;EAExC;EACD"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["PluginDriver","path","pluginTsName","fakerGenerator"],"sources":["../../../internals/utils/src/casing.ts","../src/resolvers/resolverFaker.ts","../src/plugin.ts"],"sourcesContent":["type Options = {\n /**\n * When `true`, dot-separated segments are split on `.` and joined with `/` after casing.\n */\n isFile?: boolean\n /**\n * Text prepended before casing is applied.\n */\n prefix?: string\n /**\n * Text appended before casing is applied.\n */\n suffix?: string\n}\n\n/**\n * Shared implementation for camelCase and PascalCase conversion.\n * Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)\n * and capitalizes each word according to `pascal`.\n *\n * When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.\n */\nfunction toCamelOrPascal(text: string, pascal: boolean): string {\n const normalized = text\n .trim()\n .replace(/([a-z\\d])([A-Z])/g, '$1 $2')\n .replace(/([A-Z]+)([A-Z][a-z])/g, '$1 $2')\n .replace(/(\\d)([a-z])/g, '$1 $2')\n\n const words = normalized.split(/[\\s\\-_./\\\\:]+/).filter(Boolean)\n\n return words\n .map((word, i) => {\n const allUpper = word.length > 1 && word === word.toUpperCase()\n if (allUpper) return word\n if (i === 0 && !pascal) return word.charAt(0).toLowerCase() + word.slice(1)\n return word.charAt(0).toUpperCase() + word.slice(1)\n })\n .join('')\n .replace(/[^a-zA-Z0-9]/g, '')\n}\n\n/**\n * Splits `text` on `.` and applies `transformPart` to each segment.\n * The last segment receives `isLast = true`, all earlier segments receive `false`.\n * Segments are joined with `/` to form a file path.\n *\n * Only splits on dots followed by a letter so that version numbers\n * embedded in operationIds (e.g. `v2025.0`) are kept intact.\n */\nfunction applyToFileParts(text: string, transformPart: (part: string, isLast: boolean) => string): string {\n const parts = text.split(/\\.(?=[a-zA-Z])/)\n return parts.map((part, i) => transformPart(part, i === parts.length - 1)).join('/')\n}\n\n/**\n * Converts `text` to camelCase.\n * When `isFile` is `true`, dot-separated segments are each cased independently and joined with `/`.\n *\n * @example\n * camelCase('hello-world') // 'helloWorld'\n * camelCase('pet.petId', { isFile: true }) // 'pet/petId'\n */\nexport function camelCase(text: string, { isFile, prefix = '', suffix = '' }: Options = {}): string {\n if (isFile) {\n return applyToFileParts(text, (part, isLast) => camelCase(part, isLast ? { prefix, suffix } : {}))\n }\n\n return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false)\n}\n\n/**\n * Converts `text` to PascalCase.\n * When `isFile` is `true`, the last dot-separated segment is PascalCased and earlier segments are camelCased.\n *\n * @example\n * pascalCase('hello-world') // 'HelloWorld'\n * pascalCase('pet.petId', { isFile: true }) // 'pet/PetId'\n */\nexport function pascalCase(text: string, { isFile, prefix = '', suffix = '' }: Options = {}): string {\n if (isFile) {\n return applyToFileParts(text, (part, isLast) => (isLast ? pascalCase(part, { prefix, suffix }) : camelCase(part)))\n }\n\n return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true)\n}\n\n/**\n * Converts `text` to snake_case.\n *\n * @example\n * snakeCase('helloWorld') // 'hello_world'\n * snakeCase('Hello-World') // 'hello_world'\n */\nexport function snakeCase(text: string, { prefix = '', suffix = '' }: Omit<Options, 'isFile'> = {}): string {\n const processed = `${prefix} ${text} ${suffix}`.trim()\n return processed\n .replace(/([a-z])([A-Z])/g, '$1_$2')\n .replace(/[\\s\\-.]+/g, '_')\n .replace(/[^a-zA-Z0-9_]/g, '')\n .toLowerCase()\n .split('_')\n .filter(Boolean)\n .join('_')\n}\n\n/**\n * Converts `text` to SCREAMING_SNAKE_CASE.\n *\n * @example\n * screamingSnakeCase('helloWorld') // 'HELLO_WORLD'\n */\nexport function screamingSnakeCase(text: string, { prefix = '', suffix = '' }: Omit<Options, 'isFile'> = {}): string {\n return snakeCase(text, { prefix, suffix }).toUpperCase()\n}\n","import { createHash } from 'node:crypto'\nimport path from 'node:path'\nimport { camelCase } from '@internals/utils'\nimport { defineResolver, PluginDriver } from '@kubb/core'\nimport type { PluginFaker } from '../types.ts'\n\nfunction isValidStrictIdentifier(name: string): boolean {\n try {\n new Function(`\"use strict\"; const ${name} = 1;`)\n } catch {\n return false\n }\n\n return true\n}\n\n/**\n * Naming convention resolver for Faker plugin.\n *\n * Provides default naming helpers using camelCase. Prefixes invalid identifiers with `_`.\n *\n * @example\n * `resolverFaker.default('list pets', 'function') // → 'listPets'`\n */\nexport const resolverFaker = defineResolver<PluginFaker>((ctx) => {\n return {\n name: 'default',\n pluginName: 'plugin-faker',\n default(name, type) {\n const resolvedName = camelCase(name, { isFile: type === 'file' })\n\n if (type === 'file' || isValidStrictIdentifier(resolvedName)) {\n return resolvedName\n }\n\n return `_${resolvedName}`\n },\n resolveName(name, type) {\n return ctx.default(name, type)\n },\n resolvePathName(name, type) {\n return ctx.default(name, type)\n },\n resolveFile({ name, extname, tag, path: groupPath }, context) {\n const pathMode = PluginDriver.getMode(path.resolve(context.root, context.output.path))\n const baseName = `${pathMode === 'single' ? '' : ctx.resolveName(name, 'file')}${extname}` as `${string}.${string}`\n const filePath = ctx.resolvePath(\n {\n baseName,\n pathMode,\n tag,\n path: groupPath,\n },\n context,\n )\n\n return {\n kind: 'File',\n id: createHash('sha256').update(filePath).digest('hex'),\n name: path.basename(filePath, extname),\n path: filePath,\n baseName,\n extname,\n meta: { pluginName: ctx.pluginName },\n sources: [],\n imports: [],\n exports: [],\n }\n },\n resolveParamName(node, param) {\n return ctx.resolveName(`${node.operationId} ${param.in} ${param.name}`)\n },\n resolveDataName(node) {\n return ctx.resolveName(`${node.operationId} Data`)\n },\n resolveResponseStatusName(node, statusCode) {\n return ctx.resolveName(`${node.operationId} Status ${statusCode}`)\n },\n resolveResponseName(node) {\n return ctx.resolveName(`${node.operationId} Response`)\n },\n resolvePathParamsName(node, param) {\n return ctx.resolveParamName(node, param)\n },\n resolveQueryParamsName(node, param) {\n return ctx.resolveParamName(node, param)\n },\n resolveHeaderParamsName(node, param) {\n return ctx.resolveParamName(node, param)\n },\n }\n})\n","import { camelCase } from '@internals/utils'\nimport { definePlugin, type Group } from '@kubb/core'\nimport { pluginTsName } from '@kubb/plugin-ts'\nimport { fakerGenerator } from './generators/fakerGenerator.tsx'\nimport { resolverFaker } from './resolvers/resolverFaker.ts'\nimport type { PluginFaker } from './types.ts'\n\n/**\n * Canonical plugin name for `@kubb/plugin-faker`, used in driver lookups and warnings.\n */\nexport const pluginFakerName = 'plugin-faker' satisfies PluginFaker['name']\n\n/**\n * Generates Faker mock data factories from OpenAPI/AST specification.\n *\n * Creates randomized test data and mock helpers from schema definitions.\n *\n * @example\n * `import pluginFaker from '@kubb/plugin-faker'; export default defineConfig({ plugins: [pluginFaker({ output: { path: 'mocks' } })], })`\n */\nexport const pluginFaker = definePlugin<PluginFaker>((options) => {\n const {\n output = { path: 'mocks', barrelType: 'named' },\n seed,\n locale,\n group,\n exclude = [],\n include,\n override = [],\n mapper = {},\n dateParser = 'faker',\n generators: userGenerators = [],\n regexGenerator = 'faker',\n paramsCasing,\n printer,\n resolver: userResolver,\n transformer: userTransformer,\n } = options\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\n return `${camelCase(ctx.group)}Controller`\n },\n } satisfies Group)\n : undefined\n\n return {\n name: pluginFakerName,\n options,\n dependencies: [pluginTsName],\n hooks: {\n 'kubb:plugin:setup'(ctx) {\n ctx.setOptions({\n output,\n seed,\n locale,\n exclude,\n include,\n override,\n group: groupConfig,\n mapper,\n dateParser,\n regexGenerator,\n paramsCasing,\n printer,\n })\n ctx.setResolver(userResolver ? { ...resolverFaker, ...userResolver } : resolverFaker)\n if (userTransformer) {\n ctx.setTransformer(userTransformer)\n }\n ctx.addGenerator(fakerGenerator)\n for (const generator of userGenerators) {\n ctx.addGenerator(generator)\n }\n },\n },\n }\n})\n\nexport default pluginFaker\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAsBA,SAAS,gBAAgB,MAAc,QAAyB;AAS9D,QARmB,KAChB,MAAM,CACN,QAAQ,qBAAqB,QAAQ,CACrC,QAAQ,yBAAyB,QAAQ,CACzC,QAAQ,gBAAgB,QAEH,CAAC,MAAM,gBAAgB,CAAC,OAAO,QAE3C,C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Y;EAAS,EAC/C,MACA,QACA,OACA,UAAU,EAAE,EACZ,SACA,WAAW,EAAE,EACb,SAAS,EAAE,EACX,aAAa,SACb,YAAY,iBAAiB,EAAE,EAC/B,iBAAiB,SACjB,cACA,SACA,UAAU,cACV,aAAa,oBACX;CAEJ,MAAM,cAAc,QACf;EACC,GAAG;EACH,MAAM,MAAM,OACR,MAAM,QACL,QAA2B;AAC1B,OAAI,MAAM,SAAS,OACjB,QAAO,GAAG,IAAI,MAAM,MAAM,IAAI,CAAC;AAGjC,UAAO,GAAG,UAAU,IAAI,MAAM,CAAC;;EAEtC,GACD,KAAA;AAEJ,QAAO;EACL,MAAM;EACN;EACA,cAAc,CAACC,gBAAAA,aAAa;EAC5B,OAAO,EACL,oBAAoB,KAAK;AACvB,OAAI,WAAW;IACb;IACA;IACA;IACA;IACA;IACA;IACA,OAAO;IACP;IACA;IACA;IACA;IACA;IACD,CAAC;AACF,OAAI,YAAY,eAAe;IAAE,GAAG;IAAe,GAAG;IAAc,GAAG,cAAc;AACrF,OAAI,gBACF,KAAI,eAAe,gBAAgB;AAErC,OAAI,aAAaC,uBAAAA,eAAe;AAChC,QAAK,MAAM,aAAa,eACtB,KAAI,aAAa,UAAU;KAGhC;EACF;EACD"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,34 @@
|
|
|
1
1
|
import { t as __name } from "./chunk--u3MIqq1.js";
|
|
2
|
-
import { n as PluginFaker, t as
|
|
3
|
-
import
|
|
2
|
+
import { a as Options, i as printerFaker, n as PrinterFakerNodes, o as PluginFaker, r as PrinterFakerOptions, s as ResolverFaker, t as PrinterFakerFactory } from "./printerFaker-CJiwzoto.js";
|
|
3
|
+
import { t as Faker } from "./Faker-CdyPfOPg.js";
|
|
4
|
+
import { t as fakerGenerator } from "./fakerGenerator-C3Ho3BaI.js";
|
|
5
|
+
import * as _$_kubb_core0 from "@kubb/core";
|
|
4
6
|
|
|
5
7
|
//#region src/plugin.d.ts
|
|
8
|
+
/**
|
|
9
|
+
* Canonical plugin name for `@kubb/plugin-faker`, used in driver lookups and warnings.
|
|
10
|
+
*/
|
|
6
11
|
declare const pluginFakerName = "plugin-faker";
|
|
7
|
-
|
|
12
|
+
/**
|
|
13
|
+
* Generates Faker mock data factories from OpenAPI/AST specification.
|
|
14
|
+
*
|
|
15
|
+
* Creates randomized test data and mock helpers from schema definitions.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* `import pluginFaker from '@kubb/plugin-faker'; export default defineConfig({ plugins: [pluginFaker({ output: { path: 'mocks' } })], })`
|
|
19
|
+
*/
|
|
20
|
+
declare const pluginFaker: (options?: Options | undefined) => _$_kubb_core0.Plugin<PluginFaker>;
|
|
8
21
|
//#endregion
|
|
9
|
-
|
|
22
|
+
//#region src/resolvers/resolverFaker.d.ts
|
|
23
|
+
/**
|
|
24
|
+
* Naming convention resolver for Faker plugin.
|
|
25
|
+
*
|
|
26
|
+
* Provides default naming helpers using camelCase. Prefixes invalid identifiers with `_`.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* `resolverFaker.default('list pets', 'function') // → 'listPets'`
|
|
30
|
+
*/
|
|
31
|
+
declare const resolverFaker: ResolverFaker;
|
|
32
|
+
//#endregion
|
|
33
|
+
export { Faker, type PluginFaker, type PrinterFakerFactory, type PrinterFakerNodes, type PrinterFakerOptions, type ResolverFaker, pluginFaker as default, pluginFaker, fakerGenerator, pluginFakerName, printerFaker, resolverFaker };
|
|
10
34
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import "./chunk--u3MIqq1.js";
|
|
2
|
-
import { t as
|
|
2
|
+
import { t as Faker } from "./Faker-fcQEB9i5.js";
|
|
3
|
+
import { n as printerFaker, t as fakerGenerator } from "./fakerGenerator-D7daHCh6.js";
|
|
3
4
|
import path from "node:path";
|
|
4
|
-
import {
|
|
5
|
-
import { OperationGenerator, SchemaGenerator, pluginOasName } from "@kubb/plugin-oas";
|
|
5
|
+
import { PluginDriver, definePlugin, defineResolver } from "@kubb/core";
|
|
6
6
|
import { pluginTsName } from "@kubb/plugin-ts";
|
|
7
|
+
import { createHash } from "node:crypto";
|
|
7
8
|
//#region ../../internals/utils/src/casing.ts
|
|
8
9
|
/**
|
|
9
10
|
* Shared implementation for camelCase and PascalCase conversion.
|
|
@@ -23,9 +24,12 @@ function toCamelOrPascal(text, pascal) {
|
|
|
23
24
|
* Splits `text` on `.` and applies `transformPart` to each segment.
|
|
24
25
|
* The last segment receives `isLast = true`, all earlier segments receive `false`.
|
|
25
26
|
* Segments are joined with `/` to form a file path.
|
|
27
|
+
*
|
|
28
|
+
* Only splits on dots followed by a letter so that version numbers
|
|
29
|
+
* embedded in operationIds (e.g. `v2025.0`) are kept intact.
|
|
26
30
|
*/
|
|
27
31
|
function applyToFileParts(text, transformPart) {
|
|
28
|
-
const parts = text.split(
|
|
32
|
+
const parts = text.split(/\.(?=[a-zA-Z])/);
|
|
29
33
|
return parts.map((part, i) => transformPart(part, i === parts.length - 1)).join("/");
|
|
30
34
|
}
|
|
31
35
|
/**
|
|
@@ -44,98 +48,139 @@ function camelCase(text, { isFile, prefix = "", suffix = "" } = {}) {
|
|
|
44
48
|
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
|
|
45
49
|
}
|
|
46
50
|
//#endregion
|
|
51
|
+
//#region src/resolvers/resolverFaker.ts
|
|
52
|
+
function isValidStrictIdentifier(name) {
|
|
53
|
+
try {
|
|
54
|
+
new Function(`"use strict"; const ${name} = 1;`);
|
|
55
|
+
} catch {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Naming convention resolver for Faker plugin.
|
|
62
|
+
*
|
|
63
|
+
* Provides default naming helpers using camelCase. Prefixes invalid identifiers with `_`.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* `resolverFaker.default('list pets', 'function') // → 'listPets'`
|
|
67
|
+
*/
|
|
68
|
+
const resolverFaker = defineResolver((ctx) => {
|
|
69
|
+
return {
|
|
70
|
+
name: "default",
|
|
71
|
+
pluginName: "plugin-faker",
|
|
72
|
+
default(name, type) {
|
|
73
|
+
const resolvedName = camelCase(name, { isFile: type === "file" });
|
|
74
|
+
if (type === "file" || isValidStrictIdentifier(resolvedName)) return resolvedName;
|
|
75
|
+
return `_${resolvedName}`;
|
|
76
|
+
},
|
|
77
|
+
resolveName(name, type) {
|
|
78
|
+
return ctx.default(name, type);
|
|
79
|
+
},
|
|
80
|
+
resolvePathName(name, type) {
|
|
81
|
+
return ctx.default(name, type);
|
|
82
|
+
},
|
|
83
|
+
resolveFile({ name, extname, tag, path: groupPath }, context) {
|
|
84
|
+
const pathMode = PluginDriver.getMode(path.resolve(context.root, context.output.path));
|
|
85
|
+
const baseName = `${pathMode === "single" ? "" : ctx.resolveName(name, "file")}${extname}`;
|
|
86
|
+
const filePath = ctx.resolvePath({
|
|
87
|
+
baseName,
|
|
88
|
+
pathMode,
|
|
89
|
+
tag,
|
|
90
|
+
path: groupPath
|
|
91
|
+
}, context);
|
|
92
|
+
return {
|
|
93
|
+
kind: "File",
|
|
94
|
+
id: createHash("sha256").update(filePath).digest("hex"),
|
|
95
|
+
name: path.basename(filePath, extname),
|
|
96
|
+
path: filePath,
|
|
97
|
+
baseName,
|
|
98
|
+
extname,
|
|
99
|
+
meta: { pluginName: ctx.pluginName },
|
|
100
|
+
sources: [],
|
|
101
|
+
imports: [],
|
|
102
|
+
exports: []
|
|
103
|
+
};
|
|
104
|
+
},
|
|
105
|
+
resolveParamName(node, param) {
|
|
106
|
+
return ctx.resolveName(`${node.operationId} ${param.in} ${param.name}`);
|
|
107
|
+
},
|
|
108
|
+
resolveDataName(node) {
|
|
109
|
+
return ctx.resolveName(`${node.operationId} Data`);
|
|
110
|
+
},
|
|
111
|
+
resolveResponseStatusName(node, statusCode) {
|
|
112
|
+
return ctx.resolveName(`${node.operationId} Status ${statusCode}`);
|
|
113
|
+
},
|
|
114
|
+
resolveResponseName(node) {
|
|
115
|
+
return ctx.resolveName(`${node.operationId} Response`);
|
|
116
|
+
},
|
|
117
|
+
resolvePathParamsName(node, param) {
|
|
118
|
+
return ctx.resolveParamName(node, param);
|
|
119
|
+
},
|
|
120
|
+
resolveQueryParamsName(node, param) {
|
|
121
|
+
return ctx.resolveParamName(node, param);
|
|
122
|
+
},
|
|
123
|
+
resolveHeaderParamsName(node, param) {
|
|
124
|
+
return ctx.resolveParamName(node, param);
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
});
|
|
128
|
+
//#endregion
|
|
47
129
|
//#region src/plugin.ts
|
|
130
|
+
/**
|
|
131
|
+
* Canonical plugin name for `@kubb/plugin-faker`, used in driver lookups and warnings.
|
|
132
|
+
*/
|
|
48
133
|
const pluginFakerName = "plugin-faker";
|
|
49
|
-
|
|
134
|
+
/**
|
|
135
|
+
* Generates Faker mock data factories from OpenAPI/AST specification.
|
|
136
|
+
*
|
|
137
|
+
* Creates randomized test data and mock helpers from schema definitions.
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* `import pluginFaker from '@kubb/plugin-faker'; export default defineConfig({ plugins: [pluginFaker({ output: { path: 'mocks' } })], })`
|
|
141
|
+
*/
|
|
142
|
+
const pluginFaker = definePlugin((options) => {
|
|
50
143
|
const { output = {
|
|
51
144
|
path: "mocks",
|
|
52
145
|
barrelType: "named"
|
|
53
|
-
}, seed, group, exclude = [], include, override = [],
|
|
146
|
+
}, seed, locale, group, exclude = [], include, override = [], mapper = {}, dateParser = "faker", generators: userGenerators = [], regexGenerator = "faker", paramsCasing, printer, resolver: userResolver, transformer: userTransformer } = options;
|
|
147
|
+
const groupConfig = group ? {
|
|
148
|
+
...group,
|
|
149
|
+
name: group.name ? group.name : (ctx) => {
|
|
150
|
+
if (group.type === "path") return `${ctx.group.split("/")[1]}`;
|
|
151
|
+
return `${camelCase(ctx.group)}Controller`;
|
|
152
|
+
}
|
|
153
|
+
} : void 0;
|
|
54
154
|
return {
|
|
55
155
|
name: pluginFakerName,
|
|
56
|
-
options
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
emptySchemaType,
|
|
64
|
-
dateParser,
|
|
65
|
-
mapper,
|
|
66
|
-
override,
|
|
67
|
-
regexGenerator,
|
|
68
|
-
paramsCasing,
|
|
69
|
-
group,
|
|
70
|
-
usedEnumNames: {}
|
|
71
|
-
},
|
|
72
|
-
pre: [pluginOasName, pluginTsName],
|
|
73
|
-
resolvePath(baseName, pathMode, options) {
|
|
74
|
-
const root = path.resolve(this.config.root, this.config.output.path);
|
|
75
|
-
if ((pathMode ?? getMode(path.resolve(root, output.path))) === "single")
|
|
76
|
-
/**
|
|
77
|
-
* when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
|
|
78
|
-
* Other plugins then need to call addOrAppend instead of just add from the fileManager class
|
|
79
|
-
*/
|
|
80
|
-
return path.resolve(root, output.path);
|
|
81
|
-
if (group && (options?.group?.path || options?.group?.tag)) {
|
|
82
|
-
const groupName = group?.name ? group.name : (ctx) => {
|
|
83
|
-
if (group?.type === "path") return `${ctx.group.split("/")[1]}`;
|
|
84
|
-
return `${camelCase(ctx.group)}Controller`;
|
|
85
|
-
};
|
|
86
|
-
return path.resolve(root, output.path, groupName({ group: group.type === "path" ? options.group.path : options.group.tag }), baseName);
|
|
87
|
-
}
|
|
88
|
-
return path.resolve(root, output.path, baseName);
|
|
89
|
-
},
|
|
90
|
-
resolveName(name, type) {
|
|
91
|
-
const resolvedName = camelCase(name, {
|
|
92
|
-
prefix: type ? "create" : void 0,
|
|
93
|
-
isFile: type === "file"
|
|
94
|
-
});
|
|
95
|
-
if (type) return transformers?.name?.(resolvedName, type) || resolvedName;
|
|
96
|
-
return resolvedName;
|
|
97
|
-
},
|
|
98
|
-
async install() {
|
|
99
|
-
const root = path.resolve(this.config.root, this.config.output.path);
|
|
100
|
-
const mode = getMode(path.resolve(root, output.path));
|
|
101
|
-
const oas = await this.getOas();
|
|
102
|
-
const schemaFiles = await new SchemaGenerator(this.plugin.options, {
|
|
103
|
-
fabric: this.fabric,
|
|
104
|
-
oas,
|
|
105
|
-
driver: this.driver,
|
|
106
|
-
events: this.events,
|
|
107
|
-
plugin: this.plugin,
|
|
108
|
-
contentType,
|
|
109
|
-
include: void 0,
|
|
110
|
-
override,
|
|
111
|
-
mode,
|
|
112
|
-
output: output.path
|
|
113
|
-
}).build(...generators);
|
|
114
|
-
await this.upsertFile(...schemaFiles);
|
|
115
|
-
const operationFiles = await new OperationGenerator(this.plugin.options, {
|
|
116
|
-
fabric: this.fabric,
|
|
117
|
-
oas,
|
|
118
|
-
driver: this.driver,
|
|
119
|
-
events: this.events,
|
|
120
|
-
plugin: this.plugin,
|
|
121
|
-
contentType,
|
|
156
|
+
options,
|
|
157
|
+
dependencies: [pluginTsName],
|
|
158
|
+
hooks: { "kubb:plugin:setup"(ctx) {
|
|
159
|
+
ctx.setOptions({
|
|
160
|
+
output,
|
|
161
|
+
seed,
|
|
162
|
+
locale,
|
|
122
163
|
exclude,
|
|
123
164
|
include,
|
|
124
165
|
override,
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
output,
|
|
132
|
-
meta: { pluginName: this.plugin.name }
|
|
166
|
+
group: groupConfig,
|
|
167
|
+
mapper,
|
|
168
|
+
dateParser,
|
|
169
|
+
regexGenerator,
|
|
170
|
+
paramsCasing,
|
|
171
|
+
printer
|
|
133
172
|
});
|
|
134
|
-
|
|
135
|
-
|
|
173
|
+
ctx.setResolver(userResolver ? {
|
|
174
|
+
...resolverFaker,
|
|
175
|
+
...userResolver
|
|
176
|
+
} : resolverFaker);
|
|
177
|
+
if (userTransformer) ctx.setTransformer(userTransformer);
|
|
178
|
+
ctx.addGenerator(fakerGenerator);
|
|
179
|
+
for (const generator of userGenerators) ctx.addGenerator(generator);
|
|
180
|
+
} }
|
|
136
181
|
};
|
|
137
182
|
});
|
|
138
183
|
//#endregion
|
|
139
|
-
export { pluginFaker, pluginFakerName };
|
|
184
|
+
export { Faker, pluginFaker as default, pluginFaker, fakerGenerator, pluginFakerName, printerFaker, resolverFaker };
|
|
140
185
|
|
|
141
186
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../../internals/utils/src/casing.ts","../src/plugin.ts"],"sourcesContent":["type Options = {\n /** When `true`, dot-separated segments are split on `.` and joined with `/` after casing. */\n isFile?: boolean\n /** Text prepended before casing is applied. */\n prefix?: string\n /** Text appended before casing is applied. */\n suffix?: string\n}\n\n/**\n * Shared implementation for camelCase and PascalCase conversion.\n * Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)\n * and capitalizes each word according to `pascal`.\n *\n * When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.\n */\nfunction toCamelOrPascal(text: string, pascal: boolean): string {\n const normalized = text\n .trim()\n .replace(/([a-z\\d])([A-Z])/g, '$1 $2')\n .replace(/([A-Z]+)([A-Z][a-z])/g, '$1 $2')\n .replace(/(\\d)([a-z])/g, '$1 $2')\n\n const words = normalized.split(/[\\s\\-_./\\\\:]+/).filter(Boolean)\n\n return words\n .map((word, i) => {\n const allUpper = word.length > 1 && word === word.toUpperCase()\n if (allUpper) return word\n if (i === 0 && !pascal) return word.charAt(0).toLowerCase() + word.slice(1)\n return word.charAt(0).toUpperCase() + word.slice(1)\n })\n .join('')\n .replace(/[^a-zA-Z0-9]/g, '')\n}\n\n/**\n * Splits `text` on `.` and applies `transformPart` to each segment.\n * The last segment receives `isLast = true`, all earlier segments receive `false`.\n * Segments are joined with `/` to form a file path.\n */\nfunction applyToFileParts(text: string, transformPart: (part: string, isLast: boolean) => string): string {\n const parts = text.split('.')\n return parts.map((part, i) => transformPart(part, i === parts.length - 1)).join('/')\n}\n\n/**\n * Converts `text` to camelCase.\n * When `isFile` is `true`, dot-separated segments are each cased independently and joined with `/`.\n *\n * @example\n * camelCase('hello-world') // 'helloWorld'\n * camelCase('pet.petId', { isFile: true }) // 'pet/petId'\n */\nexport function camelCase(text: string, { isFile, prefix = '', suffix = '' }: Options = {}): string {\n if (isFile) {\n return applyToFileParts(text, (part, isLast) => camelCase(part, isLast ? { prefix, suffix } : {}))\n }\n\n return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false)\n}\n\n/**\n * Converts `text` to PascalCase.\n * When `isFile` is `true`, the last dot-separated segment is PascalCased and earlier segments are camelCased.\n *\n * @example\n * pascalCase('hello-world') // 'HelloWorld'\n * pascalCase('pet.petId', { isFile: true }) // 'pet/PetId'\n */\nexport function pascalCase(text: string, { isFile, prefix = '', suffix = '' }: Options = {}): string {\n if (isFile) {\n return applyToFileParts(text, (part, isLast) => (isLast ? pascalCase(part, { prefix, suffix }) : camelCase(part)))\n }\n\n return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true)\n}\n\n/**\n * Converts `text` to snake_case.\n *\n * @example\n * snakeCase('helloWorld') // 'hello_world'\n * snakeCase('Hello-World') // 'hello_world'\n */\nexport function snakeCase(text: string, { prefix = '', suffix = '' }: Omit<Options, 'isFile'> = {}): string {\n const processed = `${prefix} ${text} ${suffix}`.trim()\n return processed\n .replace(/([a-z])([A-Z])/g, '$1_$2')\n .replace(/[\\s\\-.]+/g, '_')\n .replace(/[^a-zA-Z0-9_]/g, '')\n .toLowerCase()\n .split('_')\n .filter(Boolean)\n .join('_')\n}\n\n/**\n * Converts `text` to SCREAMING_SNAKE_CASE.\n *\n * @example\n * screamingSnakeCase('helloWorld') // 'HELLO_WORLD'\n */\nexport function screamingSnakeCase(text: string, { prefix = '', suffix = '' }: Omit<Options, 'isFile'> = {}): string {\n return snakeCase(text, { prefix, suffix }).toUpperCase()\n}\n","import path from 'node:path'\nimport { camelCase } from '@internals/utils'\nimport { createPlugin, type Group, getBarrelFiles, getMode } from '@kubb/core'\nimport { OperationGenerator, pluginOasName, SchemaGenerator } from '@kubb/plugin-oas'\nimport { pluginTsName } from '@kubb/plugin-ts'\nimport { fakerGenerator } from './generators/fakerGenerator.tsx'\nimport type { PluginFaker } from './types.ts'\n\nexport const pluginFakerName = 'plugin-faker' satisfies PluginFaker['name']\n\nexport const pluginFaker = createPlugin<PluginFaker>((options) => {\n const {\n output = { path: 'mocks', barrelType: 'named' },\n seed,\n group,\n exclude = [],\n include,\n override = [],\n transformers = {},\n mapper = {},\n unknownType = 'any',\n emptySchemaType = unknownType,\n dateType = 'string',\n integerType = 'number',\n dateParser = 'faker',\n generators = [fakerGenerator].filter(Boolean),\n regexGenerator = 'faker',\n paramsCasing,\n contentType,\n } = options\n\n // @deprecated Will be removed in v5 when collisionDetection defaults to true\n const usedEnumNames = {}\n\n return {\n name: pluginFakerName,\n options: {\n output,\n transformers,\n seed,\n dateType,\n integerType,\n unknownType,\n emptySchemaType,\n dateParser,\n mapper,\n override,\n regexGenerator,\n paramsCasing,\n group,\n usedEnumNames,\n },\n pre: [pluginOasName, pluginTsName],\n resolvePath(baseName, pathMode, options) {\n const root = path.resolve(this.config.root, this.config.output.path)\n const mode = pathMode ?? getMode(path.resolve(root, output.path))\n\n if (mode === 'single') {\n /**\n * when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend\n * Other plugins then need to call addOrAppend instead of just add from the fileManager class\n */\n return path.resolve(root, output.path)\n }\n\n if (group && (options?.group?.path || options?.group?.tag)) {\n const groupName: Group['name'] = group?.name\n ? group.name\n : (ctx) => {\n if (group?.type === 'path') {\n return `${ctx.group.split('/')[1]}`\n }\n return `${camelCase(ctx.group)}Controller`\n }\n\n return path.resolve(\n root,\n output.path,\n groupName({\n group: group.type === 'path' ? options.group.path! : options.group.tag!,\n }),\n baseName,\n )\n }\n\n return path.resolve(root, output.path, baseName)\n },\n resolveName(name, type) {\n const resolvedName = camelCase(name, {\n prefix: type ? 'create' : undefined,\n isFile: type === 'file',\n })\n\n if (type) {\n return transformers?.name?.(resolvedName, type) || resolvedName\n }\n\n return resolvedName\n },\n async install() {\n const root = path.resolve(this.config.root, this.config.output.path)\n const mode = getMode(path.resolve(root, output.path))\n const oas = await this.getOas()\n\n const schemaGenerator = new SchemaGenerator(this.plugin.options, {\n fabric: this.fabric,\n oas,\n driver: this.driver,\n events: this.events,\n plugin: this.plugin,\n contentType,\n include: undefined,\n override,\n mode,\n output: output.path,\n })\n\n const schemaFiles = await schemaGenerator.build(...generators)\n await this.upsertFile(...schemaFiles)\n\n const operationGenerator = new OperationGenerator(this.plugin.options, {\n fabric: this.fabric,\n oas,\n driver: this.driver,\n events: this.events,\n plugin: this.plugin,\n contentType,\n exclude,\n include,\n override,\n mode,\n })\n\n const operationFiles = await operationGenerator.build(...generators)\n await this.upsertFile(...operationFiles)\n\n const barrelFiles = await getBarrelFiles(this.fabric.files, {\n type: output.barrelType ?? 'named',\n root,\n output,\n meta: {\n pluginName: this.plugin.name,\n },\n })\n\n await this.upsertFile(...barrelFiles)\n },\n }\n})\n"],"mappings":";;;;;;;;;;;;;;AAgBA,SAAS,gBAAgB,MAAc,QAAyB;AAS9D,QARmB,KAChB,MAAM,CACN,QAAQ,qBAAqB,QAAQ,CACrC,QAAQ,yBAAyB,QAAQ,CACzC,QAAQ,gBAAgB,QAAQ,CAEV,MAAM,gBAAgB,CAAC,OAAO,QAAQ,CAG5D,KAAK,MAAM,MAAM;AAEhB,MADiB,KAAK,SAAS,KAAK,SAAS,KAAK,aAAa,CACjD,QAAO;AACrB,MAAI,MAAM,KAAK,CAAC,OAAQ,QAAO,KAAK,OAAO,EAAE,CAAC,aAAa,GAAG,KAAK,MAAM,EAAE;AAC3E,SAAO,KAAK,OAAO,EAAE,CAAC,aAAa,GAAG,KAAK,MAAM,EAAE;GACnD,CACD,KAAK,GAAG,CACR,QAAQ,iBAAiB,GAAG;;;;;;;AAQjC,SAAS,iBAAiB,MAAc,eAAkE;CACxG,MAAM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,EACJ,YAAY,KAAK,OAAO,MACzB;IACF,CAAC;AAEF,SAAM,KAAK,WAAW,GAAG,YAAY;;EAExC;EACD"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../../internals/utils/src/casing.ts","../src/resolvers/resolverFaker.ts","../src/plugin.ts"],"sourcesContent":["type Options = {\n /**\n * When `true`, dot-separated segments are split on `.` and joined with `/` after casing.\n */\n isFile?: boolean\n /**\n * Text prepended before casing is applied.\n */\n prefix?: string\n /**\n * Text appended before casing is applied.\n */\n suffix?: string\n}\n\n/**\n * Shared implementation for camelCase and PascalCase conversion.\n * Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)\n * and capitalizes each word according to `pascal`.\n *\n * When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.\n */\nfunction toCamelOrPascal(text: string, pascal: boolean): string {\n const normalized = text\n .trim()\n .replace(/([a-z\\d])([A-Z])/g, '$1 $2')\n .replace(/([A-Z]+)([A-Z][a-z])/g, '$1 $2')\n .replace(/(\\d)([a-z])/g, '$1 $2')\n\n const words = normalized.split(/[\\s\\-_./\\\\:]+/).filter(Boolean)\n\n return words\n .map((word, i) => {\n const allUpper = word.length > 1 && word === word.toUpperCase()\n if (allUpper) return word\n if (i === 0 && !pascal) return word.charAt(0).toLowerCase() + word.slice(1)\n return word.charAt(0).toUpperCase() + word.slice(1)\n })\n .join('')\n .replace(/[^a-zA-Z0-9]/g, '')\n}\n\n/**\n * Splits `text` on `.` and applies `transformPart` to each segment.\n * The last segment receives `isLast = true`, all earlier segments receive `false`.\n * Segments are joined with `/` to form a file path.\n *\n * Only splits on dots followed by a letter so that version numbers\n * embedded in operationIds (e.g. `v2025.0`) are kept intact.\n */\nfunction applyToFileParts(text: string, transformPart: (part: string, isLast: boolean) => string): string {\n const parts = text.split(/\\.(?=[a-zA-Z])/)\n return parts.map((part, i) => transformPart(part, i === parts.length - 1)).join('/')\n}\n\n/**\n * Converts `text` to camelCase.\n * When `isFile` is `true`, dot-separated segments are each cased independently and joined with `/`.\n *\n * @example\n * camelCase('hello-world') // 'helloWorld'\n * camelCase('pet.petId', { isFile: true }) // 'pet/petId'\n */\nexport function camelCase(text: string, { isFile, prefix = '', suffix = '' }: Options = {}): string {\n if (isFile) {\n return applyToFileParts(text, (part, isLast) => camelCase(part, isLast ? { prefix, suffix } : {}))\n }\n\n return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false)\n}\n\n/**\n * Converts `text` to PascalCase.\n * When `isFile` is `true`, the last dot-separated segment is PascalCased and earlier segments are camelCased.\n *\n * @example\n * pascalCase('hello-world') // 'HelloWorld'\n * pascalCase('pet.petId', { isFile: true }) // 'pet/PetId'\n */\nexport function pascalCase(text: string, { isFile, prefix = '', suffix = '' }: Options = {}): string {\n if (isFile) {\n return applyToFileParts(text, (part, isLast) => (isLast ? pascalCase(part, { prefix, suffix }) : camelCase(part)))\n }\n\n return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true)\n}\n\n/**\n * Converts `text` to snake_case.\n *\n * @example\n * snakeCase('helloWorld') // 'hello_world'\n * snakeCase('Hello-World') // 'hello_world'\n */\nexport function snakeCase(text: string, { prefix = '', suffix = '' }: Omit<Options, 'isFile'> = {}): string {\n const processed = `${prefix} ${text} ${suffix}`.trim()\n return processed\n .replace(/([a-z])([A-Z])/g, '$1_$2')\n .replace(/[\\s\\-.]+/g, '_')\n .replace(/[^a-zA-Z0-9_]/g, '')\n .toLowerCase()\n .split('_')\n .filter(Boolean)\n .join('_')\n}\n\n/**\n * Converts `text` to SCREAMING_SNAKE_CASE.\n *\n * @example\n * screamingSnakeCase('helloWorld') // 'HELLO_WORLD'\n */\nexport function screamingSnakeCase(text: string, { prefix = '', suffix = '' }: Omit<Options, 'isFile'> = {}): string {\n return snakeCase(text, { prefix, suffix }).toUpperCase()\n}\n","import { createHash } from 'node:crypto'\nimport path from 'node:path'\nimport { camelCase } from '@internals/utils'\nimport { defineResolver, PluginDriver } from '@kubb/core'\nimport type { PluginFaker } from '../types.ts'\n\nfunction isValidStrictIdentifier(name: string): boolean {\n try {\n new Function(`\"use strict\"; const ${name} = 1;`)\n } catch {\n return false\n }\n\n return true\n}\n\n/**\n * Naming convention resolver for Faker plugin.\n *\n * Provides default naming helpers using camelCase. Prefixes invalid identifiers with `_`.\n *\n * @example\n * `resolverFaker.default('list pets', 'function') // → 'listPets'`\n */\nexport const resolverFaker = defineResolver<PluginFaker>((ctx) => {\n return {\n name: 'default',\n pluginName: 'plugin-faker',\n default(name, type) {\n const resolvedName = camelCase(name, { isFile: type === 'file' })\n\n if (type === 'file' || isValidStrictIdentifier(resolvedName)) {\n return resolvedName\n }\n\n return `_${resolvedName}`\n },\n resolveName(name, type) {\n return ctx.default(name, type)\n },\n resolvePathName(name, type) {\n return ctx.default(name, type)\n },\n resolveFile({ name, extname, tag, path: groupPath }, context) {\n const pathMode = PluginDriver.getMode(path.resolve(context.root, context.output.path))\n const baseName = `${pathMode === 'single' ? '' : ctx.resolveName(name, 'file')}${extname}` as `${string}.${string}`\n const filePath = ctx.resolvePath(\n {\n baseName,\n pathMode,\n tag,\n path: groupPath,\n },\n context,\n )\n\n return {\n kind: 'File',\n id: createHash('sha256').update(filePath).digest('hex'),\n name: path.basename(filePath, extname),\n path: filePath,\n baseName,\n extname,\n meta: { pluginName: ctx.pluginName },\n sources: [],\n imports: [],\n exports: [],\n }\n },\n resolveParamName(node, param) {\n return ctx.resolveName(`${node.operationId} ${param.in} ${param.name}`)\n },\n resolveDataName(node) {\n return ctx.resolveName(`${node.operationId} Data`)\n },\n resolveResponseStatusName(node, statusCode) {\n return ctx.resolveName(`${node.operationId} Status ${statusCode}`)\n },\n resolveResponseName(node) {\n return ctx.resolveName(`${node.operationId} Response`)\n },\n resolvePathParamsName(node, param) {\n return ctx.resolveParamName(node, param)\n },\n resolveQueryParamsName(node, param) {\n return ctx.resolveParamName(node, param)\n },\n resolveHeaderParamsName(node, param) {\n return ctx.resolveParamName(node, param)\n },\n }\n})\n","import { camelCase } from '@internals/utils'\nimport { definePlugin, type Group } from '@kubb/core'\nimport { pluginTsName } from '@kubb/plugin-ts'\nimport { fakerGenerator } from './generators/fakerGenerator.tsx'\nimport { resolverFaker } from './resolvers/resolverFaker.ts'\nimport type { PluginFaker } from './types.ts'\n\n/**\n * Canonical plugin name for `@kubb/plugin-faker`, used in driver lookups and warnings.\n */\nexport const pluginFakerName = 'plugin-faker' satisfies PluginFaker['name']\n\n/**\n * Generates Faker mock data factories from OpenAPI/AST specification.\n *\n * Creates randomized test data and mock helpers from schema definitions.\n *\n * @example\n * `import pluginFaker from '@kubb/plugin-faker'; export default defineConfig({ plugins: [pluginFaker({ output: { path: 'mocks' } })], })`\n */\nexport const pluginFaker = definePlugin<PluginFaker>((options) => {\n const {\n output = { path: 'mocks', barrelType: 'named' },\n seed,\n locale,\n group,\n exclude = [],\n include,\n override = [],\n mapper = {},\n dateParser = 'faker',\n generators: userGenerators = [],\n regexGenerator = 'faker',\n paramsCasing,\n printer,\n resolver: userResolver,\n transformer: userTransformer,\n } = options\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\n return `${camelCase(ctx.group)}Controller`\n },\n } satisfies Group)\n : undefined\n\n return {\n name: pluginFakerName,\n options,\n dependencies: [pluginTsName],\n hooks: {\n 'kubb:plugin:setup'(ctx) {\n ctx.setOptions({\n output,\n seed,\n locale,\n exclude,\n include,\n override,\n group: groupConfig,\n mapper,\n dateParser,\n regexGenerator,\n paramsCasing,\n printer,\n })\n ctx.setResolver(userResolver ? { ...resolverFaker, ...userResolver } : resolverFaker)\n if (userTransformer) {\n ctx.setTransformer(userTransformer)\n }\n ctx.addGenerator(fakerGenerator)\n for (const generator of userGenerators) {\n ctx.addGenerator(generator)\n }\n },\n },\n }\n})\n\nexport default pluginFaker\n"],"mappings":";;;;;;;;;;;;;;;AAsBA,SAAS,gBAAgB,MAAc,QAAyB;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