@kubb/plugin-cypress 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 -3
- package/dist/index.cjs +511 -61
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +179 -4
- package/dist/index.js +505 -62
- package/dist/index.js.map +1 -1
- package/package.json +43 -64
- package/src/components/Request.tsx +75 -108
- package/src/generators/cypressGenerator.tsx +51 -44
- package/src/index.ts +9 -2
- package/src/plugin.ts +68 -92
- package/src/resolvers/resolverCypress.ts +22 -0
- package/src/types.ts +75 -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/LICENSE
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
MIT License
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Copyright (c) 2026 Kubb Labs
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
7
11
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
— see licenses/LICENSE-AGPL-3.0 for the full license text:
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
11
14
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -4,12 +4,12 @@
|
|
|
4
4
|
<img width="180" src="https://raw.githubusercontent.com/kubb-labs/kubb/main/assets/logo.png" alt="Kubb logo">
|
|
5
5
|
</a>
|
|
6
6
|
|
|
7
|
-
|
|
8
7
|
[![npm version][npm-version-src]][npm-version-href]
|
|
9
8
|
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
|
10
9
|
[![Coverage][coverage-src]][coverage-href]
|
|
11
10
|
[![License][license-src]][license-href]
|
|
12
11
|
[![Sponsors][sponsors-src]][sponsors-href]
|
|
12
|
+
|
|
13
13
|
<h4>
|
|
14
14
|
<a href="https://codesandbox.io/s/github/kubb-labs/kubb/tree/main//examples/typescript" target="_blank">View Demo</a>
|
|
15
15
|
<span> · </span>
|
|
@@ -23,7 +23,6 @@
|
|
|
23
23
|
|
|
24
24
|
Swagger integration to create Cypress requests commands.
|
|
25
25
|
|
|
26
|
-
|
|
27
26
|
## Supporting Kubb
|
|
28
27
|
|
|
29
28
|
Kubb uses an MIT-licensed open source project with its ongoing development made possible entirely by the support of Sponsors. If you would like to become a sponsor, please consider:
|
|
@@ -36,7 +35,6 @@ Kubb uses an MIT-licensed open source project with its ongoing development made
|
|
|
36
35
|
</a>
|
|
37
36
|
</p>
|
|
38
37
|
|
|
39
|
-
|
|
40
38
|
<!-- Badges -->
|
|
41
39
|
|
|
42
40
|
[npm-version-src]: https://img.shields.io/npm/v/@kubb/plugin-cypress?flat&colorA=18181B&colorB=f58517
|
package/dist/index.cjs
CHANGED
|
@@ -1,81 +1,531 @@
|
|
|
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.getParams();
|
|
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.getParams()
|
|
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}').getParams()
|
|
279
|
+
* // { petId: 'petId', tagId: 'tagId' }
|
|
280
|
+
* ```
|
|
281
|
+
*/
|
|
282
|
+
getParams(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 src/components/Request.tsx
|
|
303
|
+
const declarationPrinter = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
304
|
+
function Request({ baseURL = "", name, dataReturnType, resolver, node, paramsType, pathParamsType, paramsCasing }) {
|
|
305
|
+
const paramsNode = _kubb_core.ast.createOperationParams(node, {
|
|
306
|
+
paramsType,
|
|
307
|
+
pathParamsType,
|
|
308
|
+
paramsCasing,
|
|
309
|
+
resolver,
|
|
310
|
+
extraParams: [_kubb_core.ast.createFunctionParameter({
|
|
311
|
+
name: "options",
|
|
312
|
+
type: _kubb_core.ast.createParamsType({
|
|
313
|
+
variant: "reference",
|
|
314
|
+
name: "Partial<Cypress.RequestOptions>"
|
|
315
|
+
}),
|
|
316
|
+
default: "{}"
|
|
317
|
+
})]
|
|
318
|
+
});
|
|
319
|
+
const paramsSignature = declarationPrinter.print(paramsNode) ?? "";
|
|
320
|
+
const responseType = resolver.resolveResponseName(node);
|
|
321
|
+
const returnType = dataReturnType === "data" ? `Cypress.Chainable<${responseType}>` : `Cypress.Chainable<Cypress.Response<${responseType}>>`;
|
|
322
|
+
const casedPathParams = _kubb_core.ast.caseParams(node.parameters.filter((p) => p.in === "path"), paramsCasing);
|
|
323
|
+
const pathParamNameMap = new Map(casedPathParams.map((p) => [camelCase(p.name), p.name]));
|
|
324
|
+
const urlTemplate = new URLPath(node.path, { casing: paramsCasing }).toTemplateString({
|
|
325
|
+
prefix: baseURL,
|
|
326
|
+
replacer: (param) => pathParamNameMap.get(camelCase(param)) ?? param
|
|
327
|
+
});
|
|
328
|
+
const requestOptions = [`method: '${node.method}'`, `url: ${urlTemplate}`];
|
|
329
|
+
const queryParams = node.parameters.filter((p) => p.in === "query");
|
|
330
|
+
if (queryParams.length > 0) {
|
|
331
|
+
const casedQueryParams = _kubb_core.ast.caseParams(queryParams, paramsCasing);
|
|
332
|
+
if (casedQueryParams.some((p, i) => p.name !== queryParams[i].name)) {
|
|
333
|
+
const pairs = queryParams.map((orig, i) => `${orig.name}: params.${casedQueryParams[i].name}`).join(", ");
|
|
334
|
+
requestOptions.push(`qs: params ? { ${pairs} } : undefined`);
|
|
335
|
+
} else requestOptions.push("qs: params");
|
|
336
|
+
}
|
|
337
|
+
const headerParams = node.parameters.filter((p) => p.in === "header");
|
|
338
|
+
if (headerParams.length > 0) {
|
|
339
|
+
const casedHeaderParams = _kubb_core.ast.caseParams(headerParams, paramsCasing);
|
|
340
|
+
if (casedHeaderParams.some((p, i) => p.name !== headerParams[i].name)) {
|
|
341
|
+
const pairs = headerParams.map((orig, i) => `'${orig.name}': headers.${casedHeaderParams[i].name}`).join(", ");
|
|
342
|
+
requestOptions.push(`headers: headers ? { ${pairs} } : undefined`);
|
|
343
|
+
} else requestOptions.push("headers");
|
|
344
|
+
}
|
|
345
|
+
if (node.requestBody?.content?.[0]?.schema) requestOptions.push("body: data");
|
|
346
|
+
requestOptions.push("...options");
|
|
347
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
348
|
+
name,
|
|
349
|
+
isIndexable: true,
|
|
350
|
+
isExportable: true,
|
|
351
|
+
children: /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.Function, {
|
|
352
|
+
name,
|
|
353
|
+
export: true,
|
|
354
|
+
params: paramsSignature,
|
|
355
|
+
returnType,
|
|
356
|
+
children: dataReturnType === "data" ? `return cy.request<${responseType}>({
|
|
357
|
+
${requestOptions.join(",\n ")}
|
|
358
|
+
}).then((res) => res.body)` : `return cy.request<${responseType}>({
|
|
359
|
+
${requestOptions.join(",\n ")}
|
|
360
|
+
})`
|
|
361
|
+
})
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
//#endregion
|
|
365
|
+
//#region src/generators/cypressGenerator.tsx
|
|
366
|
+
const cypressGenerator = (0, _kubb_core.defineGenerator)({
|
|
367
|
+
name: "cypress",
|
|
368
|
+
renderer: _kubb_renderer_jsx.jsxRenderer,
|
|
369
|
+
operation(node, ctx) {
|
|
370
|
+
const { adapter, config, resolver, driver, root } = ctx;
|
|
371
|
+
const { output, baseURL, dataReturnType, paramsCasing, paramsType, pathParamsType, group } = ctx.options;
|
|
372
|
+
const pluginTs = driver.getPlugin(_kubb_plugin_ts.pluginTsName);
|
|
373
|
+
if (!pluginTs) return null;
|
|
374
|
+
const tsResolver = driver.getResolver(_kubb_plugin_ts.pluginTsName);
|
|
375
|
+
const casedParams = _kubb_core.ast.caseParams(node.parameters, paramsCasing);
|
|
376
|
+
const pathParams = casedParams.filter((p) => p.in === "path");
|
|
377
|
+
const queryParams = casedParams.filter((p) => p.in === "query");
|
|
378
|
+
const headerParams = casedParams.filter((p) => p.in === "header");
|
|
379
|
+
const importedTypeNames = [
|
|
380
|
+
...pathParams.map((p) => tsResolver.resolvePathParamsName(node, p)),
|
|
381
|
+
...queryParams.map((p) => tsResolver.resolveQueryParamsName(node, p)),
|
|
382
|
+
...headerParams.map((p) => tsResolver.resolveHeaderParamsName(node, p)),
|
|
383
|
+
node.requestBody?.content?.[0]?.schema ? tsResolver.resolveDataName(node) : void 0,
|
|
384
|
+
tsResolver.resolveResponseName(node)
|
|
385
|
+
].filter(Boolean);
|
|
386
|
+
const meta = {
|
|
387
|
+
name: resolver.resolveName(node.operationId),
|
|
388
|
+
file: resolver.resolveFile({
|
|
389
|
+
name: node.operationId,
|
|
390
|
+
extname: ".ts",
|
|
391
|
+
tag: node.tags[0] ?? "default",
|
|
392
|
+
path: node.path
|
|
393
|
+
}, {
|
|
394
|
+
root,
|
|
395
|
+
output,
|
|
396
|
+
group
|
|
397
|
+
}),
|
|
398
|
+
fileTs: tsResolver.resolveFile({
|
|
399
|
+
name: node.operationId,
|
|
400
|
+
extname: ".ts",
|
|
401
|
+
tag: node.tags[0] ?? "default",
|
|
402
|
+
path: node.path
|
|
403
|
+
}, {
|
|
404
|
+
root,
|
|
405
|
+
output: pluginTs.options?.output ?? output,
|
|
406
|
+
group: pluginTs.options?.group
|
|
407
|
+
})
|
|
408
|
+
};
|
|
409
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx.File, {
|
|
410
|
+
baseName: meta.file.baseName,
|
|
411
|
+
path: meta.file.path,
|
|
412
|
+
meta: meta.file.meta,
|
|
413
|
+
banner: resolver.resolveBanner(adapter.inputNode, {
|
|
414
|
+
output,
|
|
415
|
+
config
|
|
416
|
+
}),
|
|
417
|
+
footer: resolver.resolveFooter(adapter.inputNode, {
|
|
418
|
+
output,
|
|
419
|
+
config
|
|
420
|
+
}),
|
|
421
|
+
children: [meta.fileTs && importedTypeNames.length > 0 && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
422
|
+
name: Array.from(new Set(importedTypeNames)),
|
|
423
|
+
root: meta.file.path,
|
|
424
|
+
path: meta.fileTs.path,
|
|
425
|
+
isTypeOnly: true
|
|
426
|
+
}), /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(Request, {
|
|
427
|
+
name: meta.name,
|
|
428
|
+
node,
|
|
429
|
+
resolver: tsResolver,
|
|
430
|
+
dataReturnType,
|
|
431
|
+
paramsCasing,
|
|
432
|
+
paramsType,
|
|
433
|
+
pathParamsType,
|
|
434
|
+
baseURL
|
|
435
|
+
})]
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
});
|
|
439
|
+
//#endregion
|
|
440
|
+
//#region src/resolvers/resolverCypress.ts
|
|
441
|
+
/**
|
|
442
|
+
* Naming convention resolver for Cypress plugin.
|
|
443
|
+
*
|
|
444
|
+
* Provides default naming helpers using camelCase for functions and file paths.
|
|
445
|
+
*
|
|
446
|
+
* @example
|
|
447
|
+
* `resolverCypress.default('list pets', 'function') // → 'listPets'`
|
|
448
|
+
*/
|
|
449
|
+
const resolverCypress = (0, _kubb_core.defineResolver)((ctx) => ({
|
|
450
|
+
name: "default",
|
|
451
|
+
pluginName: "plugin-cypress",
|
|
452
|
+
default(name, type) {
|
|
453
|
+
return camelCase(name, { isFile: type === "file" });
|
|
454
|
+
},
|
|
455
|
+
resolveName(name) {
|
|
456
|
+
return ctx.default(name, "function");
|
|
457
|
+
}
|
|
458
|
+
}));
|
|
459
|
+
//#endregion
|
|
9
460
|
//#region src/plugin.ts
|
|
461
|
+
/**
|
|
462
|
+
* Canonical plugin name for `@kubb/plugin-cypress`, used to identify the plugin
|
|
463
|
+
* in driver lookups and warnings.
|
|
464
|
+
*/
|
|
10
465
|
const pluginCypressName = "plugin-cypress";
|
|
11
|
-
|
|
466
|
+
/**
|
|
467
|
+
* The `@kubb/plugin-cypress` plugin factory.
|
|
468
|
+
*
|
|
469
|
+
* Generates Cypress `cy.request()` test functions from an OpenAPI/AST `RootNode`.
|
|
470
|
+
* Walks operations, delegates rendering to the active generators,
|
|
471
|
+
* and writes barrel files based on `output.barrelType`.
|
|
472
|
+
*
|
|
473
|
+
* @example
|
|
474
|
+
* ```ts
|
|
475
|
+
* import pluginCypress from '@kubb/plugin-cypress'
|
|
476
|
+
*
|
|
477
|
+
* export default defineConfig({
|
|
478
|
+
* plugins: [pluginCypress({ output: { path: 'cypress' } })],
|
|
479
|
+
* })
|
|
480
|
+
* ```
|
|
481
|
+
*/
|
|
482
|
+
const pluginCypress = (0, _kubb_core.definePlugin)((options) => {
|
|
12
483
|
const { output = {
|
|
13
484
|
path: "cypress",
|
|
14
485
|
barrelType: "named"
|
|
15
|
-
}, group,
|
|
486
|
+
}, group, exclude = [], include, override = [], dataReturnType = "data", baseURL, paramsCasing, paramsType = "inline", pathParamsType = paramsType === "object" ? "object" : options.pathParamsType || "inline", resolver: userResolver, transformer: userTransformer, generators: userGenerators = [] } = options;
|
|
487
|
+
const groupConfig = group ? {
|
|
488
|
+
...group,
|
|
489
|
+
name: group.name ? group.name : (ctx) => {
|
|
490
|
+
if (group.type === "path") return `${ctx.group.split("/")[1]}`;
|
|
491
|
+
return `${camelCase(ctx.group)}Requests`;
|
|
492
|
+
}
|
|
493
|
+
} : void 0;
|
|
16
494
|
return {
|
|
17
495
|
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,
|
|
496
|
+
options,
|
|
497
|
+
dependencies: [_kubb_plugin_ts.pluginTsName],
|
|
498
|
+
hooks: { "kubb:plugin:setup"(ctx) {
|
|
499
|
+
const resolver = userResolver ? {
|
|
500
|
+
...resolverCypress,
|
|
501
|
+
...userResolver
|
|
502
|
+
} : resolverCypress;
|
|
503
|
+
ctx.setOptions({
|
|
504
|
+
output,
|
|
61
505
|
exclude,
|
|
62
506
|
include,
|
|
63
507
|
override,
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
meta: { pluginName: this.plugin.name }
|
|
508
|
+
dataReturnType,
|
|
509
|
+
group: groupConfig,
|
|
510
|
+
baseURL,
|
|
511
|
+
paramsCasing,
|
|
512
|
+
paramsType,
|
|
513
|
+
pathParamsType,
|
|
514
|
+
resolver
|
|
72
515
|
});
|
|
73
|
-
|
|
74
|
-
|
|
516
|
+
ctx.setResolver(resolver);
|
|
517
|
+
if (userTransformer) ctx.setTransformer(userTransformer);
|
|
518
|
+
ctx.addGenerator(cypressGenerator);
|
|
519
|
+
for (const gen of userGenerators) ctx.addGenerator(gen);
|
|
520
|
+
} }
|
|
75
521
|
};
|
|
76
522
|
});
|
|
77
523
|
//#endregion
|
|
524
|
+
exports.Request = Request;
|
|
525
|
+
exports.cypressGenerator = cypressGenerator;
|
|
526
|
+
exports.default = pluginCypress;
|
|
78
527
|
exports.pluginCypress = pluginCypress;
|
|
79
528
|
exports.pluginCypressName = pluginCypressName;
|
|
529
|
+
exports.resolverCypress = resolverCypress;
|
|
80
530
|
|
|
81
531
|
//# sourceMappingURL=index.cjs.map
|