@kubb/plugin-cypress 5.3.6 → 5.3.7
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/dist/index.cjs +116 -62
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +34 -22
- package/dist/index.d.ts +34 -22
- package/dist/index.js +117 -63
- package/dist/index.js.map +1 -1
- package/package.json +6 -4
package/dist/index.cjs
CHANGED
|
@@ -2,10 +2,107 @@ Object.defineProperties(exports, {
|
|
|
2
2
|
__esModule: { value: true },
|
|
3
3
|
[Symbol.toStringTag]: { value: "Module" }
|
|
4
4
|
});
|
|
5
|
+
let _kubb_ast = require("@kubb/ast");
|
|
5
6
|
let _kubb_kit = require("@kubb/kit");
|
|
6
7
|
let _kubb_renderer_jsx = require("@kubb/renderer-jsx");
|
|
7
8
|
let _kubb_renderer_jsx_jsx_runtime = require("@kubb/renderer-jsx/jsx-runtime");
|
|
8
9
|
let _kubb_plugin_ts = require("@kubb/plugin-ts");
|
|
10
|
+
// ../../internals/ast/src/http/requestBody.ts
|
|
11
|
+
/** The schema of the first request body entry, else `undefined` when the operation takes no body. */
|
|
12
|
+
function getSchema(node) {
|
|
13
|
+
return node.requestBody?.content?.[0]?.schema;
|
|
14
|
+
}
|
|
15
|
+
// ../../internals/ast/src/http/status.ts
|
|
16
|
+
const classByRange = /* @__PURE__ */ new Map([
|
|
17
|
+
["1xx", 1],
|
|
18
|
+
["2xx", 2],
|
|
19
|
+
["3xx", 3],
|
|
20
|
+
["4xx", 4],
|
|
21
|
+
["5xx", 5]
|
|
22
|
+
]);
|
|
23
|
+
/** The class digit of a status code or range: `2` for `201` and `2xx`, `null` for `default`. */
|
|
24
|
+
function statusClass(input) {
|
|
25
|
+
const statusCode = typeof input === "object" ? input.statusCode : input;
|
|
26
|
+
const rangeClass = classByRange.get(String(statusCode));
|
|
27
|
+
if (rangeClass) return rangeClass;
|
|
28
|
+
const code = Number(statusCode);
|
|
29
|
+
return Number.isNaN(code) ? null : Math.floor(code / 100);
|
|
30
|
+
}
|
|
31
|
+
function isSuccess(statusCode) {
|
|
32
|
+
return statusClass(statusCode) === 2;
|
|
33
|
+
}
|
|
34
|
+
function isError(statusCode) {
|
|
35
|
+
const code = statusClass(statusCode);
|
|
36
|
+
return code !== null && code >= 4;
|
|
37
|
+
}
|
|
38
|
+
// ../../internals/ast/src/http/responses.ts
|
|
39
|
+
/** The `4xx` and `5xx` responses, ranges included. */
|
|
40
|
+
function filterError(node) {
|
|
41
|
+
return node.responses.filter((response) => isError(response.statusCode));
|
|
42
|
+
}
|
|
43
|
+
function filterSuccess(node) {
|
|
44
|
+
return node.responses.filter((response) => isSuccess(response.statusCode));
|
|
45
|
+
}
|
|
46
|
+
/** The success response a client returns: one with a body first, then an exact code over a `2xx` range. */
|
|
47
|
+
function findPrimary(node) {
|
|
48
|
+
const responses = filterSuccess(node);
|
|
49
|
+
const withBody = responses.filter((response) => response.content?.some((entry) => entry.contentType));
|
|
50
|
+
const candidates = withBody.length ? withBody : responses;
|
|
51
|
+
return candidates.find((response) => !Number.isNaN(Number(response.statusCode))) ?? candidates[0] ?? null;
|
|
52
|
+
}
|
|
53
|
+
// ../../internals/ast/src/http/index.ts
|
|
54
|
+
const isOperation = _kubb_ast.ast.http.isOperation;
|
|
55
|
+
/** Computes once per key; generators visit the same node many times. */
|
|
56
|
+
function cached(store, key, compute) {
|
|
57
|
+
if (!store.has(key)) store.set(key, compute());
|
|
58
|
+
return store.get(key);
|
|
59
|
+
}
|
|
60
|
+
const parametersByNode = /* @__PURE__ */ new WeakMap();
|
|
61
|
+
/** The operation's parameters by location, with a `querystring` parameter in `query`. A repeated name keeps its first parameter. */
|
|
62
|
+
function groupParameters(node) {
|
|
63
|
+
return cached(parametersByNode, node, () => {
|
|
64
|
+
const group = (...locations) => {
|
|
65
|
+
const seen = /* @__PURE__ */ new Set();
|
|
66
|
+
return locations.flatMap((location) => node.parameters.filter((param) => {
|
|
67
|
+
if (param.in !== location || seen.has(param.name)) return false;
|
|
68
|
+
seen.add(param.name);
|
|
69
|
+
return true;
|
|
70
|
+
}));
|
|
71
|
+
};
|
|
72
|
+
return {
|
|
73
|
+
path: group("path"),
|
|
74
|
+
query: group("query", "querystring"),
|
|
75
|
+
header: group("header"),
|
|
76
|
+
cookie: group("cookie")
|
|
77
|
+
};
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
const requestGroupsByNode = /* @__PURE__ */ new WeakMap();
|
|
81
|
+
/** Which request groups are present, which hold a required member, and the body's media types. Built once per operation. */
|
|
82
|
+
function getRequestGroups(node) {
|
|
83
|
+
return cached(requestGroupsByNode, node, () => {
|
|
84
|
+
const { path, query, header, cookie } = groupParameters(node);
|
|
85
|
+
const content = node.requestBody?.content ?? [];
|
|
86
|
+
const body = Boolean(content[0]?.schema);
|
|
87
|
+
const required = {
|
|
88
|
+
path: path.some((param) => param.required),
|
|
89
|
+
query: query.some((param) => param.required),
|
|
90
|
+
headers: header.some((param) => param.required),
|
|
91
|
+
body: body && node.requestBody?.required === true
|
|
92
|
+
};
|
|
93
|
+
return {
|
|
94
|
+
path: path.length > 0,
|
|
95
|
+
query: query.length > 0,
|
|
96
|
+
body,
|
|
97
|
+
headers: header.length > 0,
|
|
98
|
+
cookies: cookie.length > 0,
|
|
99
|
+
required,
|
|
100
|
+
isOptional: !required.path && !required.query && !required.headers && !required.body,
|
|
101
|
+
contentTypes: content.map((entry) => entry.contentType),
|
|
102
|
+
hasContentTypeChoice: content.filter((entry) => entry.schema).length > 1
|
|
103
|
+
};
|
|
104
|
+
});
|
|
105
|
+
}
|
|
9
106
|
// ../../internals/utils/src/reserved.ts
|
|
10
107
|
/**
|
|
11
108
|
* JavaScript and Java reserved words.
|
|
@@ -225,14 +322,14 @@ function buildContentTypeInfo(contentTypes) {
|
|
|
225
322
|
};
|
|
226
323
|
}
|
|
227
324
|
function getContentTypeInfo(node) {
|
|
228
|
-
return buildContentTypeInfo(node
|
|
325
|
+
return buildContentTypeInfo(getRequestGroups(node).contentTypes);
|
|
229
326
|
}
|
|
230
327
|
/**
|
|
231
328
|
* The request-body counterpart for the primary success response: the content types it documents and
|
|
232
329
|
* whether several are present, so the client can let a caller pick which one to accept.
|
|
233
330
|
*/
|
|
234
331
|
function getResponseContentTypeInfo(node) {
|
|
235
|
-
return buildContentTypeInfo(
|
|
332
|
+
return buildContentTypeInfo(findPrimary(node)?.content?.map((e) => e.contentType) ?? []);
|
|
236
333
|
}
|
|
237
334
|
/**
|
|
238
335
|
* Builds the `client?:` option type shared by the generated query hooks (`useQuery`,
|
|
@@ -256,7 +353,7 @@ function buildClientOptionType(options = {}) {
|
|
|
256
353
|
* generated call.
|
|
257
354
|
*/
|
|
258
355
|
function buildRequestConfigType(node) {
|
|
259
|
-
if (
|
|
356
|
+
if (getRequestGroups(node).hasContentTypeChoice) return buildClientOptionType({ omit: ["contentType"] });
|
|
260
357
|
const request = getContentTypeInfo(node);
|
|
261
358
|
const response = getResponseContentTypeInfo(node);
|
|
262
359
|
const members = [request.isMultipleContentTypes ? `request?: ${request.contentTypeUnion}` : null, response.isMultipleContentTypes ? `response?: ${response.contentTypeUnion}` : null].filter(Boolean);
|
|
@@ -272,38 +369,6 @@ const requestGroupOrder = [
|
|
|
272
369
|
"cookies"
|
|
273
370
|
];
|
|
274
371
|
/**
|
|
275
|
-
* Which of the grouped request options an operation carries.
|
|
276
|
-
*/
|
|
277
|
-
function getRequestGroups(node) {
|
|
278
|
-
const { path, query, header, cookie } = _kubb_kit.ast.http.groupParameters(node);
|
|
279
|
-
return {
|
|
280
|
-
path: path.length > 0,
|
|
281
|
-
query: query.length > 0,
|
|
282
|
-
body: Boolean(_kubb_kit.ast.http.requestBody.getSchema(node)),
|
|
283
|
-
headers: header.length > 0,
|
|
284
|
-
cookies: cookie.length > 0
|
|
285
|
-
};
|
|
286
|
-
}
|
|
287
|
-
/**
|
|
288
|
-
* Resolves which grouped request options an operation carries together with whether each group
|
|
289
|
-
* holds a required member. The grouped parameter stays optional only when nothing inside it is
|
|
290
|
-
* required, matching the generated `RequestConfig` type.
|
|
291
|
-
*/
|
|
292
|
-
function getRequestGroupOptionality(node) {
|
|
293
|
-
const groups = getRequestGroups(node);
|
|
294
|
-
const { path, query, header } = _kubb_kit.ast.http.groupParameters(node);
|
|
295
|
-
const hasRequiredPath = path.some((param) => param.required);
|
|
296
|
-
const hasRequiredQuery = query.some((param) => param.required);
|
|
297
|
-
const hasRequiredHeader = header.some((param) => param.required);
|
|
298
|
-
return {
|
|
299
|
-
groups,
|
|
300
|
-
hasRequiredPath,
|
|
301
|
-
hasRequiredQuery,
|
|
302
|
-
hasRequiredHeader,
|
|
303
|
-
isOptional: !hasRequiredPath && !hasRequiredQuery && !hasRequiredHeader && !(groups.body && node.requestBody?.required === true)
|
|
304
|
-
};
|
|
305
|
-
}
|
|
306
|
-
/**
|
|
307
372
|
* Builds the grouped `{ path, query, body, headers, cookies }` parameter for a generated client
|
|
308
373
|
* function, typed from the operation's `Options` (minus `url`). Only the groups the
|
|
309
374
|
* operation actually has are destructured. The trailing `config` parameter carries the
|
|
@@ -311,23 +376,17 @@ function getRequestGroupOptionality(node) {
|
|
|
311
376
|
*/
|
|
312
377
|
function buildRequestParamsSignature(node, resolver, options = {}) {
|
|
313
378
|
const { isConfigurable = true } = options;
|
|
314
|
-
const
|
|
379
|
+
const groups = getRequestGroups(node);
|
|
315
380
|
const names = requestGroupOrder.filter((key) => groups[key]);
|
|
316
|
-
const optionsType =
|
|
381
|
+
const optionsType = groups.hasContentTypeChoice ? `Pick<${resolver.response.options(node)}, ${names.map((name) => `'${name}'`).join(" | ")}>` : resolver.response.options(node);
|
|
317
382
|
return {
|
|
318
|
-
signature: [names.length > 0 ? `{ ${names.join(", ")} }: ${optionsType}${isOptional ? " = {}" : ""}` : null, isConfigurable ? `config: ${buildRequestConfigType(node)} = {}` : null].filter(Boolean).join(", "),
|
|
383
|
+
signature: [names.length > 0 ? `{ ${names.join(", ")} }: ${optionsType}${groups.isOptional ? " = {}" : ""}` : null, isConfigurable ? `config: ${buildRequestConfigType(node)} = {}` : null].filter(Boolean).join(", "),
|
|
319
384
|
groups,
|
|
320
385
|
names
|
|
321
386
|
};
|
|
322
387
|
}
|
|
323
|
-
/**
|
|
324
|
-
* Whether the operation declares more than one request media type with a schema, so a caller must pick one on `contentType`.
|
|
325
|
-
*/
|
|
326
|
-
function hasRequestContentTypeChoice(node) {
|
|
327
|
-
return (node.requestBody?.content ?? []).filter((entry) => entry.schema).length > 1;
|
|
328
|
-
}
|
|
329
388
|
function resolveErrorNames(node, resolver) {
|
|
330
|
-
return
|
|
389
|
+
return filterError(node).map((response) => resolver.response.status(node, response.statusCode));
|
|
331
390
|
}
|
|
332
391
|
function resolveStatusCodeNames(node, resolver) {
|
|
333
392
|
return node.responses.map((response) => resolver.response.status(node, response.statusCode));
|
|
@@ -339,7 +398,7 @@ function resolveStatusCodeNames(node, resolver) {
|
|
|
339
398
|
function resolveOperationTypeNames(node, resolver, options = {}) {
|
|
340
399
|
const key = `${node.operationId}\0${options.order ?? ""}\0${options.responseStatusNames ?? ""}\0${options.includeParams === false ? "noparams" : ""}\0${(options.exclude ?? []).join(",")}`;
|
|
341
400
|
return resolver.memo(`operationTypeNames:${key}`, () => {
|
|
342
|
-
const { path, query, header, cookie } =
|
|
401
|
+
const { path, query, header, cookie } = groupParameters(node);
|
|
343
402
|
const responseStatusNames = options.responseStatusNames === "error" ? resolveErrorNames(node, resolver) : options.responseStatusNames === false ? [] : resolveStatusCodeNames(node, resolver);
|
|
344
403
|
const exclude = new Set(options.exclude ?? []);
|
|
345
404
|
const paramNames = options.includeParams === false ? [] : [
|
|
@@ -348,7 +407,7 @@ function resolveOperationTypeNames(node, resolver, options = {}) {
|
|
|
348
407
|
...header.map((param) => resolver.param.headers(node, param)),
|
|
349
408
|
...cookie.map((param) => resolver.param.cookies(node, param))
|
|
350
409
|
];
|
|
351
|
-
const bodyAndResponseNames = [
|
|
410
|
+
const bodyAndResponseNames = [getSchema(node) ? resolver.response.body(node) : null, resolver.response.response(node)];
|
|
352
411
|
return (options.order === "body-response-first" ? [
|
|
353
412
|
...bodyAndResponseNames,
|
|
354
413
|
...paramNames,
|
|
@@ -402,7 +461,7 @@ function Request({ baseURL = "", name, resolver, node }) {
|
|
|
402
461
|
"integer",
|
|
403
462
|
"bigint",
|
|
404
463
|
"boolean"
|
|
405
|
-
].includes(
|
|
464
|
+
].includes(_kubb_ast.ref.getSchema(parameter.schema).primitive ?? "");
|
|
406
465
|
const isExploded = (parameter) => parameter.explode !== false && isComposite(parameter);
|
|
407
466
|
const explodedCookies = rawParameters.filter(isExploded).map((parameter) => _kubb_kit.text.quote(parameter.name));
|
|
408
467
|
const joinedCookies = rawParameters.filter((parameter) => !isExploded(parameter)).map((parameter) => _kubb_kit.text.quote(parameter.name));
|
|
@@ -424,8 +483,8 @@ ${compositeLines} const text = value instanceof Date ? value.toISOString() : St
|
|
|
424
483
|
const explodeRaw = explodedCookies.length ? `[${explodedCookies.join(", ")}].includes(name) ? explodeCookie(name, value, rawCookie) : ` : "";
|
|
425
484
|
const explodeForm = explodedFormCookies.length ? `[${explodedFormCookies.join(", ")}].includes(name) ? explodeCookie(name, value, formCookie) : ` : "";
|
|
426
485
|
const cookieLine = `${rawCookieLine}${explodeCookieLine}${formCookieLine}const cookie = (Object.entries(cookies ?? {}) as Array<[string, unknown]>).filter(([, value]) => value != null)${explodeRaw || explodeForm ? `.flatMap(([name, value]) => (${explodeRaw}${explodeForm}[\`\${name}=\${${encodeCookie}}\`]))` : `.map(([name, value]) => \`\${name}=\${${encodeCookie}}\`)`}.join('; ')\n`;
|
|
427
|
-
const requestSchema =
|
|
428
|
-
const bodySchema = requestSchema &&
|
|
486
|
+
const requestSchema = getSchema(node);
|
|
487
|
+
const bodySchema = requestSchema && _kubb_ast.ref.getSchema(requestSchema);
|
|
429
488
|
const castBody = bodySchema && (["number", "integer"].includes(bodySchema.primitive ?? "") || ["unknown", "any"].includes(bodySchema.type));
|
|
430
489
|
const requestOptions = [
|
|
431
490
|
`method: ${_kubb_kit.text.quote(node.method)}`,
|
|
@@ -438,17 +497,12 @@ ${compositeLines} const text = value instanceof Date ? value.toISOString() : St
|
|
|
438
497
|
const requestCall = `${groups.cookies ? cookieLine : ""}return cy.request<${responseType}>({
|
|
439
498
|
${requestOptions.join(",\n ")}
|
|
440
499
|
}).then((res) => res.body) as ${returnType}`;
|
|
441
|
-
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.
|
|
500
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.Function, {
|
|
442
501
|
name,
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
export: true,
|
|
448
|
-
params: paramsSignature,
|
|
449
|
-
returnType,
|
|
450
|
-
children: requestCall
|
|
451
|
-
})
|
|
502
|
+
export: true,
|
|
503
|
+
params: paramsSignature,
|
|
504
|
+
returnType,
|
|
505
|
+
children: requestCall
|
|
452
506
|
});
|
|
453
507
|
}
|
|
454
508
|
// src/generators/cypressGenerator.tsx
|
|
@@ -461,7 +515,7 @@ const cypressGenerator = (0, _kubb_kit.defineGenerator)({
|
|
|
461
515
|
name: "cypress",
|
|
462
516
|
renderer: _kubb_renderer_jsx.jsxRenderer,
|
|
463
517
|
operation(node, ctx) {
|
|
464
|
-
if (!
|
|
518
|
+
if (!isOperation(node)) return null;
|
|
465
519
|
const { config, resolver, root } = ctx;
|
|
466
520
|
const { output, baseURL, group } = ctx.options;
|
|
467
521
|
const pluginTs = ctx.requirePlugin(_kubb_plugin_ts.pluginTsName);
|
|
@@ -531,7 +585,7 @@ var compatibility = {
|
|
|
531
585
|
"node": ">=22.22"
|
|
532
586
|
};
|
|
533
587
|
// package.json
|
|
534
|
-
var version = "5.3.
|
|
588
|
+
var version = "5.3.7";
|
|
535
589
|
// src/resolvers/resolverCypress.ts
|
|
536
590
|
/**
|
|
537
591
|
* Default resolver used by `@kubb/plugin-cypress`. Decides the names and file
|