@scalar/blocks 0.1.20 → 0.3.0
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/CHANGELOG.md +58 -0
- package/dist/code-example/components/CodeExample.vue.d.ts +3 -2
- package/dist/code-example/components/CodeExample.vue.d.ts.map +1 -1
- package/dist/code-example/components/CodeExample.vue.js +1 -1
- package/dist/code-example/components/CodeExample.vue.js.map +1 -1
- package/dist/code-example/components/CodeExample.vue.script.js +87 -82
- package/dist/code-example/components/CodeExample.vue.script.js.map +1 -1
- package/dist/code-example/components/HttpMethod.vue.d.ts +1 -2
- package/dist/code-example/components/HttpMethod.vue.d.ts.map +1 -1
- package/dist/code-example/components/HttpMethod.vue.js.map +1 -1
- package/dist/code-example/components/HttpMethod.vue.script.js +15 -11
- package/dist/code-example/components/HttpMethod.vue.script.js.map +1 -1
- package/dist/code-example/helpers/generate-client-options.d.ts +1 -0
- package/dist/code-example/helpers/generate-client-options.d.ts.map +1 -1
- package/dist/code-example/helpers/generate-client-options.js +7 -3
- package/dist/code-example/helpers/generate-client-options.js.map +1 -1
- package/dist/code-example/helpers/generate-code-snippet.d.ts +3 -4
- package/dist/code-example/helpers/generate-code-snippet.d.ts.map +1 -1
- package/dist/code-example/helpers/generate-code-snippet.js +24 -18
- package/dist/code-example/helpers/generate-code-snippet.js.map +1 -1
- package/dist/code-example/helpers/get-clients.d.ts.map +1 -1
- package/dist/code-example/helpers/get-clients.js +4 -2
- package/dist/code-example/helpers/get-clients.js.map +1 -1
- package/dist/code-example/helpers/operation-to-har/operation-to-har.d.ts +1 -2
- package/dist/code-example/helpers/operation-to-har/operation-to-har.d.ts.map +1 -1
- package/dist/code-example/helpers/operation-to-har/operation-to-har.js +25 -12
- package/dist/code-example/helpers/operation-to-har/operation-to-har.js.map +1 -1
- package/dist/code-example/helpers/operation-to-har/process-body.d.ts.map +1 -1
- package/dist/code-example/helpers/operation-to-har/process-body.js +89 -68
- package/dist/code-example/helpers/operation-to-har/process-body.js.map +1 -1
- package/dist/code-example/helpers/operation-to-har/process-parameters.d.ts +4 -0
- package/dist/code-example/helpers/operation-to-har/process-parameters.d.ts.map +1 -1
- package/dist/code-example/helpers/operation-to-har/process-parameters.js +169 -83
- package/dist/code-example/helpers/operation-to-har/process-parameters.js.map +1 -1
- package/dist/code-example/mount.d.ts +3 -2
- package/dist/code-example/mount.d.ts.map +1 -1
- package/dist/code-example/mount.js +32 -28
- package/dist/code-example/mount.js.map +1 -1
- package/dist/constants.js +1 -1
- package/dist/style.css +1 -1
- package/dist/vue-styles.css +1 -1
- package/package.json +6 -6
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
import { getResolvedRefDeep as e } from "@scalar/workspace-store/helpers/get-resolved-ref-deep";
|
|
2
2
|
import { getResolvedRef as t, mergeSiblingReferences as n } from "@scalar/workspace-store/helpers/get-resolved-ref";
|
|
3
|
-
import {
|
|
4
|
-
import { json2xml as
|
|
5
|
-
import {
|
|
3
|
+
import { buildRequestBody as r, coerceLeafValueToSchemaType as i, getExample as a, getExampleFromSchema as o, needsMultipartEncoding as s, resolveLeafSchema as c, serializeFormPropertyWithEncoding as l, serializeMultipartArray as u, serializeMultipartBody as d } from "@scalar/workspace-store/request-example";
|
|
4
|
+
import { json2xml as f } from "@scalar/helpers/file/json2xml";
|
|
5
|
+
import { getExampleValue as p, getExplicitExampleText as m } from "@scalar/workspace-store/helpers/get-example-value";
|
|
6
|
+
import { serializeStreamExample as h } from "@scalar/workspace-store/helpers/serialize-stream-example";
|
|
7
|
+
import { unpackProxyObject as g } from "@scalar/workspace-store/helpers/unpack-proxy";
|
|
6
8
|
//#region src/code-example/helpers/operation-to-har/process-body.ts
|
|
7
|
-
var
|
|
8
|
-
let
|
|
9
|
-
for (let [
|
|
10
|
-
if (
|
|
11
|
-
let
|
|
12
|
-
if (
|
|
13
|
-
let t = Array.isArray(e) ?
|
|
9
|
+
var _ = (e, t, n, r = !1, a) => {
|
|
10
|
+
let o = [], s = Array.isArray(e) ? e.filter((e) => !e.isDisabled).map((e) => [String(e.name), e.value]) : Object.entries(e);
|
|
11
|
+
for (let [d, f] of s) {
|
|
12
|
+
if (f == null) continue;
|
|
13
|
+
let s = n ? void 0 : t?.[d];
|
|
14
|
+
if (r && !n) {
|
|
15
|
+
let t = Array.isArray(e) ? i(f, c(a, [d])) : f, n = u(d, Array.isArray(t) ? t : f, s);
|
|
14
16
|
if (n) {
|
|
15
|
-
for (let e of n)
|
|
17
|
+
for (let e of n) o.push({
|
|
16
18
|
name: e.key,
|
|
17
19
|
value: e.value instanceof File ? `@${e.value.name}` : e.value,
|
|
18
20
|
...e.contentType ? { contentType: e.contentType } : {}
|
|
@@ -20,89 +22,108 @@ var d = (e, t, n, i = !1, a) => {
|
|
|
20
22
|
continue;
|
|
21
23
|
}
|
|
22
24
|
}
|
|
23
|
-
let
|
|
24
|
-
if (
|
|
25
|
+
let p = s && (s.style !== void 0 || s.explode !== void 0 || s.allowReserved !== void 0), m = p || !r ? void 0 : s?.contentType, h = n ? null : l(d, f, s);
|
|
26
|
+
if (h) for (let e of h) o.push({
|
|
25
27
|
name: e.key,
|
|
26
28
|
value: e.value
|
|
27
29
|
});
|
|
28
|
-
else if (
|
|
29
|
-
let e =
|
|
30
|
-
|
|
31
|
-
name:
|
|
30
|
+
else if (f instanceof File) {
|
|
31
|
+
let e = g(f);
|
|
32
|
+
o.push({
|
|
33
|
+
name: d,
|
|
32
34
|
value: `@${e.name}`,
|
|
33
|
-
...
|
|
35
|
+
...m ? { contentType: m } : {}
|
|
34
36
|
});
|
|
35
|
-
} else if (
|
|
36
|
-
name:
|
|
37
|
-
value: JSON.stringify(
|
|
38
|
-
contentType:
|
|
37
|
+
} else if (m && typeof f == "object") o.push({
|
|
38
|
+
name: d,
|
|
39
|
+
value: JSON.stringify(g(f)),
|
|
40
|
+
contentType: m
|
|
39
41
|
});
|
|
40
|
-
else if (
|
|
41
|
-
name:
|
|
42
|
-
value: JSON.stringify(
|
|
42
|
+
else if (r && !n && !p && typeof f == "object" && !Array.isArray(f)) o.push({
|
|
43
|
+
name: d,
|
|
44
|
+
value: JSON.stringify(g(f)),
|
|
43
45
|
contentType: "application/json"
|
|
44
46
|
});
|
|
45
|
-
else if (Array.isArray(
|
|
46
|
-
let t =
|
|
47
|
-
|
|
48
|
-
name:
|
|
47
|
+
else if (Array.isArray(f)) for (let e of f) if (e instanceof File) {
|
|
48
|
+
let t = g(e);
|
|
49
|
+
o.push({
|
|
50
|
+
name: d,
|
|
49
51
|
value: `@${t.name}`
|
|
50
52
|
});
|
|
51
|
-
} else
|
|
52
|
-
name:
|
|
53
|
+
} else o.push({
|
|
54
|
+
name: d,
|
|
53
55
|
value: String(e)
|
|
54
56
|
});
|
|
55
|
-
else if (typeof
|
|
56
|
-
let e =
|
|
57
|
-
for (let t of e)
|
|
58
|
-
name: `${
|
|
57
|
+
else if (typeof f == "object") {
|
|
58
|
+
let e = _(f, void 0, d);
|
|
59
|
+
for (let t of e) o.push({
|
|
60
|
+
name: `${d}.${t.name}`,
|
|
59
61
|
value: t.value
|
|
60
62
|
});
|
|
61
|
-
} else
|
|
62
|
-
name:
|
|
63
|
-
value: String(
|
|
64
|
-
...
|
|
63
|
+
} else o.push({
|
|
64
|
+
name: d,
|
|
65
|
+
value: String(f),
|
|
66
|
+
...m ? { contentType: m } : {}
|
|
65
67
|
});
|
|
66
68
|
}
|
|
67
|
-
return
|
|
68
|
-
},
|
|
69
|
-
let
|
|
70
|
-
if (
|
|
71
|
-
let e =
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
69
|
+
return o;
|
|
70
|
+
}, v = ({ requestBody: i, contentType: c, example: l, requestBodyCompositionSelection: u }) => {
|
|
71
|
+
let v = c || Object.keys(i.content)[0] || "", y = v === "other" ? "" : v, b = (e) => `@${g(e).name || "filename"}`, x = i.content[v]?.encoding, S = i.content[v];
|
|
72
|
+
if (S && s(v, S)) {
|
|
73
|
+
let e = l ?? Object.keys(S.examples ?? {})[0] ?? "default", t = r({
|
|
74
|
+
...i,
|
|
75
|
+
"x-scalar-selected-content-type": { [e]: v }
|
|
76
|
+
}, e, u);
|
|
77
|
+
if (t?.mode === "multipart") {
|
|
78
|
+
let e = d(t.value, t.contentType);
|
|
79
|
+
return {
|
|
80
|
+
mimeType: e.contentType,
|
|
81
|
+
text: e.chunks.map((e) => e instanceof File ? b(e) : e instanceof Blob ? "BINARY" : String(e)).join("")
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
let C = v === "multipart/form-data" || v === "application/x-www-form-urlencoded", w = v === "application/xml", T = p(a(i, l, c)), E = m(T, v);
|
|
86
|
+
if (E !== void 0) return {
|
|
87
|
+
mimeType: y,
|
|
88
|
+
text: E
|
|
89
|
+
};
|
|
90
|
+
let D = T?.value;
|
|
91
|
+
if (D !== void 0) {
|
|
92
|
+
let e = typeof D == "object" && D ? g(D) : D;
|
|
93
|
+
return C && typeof e == "object" && e ? {
|
|
94
|
+
mimeType: y,
|
|
95
|
+
params: _(e, x, void 0, v === "multipart/form-data", t(i.content[v]?.schema, n))
|
|
96
|
+
} : w && typeof e == "object" && e ? {
|
|
97
|
+
mimeType: y,
|
|
98
|
+
text: f(e)
|
|
78
99
|
} : e instanceof File ? {
|
|
79
|
-
mimeType:
|
|
80
|
-
text:
|
|
100
|
+
mimeType: y,
|
|
101
|
+
text: b(e)
|
|
81
102
|
} : {
|
|
82
|
-
mimeType:
|
|
83
|
-
text: typeof e == "string" ? e : JSON.stringify(e)
|
|
103
|
+
mimeType: y,
|
|
104
|
+
text: typeof e == "string" ? e : h(e, v, !1) ?? JSON.stringify(e)
|
|
84
105
|
};
|
|
85
106
|
}
|
|
86
|
-
let
|
|
87
|
-
if (
|
|
88
|
-
let t =
|
|
89
|
-
compositionSelection:
|
|
107
|
+
let O = i.content[v], k = t(O?.schema ?? O?.itemSchema);
|
|
108
|
+
if (k !== void 0) {
|
|
109
|
+
let t = o(e(k), {
|
|
110
|
+
compositionSelection: u,
|
|
90
111
|
mode: "write",
|
|
91
|
-
xml:
|
|
112
|
+
xml: w
|
|
92
113
|
}, { schemaPath: ["requestBody"] });
|
|
93
|
-
if (t !== void 0) return
|
|
94
|
-
mimeType:
|
|
95
|
-
params:
|
|
96
|
-
} :
|
|
97
|
-
mimeType:
|
|
98
|
-
text:
|
|
114
|
+
if (t !== void 0) return C && typeof t == "object" && t ? {
|
|
115
|
+
mimeType: y,
|
|
116
|
+
params: _(t, x, void 0, v === "multipart/form-data")
|
|
117
|
+
} : w && typeof t == "object" && t ? {
|
|
118
|
+
mimeType: y,
|
|
119
|
+
text: f(t)
|
|
99
120
|
} : {
|
|
100
|
-
mimeType:
|
|
101
|
-
text: typeof t == "string" ? t : JSON.stringify(t)
|
|
121
|
+
mimeType: y,
|
|
122
|
+
text: h(t, v, O?.schema === void 0) ?? (typeof t == "string" ? t : JSON.stringify(t))
|
|
102
123
|
};
|
|
103
124
|
}
|
|
104
125
|
};
|
|
105
126
|
//#endregion
|
|
106
|
-
export {
|
|
127
|
+
export { v as processBody };
|
|
107
128
|
|
|
108
129
|
//# sourceMappingURL=process-body.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"process-body.js","names":[],"sources":["../../../../src/code-example/helpers/operation-to-har/process-body.ts"],"sourcesContent":["import { json2xml } from '@scalar/helpers/file/json2xml'\nimport { getResolvedRef, mergeSiblingReferences } from '@scalar/workspace-store/helpers/get-resolved-ref'\nimport { getResolvedRefDeep } from '@scalar/workspace-store/helpers/get-resolved-ref-deep'\nimport { unpackProxyObject } from '@scalar/workspace-store/helpers/unpack-proxy'\nimport {\n coerceLeafValueToSchemaType,\n getExample,\n getExampleFromSchema,\n resolveLeafSchema,\n serializeFormPropertyWithEncoding,\n serializeMultipartArray,\n} from '@scalar/workspace-store/request-example'\nimport type {\n MediaTypeObject,\n RequestBodyObject,\n SchemaObject,\n} from '@scalar/workspace-store/schemas/v3.2/strict/openapi-document'\nimport type { Param, PostData } from 'har-format'\n\nimport type { OperationToHarProps } from './operation-to-har'\n\ntype ProcessBodyProps = Pick<OperationToHarProps, 'contentType' | 'example' | 'requestBodyCompositionSelection'> & {\n requestBody: RequestBodyObject\n}\n\ntype MultipartEncodingMap = MediaTypeObject['encoding']\n\n/**\n * Converts a form-data body into HAR `Param[]` entries for `multipart/form-data`\n * and `application/x-www-form-urlencoded` requests.\n *\n * Per OpenAPI 3.1.x Encoding Object, each property's serialization is governed\n * by an optional Encoding entry. When `style` / `explode` / `allowReserved` is\n * set, the value is serialized RFC6570-style and `contentType` is ignored.\n * Multipart arrays apply the encoding to each item and repeat the property name.\n * Object items default to JSON, and uploaded files retain their filename references.\n *\n * @param obj - The form-data payload, either an object keyed by property name\n * or a HAR-style `{ name, value, isDisabled }[]` array.\n * @param encoding - The `encoding` map from the Media Type Object, if any.\n * Used only at the top level (`parentKey` undefined).\n * @param parentKey - When set, we are flattening a nested object inside another\n * property; encoding is ignored and keys are joined with `.` (legacy default).\n * @param isMultipart - True for `multipart/form-data`. Gates the spec defaults\n * for object properties and array items. Urlencoded bodies retain their existing\n * serialization and flattening behavior.\n */\nconst objectToFormParams = (\n obj: object | { name: string; value: unknown; isDisabled: boolean }[],\n encoding?: MultipartEncodingMap,\n parentKey?: string,\n isMultipart = false,\n schema?: SchemaObject,\n): Param[] => {\n const params: Param[] = []\n\n /** Ensure we do not include disabled items */\n const entries: [string, unknown][] = Array.isArray(obj)\n ? obj.filter((item) => !item.isDisabled).map((item) => [String(item.name), item.value])\n : Object.entries(obj)\n\n for (const [key, value] of entries) {\n if (value === undefined || value === null) {\n continue\n }\n\n const partEncoding = parentKey ? undefined : encoding?.[key]\n\n if (isMultipart && !parentKey) {\n const restored = Array.isArray(obj) ? coerceLeafValueToSchemaType(value, resolveLeafSchema(schema, [key])) : value\n const arrayParts = serializeMultipartArray(key, Array.isArray(restored) ? restored : value, partEncoding)\n if (arrayParts) {\n for (const part of arrayParts) {\n params.push({\n name: part.key,\n value: part.value instanceof File ? `@${part.value.name}` : part.value,\n ...(part.contentType ? { contentType: part.contentType } : {}),\n })\n }\n continue\n }\n }\n /**\n * Per OpenAPI 3.1.1: when style, explode, or allowReserved is explicitly set on the\n * encoding entry, contentType (implicit or explicit) is ignored and the value is\n * serialized as if it were a query-style parameter.\n */\n const hasFormStyle =\n partEncoding &&\n (partEncoding.style !== undefined ||\n partEncoding.explode !== undefined ||\n partEncoding.allowReserved !== undefined)\n /**\n * Per OAS 3.1.x Encoding Object: `contentType` SHALL be ignored if the request body\n * media type is not a multipart. For `application/x-www-form-urlencoded` we still\n * honor `style`/`explode`/`allowReserved` (handled above), but a `contentType`-only\n * entry has no effect — values fall through to the dotted-key flattening default.\n */\n const explicitContentType = hasFormStyle || !isMultipart ? undefined : partEncoding?.contentType\n\n /**\n * When the encoding sets style/explode/allowReserved, serialize the value RFC6570-style\n * (bracket/exploded notation) and ignore contentType. See `serializeFormPropertyWithEncoding`\n * for the per-style rules; it returns null for primitives, Files, and arrays of Files, which\n * then fall through to the dedicated branches below. HAR represents multipart and urlencoded\n * the same way via `PostData.params`, so the resulting parts work for both.\n */\n const styleParams = parentKey ? null : serializeFormPropertyWithEncoding(key, value, partEncoding)\n if (styleParams) {\n for (const param of styleParams) {\n params.push({ name: param.key, value: param.value })\n }\n } else if (value instanceof File) {\n /**\n * File values render as `@filename` references, the conventional cURL syntax for an\n * attached file. Picked up by snippet renderers downstream (e.g. `--form 'x=@file.png'`).\n */\n const file = unpackProxyObject(value)\n params.push({\n name: key,\n value: `@${file.name}`,\n ...(explicitContentType ? { contentType: explicitContentType } : {}),\n })\n } else if (explicitContentType && typeof value === 'object') {\n /**\n * Per OAS 3.1.x Encoding Object: an explicit `encoding[key].contentType` on a\n * complex value overrides the default and emits a single part containing the value\n * JSON-stringified into that media type. Only reachable when style/explode/allowReserved\n * are unset (otherwise contentType is ignored — see the style branch above).\n */\n params.push({\n name: key,\n value: JSON.stringify(unpackProxyObject(value)),\n contentType: explicitContentType,\n })\n } else if (isMultipart && !parentKey && !hasFormStyle && typeof value === 'object' && !Array.isArray(value)) {\n /**\n * Per OpenAPI 3.x: a top-level multipart property whose value is an object (and not a File)\n * defaults to a single part encoded as application/json, rather than being flattened\n * into multiple parts with dotted keys.\n */\n params.push({\n name: key,\n value: JSON.stringify(unpackProxyObject(value)),\n contentType: 'application/json',\n })\n } else if (Array.isArray(value)) {\n for (const item of value) {\n if (item instanceof File) {\n const file = unpackProxyObject(item)\n params.push({ name: key, value: `@${file.name}` })\n } else {\n params.push({ name: key, value: String(item) })\n }\n }\n } else if (typeof value === 'object') {\n /**\n * Legacy fallback: flatten nested objects into dotted-key parts. Reached when the\n * multipart JSON-default branches above don't apply — i.e. either inside a recursive\n * call (`parentKey` set) or on `application/x-www-form-urlencoded` bodies without an\n * explicit encoding entry. Strict OAS 3.1.x would use `style: form, explode: true` as\n * the urlencoded default (inner keys become top-level params); we keep dotted keys for\n * backwards compatibility with consumers that already parse this shape.\n */\n const nestedParams = objectToFormParams(value, undefined, key)\n\n for (const param of nestedParams) {\n params.push({ name: `${key}.${param.name}`, value: param.value })\n }\n } else {\n params.push({\n name: key,\n value: String(value),\n ...(explicitContentType ? { contentType: explicitContentType } : {}),\n })\n }\n }\n\n return params\n}\n\n/**\n * Processes the request body and returns the processed data\n * Returns undefined if no example is found\n */\nexport const processBody = ({\n requestBody,\n contentType,\n example,\n requestBodyCompositionSelection,\n}: ProcessBodyProps): PostData | undefined => {\n const _contentType = contentType || Object.keys(requestBody.content)[0] || ''\n /** Empty when \"Other\" is selected so snippets do not inject `Content-Type: other`. */\n const harMimeType = _contentType === 'other' ? '' : _contentType\n const formatBinaryFile = (file: File) => {\n const unwrappedFile = unpackProxyObject(file)\n return `@${unwrappedFile.name || 'filename'}`\n }\n const encoding = requestBody.content[_contentType]?.encoding\n\n // Check if this is a form data content type\n const isFormData = _contentType === 'multipart/form-data' || _contentType === 'application/x-www-form-urlencoded'\n\n // Check if this is an XML content type\n const isXml = _contentType === 'application/xml'\n\n // Get the example value\n const _example = getExample(requestBody, example, contentType)?.value\n\n // Return the provided top level example\n if (typeof _example !== 'undefined') {\n const exampleValue = _example !== null && typeof _example === 'object' ? unpackProxyObject(_example) : _example\n\n if (isFormData && typeof exampleValue === 'object' && exampleValue !== null) {\n return {\n mimeType: harMimeType,\n params: objectToFormParams(\n exampleValue,\n encoding,\n undefined,\n _contentType === 'multipart/form-data',\n getResolvedRef(requestBody.content[_contentType]?.schema, mergeSiblingReferences),\n ),\n }\n }\n\n if (isXml && typeof exampleValue === 'object' && exampleValue !== null) {\n return {\n mimeType: harMimeType,\n text: json2xml(exampleValue),\n }\n }\n\n if (exampleValue instanceof File) {\n return {\n mimeType: harMimeType,\n text: formatBinaryFile(exampleValue),\n }\n }\n\n return {\n mimeType: harMimeType,\n text: typeof exampleValue === 'string' ? exampleValue : JSON.stringify(exampleValue),\n }\n }\n\n // Try to extract examples from the schema\n const contentSchema = getResolvedRef(requestBody.content[_contentType]?.schema)\n if (typeof contentSchema !== 'undefined') {\n const resolvedContentSchema = getResolvedRefDeep(contentSchema) as SchemaObject\n const extractedExample = getExampleFromSchema(\n resolvedContentSchema,\n {\n compositionSelection: requestBodyCompositionSelection,\n mode: 'write',\n xml: isXml,\n },\n {\n schemaPath: ['requestBody'],\n },\n )\n\n if (extractedExample !== undefined) {\n if (isFormData && typeof extractedExample === 'object' && extractedExample !== null) {\n return {\n mimeType: harMimeType,\n params: objectToFormParams(extractedExample, encoding, undefined, _contentType === 'multipart/form-data'),\n }\n }\n\n if (isXml && typeof extractedExample === 'object' && extractedExample !== null) {\n return {\n mimeType: harMimeType,\n text: json2xml(extractedExample),\n }\n }\n\n return {\n mimeType: harMimeType,\n text: typeof extractedExample === 'string' ? extractedExample : JSON.stringify(extractedExample),\n }\n }\n }\n\n return undefined\n}\n"],"mappings":";;;;;;AA+CA,IAAM,KACJ,GACA,GACA,GACA,IAAc,IACd,MACY;CACZ,IAAM,IAAkB,CAAC,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IAAM,IAAoC,OAAO,KAAa,YAAzC,IAAoD,EAAkB,CAAQ,IAAI;EA6BvG,OA3BI,KAAc,OAAO,KAAiB,YAAY,IAC7C;GACL,UAAU;GACV,QAAQ,EACN,GACA,GACA,KAAA,GACA,MAAiB,uBACjB,EAAe,EAAY,QAAQ,EAAa,EAAE,QAAQ,CAAsB,CAClF;EACF,IAGE,KAAS,OAAO,KAAiB,YAAY,IACxC;GACL,UAAU;GACV,MAAM,EAAS,CAAY;EAC7B,IAGE,aAAwB,OACnB;GACL,UAAU;GACV,MAAM,EAAiB,CAAY;EACrC,IAGK;GACL,UAAU;GACV,MAAM,OAAO,KAAiB,WAAW,IAAe,KAAK,UAAU,CAAY;EACrF;CACF;CAGA,IAAM,IAAgB,EAAe,EAAY,QAAQ,EAAa,EAAE,MAAM;CAC9E,IAAW,MAAkB,QAAa;EAExC,IAAM,IAAmB,EADK,EAAmB,CAE/C,GACA;GACE,sBAAsB;GACtB,MAAM;GACN,KAAK;EACP,GACA,EACE,YAAY,CAAC,aAAa,EAC5B,CACF;EAEA,IAAI,MAAqB,KAAA,GAevB,OAdI,KAAc,OAAO,KAAqB,YAAY,IACjD;GACL,UAAU;GACV,QAAQ,EAAmB,GAAkB,GAAU,KAAA,GAAW,MAAiB,qBAAqB;EAC1G,IAGE,KAAS,OAAO,KAAqB,YAAY,IAC5C;GACL,UAAU;GACV,MAAM,EAAS,CAAgB;EACjC,IAGK;GACL,UAAU;GACV,MAAM,OAAO,KAAqB,WAAW,IAAmB,KAAK,UAAU,CAAgB;EACjG;CAEJ;AAGF"}
|
|
1
|
+
{"version":3,"file":"process-body.js","names":[],"sources":["../../../../src/code-example/helpers/operation-to-har/process-body.ts"],"sourcesContent":["import { json2xml } from '@scalar/helpers/file/json2xml'\nimport { getExampleValue, getExplicitExampleText } from '@scalar/workspace-store/helpers/get-example-value'\nimport { getResolvedRef, mergeSiblingReferences } from '@scalar/workspace-store/helpers/get-resolved-ref'\nimport { getResolvedRefDeep } from '@scalar/workspace-store/helpers/get-resolved-ref-deep'\nimport { serializeStreamExample } from '@scalar/workspace-store/helpers/serialize-stream-example'\nimport { unpackProxyObject } from '@scalar/workspace-store/helpers/unpack-proxy'\nimport {\n buildRequestBody,\n coerceLeafValueToSchemaType,\n getExample,\n getExampleFromSchema,\n needsMultipartEncoding,\n resolveLeafSchema,\n serializeFormPropertyWithEncoding,\n serializeMultipartArray,\n serializeMultipartBody,\n} from '@scalar/workspace-store/request-example'\nimport type {\n MediaTypeObject,\n RequestBodyObject,\n SchemaObject,\n} from '@scalar/workspace-store/schemas/v3.2/strict/openapi-document'\nimport type { Param, PostData } from 'har-format'\n\nimport type { OperationToHarProps } from './operation-to-har'\n\ntype ProcessBodyProps = Pick<OperationToHarProps, 'contentType' | 'example' | 'requestBodyCompositionSelection'> & {\n requestBody: RequestBodyObject\n}\n\ntype MultipartEncodingMap = MediaTypeObject['encoding']\n\n/**\n * Converts a form-data body into HAR `Param[]` entries for `multipart/form-data`\n * and `application/x-www-form-urlencoded` requests.\n *\n * Per OpenAPI 3.1.x Encoding Object, each property's serialization is governed\n * by an optional Encoding entry. When `style` / `explode` / `allowReserved` is\n * set, the value is serialized RFC6570-style and `contentType` is ignored.\n * Multipart arrays apply the encoding to each item and repeat the property name.\n * Object items default to JSON, and uploaded files retain their filename references.\n *\n * @param obj - The form-data payload, either an object keyed by property name\n * or a HAR-style `{ name, value, isDisabled }[]` array.\n * @param encoding - The `encoding` map from the Media Type Object, if any.\n * Used only at the top level (`parentKey` undefined).\n * @param parentKey - When set, we are flattening a nested object inside another\n * property; encoding is ignored and keys are joined with `.` (legacy default).\n * @param isMultipart - True for `multipart/form-data`. Gates the spec defaults\n * for object properties and array items. Urlencoded bodies retain their existing\n * serialization and flattening behavior.\n */\nconst objectToFormParams = (\n obj: object | { name: string; value: unknown; isDisabled: boolean }[],\n encoding?: MultipartEncodingMap,\n parentKey?: string,\n isMultipart = false,\n schema?: SchemaObject,\n): Param[] => {\n const params: Param[] = []\n\n /** Ensure we do not include disabled items */\n const entries: [string, unknown][] = Array.isArray(obj)\n ? obj.filter((item) => !item.isDisabled).map((item) => [String(item.name), item.value])\n : Object.entries(obj)\n\n for (const [key, value] of entries) {\n if (value === undefined || value === null) {\n continue\n }\n\n const partEncoding = parentKey ? undefined : encoding?.[key]\n\n if (isMultipart && !parentKey) {\n const restored = Array.isArray(obj) ? coerceLeafValueToSchemaType(value, resolveLeafSchema(schema, [key])) : value\n const arrayParts = serializeMultipartArray(key, Array.isArray(restored) ? restored : value, partEncoding)\n if (arrayParts) {\n for (const part of arrayParts) {\n params.push({\n name: part.key,\n value: part.value instanceof File ? `@${part.value.name}` : part.value,\n ...(part.contentType ? { contentType: part.contentType } : {}),\n })\n }\n continue\n }\n }\n /**\n * Per OpenAPI 3.1.1: when style, explode, or allowReserved is explicitly set on the\n * encoding entry, contentType (implicit or explicit) is ignored and the value is\n * serialized as if it were a query-style parameter.\n */\n const hasFormStyle =\n partEncoding &&\n (partEncoding.style !== undefined ||\n partEncoding.explode !== undefined ||\n partEncoding.allowReserved !== undefined)\n /**\n * Per OAS 3.1.x Encoding Object: `contentType` SHALL be ignored if the request body\n * media type is not a multipart. For `application/x-www-form-urlencoded` we still\n * honor `style`/`explode`/`allowReserved` (handled above), but a `contentType`-only\n * entry has no effect — values fall through to the dotted-key flattening default.\n */\n const explicitContentType = hasFormStyle || !isMultipart ? undefined : partEncoding?.contentType\n\n /**\n * When the encoding sets style/explode/allowReserved, serialize the value RFC6570-style\n * (bracket/exploded notation) and ignore contentType. See `serializeFormPropertyWithEncoding`\n * for the per-style rules; it returns null for primitives, Files, and arrays of Files, which\n * then fall through to the dedicated branches below. HAR represents multipart and urlencoded\n * the same way via `PostData.params`, so the resulting parts work for both.\n */\n const styleParams = parentKey ? null : serializeFormPropertyWithEncoding(key, value, partEncoding)\n if (styleParams) {\n for (const param of styleParams) {\n params.push({ name: param.key, value: param.value })\n }\n } else if (value instanceof File) {\n /**\n * File values render as `@filename` references, the conventional cURL syntax for an\n * attached file. Picked up by snippet renderers downstream (e.g. `--form 'x=@file.png'`).\n */\n const file = unpackProxyObject(value)\n params.push({\n name: key,\n value: `@${file.name}`,\n ...(explicitContentType ? { contentType: explicitContentType } : {}),\n })\n } else if (explicitContentType && typeof value === 'object') {\n /**\n * Per OAS 3.1.x Encoding Object: an explicit `encoding[key].contentType` on a\n * complex value overrides the default and emits a single part containing the value\n * JSON-stringified into that media type. Only reachable when style/explode/allowReserved\n * are unset (otherwise contentType is ignored — see the style branch above).\n */\n params.push({\n name: key,\n value: JSON.stringify(unpackProxyObject(value)),\n contentType: explicitContentType,\n })\n } else if (isMultipart && !parentKey && !hasFormStyle && typeof value === 'object' && !Array.isArray(value)) {\n /**\n * Per OpenAPI 3.x: a top-level multipart property whose value is an object (and not a File)\n * defaults to a single part encoded as application/json, rather than being flattened\n * into multiple parts with dotted keys.\n */\n params.push({\n name: key,\n value: JSON.stringify(unpackProxyObject(value)),\n contentType: 'application/json',\n })\n } else if (Array.isArray(value)) {\n for (const item of value) {\n if (item instanceof File) {\n const file = unpackProxyObject(item)\n params.push({ name: key, value: `@${file.name}` })\n } else {\n params.push({ name: key, value: String(item) })\n }\n }\n } else if (typeof value === 'object') {\n /**\n * Legacy fallback: flatten nested objects into dotted-key parts. Reached when the\n * multipart JSON-default branches above don't apply — i.e. either inside a recursive\n * call (`parentKey` set) or on `application/x-www-form-urlencoded` bodies without an\n * explicit encoding entry. Strict OAS 3.1.x would use `style: form, explode: true` as\n * the urlencoded default (inner keys become top-level params); we keep dotted keys for\n * backwards compatibility with consumers that already parse this shape.\n */\n const nestedParams = objectToFormParams(value, undefined, key)\n\n for (const param of nestedParams) {\n params.push({ name: `${key}.${param.name}`, value: param.value })\n }\n } else {\n params.push({\n name: key,\n value: String(value),\n ...(explicitContentType ? { contentType: explicitContentType } : {}),\n })\n }\n }\n\n return params\n}\n\n/**\n * Processes the request body and returns the processed data\n * Returns undefined if no example is found\n */\nexport const processBody = ({\n requestBody,\n contentType,\n example,\n requestBodyCompositionSelection,\n}: ProcessBodyProps): PostData | undefined => {\n const _contentType = contentType || Object.keys(requestBody.content)[0] || ''\n /** Empty when \"Other\" is selected so snippets do not inject `Content-Type: other`. */\n const harMimeType = _contentType === 'other' ? '' : _contentType\n const formatBinaryFile = (file: File) => {\n const unwrappedFile = unpackProxyObject(file)\n return `@${unwrappedFile.name || 'filename'}`\n }\n const encoding = requestBody.content[_contentType]?.encoding\n\n const media = requestBody.content[_contentType]\n if (media && needsMultipartEncoding(_contentType, media)) {\n const exampleName = example ?? Object.keys(media.examples ?? {})[0] ?? 'default'\n const body = buildRequestBody(\n { ...requestBody, 'x-scalar-selected-content-type': { [exampleName]: _contentType } },\n exampleName,\n requestBodyCompositionSelection,\n )\n if (body?.mode === 'multipart') {\n const encoded = serializeMultipartBody(body.value, body.contentType)\n return {\n mimeType: encoded.contentType,\n // HAR text cannot embed file bytes synchronously. Keep visible file placeholders.\n text: encoded.chunks\n .map((chunk) =>\n chunk instanceof File ? formatBinaryFile(chunk) : chunk instanceof Blob ? 'BINARY' : String(chunk),\n )\n .join(''),\n }\n }\n }\n\n // Check if this is a form data content type\n const isFormData = _contentType === 'multipart/form-data' || _contentType === 'application/x-www-form-urlencoded'\n\n // Check if this is an XML content type\n const isXml = _contentType === 'application/xml'\n\n // Get the example value\n const selected = getExampleValue(getExample(requestBody, example, contentType))\n const explicitText = getExplicitExampleText(selected, _contentType)\n if (explicitText !== undefined) {\n return { mimeType: harMimeType, text: explicitText }\n }\n const _example = selected?.value\n\n // Return the provided top level example\n if (typeof _example !== 'undefined') {\n const exampleValue = _example !== null && typeof _example === 'object' ? unpackProxyObject(_example) : _example\n\n if (isFormData && typeof exampleValue === 'object' && exampleValue !== null) {\n return {\n mimeType: harMimeType,\n params: objectToFormParams(\n exampleValue,\n encoding,\n undefined,\n _contentType === 'multipart/form-data',\n getResolvedRef(requestBody.content[_contentType]?.schema, mergeSiblingReferences),\n ),\n }\n }\n\n if (isXml && typeof exampleValue === 'object' && exampleValue !== null) {\n return {\n mimeType: harMimeType,\n text: json2xml(exampleValue),\n }\n }\n\n if (exampleValue instanceof File) {\n return {\n mimeType: harMimeType,\n text: formatBinaryFile(exampleValue),\n }\n }\n\n return {\n mimeType: harMimeType,\n text:\n typeof exampleValue === 'string'\n ? exampleValue\n : (serializeStreamExample(exampleValue, _contentType, false) ?? JSON.stringify(exampleValue)),\n }\n }\n\n // Try to extract examples from the schema\n const mediaType = requestBody.content[_contentType]\n const contentSchema = getResolvedRef(mediaType?.schema ?? mediaType?.itemSchema)\n if (typeof contentSchema !== 'undefined') {\n const resolvedContentSchema = getResolvedRefDeep(contentSchema) as SchemaObject\n const extractedExample = getExampleFromSchema(\n resolvedContentSchema,\n {\n compositionSelection: requestBodyCompositionSelection,\n mode: 'write',\n xml: isXml,\n },\n {\n schemaPath: ['requestBody'],\n },\n )\n\n if (extractedExample !== undefined) {\n if (isFormData && typeof extractedExample === 'object' && extractedExample !== null) {\n return {\n mimeType: harMimeType,\n params: objectToFormParams(extractedExample, encoding, undefined, _contentType === 'multipart/form-data'),\n }\n }\n\n if (isXml && typeof extractedExample === 'object' && extractedExample !== null) {\n return {\n mimeType: harMimeType,\n text: json2xml(extractedExample),\n }\n }\n\n return {\n mimeType: harMimeType,\n text:\n serializeStreamExample(extractedExample, _contentType, mediaType?.schema === undefined) ??\n (typeof extractedExample === 'string' ? extractedExample : JSON.stringify(extractedExample)),\n }\n }\n }\n\n return undefined\n}\n"],"mappings":";;;;;;;;AAoDA,IAAM,KACJ,GACA,GACA,GACA,IAAc,IACd,MACY;CACZ,IAAM,IAAkB,CAAC,GAGnB,IAA+B,MAAM,QAAQ,CAAG,IAClD,EAAI,QAAQ,MAAS,CAAC,EAAK,UAAU,CAAC,CAAC,KAAK,MAAS,CAAC,OAAO,EAAK,IAAI,GAAG,EAAK,KAAK,CAAC,IACpF,OAAO,QAAQ,CAAG;CAEtB,KAAK,IAAM,CAAC,GAAK,MAAU,GAAS;EAClC,IAAI,K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IAGE,KAAS,OAAO,KAAqB,YAAY,IAC5C;GACL,UAAU;GACV,MAAM,EAAS,CAAgB;EACjC,IAGK;GACL,UAAU;GACV,MACE,EAAuB,GAAkB,GAAc,GAAW,WAAW,KAAA,CAAS,MACrF,OAAO,KAAqB,WAAW,IAAmB,KAAK,UAAU,CAAgB;EAC9F;CAEJ;AAGF"}
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import type { OperationObject } from '@scalar/workspace-store/schemas/v3.2/strict/openapi-document';
|
|
2
2
|
import type { Request as HarRequest } from 'har-format';
|
|
3
3
|
type ProcessedParameters = {
|
|
4
|
+
/** Named serialized examples already embedded in the URL. */
|
|
5
|
+
hasSerializedQuery?: boolean;
|
|
4
6
|
url: string;
|
|
5
7
|
headers: HarRequest['headers'];
|
|
6
8
|
queryString: HarRequest['queryString'];
|
|
7
9
|
cookies: HarRequest['cookies'];
|
|
10
|
+
/** Whether serialized cookie-style values require a complete Cookie header. */
|
|
11
|
+
hasCookieStyleEntries: boolean;
|
|
8
12
|
};
|
|
9
13
|
/**
|
|
10
14
|
* Process OpenAPI parameters and return the updated properties.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"process-parameters.d.ts","sourceRoot":"","sources":["../../../../src/code-example/helpers/operation-to-har/process-parameters.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"process-parameters.d.ts","sourceRoot":"","sources":["../../../../src/code-example/helpers/operation-to-har/process-parameters.ts"],"names":[],"mappings":"AAoBA,OAAO,KAAK,EAAE,eAAe,EAAmB,MAAM,8DAA8D,CAAA;AACpH,OAAO,KAAK,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,YAAY,CAAA;AAEvD,KAAK,mBAAmB,GAAG;IACzB,6DAA6D;IAC7D,kBAAkB,CAAC,EAAE,OAAO,CAAA;IAC5B,GAAG,EAAE,MAAM,CAAA;IACX,OAAO,EAAE,UAAU,CAAC,SAAS,CAAC,CAAA;IAC9B,WAAW,EAAE,UAAU,CAAC,aAAa,CAAC,CAAA;IACtC,OAAO,EAAE,UAAU,CAAC,SAAS,CAAC,CAAA;IAC9B,+EAA+E;IAC/E,qBAAqB,EAAE,OAAO,CAAA;CAC/B,CAAA;AAyFD;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,GAAI,uDAK/B;IACD,UAAU,EAAE,UAAU,CAAA;IACtB,UAAU,EAAE,eAAe,CAAC,YAAY,CAAC,CAAA;IACzC,qCAAqC;IACrC,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC5B,gDAAgD;IAChD,eAAe,EAAE,OAAO,CAAA;CACzB,KAAG,mBA0NH,CAAA"}
|
|
@@ -1,149 +1,235 @@
|
|
|
1
1
|
import { getResolvedRef as e } from "@scalar/workspace-store/helpers/get-resolved-ref";
|
|
2
|
-
import { deSerializeParameter as t,
|
|
2
|
+
import { deSerializeParameter as t, getExampleFromSchema as n, isParamDisabled as r, serializeContentValue as i, serializeCookieStyle as a, serializeDeepObjectStyle as o, serializeFormStyle as s, serializeFormStyleForCookies as c, serializePipeDelimitedStyle as l, serializeSimpleStyle as u, serializeSpaceDelimitedStyle as d } from "@scalar/workspace-store/request-example";
|
|
3
3
|
import { isObjectLike as f } from "@scalar/helpers/object/is-object";
|
|
4
|
+
import { getParameterExample as p } from "@scalar/workspace-store/helpers/get-parameter-example";
|
|
5
|
+
import { getQuerystringParameter as m, serializeQuerystringParameter as h } from "@scalar/workspace-store/helpers/querystring-parameter";
|
|
4
6
|
//#region src/code-example/helpers/operation-to-har/process-parameters.ts
|
|
5
|
-
var
|
|
7
|
+
var g = (t) => (t ?? []).map((t) => e(t)).filter((e) => e !== void 0), _ = (e) => "allowReserved" in e && e.allowReserved !== void 0 ? e.allowReserved : "schema" in e && e.schema && typeof e.schema == "object" && "allowReserved" in e.schema ? e.schema.allowReserved === !0 : !1, v = (e, t) => _(t) ? e : encodeURIComponent(e), y = (e) => {
|
|
6
8
|
if (e.in === "header") return {
|
|
7
9
|
style: "simple",
|
|
8
10
|
explode: "explode" in e && e.explode !== void 0 && e.explode
|
|
9
11
|
};
|
|
10
|
-
if (e.in === "cookie")
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
if (e.in === "cookie") {
|
|
13
|
+
let t = "explode" in e && e.explode !== void 0 ? e.explode : !0;
|
|
14
|
+
return {
|
|
15
|
+
style: "style" in e && e.style === "cookie" ? "cookie" : "form",
|
|
16
|
+
explode: t
|
|
17
|
+
};
|
|
18
|
+
}
|
|
14
19
|
let t = {
|
|
15
20
|
path: "simple",
|
|
16
21
|
query: "form",
|
|
22
|
+
querystring: "form",
|
|
17
23
|
header: "simple",
|
|
18
24
|
cookie: "form"
|
|
19
|
-
}[e.in]
|
|
25
|
+
}[e.in], n = "style" in e && e.style ? e.style : t;
|
|
20
26
|
return {
|
|
21
27
|
style: n,
|
|
22
28
|
explode: "explode" in e && e.explode !== void 0 ? e.explode : n === "form"
|
|
23
29
|
};
|
|
24
|
-
},
|
|
25
|
-
let
|
|
26
|
-
if (i
|
|
27
|
-
if (
|
|
28
|
-
if (!("schema" in
|
|
29
|
-
let
|
|
30
|
-
return
|
|
31
|
-
},
|
|
32
|
-
let
|
|
33
|
-
for (let
|
|
34
|
-
if (!
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
30
|
+
}, b = (i, a, o) => {
|
|
31
|
+
let s = p(i, a), c = s.example;
|
|
32
|
+
if (r(i, c, o)) return;
|
|
33
|
+
if (s.value !== void 0) return t(s.value, i);
|
|
34
|
+
if (!("schema" in i) || !i.schema) return;
|
|
35
|
+
let l = i.in === "path" ? { emptyString: `{${i.name}}` } : {}, u = e(i.schema);
|
|
36
|
+
return u ? n(u, l) : void 0;
|
|
37
|
+
}, x = ({ harRequest: e, parameters: t, example: n, defaultDisabled: f }) => {
|
|
38
|
+
let _ = [...e.headers], x = [...e.queryString], C = [...e.cookies], w = [], T = e.url, E = [], D = [], O = g(t);
|
|
39
|
+
for (let e of O) {
|
|
40
|
+
if (!e.in || !e.name) continue;
|
|
41
|
+
if (e.in === "querystring") {
|
|
42
|
+
let t = m(e, n, { defaultDisabled: f });
|
|
43
|
+
if (t) {
|
|
44
|
+
let e = T.indexOf("#"), n = e === -1 ? "" : T.slice(e), r = (e === -1 ? T : T.slice(0, e)).split("?")[0], i = h(t);
|
|
45
|
+
T = `${r}${i ? `?${i}` : ""}${n}`;
|
|
46
|
+
}
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
let t = p(e, n);
|
|
50
|
+
if (t.serialized && !r(e, t.example, f)) {
|
|
51
|
+
let n = String(t.value);
|
|
52
|
+
switch (e.in) {
|
|
53
|
+
case "path":
|
|
54
|
+
T = T.replaceAll(`{${e.name}}`, () => n);
|
|
55
|
+
break;
|
|
56
|
+
case "query":
|
|
57
|
+
E.push(n);
|
|
58
|
+
break;
|
|
59
|
+
case "header":
|
|
60
|
+
_.push({
|
|
61
|
+
name: e.name,
|
|
62
|
+
value: n
|
|
63
|
+
});
|
|
64
|
+
break;
|
|
65
|
+
case "cookie":
|
|
66
|
+
D.push(n);
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
if (t.mediaSerialized && !r(e, t.example, f)) {
|
|
72
|
+
let n = String(t.value);
|
|
73
|
+
switch (e.in) {
|
|
74
|
+
case "query":
|
|
75
|
+
x.push({
|
|
76
|
+
name: e.name,
|
|
77
|
+
value: v(n, e)
|
|
78
|
+
});
|
|
79
|
+
break;
|
|
80
|
+
case "header":
|
|
81
|
+
_.push({
|
|
82
|
+
name: e.name,
|
|
83
|
+
value: n
|
|
84
|
+
});
|
|
85
|
+
break;
|
|
86
|
+
case "path":
|
|
87
|
+
T = T.replaceAll(`{${e.name}}`, () => encodeURIComponent(n));
|
|
88
|
+
break;
|
|
89
|
+
case "cookie":
|
|
90
|
+
C.push({
|
|
91
|
+
name: e.name,
|
|
92
|
+
value: n
|
|
93
|
+
});
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
let g = b(e, n, f);
|
|
99
|
+
if (g === void 0) continue;
|
|
100
|
+
let { style: O, explode: k } = y(e);
|
|
101
|
+
switch (e.in) {
|
|
39
102
|
case "path":
|
|
40
|
-
|
|
103
|
+
T = S(T, e, g, O, k);
|
|
41
104
|
break;
|
|
42
105
|
case "query":
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
value: h(e, t)
|
|
106
|
+
if ("content" in e && e.content) {
|
|
107
|
+
let t = i(g, Object.keys(e.content)[0] ?? "application/json");
|
|
108
|
+
x.push({
|
|
109
|
+
name: e.name,
|
|
110
|
+
value: v(t, e)
|
|
49
111
|
});
|
|
50
112
|
break;
|
|
51
113
|
}
|
|
52
|
-
switch (
|
|
114
|
+
switch (O) {
|
|
53
115
|
case "form": {
|
|
54
|
-
let
|
|
55
|
-
if (Array.isArray(
|
|
56
|
-
let
|
|
57
|
-
|
|
58
|
-
name:
|
|
59
|
-
value:
|
|
116
|
+
let t = s(g, k);
|
|
117
|
+
if (Array.isArray(t)) for (let n of t) {
|
|
118
|
+
let t = n.key || e.name;
|
|
119
|
+
x.push({
|
|
120
|
+
name: t,
|
|
121
|
+
value: v(String(n.value), e)
|
|
60
122
|
});
|
|
61
123
|
}
|
|
62
|
-
else
|
|
63
|
-
name:
|
|
64
|
-
value:
|
|
124
|
+
else x.push({
|
|
125
|
+
name: e.name,
|
|
126
|
+
value: v(String(t), e)
|
|
65
127
|
});
|
|
66
128
|
break;
|
|
67
129
|
}
|
|
68
130
|
case "spaceDelimited": {
|
|
69
|
-
let
|
|
70
|
-
|
|
71
|
-
name:
|
|
72
|
-
value:
|
|
131
|
+
let t = d(g);
|
|
132
|
+
x.push({
|
|
133
|
+
name: e.name,
|
|
134
|
+
value: v(t, e)
|
|
73
135
|
});
|
|
74
136
|
break;
|
|
75
137
|
}
|
|
76
138
|
case "pipeDelimited": {
|
|
77
|
-
let
|
|
78
|
-
|
|
79
|
-
name:
|
|
80
|
-
value:
|
|
139
|
+
let t = l(g);
|
|
140
|
+
x.push({
|
|
141
|
+
name: e.name,
|
|
142
|
+
value: v(t, e)
|
|
81
143
|
});
|
|
82
144
|
break;
|
|
83
145
|
}
|
|
84
146
|
case "deepObject":
|
|
85
|
-
if (Array.isArray(
|
|
86
|
-
let
|
|
87
|
-
if (Array.isArray(
|
|
88
|
-
let
|
|
89
|
-
|
|
90
|
-
name:
|
|
91
|
-
value:
|
|
147
|
+
if (Array.isArray(g)) {
|
|
148
|
+
let t = s(g, !0);
|
|
149
|
+
if (Array.isArray(t)) for (let n of t) {
|
|
150
|
+
let t = n.key || e.name;
|
|
151
|
+
x.push({
|
|
152
|
+
name: t,
|
|
153
|
+
value: v(String(n.value), e)
|
|
92
154
|
});
|
|
93
155
|
}
|
|
94
|
-
else
|
|
95
|
-
name:
|
|
96
|
-
value:
|
|
156
|
+
else x.push({
|
|
157
|
+
name: e.name,
|
|
158
|
+
value: v(String(t), e)
|
|
97
159
|
});
|
|
98
160
|
} else {
|
|
99
|
-
let
|
|
100
|
-
for (let n of
|
|
161
|
+
let t = o(e.name, g);
|
|
162
|
+
for (let n of t) x.push({
|
|
101
163
|
name: n.key,
|
|
102
|
-
value:
|
|
164
|
+
value: v(n.value, e)
|
|
103
165
|
});
|
|
104
166
|
}
|
|
105
167
|
break;
|
|
106
|
-
default:
|
|
107
|
-
name:
|
|
108
|
-
value:
|
|
168
|
+
default: x.push({
|
|
169
|
+
name: e.name,
|
|
170
|
+
value: v(String(g), e)
|
|
109
171
|
});
|
|
110
172
|
}
|
|
111
173
|
break;
|
|
112
174
|
case "header": {
|
|
113
|
-
let
|
|
114
|
-
|
|
115
|
-
name:
|
|
116
|
-
value: String(
|
|
175
|
+
let t = u(g, k);
|
|
176
|
+
_.push({
|
|
177
|
+
name: e.name,
|
|
178
|
+
value: String(t)
|
|
117
179
|
});
|
|
118
180
|
break;
|
|
119
181
|
}
|
|
120
182
|
case "cookie": {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
183
|
+
if (O === "cookie") {
|
|
184
|
+
w.push(...a(e.name, g, k));
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
let t = c(g, k);
|
|
188
|
+
if (Array.isArray(t)) for (let n of t) {
|
|
189
|
+
let t = n.key || e.name, r = n.value === null ? "null" : String(n.value);
|
|
190
|
+
C.push({
|
|
191
|
+
name: t,
|
|
192
|
+
value: r
|
|
127
193
|
});
|
|
128
194
|
}
|
|
129
195
|
else {
|
|
130
|
-
let
|
|
131
|
-
|
|
132
|
-
name:
|
|
133
|
-
value:
|
|
196
|
+
let n = t === null ? "null" : String(t);
|
|
197
|
+
C.push({
|
|
198
|
+
name: e.name,
|
|
199
|
+
value: n
|
|
134
200
|
});
|
|
135
201
|
}
|
|
136
202
|
break;
|
|
137
203
|
}
|
|
138
204
|
}
|
|
139
205
|
}
|
|
206
|
+
if (w.length || D.length) {
|
|
207
|
+
let e = [
|
|
208
|
+
...C.map((e) => `${encodeURIComponent(e.name)}=${encodeURIComponent(e.value)}`),
|
|
209
|
+
...w.map((e) => `${e.name}=${e.value}`),
|
|
210
|
+
...D
|
|
211
|
+
].join("; "), t = _.find((e) => e.name.toLowerCase() === "cookie");
|
|
212
|
+
t ? _.splice(_.indexOf(t), 1, {
|
|
213
|
+
...t,
|
|
214
|
+
value: t.value ? `${t.value}; ${e}` : e
|
|
215
|
+
}) : _.push({
|
|
216
|
+
name: "Cookie",
|
|
217
|
+
value: e
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
if (E.length) {
|
|
221
|
+
let e = T.indexOf("#"), t = e < 0 ? "" : T.slice(e), n = e < 0 ? T : T.slice(0, e);
|
|
222
|
+
T = `${n}${n.includes("?") ? "&" : "?"}${E.join("&")}${t}`;
|
|
223
|
+
}
|
|
140
224
|
return {
|
|
141
|
-
url:
|
|
142
|
-
headers:
|
|
143
|
-
queryString:
|
|
144
|
-
cookies:
|
|
225
|
+
url: T,
|
|
226
|
+
headers: _,
|
|
227
|
+
queryString: x,
|
|
228
|
+
cookies: w.length || D.length ? [] : C,
|
|
229
|
+
hasCookieStyleEntries: w.length > 0 || D.length > 0,
|
|
230
|
+
...E.length ? { hasSerializedQuery: !0 } : {}
|
|
145
231
|
};
|
|
146
|
-
},
|
|
232
|
+
}, S = (e, t, n, r, i) => {
|
|
147
233
|
switch (r) {
|
|
148
234
|
case "matrix":
|
|
149
235
|
if (i) {
|
|
@@ -186,6 +272,6 @@ var p = (t) => (t ?? []).map((t) => e(t)).filter((e) => e !== void 0), m = (e) =
|
|
|
186
272
|
}
|
|
187
273
|
};
|
|
188
274
|
//#endregion
|
|
189
|
-
export {
|
|
275
|
+
export { x as processParameters };
|
|
190
276
|
|
|
191
277
|
//# sourceMappingURL=process-parameters.js.map
|