@kubb/plugin-cypress 5.0.0-alpha.9 → 5.0.0-beta.15
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 +25 -7
- package/dist/index.cjs +561 -61
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +183 -4
- package/dist/index.js +555 -62
- package/dist/index.js.map +1 -1
- package/extension.yaml +444 -0
- package/package.json +41 -67
- package/src/components/Request.tsx +72 -107
- package/src/generators/cypressGenerator.tsx +38 -44
- package/src/index.ts +9 -2
- package/src/plugin.ts +68 -92
- package/src/resolvers/resolverCypress.ts +25 -0
- package/src/types.ts +79 -43
- package/dist/components-BK_6GU4v.js +0 -257
- package/dist/components-BK_6GU4v.js.map +0 -1
- package/dist/components-Drg_gLu2.cjs +0 -305
- package/dist/components-Drg_gLu2.cjs.map +0 -1
- package/dist/components.cjs +0 -3
- package/dist/components.d.ts +0 -50
- package/dist/components.js +0 -2
- package/dist/generators-B3FWG2Ck.js +0 -71
- package/dist/generators-B3FWG2Ck.js.map +0 -1
- package/dist/generators-C73nd-xB.cjs +0 -75
- package/dist/generators-C73nd-xB.cjs.map +0 -1
- package/dist/generators.cjs +0 -3
- package/dist/generators.d.ts +0 -505
- package/dist/generators.js +0 -2
- package/dist/types-DGvL0jsn.d.ts +0 -85
- package/src/components/index.ts +0 -1
- package/src/generators/index.ts +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,81 +1,581 @@
|
|
|
1
|
-
Object.
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
Object.defineProperties(exports, {
|
|
2
|
+
__esModule: { value: true },
|
|
3
|
+
[Symbol.toStringTag]: { value: "Module" }
|
|
4
|
+
});
|
|
5
|
+
//#endregion
|
|
6
6
|
let _kubb_core = require("@kubb/core");
|
|
7
|
-
let _kubb_plugin_oas = require("@kubb/plugin-oas");
|
|
8
7
|
let _kubb_plugin_ts = require("@kubb/plugin-ts");
|
|
8
|
+
let _kubb_renderer_jsx = require("@kubb/renderer-jsx");
|
|
9
|
+
let _kubb_renderer_jsx_jsx_runtime = require("@kubb/renderer-jsx/jsx-runtime");
|
|
10
|
+
//#region ../../internals/utils/src/casing.ts
|
|
11
|
+
/**
|
|
12
|
+
* Shared implementation for camelCase and PascalCase conversion.
|
|
13
|
+
* Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
|
|
14
|
+
* and capitalizes each word according to `pascal`.
|
|
15
|
+
*
|
|
16
|
+
* When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
|
|
17
|
+
*/
|
|
18
|
+
function toCamelOrPascal(text, pascal) {
|
|
19
|
+
return text.trim().replace(/([a-z\d])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").replace(/(\d)([a-z])/g, "$1 $2").split(/[\s\-_./\\:]+/).filter(Boolean).map((word, i) => {
|
|
20
|
+
if (word.length > 1 && word === word.toUpperCase()) return word;
|
|
21
|
+
if (i === 0 && !pascal) return word.charAt(0).toLowerCase() + word.slice(1);
|
|
22
|
+
return word.charAt(0).toUpperCase() + word.slice(1);
|
|
23
|
+
}).join("").replace(/[^a-zA-Z0-9]/g, "");
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Splits `text` on `.` and applies `transformPart` to each segment.
|
|
27
|
+
* The last segment receives `isLast = true`, all earlier segments receive `false`.
|
|
28
|
+
* Segments are joined with `/` to form a file path.
|
|
29
|
+
*
|
|
30
|
+
* Only splits on dots followed by a letter so that version numbers
|
|
31
|
+
* embedded in operationIds (e.g. `v2025.0`) are kept intact.
|
|
32
|
+
*/
|
|
33
|
+
function applyToFileParts(text, transformPart) {
|
|
34
|
+
const parts = text.split(/\.(?=[a-zA-Z])/);
|
|
35
|
+
return parts.map((part, i) => transformPart(part, i === parts.length - 1)).join("/");
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Converts `text` to camelCase.
|
|
39
|
+
* When `isFile` is `true`, dot-separated segments are each cased independently and joined with `/`.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* camelCase('hello-world') // 'helloWorld'
|
|
43
|
+
* camelCase('pet.petId', { isFile: true }) // 'pet/petId'
|
|
44
|
+
*/
|
|
45
|
+
function camelCase(text, { isFile, prefix = "", suffix = "" } = {}) {
|
|
46
|
+
if (isFile) return applyToFileParts(text, (part, isLast) => camelCase(part, isLast ? {
|
|
47
|
+
prefix,
|
|
48
|
+
suffix
|
|
49
|
+
} : {}));
|
|
50
|
+
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
|
|
51
|
+
}
|
|
52
|
+
//#endregion
|
|
53
|
+
//#region ../../internals/utils/src/reserved.ts
|
|
54
|
+
/**
|
|
55
|
+
* JavaScript and Java reserved words.
|
|
56
|
+
* @link https://github.com/jonschlinkert/reserved/blob/master/index.js
|
|
57
|
+
*/
|
|
58
|
+
const reservedWords = new Set([
|
|
59
|
+
"abstract",
|
|
60
|
+
"arguments",
|
|
61
|
+
"boolean",
|
|
62
|
+
"break",
|
|
63
|
+
"byte",
|
|
64
|
+
"case",
|
|
65
|
+
"catch",
|
|
66
|
+
"char",
|
|
67
|
+
"class",
|
|
68
|
+
"const",
|
|
69
|
+
"continue",
|
|
70
|
+
"debugger",
|
|
71
|
+
"default",
|
|
72
|
+
"delete",
|
|
73
|
+
"do",
|
|
74
|
+
"double",
|
|
75
|
+
"else",
|
|
76
|
+
"enum",
|
|
77
|
+
"eval",
|
|
78
|
+
"export",
|
|
79
|
+
"extends",
|
|
80
|
+
"false",
|
|
81
|
+
"final",
|
|
82
|
+
"finally",
|
|
83
|
+
"float",
|
|
84
|
+
"for",
|
|
85
|
+
"function",
|
|
86
|
+
"goto",
|
|
87
|
+
"if",
|
|
88
|
+
"implements",
|
|
89
|
+
"import",
|
|
90
|
+
"in",
|
|
91
|
+
"instanceof",
|
|
92
|
+
"int",
|
|
93
|
+
"interface",
|
|
94
|
+
"let",
|
|
95
|
+
"long",
|
|
96
|
+
"native",
|
|
97
|
+
"new",
|
|
98
|
+
"null",
|
|
99
|
+
"package",
|
|
100
|
+
"private",
|
|
101
|
+
"protected",
|
|
102
|
+
"public",
|
|
103
|
+
"return",
|
|
104
|
+
"short",
|
|
105
|
+
"static",
|
|
106
|
+
"super",
|
|
107
|
+
"switch",
|
|
108
|
+
"synchronized",
|
|
109
|
+
"this",
|
|
110
|
+
"throw",
|
|
111
|
+
"throws",
|
|
112
|
+
"transient",
|
|
113
|
+
"true",
|
|
114
|
+
"try",
|
|
115
|
+
"typeof",
|
|
116
|
+
"var",
|
|
117
|
+
"void",
|
|
118
|
+
"volatile",
|
|
119
|
+
"while",
|
|
120
|
+
"with",
|
|
121
|
+
"yield",
|
|
122
|
+
"Array",
|
|
123
|
+
"Date",
|
|
124
|
+
"hasOwnProperty",
|
|
125
|
+
"Infinity",
|
|
126
|
+
"isFinite",
|
|
127
|
+
"isNaN",
|
|
128
|
+
"isPrototypeOf",
|
|
129
|
+
"length",
|
|
130
|
+
"Math",
|
|
131
|
+
"name",
|
|
132
|
+
"NaN",
|
|
133
|
+
"Number",
|
|
134
|
+
"Object",
|
|
135
|
+
"prototype",
|
|
136
|
+
"String",
|
|
137
|
+
"toString",
|
|
138
|
+
"undefined",
|
|
139
|
+
"valueOf"
|
|
140
|
+
]);
|
|
141
|
+
/**
|
|
142
|
+
* Returns `true` when `name` is a syntactically valid JavaScript variable name.
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```ts
|
|
146
|
+
* isValidVarName('status') // true
|
|
147
|
+
* isValidVarName('class') // false (reserved word)
|
|
148
|
+
* isValidVarName('42foo') // false (starts with digit)
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
function isValidVarName(name) {
|
|
152
|
+
if (!name || reservedWords.has(name)) return false;
|
|
153
|
+
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
|
|
154
|
+
}
|
|
155
|
+
//#endregion
|
|
156
|
+
//#region ../../internals/utils/src/urlPath.ts
|
|
157
|
+
/**
|
|
158
|
+
* Parses and transforms an OpenAPI/Swagger path string into various URL formats.
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* const p = new URLPath('/pet/{petId}')
|
|
162
|
+
* p.URL // '/pet/:petId'
|
|
163
|
+
* p.template // '`/pet/${petId}`'
|
|
164
|
+
*/
|
|
165
|
+
var URLPath = class {
|
|
166
|
+
/**
|
|
167
|
+
* The raw OpenAPI/Swagger path string, e.g. `/pet/{petId}`.
|
|
168
|
+
*/
|
|
169
|
+
path;
|
|
170
|
+
#options;
|
|
171
|
+
constructor(path, options = {}) {
|
|
172
|
+
this.path = path;
|
|
173
|
+
this.#options = options;
|
|
174
|
+
}
|
|
175
|
+
/** Converts the OpenAPI path to Express-style colon syntax, e.g. `/pet/{petId}` → `/pet/:petId`.
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```ts
|
|
179
|
+
* new URLPath('/pet/{petId}').URL // '/pet/:petId'
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
get URL() {
|
|
183
|
+
return this.toURLPath();
|
|
184
|
+
}
|
|
185
|
+
/** Returns `true` when `path` is a fully-qualified URL (e.g. starts with `https://`).
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* ```ts
|
|
189
|
+
* new URLPath('https://petstore.swagger.io/v2/pet').isURL // true
|
|
190
|
+
* new URLPath('/pet/{petId}').isURL // false
|
|
191
|
+
* ```
|
|
192
|
+
*/
|
|
193
|
+
get isURL() {
|
|
194
|
+
try {
|
|
195
|
+
return !!new URL(this.path).href;
|
|
196
|
+
} catch {
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Converts the OpenAPI path to a TypeScript template literal string.
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
* new URLPath('/pet/{petId}').template // '`/pet/${petId}`'
|
|
205
|
+
* new URLPath('/account/monetary-accountID').template // '`/account/${monetaryAccountId}`'
|
|
206
|
+
*/
|
|
207
|
+
get template() {
|
|
208
|
+
return this.toTemplateString();
|
|
209
|
+
}
|
|
210
|
+
/** Returns the path and its extracted params as a structured `URLObject`, or as a stringified expression when `stringify` is set.
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* ```ts
|
|
214
|
+
* new URLPath('/pet/{petId}').object
|
|
215
|
+
* // { url: '/pet/:petId', params: { petId: 'petId' } }
|
|
216
|
+
* ```
|
|
217
|
+
*/
|
|
218
|
+
get object() {
|
|
219
|
+
return this.toObject();
|
|
220
|
+
}
|
|
221
|
+
/** Returns a map of path parameter names, or `undefined` when the path has no parameters.
|
|
222
|
+
*
|
|
223
|
+
* @example
|
|
224
|
+
* ```ts
|
|
225
|
+
* new URLPath('/pet/{petId}').params // { petId: 'petId' }
|
|
226
|
+
* new URLPath('/pet').params // undefined
|
|
227
|
+
* ```
|
|
228
|
+
*/
|
|
229
|
+
get params() {
|
|
230
|
+
return this.toParamsObject();
|
|
231
|
+
}
|
|
232
|
+
#transformParam(raw) {
|
|
233
|
+
const param = isValidVarName(raw) ? raw : camelCase(raw);
|
|
234
|
+
return this.#options.casing === "camelcase" ? camelCase(param) : param;
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Iterates over every `{param}` token in `path`, calling `fn` with the raw token and transformed name.
|
|
238
|
+
*/
|
|
239
|
+
#eachParam(fn) {
|
|
240
|
+
for (const match of this.path.matchAll(/\{([^}]+)\}/g)) {
|
|
241
|
+
const raw = match[1];
|
|
242
|
+
fn(raw, this.#transformParam(raw));
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
toObject({ type = "path", replacer, stringify } = {}) {
|
|
246
|
+
const object = {
|
|
247
|
+
url: type === "path" ? this.toURLPath() : this.toTemplateString({ replacer }),
|
|
248
|
+
params: this.toParamsObject()
|
|
249
|
+
};
|
|
250
|
+
if (stringify) {
|
|
251
|
+
if (type === "template") return JSON.stringify(object).replaceAll("'", "").replaceAll(`"`, "");
|
|
252
|
+
if (object.params) return `{ url: '${object.url}', params: ${JSON.stringify(object.params).replaceAll("'", "").replaceAll(`"`, "")} }`;
|
|
253
|
+
return `{ url: '${object.url}' }`;
|
|
254
|
+
}
|
|
255
|
+
return object;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Converts the OpenAPI path to a TypeScript template literal string.
|
|
259
|
+
* An optional `replacer` can transform each extracted parameter name before interpolation.
|
|
260
|
+
*
|
|
261
|
+
* @example
|
|
262
|
+
* new URLPath('/pet/{petId}').toTemplateString() // '`/pet/${petId}`'
|
|
263
|
+
*/
|
|
264
|
+
toTemplateString({ prefix = "", replacer } = {}) {
|
|
265
|
+
return `\`${prefix}${this.path.split(/\{([^}]+)\}/).map((part, i) => {
|
|
266
|
+
if (i % 2 === 0) return part;
|
|
267
|
+
const param = this.#transformParam(part);
|
|
268
|
+
return `\${${replacer ? replacer(param) : param}}`;
|
|
269
|
+
}).join("")}\``;
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Extracts all `{param}` segments from the path and returns them as a key-value map.
|
|
273
|
+
* An optional `replacer` transforms each parameter name in both key and value positions.
|
|
274
|
+
* Returns `undefined` when no path parameters are found.
|
|
275
|
+
*
|
|
276
|
+
* @example
|
|
277
|
+
* ```ts
|
|
278
|
+
* new URLPath('/pet/{petId}/tag/{tagId}').toParamsObject()
|
|
279
|
+
* // { petId: 'petId', tagId: 'tagId' }
|
|
280
|
+
* ```
|
|
281
|
+
*/
|
|
282
|
+
toParamsObject(replacer) {
|
|
283
|
+
const params = {};
|
|
284
|
+
this.#eachParam((_raw, param) => {
|
|
285
|
+
const key = replacer ? replacer(param) : param;
|
|
286
|
+
params[key] = key;
|
|
287
|
+
});
|
|
288
|
+
return Object.keys(params).length > 0 ? params : void 0;
|
|
289
|
+
}
|
|
290
|
+
/** Converts the OpenAPI path to Express-style colon syntax.
|
|
291
|
+
*
|
|
292
|
+
* @example
|
|
293
|
+
* ```ts
|
|
294
|
+
* new URLPath('/pet/{petId}').toURLPath() // '/pet/:petId'
|
|
295
|
+
* ```
|
|
296
|
+
*/
|
|
297
|
+
toURLPath() {
|
|
298
|
+
return this.path.replace(/\{([^}]+)\}/g, ":$1");
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
//#endregion
|
|
302
|
+
//#region ../../internals/shared/src/operation.ts
|
|
303
|
+
function getOperationParameters(node, options = {}) {
|
|
304
|
+
const params = _kubb_core.ast.caseParams(node.parameters, options.paramsCasing);
|
|
305
|
+
return {
|
|
306
|
+
path: params.filter((param) => param.in === "path"),
|
|
307
|
+
query: params.filter((param) => param.in === "query"),
|
|
308
|
+
header: params.filter((param) => param.in === "header"),
|
|
309
|
+
cookie: params.filter((param) => param.in === "cookie")
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
function getStatusCodeNumber(statusCode) {
|
|
313
|
+
const code = Number(statusCode);
|
|
314
|
+
return Number.isNaN(code) ? void 0 : code;
|
|
315
|
+
}
|
|
316
|
+
function isErrorStatusCode(statusCode) {
|
|
317
|
+
const code = getStatusCodeNumber(statusCode);
|
|
318
|
+
return code !== void 0 && code >= 400;
|
|
319
|
+
}
|
|
320
|
+
function resolveErrorNames(node, resolver) {
|
|
321
|
+
return node.responses.filter((response) => isErrorStatusCode(response.statusCode)).map((response) => resolver.resolveResponseStatusName(node, response.statusCode));
|
|
322
|
+
}
|
|
323
|
+
function resolveStatusCodeNames(node, resolver) {
|
|
324
|
+
return node.responses.map((response) => resolver.resolveResponseStatusName(node, response.statusCode));
|
|
325
|
+
}
|
|
326
|
+
const typeNamesByResolver = /* @__PURE__ */ new WeakMap();
|
|
327
|
+
function resolveOperationTypeNames(node, resolver, options = {}) {
|
|
328
|
+
const cacheKey = `${node.operationId}\0${options.paramsCasing ?? ""}\0${options.order ?? ""}\0${options.responseStatusNames ?? ""}\0${(options.exclude ?? []).join(",")}`;
|
|
329
|
+
let byResolver = typeNamesByResolver.get(resolver);
|
|
330
|
+
if (byResolver) {
|
|
331
|
+
const cached = byResolver.get(cacheKey);
|
|
332
|
+
if (cached) return cached;
|
|
333
|
+
} else {
|
|
334
|
+
byResolver = /* @__PURE__ */ new Map();
|
|
335
|
+
typeNamesByResolver.set(resolver, byResolver);
|
|
336
|
+
}
|
|
337
|
+
const { path, query, header } = getOperationParameters(node, { paramsCasing: options.paramsCasing });
|
|
338
|
+
const responseStatusNames = options.responseStatusNames === "error" ? resolveErrorNames(node, resolver) : options.responseStatusNames === false ? [] : resolveStatusCodeNames(node, resolver);
|
|
339
|
+
const exclude = new Set(options.exclude ?? []);
|
|
340
|
+
const paramNames = [
|
|
341
|
+
...path.map((param) => resolver.resolvePathParamsName(node, param)),
|
|
342
|
+
...query.map((param) => resolver.resolveQueryParamsName(node, param)),
|
|
343
|
+
...header.map((param) => resolver.resolveHeaderParamsName(node, param))
|
|
344
|
+
];
|
|
345
|
+
const bodyAndResponseNames = [node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : void 0, resolver.resolveResponseName(node)];
|
|
346
|
+
const result = (options.order === "body-response-first" ? [
|
|
347
|
+
...bodyAndResponseNames,
|
|
348
|
+
...paramNames,
|
|
349
|
+
...responseStatusNames
|
|
350
|
+
] : [
|
|
351
|
+
...paramNames,
|
|
352
|
+
...bodyAndResponseNames,
|
|
353
|
+
...responseStatusNames
|
|
354
|
+
]).filter((name) => Boolean(name) && !exclude.has(name));
|
|
355
|
+
byResolver.set(cacheKey, result);
|
|
356
|
+
return result;
|
|
357
|
+
}
|
|
358
|
+
//#endregion
|
|
359
|
+
//#region src/components/Request.tsx
|
|
360
|
+
const declarationPrinter = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
361
|
+
function Request({ baseURL = "", name, dataReturnType, resolver, node, paramsType, pathParamsType, paramsCasing }) {
|
|
362
|
+
const paramsNode = _kubb_core.ast.createOperationParams(node, {
|
|
363
|
+
paramsType,
|
|
364
|
+
pathParamsType,
|
|
365
|
+
paramsCasing,
|
|
366
|
+
resolver,
|
|
367
|
+
extraParams: [_kubb_core.ast.createFunctionParameter({
|
|
368
|
+
name: "options",
|
|
369
|
+
type: _kubb_core.ast.createParamsType({
|
|
370
|
+
variant: "reference",
|
|
371
|
+
name: "Partial<Cypress.RequestOptions>"
|
|
372
|
+
}),
|
|
373
|
+
default: "{}"
|
|
374
|
+
})]
|
|
375
|
+
});
|
|
376
|
+
const paramsSignature = declarationPrinter.print(paramsNode) ?? "";
|
|
377
|
+
const responseType = resolver.resolveResponseName(node);
|
|
378
|
+
const returnType = dataReturnType === "data" ? `Cypress.Chainable<${responseType}>` : `Cypress.Chainable<Cypress.Response<${responseType}>>`;
|
|
379
|
+
const casedPathParams = getOperationParameters(node, { paramsCasing }).path;
|
|
380
|
+
const pathParamNameMap = new Map(casedPathParams.map((p) => [camelCase(p.name), p.name]));
|
|
381
|
+
const urlTemplate = new URLPath(node.path, { casing: paramsCasing }).toTemplateString({
|
|
382
|
+
prefix: baseURL,
|
|
383
|
+
replacer: (param) => pathParamNameMap.get(camelCase(param)) ?? param
|
|
384
|
+
});
|
|
385
|
+
const requestOptions = [`method: '${node.method}'`, `url: ${urlTemplate}`];
|
|
386
|
+
const queryParams = getOperationParameters(node).query;
|
|
387
|
+
if (queryParams.length > 0) {
|
|
388
|
+
const casedQueryParams = getOperationParameters(node, { paramsCasing }).query;
|
|
389
|
+
if (casedQueryParams.some((p, i) => p.name !== queryParams[i].name)) {
|
|
390
|
+
const pairs = queryParams.map((orig, i) => `${orig.name}: params.${casedQueryParams[i].name}`).join(", ");
|
|
391
|
+
requestOptions.push(`qs: params ? { ${pairs} } : undefined`);
|
|
392
|
+
} else requestOptions.push("qs: params");
|
|
393
|
+
}
|
|
394
|
+
const headerParams = getOperationParameters(node).header;
|
|
395
|
+
if (headerParams.length > 0) {
|
|
396
|
+
const casedHeaderParams = getOperationParameters(node, { paramsCasing }).header;
|
|
397
|
+
if (casedHeaderParams.some((p, i) => p.name !== headerParams[i].name)) {
|
|
398
|
+
const pairs = headerParams.map((orig, i) => `'${orig.name}': headers.${casedHeaderParams[i].name}`).join(", ");
|
|
399
|
+
requestOptions.push(`headers: headers ? { ${pairs} } : undefined`);
|
|
400
|
+
} else requestOptions.push("headers");
|
|
401
|
+
}
|
|
402
|
+
if (node.requestBody?.content?.[0]?.schema) requestOptions.push("body: data");
|
|
403
|
+
requestOptions.push("...options");
|
|
404
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
405
|
+
name,
|
|
406
|
+
isIndexable: true,
|
|
407
|
+
isExportable: true,
|
|
408
|
+
children: /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.Function, {
|
|
409
|
+
name,
|
|
410
|
+
export: true,
|
|
411
|
+
params: paramsSignature,
|
|
412
|
+
returnType,
|
|
413
|
+
children: dataReturnType === "data" ? `return cy.request<${responseType}>({
|
|
414
|
+
${requestOptions.join(",\n ")}
|
|
415
|
+
}).then((res) => res.body)` : `return cy.request<${responseType}>({
|
|
416
|
+
${requestOptions.join(",\n ")}
|
|
417
|
+
})`
|
|
418
|
+
})
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
//#endregion
|
|
422
|
+
//#region src/generators/cypressGenerator.tsx
|
|
423
|
+
const cypressGenerator = (0, _kubb_core.defineGenerator)({
|
|
424
|
+
name: "cypress",
|
|
425
|
+
renderer: _kubb_renderer_jsx.jsxRendererSync,
|
|
426
|
+
operation(node, ctx) {
|
|
427
|
+
const { config, resolver, driver, root, inputNode } = ctx;
|
|
428
|
+
const { output, baseURL, dataReturnType, paramsCasing, paramsType, pathParamsType, group } = ctx.options;
|
|
429
|
+
const pluginTs = driver.getPlugin(_kubb_plugin_ts.pluginTsName);
|
|
430
|
+
if (!pluginTs) return null;
|
|
431
|
+
const tsResolver = driver.getResolver(_kubb_plugin_ts.pluginTsName);
|
|
432
|
+
const importedTypeNames = resolveOperationTypeNames(node, tsResolver, { paramsCasing });
|
|
433
|
+
const meta = {
|
|
434
|
+
name: resolver.resolveName(node.operationId),
|
|
435
|
+
file: resolver.resolveFile({
|
|
436
|
+
name: node.operationId,
|
|
437
|
+
extname: ".ts",
|
|
438
|
+
tag: node.tags[0] ?? "default",
|
|
439
|
+
path: node.path
|
|
440
|
+
}, {
|
|
441
|
+
root,
|
|
442
|
+
output,
|
|
443
|
+
group
|
|
444
|
+
}),
|
|
445
|
+
fileTs: tsResolver.resolveFile({
|
|
446
|
+
name: node.operationId,
|
|
447
|
+
extname: ".ts",
|
|
448
|
+
tag: node.tags[0] ?? "default",
|
|
449
|
+
path: node.path
|
|
450
|
+
}, {
|
|
451
|
+
root,
|
|
452
|
+
output: pluginTs.options?.output ?? output,
|
|
453
|
+
group: pluginTs.options?.group
|
|
454
|
+
})
|
|
455
|
+
};
|
|
456
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx.File, {
|
|
457
|
+
baseName: meta.file.baseName,
|
|
458
|
+
path: meta.file.path,
|
|
459
|
+
meta: meta.file.meta,
|
|
460
|
+
banner: resolver.resolveBanner(inputNode, {
|
|
461
|
+
output,
|
|
462
|
+
config
|
|
463
|
+
}),
|
|
464
|
+
footer: resolver.resolveFooter(inputNode, {
|
|
465
|
+
output,
|
|
466
|
+
config
|
|
467
|
+
}),
|
|
468
|
+
children: [meta.fileTs && importedTypeNames.length > 0 && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
469
|
+
name: importedTypeNames,
|
|
470
|
+
root: meta.file.path,
|
|
471
|
+
path: meta.fileTs.path,
|
|
472
|
+
isTypeOnly: true
|
|
473
|
+
}), /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(Request, {
|
|
474
|
+
name: meta.name,
|
|
475
|
+
node,
|
|
476
|
+
resolver: tsResolver,
|
|
477
|
+
dataReturnType,
|
|
478
|
+
paramsCasing,
|
|
479
|
+
paramsType,
|
|
480
|
+
pathParamsType,
|
|
481
|
+
baseURL
|
|
482
|
+
})]
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
});
|
|
486
|
+
//#endregion
|
|
487
|
+
//#region src/resolvers/resolverCypress.ts
|
|
488
|
+
/**
|
|
489
|
+
* Naming convention resolver for Cypress plugin.
|
|
490
|
+
*
|
|
491
|
+
* Provides default naming helpers using camelCase for functions and file paths.
|
|
492
|
+
*
|
|
493
|
+
* @example
|
|
494
|
+
* `resolverCypress.default('list pets', 'function') // → 'listPets'`
|
|
495
|
+
*/
|
|
496
|
+
const resolverCypress = (0, _kubb_core.defineResolver)(() => ({
|
|
497
|
+
name: "default",
|
|
498
|
+
pluginName: "plugin-cypress",
|
|
499
|
+
default(name, type) {
|
|
500
|
+
return camelCase(name, { isFile: type === "file" });
|
|
501
|
+
},
|
|
502
|
+
resolveName(name) {
|
|
503
|
+
return this.default(name, "function");
|
|
504
|
+
},
|
|
505
|
+
resolvePathName(name, type) {
|
|
506
|
+
return this.default(name, type);
|
|
507
|
+
}
|
|
508
|
+
}));
|
|
509
|
+
//#endregion
|
|
9
510
|
//#region src/plugin.ts
|
|
511
|
+
/**
|
|
512
|
+
* Canonical plugin name for `@kubb/plugin-cypress`, used to identify the plugin
|
|
513
|
+
* in driver lookups and warnings.
|
|
514
|
+
*/
|
|
10
515
|
const pluginCypressName = "plugin-cypress";
|
|
11
|
-
|
|
516
|
+
/**
|
|
517
|
+
* The `@kubb/plugin-cypress` plugin factory.
|
|
518
|
+
*
|
|
519
|
+
* Generates Cypress `cy.request()` test functions from an OpenAPI/AST `RootNode`.
|
|
520
|
+
* Walks operations, delegates rendering to the active generators,
|
|
521
|
+
* and writes barrel files based on `output.barrelType`.
|
|
522
|
+
*
|
|
523
|
+
* @example
|
|
524
|
+
* ```ts
|
|
525
|
+
* import pluginCypress from '@kubb/plugin-cypress'
|
|
526
|
+
*
|
|
527
|
+
* export default defineConfig({
|
|
528
|
+
* plugins: [pluginCypress({ output: { path: 'cypress' } })],
|
|
529
|
+
* })
|
|
530
|
+
* ```
|
|
531
|
+
*/
|
|
532
|
+
const pluginCypress = (0, _kubb_core.definePlugin)((options) => {
|
|
12
533
|
const { output = {
|
|
13
534
|
path: "cypress",
|
|
14
535
|
barrelType: "named"
|
|
15
|
-
}, group,
|
|
536
|
+
}, group, exclude = [], include, override = [], dataReturnType = "data", baseURL, paramsCasing, paramsType = "inline", pathParamsType = paramsType === "object" ? "object" : options.pathParamsType || "inline", resolver: userResolver, transformer: userTransformer, generators: userGenerators = [] } = options;
|
|
537
|
+
const groupConfig = group ? {
|
|
538
|
+
...group,
|
|
539
|
+
name: group.name ? group.name : (ctx) => {
|
|
540
|
+
if (group.type === "path") return `${ctx.group.split("/")[1]}`;
|
|
541
|
+
return `${camelCase(ctx.group)}Requests`;
|
|
542
|
+
}
|
|
543
|
+
} : void 0;
|
|
16
544
|
return {
|
|
17
545
|
name: pluginCypressName,
|
|
18
|
-
options
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
pre: [_kubb_plugin_oas.pluginOasName, _kubb_plugin_ts.pluginTsName].filter(Boolean),
|
|
28
|
-
resolvePath(baseName, pathMode, options) {
|
|
29
|
-
const root = node_path.default.resolve(this.config.root, this.config.output.path);
|
|
30
|
-
if ((pathMode ?? (0, _kubb_core.getMode)(node_path.default.resolve(root, output.path))) === "single")
|
|
31
|
-
/**
|
|
32
|
-
* when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
|
|
33
|
-
* Other plugins then need to call addOrAppend instead of just add from the fileManager class
|
|
34
|
-
*/
|
|
35
|
-
return node_path.default.resolve(root, output.path);
|
|
36
|
-
if (group && (options?.group?.path || options?.group?.tag)) {
|
|
37
|
-
const groupName = group?.name ? group.name : (ctx) => {
|
|
38
|
-
if (group?.type === "path") return `${ctx.group.split("/")[1]}`;
|
|
39
|
-
return `${require_components.camelCase(ctx.group)}Requests`;
|
|
40
|
-
};
|
|
41
|
-
return node_path.default.resolve(root, output.path, groupName({ group: group.type === "path" ? options.group.path : options.group.tag }), baseName);
|
|
42
|
-
}
|
|
43
|
-
return node_path.default.resolve(root, output.path, baseName);
|
|
44
|
-
},
|
|
45
|
-
resolveName(name, type) {
|
|
46
|
-
const resolvedName = require_components.camelCase(name, { isFile: type === "file" });
|
|
47
|
-
if (type) return transformers?.name?.(resolvedName, type) || resolvedName;
|
|
48
|
-
return resolvedName;
|
|
49
|
-
},
|
|
50
|
-
async install() {
|
|
51
|
-
const root = node_path.default.resolve(this.config.root, this.config.output.path);
|
|
52
|
-
const mode = (0, _kubb_core.getMode)(node_path.default.resolve(root, output.path));
|
|
53
|
-
const oas = await this.getOas();
|
|
54
|
-
const files = await new _kubb_plugin_oas.OperationGenerator(this.plugin.options, {
|
|
55
|
-
fabric: this.fabric,
|
|
56
|
-
oas,
|
|
57
|
-
driver: this.driver,
|
|
58
|
-
events: this.events,
|
|
59
|
-
plugin: this.plugin,
|
|
60
|
-
contentType,
|
|
546
|
+
options,
|
|
547
|
+
dependencies: [_kubb_plugin_ts.pluginTsName],
|
|
548
|
+
hooks: { "kubb:plugin:setup"(ctx) {
|
|
549
|
+
const resolver = userResolver ? {
|
|
550
|
+
...resolverCypress,
|
|
551
|
+
...userResolver
|
|
552
|
+
} : resolverCypress;
|
|
553
|
+
ctx.setOptions({
|
|
554
|
+
output,
|
|
61
555
|
exclude,
|
|
62
556
|
include,
|
|
63
557
|
override,
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
meta: { pluginName: this.plugin.name }
|
|
558
|
+
dataReturnType,
|
|
559
|
+
group: groupConfig,
|
|
560
|
+
baseURL,
|
|
561
|
+
paramsCasing,
|
|
562
|
+
paramsType,
|
|
563
|
+
pathParamsType,
|
|
564
|
+
resolver
|
|
72
565
|
});
|
|
73
|
-
|
|
74
|
-
|
|
566
|
+
ctx.setResolver(resolver);
|
|
567
|
+
if (userTransformer) ctx.setTransformer(userTransformer);
|
|
568
|
+
ctx.addGenerator(cypressGenerator);
|
|
569
|
+
for (const gen of userGenerators) ctx.addGenerator(gen);
|
|
570
|
+
} }
|
|
75
571
|
};
|
|
76
572
|
});
|
|
77
573
|
//#endregion
|
|
574
|
+
exports.Request = Request;
|
|
575
|
+
exports.cypressGenerator = cypressGenerator;
|
|
576
|
+
exports.default = pluginCypress;
|
|
78
577
|
exports.pluginCypress = pluginCypress;
|
|
79
578
|
exports.pluginCypressName = pluginCypressName;
|
|
579
|
+
exports.resolverCypress = resolverCypress;
|
|
80
580
|
|
|
81
581
|
//# sourceMappingURL=index.cjs.map
|