@readme/oas-to-har 39.0.1 → 40.0.1
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/README.md +1 -1
- package/dist/index.cjs +1098 -1228
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +154 -14
- package/dist/index.d.ts +156 -16
- package/dist/index.js +1070 -1226
- package/dist/index.js.map +1 -1
- package/dist/lib/configure-security.cjs +66 -6
- package/dist/lib/configure-security.cjs.map +1 -1
- package/dist/lib/configure-security.d.cts +8 -8
- package/dist/lib/configure-security.d.ts +10 -10
- package/dist/lib/configure-security.js +68 -6
- package/dist/lib/configure-security.js.map +1 -1
- package/dist/lib/types.cjs +0 -1
- package/dist/lib/types.d.cts +11 -12
- package/dist/lib/types.d.ts +11 -12
- package/dist/lib/types.js +1 -1
- package/package.json +10 -10
- package/dist/chunk-6CVT7KVT.cjs +0 -83
- package/dist/chunk-6CVT7KVT.cjs.map +0 -1
- package/dist/chunk-JWXX5N4N.js +0 -83
- package/dist/chunk-JWXX5N4N.js.map +0 -1
- package/dist/lib/types.cjs.map +0 -1
- package/dist/lib/types.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,1324 +1,1168 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
} from "./chunk-JWXX5N4N.js";
|
|
4
|
-
|
|
5
|
-
// src/index.ts
|
|
6
|
-
import { parse as parseDataUrl } from "@readme/data-urls";
|
|
1
|
+
import configureSecurity from "./lib/configure-security.js";
|
|
2
|
+
import { parse } from "@readme/data-urls";
|
|
7
3
|
import { HEADERS, PROXY_ENABLED } from "oas/extensions";
|
|
8
4
|
import { Operation } from "oas/operation";
|
|
9
|
-
import { isRef
|
|
10
|
-
import { dereferenceRef
|
|
5
|
+
import { isRef } from "oas/types";
|
|
6
|
+
import { dereferenceRef, dereferenceRefDeep, getParameterContentType, jsonSchemaTypes, matchesMimeType } from "oas/utils";
|
|
11
7
|
import removeUndefinedObjects from "remove-undefined-objects";
|
|
8
|
+
import qs from "qs";
|
|
12
9
|
|
|
13
10
|
// src/lib/lodash.ts
|
|
11
|
+
/**
|
|
12
|
+
* A janky, poorly typed replacement for `lodash.get`.
|
|
13
|
+
*
|
|
14
|
+
* @see {@link https://youmightnotneed.com/lodash#get}
|
|
15
|
+
*/
|
|
14
16
|
function get(object, path) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const result = pathArray?.reduce((prevObj, key) => prevObj?.[key], object);
|
|
18
|
-
return result;
|
|
17
|
+
if (!path) return void 0;
|
|
18
|
+
return String(path).match(/([^[.\]])+/g)?.reduce((prevObj, key) => prevObj?.[key], object);
|
|
19
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* A janky, poorly typed replacement for `lodash.set`.
|
|
22
|
+
*
|
|
23
|
+
* @see {@link https://youmightnotneed.com/lodash#set}
|
|
24
|
+
*/
|
|
20
25
|
function set(object, path, value) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
acc[key] = value;
|
|
28
|
-
}
|
|
29
|
-
return acc[key];
|
|
30
|
-
}, object);
|
|
26
|
+
const pathArray = Array.isArray(path) ? path : String(path).match(/([^[.\]])+/g);
|
|
27
|
+
return pathArray?.reduce((acc, key, i) => {
|
|
28
|
+
if (acc[key] === void 0) acc[key] = {};
|
|
29
|
+
if (i === pathArray.length - 1) acc[key] = value;
|
|
30
|
+
return acc[key];
|
|
31
|
+
}, object);
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
// src/lib/style-formatting/index.ts
|
|
34
|
-
import { matchesMimeType } from "oas/utils";
|
|
35
|
-
import qs from "qs";
|
|
36
|
-
|
|
37
34
|
// src/lib/utils.ts
|
|
38
|
-
|
|
39
|
-
|
|
35
|
+
/**
|
|
36
|
+
* Determine if a schema `type` is, or contains, a specific discriminator.
|
|
37
|
+
*
|
|
38
|
+
*/
|
|
40
39
|
function hasSchemaType(schema, discriminator) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
return schema.type === discriminator;
|
|
40
|
+
if (Array.isArray(schema.type)) return schema.type.includes(discriminator);
|
|
41
|
+
return schema.type === discriminator;
|
|
45
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* When we only have a schema fragment (no request payload), peel a single top-level polymorphic
|
|
45
|
+
* schema to its first branch so we can enumerate its properties.
|
|
46
|
+
*
|
|
47
|
+
*/
|
|
46
48
|
function unwrapFirstPolymorphicBranch(obj) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (obj.anyOf && Array.isArray(obj.anyOf) && obj.anyOf.length) {
|
|
51
|
-
return unwrapFirstPolymorphicBranch(obj.anyOf[0]);
|
|
52
|
-
}
|
|
53
|
-
return obj;
|
|
49
|
+
if (obj.oneOf && Array.isArray(obj.oneOf) && obj.oneOf.length) return unwrapFirstPolymorphicBranch(obj.oneOf[0]);
|
|
50
|
+
if (obj.anyOf && Array.isArray(obj.anyOf) && obj.anyOf.length) return unwrapFirstPolymorphicBranch(obj.anyOf[0]);
|
|
51
|
+
return obj;
|
|
54
52
|
}
|
|
55
53
|
function getSubschemas(schema, api, opts, seenRefs = /* @__PURE__ */ new Set()) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
if (toPush && typeof toPush === "object") {
|
|
123
|
-
subschemas.push({
|
|
124
|
-
key: baseKey ? [baseKey, propName].join(".") : propName,
|
|
125
|
-
schema: toPush
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
if ("items" in resolvedSchema && resolvedSchema.items !== void 0 && resolvedSchema.items !== true) {
|
|
132
|
-
const itemsSchema = resolvedSchema.items;
|
|
133
|
-
let resolved;
|
|
134
|
-
if (isRef(itemsSchema)) {
|
|
135
|
-
if (!seenRefs.has(itemsSchema.$ref)) {
|
|
136
|
-
seenRefs.add(itemsSchema.$ref);
|
|
137
|
-
resolved = dereferenceRef(itemsSchema, api);
|
|
138
|
-
}
|
|
139
|
-
} else {
|
|
140
|
-
resolved = itemsSchema;
|
|
141
|
-
}
|
|
142
|
-
if (resolved && !isRef(resolved)) {
|
|
143
|
-
subschemas.push({
|
|
144
|
-
key: baseKey,
|
|
145
|
-
schema: resolved,
|
|
146
|
-
parentIsArray: true
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
return subschemas;
|
|
54
|
+
let subSchemaDataSize = 0;
|
|
55
|
+
if (opts.parentIsArray) {
|
|
56
|
+
const parentData = get(opts.payload, opts.parentKey || "");
|
|
57
|
+
if (parentData === void 0 || !Array.isArray(parentData)) return false;
|
|
58
|
+
subSchemaDataSize = parentData.length;
|
|
59
|
+
}
|
|
60
|
+
let subschemas = [];
|
|
61
|
+
if (subSchemaDataSize > 0) for (let idx = 0; idx < subSchemaDataSize; idx += 1) {
|
|
62
|
+
const foundSubschemas = getSubschemas(schema, api, {
|
|
63
|
+
...opts,
|
|
64
|
+
parentIsArray: false,
|
|
65
|
+
parentKey: opts.parentKey ? [opts.parentKey, idx].join(".") : String(idx)
|
|
66
|
+
}, seenRefs);
|
|
67
|
+
if (foundSubschemas) subschemas = subschemas.concat(foundSubschemas);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
let resolvedSchema = schema;
|
|
71
|
+
if (schema && isRef(schema)) {
|
|
72
|
+
if (seenRefs.has(schema.$ref)) return subschemas;
|
|
73
|
+
seenRefs.add(schema.$ref);
|
|
74
|
+
resolvedSchema = dereferenceRef(schema, api);
|
|
75
|
+
if (!resolvedSchema || isRef(resolvedSchema)) return subschemas;
|
|
76
|
+
}
|
|
77
|
+
const baseKey = opts.parentKey ?? "";
|
|
78
|
+
if (resolvedSchema.properties && typeof resolvedSchema.properties === "object") {
|
|
79
|
+
for (const [propName, propSchema] of Object.entries(resolvedSchema.properties)) if (propSchema && typeof propSchema === "object") {
|
|
80
|
+
let resolved;
|
|
81
|
+
if (isRef(propSchema)) {
|
|
82
|
+
if (seenRefs.has(propSchema.$ref)) continue;
|
|
83
|
+
seenRefs.add(propSchema.$ref);
|
|
84
|
+
resolved = dereferenceRef(propSchema, api);
|
|
85
|
+
} else resolved = propSchema;
|
|
86
|
+
if (resolved && !isRef(resolved)) subschemas.push({
|
|
87
|
+
key: baseKey ? [baseKey, propName].join(".") : propName,
|
|
88
|
+
schema: resolved
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (!("properties" in resolvedSchema) && typeof resolvedSchema === "object" && !("type" in resolvedSchema && resolvedSchema.type !== "object")) {
|
|
93
|
+
for (const [propName, propSchema] of Object.entries(resolvedSchema)) if (propSchema && (Array.isArray(propSchema) || typeof propSchema === "object" && propSchema !== null)) {
|
|
94
|
+
const raw = unwrapFirstPolymorphicBranch(propSchema);
|
|
95
|
+
const resolved = isRef(raw) ? dereferenceRef(raw, api) : raw;
|
|
96
|
+
const toPush = resolved && !isRef(resolved) ? resolved : raw;
|
|
97
|
+
if (toPush && typeof toPush === "object") subschemas.push({
|
|
98
|
+
key: baseKey ? [baseKey, propName].join(".") : propName,
|
|
99
|
+
schema: toPush
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if ("items" in resolvedSchema && resolvedSchema.items !== void 0 && resolvedSchema.items !== true) {
|
|
104
|
+
const itemsSchema = resolvedSchema.items;
|
|
105
|
+
let resolved;
|
|
106
|
+
if (isRef(itemsSchema)) {
|
|
107
|
+
if (!seenRefs.has(itemsSchema.$ref)) {
|
|
108
|
+
seenRefs.add(itemsSchema.$ref);
|
|
109
|
+
resolved = dereferenceRef(itemsSchema, api);
|
|
110
|
+
}
|
|
111
|
+
} else resolved = itemsSchema;
|
|
112
|
+
if (resolved && !isRef(resolved)) subschemas.push({
|
|
113
|
+
key: baseKey,
|
|
114
|
+
schema: resolved,
|
|
115
|
+
parentIsArray: true
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return subschemas;
|
|
152
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
* With a supplied JSON Schema object, spider through it for any schemas that may contain specific
|
|
123
|
+
* kind of `format` that also happen to be within the current `requestBody` payload that we're
|
|
124
|
+
* creating a HAR representation for.
|
|
125
|
+
*
|
|
126
|
+
*/
|
|
153
127
|
function getTypedFormatsInSchema(format, schema, api, opts, seenRefs = /* @__PURE__ */ new Set()) {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
return false;
|
|
185
|
-
}
|
|
186
|
-
return getTypedFormatsInSchema(
|
|
187
|
-
format,
|
|
188
|
-
subschema,
|
|
189
|
-
api,
|
|
190
|
-
{
|
|
191
|
-
payload: opts.payload,
|
|
192
|
-
parentKey: key,
|
|
193
|
-
parentIsArray: entryIsArray
|
|
194
|
-
},
|
|
195
|
-
seenRefs
|
|
196
|
-
);
|
|
197
|
-
}).filter(Boolean);
|
|
198
|
-
} catch {
|
|
199
|
-
return [];
|
|
200
|
-
}
|
|
128
|
+
try {
|
|
129
|
+
if (schema?.format === format) {
|
|
130
|
+
if (opts.parentIsArray) {
|
|
131
|
+
const parentData = get(opts.payload, opts.parentKey || "");
|
|
132
|
+
if (parentData !== void 0 && Array.isArray(parentData)) return Object.keys(parentData).map((pdk) => {
|
|
133
|
+
const currentKey = [opts.parentKey, pdk].join(".");
|
|
134
|
+
if (get(opts.payload, currentKey) !== void 0) return currentKey;
|
|
135
|
+
return false;
|
|
136
|
+
}).filter(Boolean);
|
|
137
|
+
} else if (opts.parentKey && get(opts.payload, opts.parentKey) !== void 0) return opts.parentKey;
|
|
138
|
+
else if (!opts.parentKey && opts.payload !== void 0) return true;
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
const subschemas = getSubschemas(schema, api, opts, seenRefs);
|
|
142
|
+
if (!subschemas) return false;
|
|
143
|
+
return subschemas.flatMap(({ key, schema: subschema, parentIsArray: entryIsArray }) => {
|
|
144
|
+
if (isRef(subschema)) {
|
|
145
|
+
const resolved = dereferenceRef(subschema, api);
|
|
146
|
+
if (resolved && !isRef(resolved)) return resolved;
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
return getTypedFormatsInSchema(format, subschema, api, {
|
|
150
|
+
payload: opts.payload,
|
|
151
|
+
parentKey: key,
|
|
152
|
+
parentIsArray: entryIsArray
|
|
153
|
+
}, seenRefs);
|
|
154
|
+
}).filter(Boolean);
|
|
155
|
+
} catch {
|
|
156
|
+
return [];
|
|
157
|
+
}
|
|
201
158
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
159
|
+
/**
|
|
160
|
+
* Extract content type from a parameter's `content` field.
|
|
161
|
+
* According to OAS spec, when `content` is present, `style` and `explode` are ignored.
|
|
162
|
+
* We prioritize `application/json` and other JSON-like content types over other content types.
|
|
163
|
+
* Note: this is just a safe guard. In OAS parser, we enforce that there is exactly one content type.
|
|
164
|
+
*
|
|
165
|
+
* @param param - The parameter object
|
|
166
|
+
* @returns The content type, or `null` if no content is present
|
|
167
|
+
*/
|
|
168
|
+
function getParameterContentType$1(param) {
|
|
169
|
+
if (!param.content) return null;
|
|
170
|
+
const contentKeys = Object.keys(param.content);
|
|
171
|
+
if (contentKeys.length < 1) return null;
|
|
172
|
+
return getParameterContentType(contentKeys) || null;
|
|
211
173
|
}
|
|
174
|
+
/**
|
|
175
|
+
* Extract schema from a parameter's `content` field.
|
|
176
|
+
*
|
|
177
|
+
* @param param - The parameter object
|
|
178
|
+
* @param contentType - The content type
|
|
179
|
+
* @returns The schema, or `null` if no schema is present
|
|
180
|
+
*/
|
|
212
181
|
function getParameterContentSchema(param, contentType, api) {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
}
|
|
222
|
-
return schema;
|
|
223
|
-
}
|
|
224
|
-
return null;
|
|
182
|
+
if (!param.content) return null;
|
|
183
|
+
const mediaTypeObject = param.content[contentType];
|
|
184
|
+
if (typeof mediaTypeObject === "object" && mediaTypeObject && "schema" in mediaTypeObject && mediaTypeObject.schema) {
|
|
185
|
+
const schema = isRef(mediaTypeObject.schema) ? dereferenceRef(mediaTypeObject.schema, api) : mediaTypeObject.schema;
|
|
186
|
+
if (!schema || isRef(schema)) return null;
|
|
187
|
+
return schema;
|
|
188
|
+
}
|
|
189
|
+
return null;
|
|
225
190
|
}
|
|
191
|
+
/**
|
|
192
|
+
* Recursively parse string values that are valid JSON.
|
|
193
|
+
*
|
|
194
|
+
* This is used when we're dealing with objects that have nested `format: json` descriptors.
|
|
195
|
+
*/
|
|
226
196
|
function parseJSONStrings(obj) {
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
for (const [k, v] of Object.entries(obj)) {
|
|
241
|
-
out[k] = parseJSONStrings(v);
|
|
242
|
-
}
|
|
243
|
-
return out;
|
|
244
|
-
}
|
|
245
|
-
return obj;
|
|
197
|
+
if (typeof obj === "string") try {
|
|
198
|
+
const p = JSON.parse(obj);
|
|
199
|
+
return typeof p === "object" && p !== null ? parseJSONStrings(p) : p;
|
|
200
|
+
} catch {
|
|
201
|
+
return obj;
|
|
202
|
+
}
|
|
203
|
+
if (Array.isArray(obj)) return obj.map(parseJSONStrings);
|
|
204
|
+
if (obj !== null && typeof obj === "object") {
|
|
205
|
+
const out = {};
|
|
206
|
+
for (const [k, v] of Object.entries(obj)) out[k] = parseJSONStrings(v);
|
|
207
|
+
return out;
|
|
208
|
+
}
|
|
209
|
+
return obj;
|
|
246
210
|
}
|
|
211
|
+
/**
|
|
212
|
+
* Like {@link parseJSONStrings} but only parses string values that are clearly JSON objects or
|
|
213
|
+
* arrays (after trim, starts with `{` or `[`). Used for payload keys that are not declared on the
|
|
214
|
+
* schema's `properties` map so numerical strings are not coerced into numbers.
|
|
215
|
+
*/
|
|
247
216
|
function parseJSONStringsObjectContainersOnly(obj) {
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
const out = {};
|
|
265
|
-
for (const [k, v] of Object.entries(obj)) {
|
|
266
|
-
out[k] = parseJSONStringsObjectContainersOnly(v);
|
|
267
|
-
}
|
|
268
|
-
return out;
|
|
269
|
-
}
|
|
270
|
-
return obj;
|
|
217
|
+
if (typeof obj === "string") {
|
|
218
|
+
const trimmed = obj.trim();
|
|
219
|
+
if (trimmed.startsWith("{") || trimmed.startsWith("[")) try {
|
|
220
|
+
return parseJSONStringsObjectContainersOnly(JSON.parse(obj));
|
|
221
|
+
} catch {
|
|
222
|
+
return obj;
|
|
223
|
+
}
|
|
224
|
+
return obj;
|
|
225
|
+
}
|
|
226
|
+
if (Array.isArray(obj)) return obj.map(parseJSONStringsObjectContainersOnly);
|
|
227
|
+
if (obj !== null && typeof obj === "object") {
|
|
228
|
+
const out = {};
|
|
229
|
+
for (const [k, v] of Object.entries(obj)) out[k] = parseJSONStringsObjectContainersOnly(v);
|
|
230
|
+
return out;
|
|
231
|
+
}
|
|
232
|
+
return obj;
|
|
271
233
|
}
|
|
272
234
|
function getAdditionalPropertiesSchema(schema) {
|
|
273
|
-
|
|
235
|
+
return schema.additionalProperties && typeof schema.additionalProperties === "object" ? schema.additionalProperties : void 0;
|
|
274
236
|
}
|
|
275
237
|
function mergePropertiesFromAllOf(allOf, api, seenRefs) {
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
238
|
+
const merged = {};
|
|
239
|
+
let found = false;
|
|
240
|
+
for (const branch of allOf) {
|
|
241
|
+
const collected = collectSchemaObjectProperties(branch, api, new Set(seenRefs));
|
|
242
|
+
if (collected) {
|
|
243
|
+
found = true;
|
|
244
|
+
Object.assign(merged, collected);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return found ? merged : void 0;
|
|
286
248
|
}
|
|
287
249
|
function collectSchemaObjectProperties(schema, api, seenRefs) {
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
}
|
|
304
|
-
node = safe;
|
|
305
|
-
if (node) {
|
|
306
|
-
if ("allOf" in node && Array.isArray(node.allOf) && node.allOf.length) {
|
|
307
|
-
return mergePropertiesFromAllOf(node.allOf, api, seenRefs);
|
|
308
|
-
}
|
|
309
|
-
if (node.properties && typeof node.properties === "object") {
|
|
310
|
-
return node.properties;
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
return void 0;
|
|
250
|
+
let node = schema;
|
|
251
|
+
if (isRef(node)) {
|
|
252
|
+
if (seenRefs.has(node.$ref)) return;
|
|
253
|
+
seenRefs.add(node.$ref);
|
|
254
|
+
const deref = dereferenceRef(node, api);
|
|
255
|
+
if (!deref || isRef(deref)) return;
|
|
256
|
+
node = deref;
|
|
257
|
+
}
|
|
258
|
+
const safe = unwrapFirstPolymorphicBranch(node);
|
|
259
|
+
if (isRef(safe)) return collectSchemaObjectProperties(safe, api, seenRefs);
|
|
260
|
+
node = safe;
|
|
261
|
+
if (node) {
|
|
262
|
+
if ("allOf" in node && Array.isArray(node.allOf) && node.allOf.length) return mergePropertiesFromAllOf(node.allOf, api, seenRefs);
|
|
263
|
+
if (node.properties && typeof node.properties === "object") return node.properties;
|
|
264
|
+
}
|
|
314
265
|
}
|
|
315
266
|
function getPayloadPropertyKeys(payload) {
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
}
|
|
319
|
-
return Object.keys(payload).filter(
|
|
320
|
-
(k) => typeof payload[k] !== "undefined"
|
|
321
|
-
);
|
|
267
|
+
if (typeof payload !== "object" || payload === null || Array.isArray(payload)) return [];
|
|
268
|
+
return Object.keys(payload).filter((k) => typeof payload[k] !== "undefined");
|
|
322
269
|
}
|
|
323
270
|
function getPolymorphicSchema(node) {
|
|
324
|
-
|
|
271
|
+
return node.oneOf && Array.isArray(node.oneOf) && node.oneOf.length ? node.oneOf : node.anyOf && Array.isArray(node.anyOf) && node.anyOf.length ? node.anyOf : null;
|
|
325
272
|
}
|
|
273
|
+
/**
|
|
274
|
+
* Choose a single branch from a polymorphic schema that best matches the payload we have.
|
|
275
|
+
*
|
|
276
|
+
* A branch matches when every non-`undefined` payload key exists on that branch's merged object
|
|
277
|
+
* properties. If we have no payload, or no branches match, we use the first branch.
|
|
278
|
+
*
|
|
279
|
+
* When several branches match the one with the fewest declared properties wins, which would get us
|
|
280
|
+
* as close to a best match as possible, however having more data in our payload would give us more
|
|
281
|
+
* insight into which branch is what the user wants.
|
|
282
|
+
*/
|
|
326
283
|
function pickPolymorphicBranch(alternatives, keys, api) {
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
284
|
+
if (!keys.length) return alternatives[0];
|
|
285
|
+
const scored = alternatives.map((branch, idx) => {
|
|
286
|
+
const props = collectSchemaObjectProperties(branch, api, /* @__PURE__ */ new Set());
|
|
287
|
+
return {
|
|
288
|
+
idx,
|
|
289
|
+
branch,
|
|
290
|
+
allMatch: Boolean(props && keys.every((k) => Object.prototype.hasOwnProperty.call(props, k))),
|
|
291
|
+
propCount: props ? Object.keys(props).length : Number.POSITIVE_INFINITY
|
|
292
|
+
};
|
|
293
|
+
});
|
|
294
|
+
const matches = scored.filter((entry) => entry.allMatch);
|
|
295
|
+
if (matches.length === 1) return matches[0].branch;
|
|
296
|
+
if (matches.length > 1) {
|
|
297
|
+
matches.sort((a, b) => a.propCount - b.propCount || a.idx - b.idx);
|
|
298
|
+
return matches[0].branch;
|
|
299
|
+
}
|
|
300
|
+
return scored[0].branch;
|
|
343
301
|
}
|
|
302
|
+
/**
|
|
303
|
+
* Resolve a request-body JSON Schema against a concrete payload.
|
|
304
|
+
*
|
|
305
|
+
*/
|
|
344
306
|
function getSafeRequestBody(schema, payload, api) {
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
307
|
+
let resolved = dereferenceRefDeep(schema, api);
|
|
308
|
+
const keys = getPayloadPropertyKeys(payload);
|
|
309
|
+
for (let alternatives = getPolymorphicSchema(resolved); alternatives != null; alternatives = getPolymorphicSchema(resolved)) resolved = pickPolymorphicBranch(alternatives, keys, api);
|
|
310
|
+
if ("allOf" in resolved && Array.isArray(resolved.allOf) && resolved.allOf.length) {
|
|
311
|
+
const fromAllOf = mergePropertiesFromAllOf(resolved.allOf, api, /* @__PURE__ */ new Set());
|
|
312
|
+
if (fromAllOf && Object.keys(fromAllOf).length) {
|
|
313
|
+
const existing = resolved.properties && typeof resolved.properties === "object" && resolved.properties !== null ? resolved.properties : {};
|
|
314
|
+
resolved = {
|
|
315
|
+
...resolved,
|
|
316
|
+
type: "object",
|
|
317
|
+
properties: {
|
|
318
|
+
...fromAllOf,
|
|
319
|
+
...existing
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
return resolved;
|
|
362
325
|
}
|
|
326
|
+
/**
|
|
327
|
+
* Recursively runs through a schema, parsing any values that have `format: json` attached and
|
|
328
|
+
* deserializing them into their JSON representations.
|
|
329
|
+
*
|
|
330
|
+
* @see {@link parseJSONStrings}
|
|
331
|
+
*/
|
|
363
332
|
function parseJSONStringsInBodyWithSchema(obj, schema, api, seenRefs = /* @__PURE__ */ new Set()) {
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
}
|
|
423
|
-
const out = {};
|
|
424
|
-
for (const [k, v] of Object.entries(obj)) {
|
|
425
|
-
const propSchema = resolved.properties[k];
|
|
426
|
-
const schemaForValue = propSchema ?? additionalPropertiesSchema;
|
|
427
|
-
out[k] = schemaForValue !== void 0 ? parseJSONStringsInBodyWithSchema(v, schemaForValue, api, new Set(seenRefs)) : parseJSONStringsObjectContainersOnly(v);
|
|
428
|
-
}
|
|
429
|
-
return out;
|
|
430
|
-
}
|
|
431
|
-
return obj;
|
|
333
|
+
if (schema === void 0) return parseJSONStrings(obj);
|
|
334
|
+
let resolved = schema;
|
|
335
|
+
if (isRef(schema)) {
|
|
336
|
+
if (seenRefs.has(schema.$ref)) return parseJSONStrings(obj);
|
|
337
|
+
seenRefs.add(schema.$ref);
|
|
338
|
+
const deref = dereferenceRef(schema, api);
|
|
339
|
+
if (!deref || isRef(deref)) return parseJSONStrings(obj);
|
|
340
|
+
resolved = deref;
|
|
341
|
+
}
|
|
342
|
+
const safe = getSafeRequestBody(resolved, obj, api);
|
|
343
|
+
if (isRef(safe)) return parseJSONStringsInBodyWithSchema(obj, safe, api, seenRefs);
|
|
344
|
+
resolved = safe;
|
|
345
|
+
if ("allOf" in resolved && Array.isArray(resolved.allOf) && resolved.allOf.length) {
|
|
346
|
+
const fromAllOf = mergePropertiesFromAllOf(resolved.allOf, api, new Set(seenRefs));
|
|
347
|
+
if (fromAllOf && Object.keys(fromAllOf).length) {
|
|
348
|
+
const existing = resolved.properties && typeof resolved.properties === "object" && resolved.properties !== null ? resolved.properties : {};
|
|
349
|
+
resolved = {
|
|
350
|
+
...resolved,
|
|
351
|
+
type: "object",
|
|
352
|
+
properties: {
|
|
353
|
+
...fromAllOf,
|
|
354
|
+
...existing
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
if (typeof obj === "string") {
|
|
360
|
+
if (hasSchemaType(resolved, "string") && resolved.format !== "json") return obj;
|
|
361
|
+
return parseJSONStrings(obj);
|
|
362
|
+
}
|
|
363
|
+
if (Array.isArray(obj)) {
|
|
364
|
+
let items = resolved.items;
|
|
365
|
+
if (items && typeof items === "object" && isRef(items)) {
|
|
366
|
+
if (seenRefs.has(items.$ref)) return obj.map((item) => parseJSONStrings(item));
|
|
367
|
+
seenRefs.add(items.$ref);
|
|
368
|
+
const derefItems = dereferenceRef(items, api);
|
|
369
|
+
items = derefItems && !isRef(derefItems) ? derefItems : void 0;
|
|
370
|
+
}
|
|
371
|
+
return obj.map((item) => parseJSONStringsInBodyWithSchema(item, items, api, new Set(seenRefs)));
|
|
372
|
+
}
|
|
373
|
+
if (obj !== null && typeof obj === "object") {
|
|
374
|
+
const additionalPropertiesSchema = getAdditionalPropertiesSchema(resolved);
|
|
375
|
+
if (!resolved.properties || typeof resolved.properties !== "object") {
|
|
376
|
+
if (additionalPropertiesSchema) {
|
|
377
|
+
const out = {};
|
|
378
|
+
for (const [k, v] of Object.entries(obj)) out[k] = parseJSONStringsInBodyWithSchema(v, additionalPropertiesSchema, api, new Set(seenRefs));
|
|
379
|
+
return out;
|
|
380
|
+
}
|
|
381
|
+
return parseJSONStrings(obj);
|
|
382
|
+
}
|
|
383
|
+
const out = {};
|
|
384
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
385
|
+
const schemaForValue = resolved.properties[k] ?? additionalPropertiesSchema;
|
|
386
|
+
out[k] = schemaForValue !== void 0 ? parseJSONStringsInBodyWithSchema(v, schemaForValue, api, new Set(seenRefs)) : parseJSONStringsObjectContainersOnly(v);
|
|
387
|
+
}
|
|
388
|
+
return out;
|
|
389
|
+
}
|
|
390
|
+
return obj;
|
|
432
391
|
}
|
|
433
392
|
|
|
434
393
|
// src/lib/style-formatting/style-serializer.ts
|
|
435
|
-
|
|
436
|
-
|
|
394
|
+
/**
|
|
395
|
+
* This file has been extracted and modified from `swagger-client`.
|
|
396
|
+
*
|
|
397
|
+
* @license Apache 2.0
|
|
398
|
+
* @link https://npm.im/swagger-client
|
|
399
|
+
* @link https://github.com/swagger-api/swagger-js/blob/master/src/execute/oas3/style-serializer.js
|
|
400
|
+
*/
|
|
401
|
+
const isRfc3986Reserved = (char) => ":/?#[]@!$&'()*+,;=".indexOf(char) > -1;
|
|
402
|
+
const isRfc3986Unreserved = (char) => /^[a-z0-9\-._~]+$/i.test(char);
|
|
437
403
|
function isURIEncoded(value) {
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
404
|
+
try {
|
|
405
|
+
return decodeURIComponent(value) !== value;
|
|
406
|
+
} catch {
|
|
407
|
+
return false;
|
|
408
|
+
}
|
|
443
409
|
}
|
|
444
410
|
function isObject(value) {
|
|
445
|
-
|
|
411
|
+
return typeof value === "object" && value !== null;
|
|
446
412
|
}
|
|
447
|
-
function encodeDisallowedCharacters(str, {
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
return str;
|
|
462
|
-
}
|
|
463
|
-
if (!escape) {
|
|
464
|
-
return str;
|
|
465
|
-
}
|
|
466
|
-
if (parse) {
|
|
467
|
-
return JSON.parse(str);
|
|
468
|
-
}
|
|
469
|
-
return [...str].map((char) => {
|
|
470
|
-
if (isRfc3986Unreserved(char)) {
|
|
471
|
-
return char;
|
|
472
|
-
}
|
|
473
|
-
if (isRfc3986Reserved(char) && (escape === "unsafe" || isAllowedReserved)) {
|
|
474
|
-
return char;
|
|
475
|
-
}
|
|
476
|
-
const encoder = new TextEncoder();
|
|
477
|
-
const encoded = Array.from(encoder.encode(char)).map((byte) => `0${byte.toString(16).toUpperCase()}`.slice(-2)).map((encodedByte) => `%${encodedByte}`).join("");
|
|
478
|
-
return encoded;
|
|
479
|
-
}).join("");
|
|
413
|
+
function encodeDisallowedCharacters(str, { escape, returnIfEncoded = false, isAllowedReserved } = {}, parse) {
|
|
414
|
+
if (typeof str === "number") str = str.toString();
|
|
415
|
+
if (returnIfEncoded) {
|
|
416
|
+
if (isURIEncoded(str)) return str;
|
|
417
|
+
}
|
|
418
|
+
if (typeof str !== "string" || !str.length) return str;
|
|
419
|
+
if (!escape) return str;
|
|
420
|
+
if (parse) return JSON.parse(str);
|
|
421
|
+
return [...str].map((char) => {
|
|
422
|
+
if (isRfc3986Unreserved(char)) return char;
|
|
423
|
+
if (isRfc3986Reserved(char) && (escape === "unsafe" || isAllowedReserved)) return char;
|
|
424
|
+
const encoder = new TextEncoder();
|
|
425
|
+
return Array.from(encoder.encode(char)).map((byte) => `0${byte.toString(16).toUpperCase()}`.slice(-2)).map((encodedByte) => `%${encodedByte}`).join("");
|
|
426
|
+
}).join("");
|
|
480
427
|
}
|
|
481
428
|
function stylize(config) {
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
if (isObject(value)) {
|
|
487
|
-
return encodeObject(config);
|
|
488
|
-
}
|
|
489
|
-
return encodePrimitive(config);
|
|
429
|
+
const { value } = config;
|
|
430
|
+
if (Array.isArray(value)) return encodeArray(config);
|
|
431
|
+
if (isObject(value)) return encodeObject(config);
|
|
432
|
+
return encodePrimitive(config);
|
|
490
433
|
}
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
return value.map((val) => valueEncoder(val)).join(explode ? `&${key}=` : ",");
|
|
547
|
-
/**
|
|
548
|
-
* @example <caption>`style: spaceDelimited`</caption>
|
|
549
|
-
* `["blue","black","brown"]` → `blue%20black%20brown`
|
|
550
|
-
*/
|
|
551
|
-
case "spaceDelimited":
|
|
552
|
-
return value.map((val) => valueEncoder(val)).join(` ${explode ? `${key}=` : ""}`);
|
|
553
|
-
/**
|
|
554
|
-
* @example <caption>`style: pipeDelimited`</caption>
|
|
555
|
-
* `["blue","black","brown"]` → `blue|black|brown`
|
|
556
|
-
*/
|
|
557
|
-
case "pipeDelimited":
|
|
558
|
-
return value.map((val) => valueEncoder(val)).join(`|${explode ? `${key}=` : ""}`);
|
|
559
|
-
default:
|
|
560
|
-
}
|
|
434
|
+
/**
|
|
435
|
+
* @see {@link https://spec.openapis.org/oas/v3.0.0.html#style-examples}
|
|
436
|
+
* @see {@link https://spec.openapis.org/oas/v3.1.0.html#style-examples}
|
|
437
|
+
* @see {@link https://spec.openapis.org/oas/v3.2.0.html#style-examples}
|
|
438
|
+
*/
|
|
439
|
+
function encodeArray({ location, key, value, style, explode, escape, isAllowedReserved = false }) {
|
|
440
|
+
const valueEncoder = (str) => {
|
|
441
|
+
if (str === null) return "null";
|
|
442
|
+
return encodeDisallowedCharacters(str, {
|
|
443
|
+
escape,
|
|
444
|
+
returnIfEncoded: location === "query",
|
|
445
|
+
isAllowedReserved
|
|
446
|
+
});
|
|
447
|
+
};
|
|
448
|
+
switch (style) {
|
|
449
|
+
/**
|
|
450
|
+
* @example <caption>`style: simple`</caption>
|
|
451
|
+
* `["blue","black","brown"]` → `blue,black,brown`
|
|
452
|
+
*/
|
|
453
|
+
case "simple": return value.map((val) => valueEncoder(val)).join(",");
|
|
454
|
+
/**
|
|
455
|
+
* @example <caption>`style: label`</caption>
|
|
456
|
+
* `["blue","black","brown"]` → `.blue.black.brown`
|
|
457
|
+
*/
|
|
458
|
+
case "label": return `.${value.map((val) => valueEncoder(val)).join(".")}`;
|
|
459
|
+
/**
|
|
460
|
+
* @example <caption>`style: matrix` + `explode: true`</caption>
|
|
461
|
+
* `["blue","black","brown"]` → `;color=blue;color=black;color=brown`
|
|
462
|
+
*
|
|
463
|
+
* @example <caption>`style: matrix` + `explode: false` (the default behavior)</caption>
|
|
464
|
+
* `["blue","black","brown"]` → `;color=blue,black,brown `
|
|
465
|
+
*/
|
|
466
|
+
case "matrix": return value.map((val) => valueEncoder(val)).reduce((prev, curr) => {
|
|
467
|
+
if (!prev || explode) return `${prev || ""};${key}=${curr}`;
|
|
468
|
+
return `${prev},${curr}`;
|
|
469
|
+
}, "");
|
|
470
|
+
/**
|
|
471
|
+
* @example <caption>`style: form` + `explode: true`</caption>
|
|
472
|
+
* `["blue","black","brown"]` → `color=blue&color=black&color=brown`
|
|
473
|
+
*
|
|
474
|
+
* @example <caption>`style: form` + `explode: false` (the default behavior)</caption>
|
|
475
|
+
* `["blue","black","brown"]` → `color=blue,black,brown`
|
|
476
|
+
*/
|
|
477
|
+
case "form": return value.map((val) => valueEncoder(val)).join(explode ? `&${key}=` : ",");
|
|
478
|
+
/**
|
|
479
|
+
* @example <caption>`style: spaceDelimited`</caption>
|
|
480
|
+
* `["blue","black","brown"]` → `blue%20black%20brown`
|
|
481
|
+
*/
|
|
482
|
+
case "spaceDelimited": return value.map((val) => valueEncoder(val)).join(` ${explode ? `${key}=` : ""}`);
|
|
483
|
+
/**
|
|
484
|
+
* @example <caption>`style: pipeDelimited`</caption>
|
|
485
|
+
* `["blue","black","brown"]` → `blue|black|brown`
|
|
486
|
+
*/
|
|
487
|
+
case "pipeDelimited": return value.map((val) => valueEncoder(val)).join(`|${explode ? `${key}=` : ""}`);
|
|
488
|
+
}
|
|
561
489
|
}
|
|
490
|
+
/**
|
|
491
|
+
* @see {@link https://spec.openapis.org/oas/v3.0.0.html#style-examples}
|
|
492
|
+
* @see {@link https://spec.openapis.org/oas/v3.1.0.html#style-examples}
|
|
493
|
+
* @see {@link https://spec.openapis.org/oas/v3.2.0.html#style-examples}
|
|
494
|
+
*/
|
|
562
495
|
function encodeObject({ location, key, value, style, explode, escape, isAllowedReserved = false }) {
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
*/
|
|
646
|
-
case "pipeDelimited":
|
|
647
|
-
return valueKeys.reduce((prev, curr) => {
|
|
648
|
-
const val = valueEncoder(value[curr]);
|
|
649
|
-
const prefix = prev ? `${prev}|` : "";
|
|
650
|
-
return `${prefix}${curr}|${val}`;
|
|
651
|
-
}, "");
|
|
652
|
-
/**
|
|
653
|
-
* @example <caption>`style: deepObject`</caption>
|
|
654
|
-
* `{ "R": 100, "G": 200, "B": 150 }` → `color[R]=100&color[G]=200&color[B]=150`
|
|
655
|
-
*/
|
|
656
|
-
case "deepObject":
|
|
657
|
-
return valueKeys.reduce((curr) => {
|
|
658
|
-
const val = valueEncoder(value[curr]);
|
|
659
|
-
return `${val}`;
|
|
660
|
-
}, "");
|
|
661
|
-
default:
|
|
662
|
-
}
|
|
496
|
+
const valueEncoder = (str) => encodeDisallowedCharacters(str, {
|
|
497
|
+
escape,
|
|
498
|
+
returnIfEncoded: location === "query",
|
|
499
|
+
isAllowedReserved
|
|
500
|
+
});
|
|
501
|
+
const valueKeys = Object.keys(value);
|
|
502
|
+
switch (style) {
|
|
503
|
+
/**
|
|
504
|
+
* @example <caption>`style: simple` + `explode: true`</caption>
|
|
505
|
+
* `{ "R": 100, "G": 200, "B": 150 }` → `R=100,G=200,B=150`
|
|
506
|
+
*
|
|
507
|
+
* @example <caption>`style: simple` + `explode: false` (the default behavior)</caption>
|
|
508
|
+
* `{ "R": 100, "G": 200, "B": 150 }` → `R,100,G,200,B,150`
|
|
509
|
+
*/
|
|
510
|
+
case "simple": return valueKeys.reduce((prev, curr) => {
|
|
511
|
+
const val = valueEncoder(value[curr]);
|
|
512
|
+
const middleChar = explode ? "=" : ",";
|
|
513
|
+
return `${prev ? `${prev},` : ""}${curr}${middleChar}${val}`;
|
|
514
|
+
}, "");
|
|
515
|
+
/**
|
|
516
|
+
* @example <caption>`style: label` + `explode: true`</caption>
|
|
517
|
+
* `{ "R": 100, "G": 200, "B": 150 }` → `.R=100.G=200.B=150`
|
|
518
|
+
*
|
|
519
|
+
* @example <caption>`style: label` + `explode: false` (the default behavior)</caption>
|
|
520
|
+
* `{ "R": 100, "G": 200, "B": 150 }` → `.R.100.G.200.B.150`
|
|
521
|
+
*/
|
|
522
|
+
case "label": return valueKeys.reduce((prev, curr) => {
|
|
523
|
+
const val = valueEncoder(value[curr]);
|
|
524
|
+
const middleChar = explode ? "=" : ".";
|
|
525
|
+
return `${prev ? `${prev}.` : "."}${curr}${middleChar}${val}`;
|
|
526
|
+
}, "");
|
|
527
|
+
/**
|
|
528
|
+
* @example <caption>`style: matrix` + `explode: true`</caption>
|
|
529
|
+
* `{ "R": 100, "G": 200, "B": 150 }` → `;R=100;G=200;B=150`
|
|
530
|
+
*
|
|
531
|
+
* @example <caption>`style: matrix` + `explode: false` (the default behavior)</caption>
|
|
532
|
+
* `{ "R": 100, "G": 200, "B": 150 }` → `;color=R,100,G,200,B,150`
|
|
533
|
+
*/
|
|
534
|
+
case "matrix":
|
|
535
|
+
if (explode) return valueKeys.reduce((prev, curr) => {
|
|
536
|
+
const val = valueEncoder(value[curr]);
|
|
537
|
+
return `${prev ? `${prev};` : ";"}${curr}=${val}`;
|
|
538
|
+
}, "");
|
|
539
|
+
return valueKeys.reduce((prev, curr) => {
|
|
540
|
+
const val = valueEncoder(value[curr]);
|
|
541
|
+
return `${prev ? `${prev},` : `;${key}=`}${curr},${val}`;
|
|
542
|
+
}, "");
|
|
543
|
+
/**
|
|
544
|
+
* @example <caption>`style: form` + `explode: true`</caption>
|
|
545
|
+
* `{ "R": 100, "G": 200, "B": 150 }` → `R=100&G=200&B=150`
|
|
546
|
+
*
|
|
547
|
+
* @example <caption>`style: form` + `explode: false` (the default behavior)</caption>
|
|
548
|
+
* `{ "R": 100, "G": 200, "B": 150 }` → `color=R,100,G,200,B,150`
|
|
549
|
+
*/
|
|
550
|
+
case "form": return valueKeys.reduce((prev, curr) => {
|
|
551
|
+
const val = valueEncoder(value[curr]);
|
|
552
|
+
return `${prev ? `${prev}${explode ? "&" : ","}` : ""}${curr}${explode ? "=" : ","}${val}`;
|
|
553
|
+
}, "");
|
|
554
|
+
/**
|
|
555
|
+
* @example <caption>`style: spaceDelimited`</caption>
|
|
556
|
+
* `{ "R": 100, "G": 200, "B": 150 }` → `R%20100%20G%20200%20B%20150`
|
|
557
|
+
*/
|
|
558
|
+
case "spaceDelimited": return valueKeys.reduce((prev, curr) => {
|
|
559
|
+
const val = valueEncoder(value[curr]);
|
|
560
|
+
return `${prev ? `${prev} ` : ""}${curr} ${val}`;
|
|
561
|
+
}, "");
|
|
562
|
+
/**
|
|
563
|
+
* @example <caption>`style: pipeDelimited`</caption>
|
|
564
|
+
* `{ "R": 100, "G": 200, "B": 150 }` → `R|100|G|200|B|150`
|
|
565
|
+
*/
|
|
566
|
+
case "pipeDelimited": return valueKeys.reduce((prev, curr) => {
|
|
567
|
+
const val = valueEncoder(value[curr]);
|
|
568
|
+
return `${prev ? `${prev}|` : ""}${curr}|${val}`;
|
|
569
|
+
}, "");
|
|
570
|
+
/**
|
|
571
|
+
* @example <caption>`style: deepObject`</caption>
|
|
572
|
+
* `{ "R": 100, "G": 200, "B": 150 }` → `color[R]=100&color[G]=200&color[B]=150`
|
|
573
|
+
*/
|
|
574
|
+
case "deepObject": return valueKeys.reduce((curr) => {
|
|
575
|
+
return `${valueEncoder(value[curr])}`;
|
|
576
|
+
}, "");
|
|
577
|
+
}
|
|
663
578
|
}
|
|
579
|
+
/**
|
|
580
|
+
* @see {@link https://spec.openapis.org/oas/v3.0.0.html#style-examples}
|
|
581
|
+
* @see {@link https://spec.openapis.org/oas/v3.1.0.html#style-examples}
|
|
582
|
+
* @see {@link https://spec.openapis.org/oas/v3.2.0.html#style-examples}
|
|
583
|
+
*/
|
|
664
584
|
function encodePrimitive({ location, key, value, style, escape, isAllowedReserved = false }) {
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
* @example <caption>`style: deepObject`</caption>
|
|
700
|
-
* `blue` → n/a
|
|
701
|
-
*/
|
|
702
|
-
case "deepObject":
|
|
703
|
-
return valueEncoder(value);
|
|
704
|
-
default:
|
|
705
|
-
}
|
|
585
|
+
const valueEncoder = (str) => encodeDisallowedCharacters(str, {
|
|
586
|
+
escape,
|
|
587
|
+
returnIfEncoded: location === "query" || location === "body",
|
|
588
|
+
isAllowedReserved
|
|
589
|
+
});
|
|
590
|
+
switch (style) {
|
|
591
|
+
/**
|
|
592
|
+
* @example <caption>`style: simple`</caption>
|
|
593
|
+
* `blue` → `blue`
|
|
594
|
+
*/
|
|
595
|
+
case "simple": return valueEncoder(value);
|
|
596
|
+
/**
|
|
597
|
+
* @example <caption>`style: label`</caption>
|
|
598
|
+
* `blue` → `.blue`
|
|
599
|
+
*/
|
|
600
|
+
case "label": return `.${valueEncoder(value)}`;
|
|
601
|
+
/**
|
|
602
|
+
* @example <caption>`style: matrix`</caption>
|
|
603
|
+
* `blue` → `;color=blue`
|
|
604
|
+
*/
|
|
605
|
+
case "matrix":
|
|
606
|
+
if (value === "") return `;${key}`;
|
|
607
|
+
return `;${key}=${valueEncoder(value)}`;
|
|
608
|
+
/**
|
|
609
|
+
* @example <caption>`style: form`</caption>
|
|
610
|
+
* `blue` → `color=blue`
|
|
611
|
+
*/
|
|
612
|
+
case "form": return valueEncoder(value);
|
|
613
|
+
/**
|
|
614
|
+
* @example <caption>`style: deepObject`</caption>
|
|
615
|
+
* `blue` → n/a
|
|
616
|
+
*/
|
|
617
|
+
case "deepObject": return valueEncoder(value);
|
|
618
|
+
}
|
|
706
619
|
}
|
|
707
620
|
|
|
708
621
|
// src/lib/style-formatting/index.ts
|
|
709
622
|
function shouldNotStyleEmptyValues(parameter) {
|
|
710
|
-
|
|
623
|
+
return [
|
|
624
|
+
"simple",
|
|
625
|
+
"spaceDelimited",
|
|
626
|
+
"pipeDelimited",
|
|
627
|
+
"deepObject"
|
|
628
|
+
].includes(parameter.style || "");
|
|
711
629
|
}
|
|
712
630
|
function shouldNotStyleReservedHeader(parameter) {
|
|
713
|
-
|
|
631
|
+
return [
|
|
632
|
+
"accept",
|
|
633
|
+
"authorization",
|
|
634
|
+
"content-type"
|
|
635
|
+
].includes(parameter.name.toLowerCase());
|
|
714
636
|
}
|
|
637
|
+
/**
|
|
638
|
+
* Note: This isn't necessarily part of the spec. Behavior for the value 'undefined' is, well,
|
|
639
|
+
* undefined. This code makes our system look better. If we wanted to be more accurate, we might
|
|
640
|
+
* want to remove this, restore the un-fixed behavior for undefined and have our UI pass in empty
|
|
641
|
+
* string instead of undefined.
|
|
642
|
+
*/
|
|
715
643
|
function removeUndefinedForPath(value) {
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
if (typeof finalValue === "object") {
|
|
727
|
-
Object.keys(finalValue).forEach((key) => {
|
|
728
|
-
finalValue[key] = finalValue[key] === void 0 ? "" : finalValue[key];
|
|
729
|
-
});
|
|
730
|
-
}
|
|
731
|
-
return finalValue;
|
|
644
|
+
let finalValue = value;
|
|
645
|
+
if (typeof finalValue === "undefined") return "";
|
|
646
|
+
if (Array.isArray(finalValue)) {
|
|
647
|
+
finalValue = finalValue.filter((val) => val !== void 0);
|
|
648
|
+
if (finalValue.length === 0) finalValue = "";
|
|
649
|
+
}
|
|
650
|
+
if (typeof finalValue === "object") Object.keys(finalValue).forEach((key) => {
|
|
651
|
+
finalValue[key] = finalValue[key] === void 0 ? "" : finalValue[key];
|
|
652
|
+
});
|
|
653
|
+
return finalValue;
|
|
732
654
|
}
|
|
733
655
|
function stylizeValue(value, parameter) {
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
656
|
+
let finalValue = value;
|
|
657
|
+
if (shouldNotStyleEmptyValues(parameter) && (typeof finalValue === "undefined" || finalValue === "")) {
|
|
658
|
+
if (parameter.in === "path") return "";
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
if (parameter.in === "path") finalValue = removeUndefinedForPath(finalValue);
|
|
662
|
+
/**
|
|
663
|
+
* Eventhough `accept`, `authorization`, and `content-type` headers can be defined as parameters,
|
|
664
|
+
* they should be completely ignored when it comes to serialization.
|
|
665
|
+
*
|
|
666
|
+
* > If `in` is "header" and the `name` field is "Accept", "Content-Type" or "Authorization", the
|
|
667
|
+
* > parameter definition SHALL be ignored.
|
|
668
|
+
*
|
|
669
|
+
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#fixed-fields-10}
|
|
670
|
+
*/
|
|
671
|
+
if (parameter.in === "header" && shouldNotStyleReservedHeader(parameter)) return value;
|
|
672
|
+
/**
|
|
673
|
+
* If content is present, we should use the content type to format the value. We also ignore the style and explode settings.
|
|
674
|
+
*
|
|
675
|
+
* @see {@link https://swagger.io/docs/specification/v3_0/describing-parameters/#schema-vs-content}
|
|
676
|
+
*/
|
|
677
|
+
if (parameter.content && (parameter.in === "query" || parameter.in === "header")) {
|
|
678
|
+
const contentType = getParameterContentType$1(parameter);
|
|
679
|
+
if (!contentType) return;
|
|
680
|
+
/**
|
|
681
|
+
* @todo Handle other content types
|
|
682
|
+
*/
|
|
683
|
+
let serialized;
|
|
684
|
+
if (matchesMimeType.json(contentType)) serialized = JSON.stringify(value);
|
|
685
|
+
else serialized = String(value);
|
|
686
|
+
return parameter.in === "query" ? encodeURIComponent(serialized) : serialized;
|
|
687
|
+
}
|
|
688
|
+
/**
|
|
689
|
+
* All parameter types have a default `style` format so if they don't have one prescribed we
|
|
690
|
+
* should still conform to what the spec defines.
|
|
691
|
+
*
|
|
692
|
+
* @see {@link https://spec.openapis.org/oas/v3.0.0.html#parameterStyle}
|
|
693
|
+
* @see {@link https://spec.openapis.org/oas/v3.1.0.html#parameterStyle}
|
|
694
|
+
* @see {@link https://spec.openapis.org/oas/v3.2.0.html#parameter-style}
|
|
695
|
+
*/
|
|
696
|
+
let style = parameter.style;
|
|
697
|
+
if (!style) {
|
|
698
|
+
if (parameter.in === "query") style = "form";
|
|
699
|
+
else if (parameter.in === "path") style = "simple";
|
|
700
|
+
else if (parameter.in === "header") style = "simple";
|
|
701
|
+
else if (parameter.in === "cookie") style = "form";
|
|
702
|
+
}
|
|
703
|
+
let explode = parameter.explode;
|
|
704
|
+
if (explode === void 0 && style === "form")
|
|
705
|
+
/**
|
|
706
|
+
* Per the spec if no `explode` is present but `style` is `form` then `explode` should default to `true`.
|
|
707
|
+
*
|
|
708
|
+
* @see {@link https://spec.openapis.org/oas/v3.0.0.html#parameterExplode}
|
|
709
|
+
* @see {@link https://spec.openapis.org/oas/v3.1.0.html#parameterExplode}
|
|
710
|
+
* @see {@link https://spec.openapis.org/oas/v3.2.0.html#parameter-explode}
|
|
711
|
+
*/
|
|
712
|
+
explode = true;
|
|
713
|
+
return stylize({
|
|
714
|
+
location: parameter.in,
|
|
715
|
+
value: finalValue,
|
|
716
|
+
key: parameter.name,
|
|
717
|
+
style,
|
|
718
|
+
explode,
|
|
719
|
+
/**
|
|
720
|
+
* @todo this parameter is optional to stylize. It defaults to false, and can accept falsy, truthy, or "unsafe".
|
|
721
|
+
* I do not know if it is correct for query to use this. See style-serializer for more info
|
|
722
|
+
*/
|
|
723
|
+
escape: true,
|
|
724
|
+
...parameter.in === "query" ? { isAllowedReserved: parameter.allowReserved || false } : {}
|
|
725
|
+
});
|
|
789
726
|
}
|
|
790
727
|
function handleDeepObject(value, parameter) {
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
label: split[0],
|
|
804
|
-
// `qs` will coerce null values into being `undefined` string but we want to preserve them.
|
|
805
|
-
value: split[1] === "undefined" ? null : split[1]
|
|
806
|
-
};
|
|
807
|
-
});
|
|
728
|
+
return qs.stringify(value, { encoder(str, defaultEncoder, charset, type) {
|
|
729
|
+
if (type === "key") {
|
|
730
|
+
const prefixedKey = str.split(/[[\]]/g).filter(Boolean).map((k) => `[${k}]`).join("");
|
|
731
|
+
return `${parameter.name}${prefixedKey}`;
|
|
732
|
+
} else if (type === "value") return stylizeValue(str, parameter);
|
|
733
|
+
} }).split("&").map((item) => {
|
|
734
|
+
const split = item.split("=");
|
|
735
|
+
return {
|
|
736
|
+
label: split[0],
|
|
737
|
+
value: split[1] === "undefined" ? null : split[1]
|
|
738
|
+
};
|
|
739
|
+
});
|
|
808
740
|
}
|
|
809
741
|
function handleExplode(value, parameter) {
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
} else {
|
|
832
|
-
newObj[key] = stylizeValue(value[key], parameter);
|
|
833
|
-
}
|
|
834
|
-
});
|
|
835
|
-
return newObj;
|
|
836
|
-
}
|
|
837
|
-
return stylizeValue(value, parameter);
|
|
742
|
+
if (Array.isArray(value) && parameter.schema && typeof parameter.schema === "object" && !isRef(parameter.schema) && parameter.schema.type === "array" && parameter.style === "deepObject") {
|
|
743
|
+
const newObj = {};
|
|
744
|
+
handleDeepObject(value, parameter).forEach((obj) => {
|
|
745
|
+
newObj[obj.label] = obj.value;
|
|
746
|
+
});
|
|
747
|
+
return newObj;
|
|
748
|
+
}
|
|
749
|
+
if (Array.isArray(value)) return value.map((val) => {
|
|
750
|
+
return stylizeValue(val, parameter);
|
|
751
|
+
});
|
|
752
|
+
if (typeof value === "object" && value !== null) {
|
|
753
|
+
const newObj = {};
|
|
754
|
+
Object.keys(value).forEach((key) => {
|
|
755
|
+
if (parameter.style === "deepObject") handleDeepObject(value, parameter).forEach((obj) => {
|
|
756
|
+
newObj[obj.label] = obj.value;
|
|
757
|
+
});
|
|
758
|
+
else newObj[key] = stylizeValue(value[key], parameter);
|
|
759
|
+
});
|
|
760
|
+
return newObj;
|
|
761
|
+
}
|
|
762
|
+
return stylizeValue(value, parameter);
|
|
838
763
|
}
|
|
839
764
|
function shouldExplode(parameter) {
|
|
840
|
-
|
|
841
|
-
// https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#style-examples
|
|
842
|
-
parameter.style === "deepObject") && // header and path doesn't explode into separate parameters like query and cookie do
|
|
843
|
-
parameter.in !== "header" && parameter.in !== "path" && !parameter.content;
|
|
765
|
+
return (parameter.explode || parameter.explode !== false && parameter.style === "form" || parameter.style === "deepObject") && parameter.in !== "header" && parameter.in !== "path" && !parameter.content;
|
|
844
766
|
}
|
|
845
767
|
function formatStyle(value, parameter) {
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
if (shouldExplode(parameter)) {
|
|
850
|
-
return handleExplode(value, parameter);
|
|
851
|
-
}
|
|
852
|
-
return stylizeValue(value, parameter);
|
|
768
|
+
if (!parameter.content && parameter.style === "deepObject" && (!value || typeof value !== "object" || parameter.explode === false)) return;
|
|
769
|
+
if (shouldExplode(parameter)) return handleExplode(value, parameter);
|
|
770
|
+
return stylizeValue(value, parameter);
|
|
853
771
|
}
|
|
854
772
|
|
|
855
773
|
// src/index.ts
|
|
774
|
+
/**
|
|
775
|
+
* Retrieves the value for a given parameter out of the bucket of values for its `in` type (e.g.
|
|
776
|
+
* `values.header`).
|
|
777
|
+
*
|
|
778
|
+
* HTTP header names are case-insensitive, but incoming header values (e.g. as normalized by
|
|
779
|
+
* `api/core`) are often lowercased while the OAS document defining the header parameter may use
|
|
780
|
+
* any casing (e.g. `X-Customer-Code`). So for headers we fall back to a case-insensitive lookup
|
|
781
|
+
* if there wasn't an exact match on the parameter name.
|
|
782
|
+
*/
|
|
856
783
|
function getParamValue(values, param, type) {
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
}
|
|
864
|
-
return bucket?.[param.name];
|
|
784
|
+
const bucket = values[type];
|
|
785
|
+
if (bucket && typeof bucket === "object" && !(param.name in bucket) && type === "header") {
|
|
786
|
+
const matchedKey = Object.keys(bucket).find((key) => key.toLowerCase() === param.name.toLowerCase());
|
|
787
|
+
if (matchedKey !== void 0) return bucket[matchedKey];
|
|
788
|
+
}
|
|
789
|
+
return bucket?.[param.name];
|
|
865
790
|
}
|
|
866
791
|
function formatter(values, param, type, onlyIfExists = false, api) {
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
if (Array.isArray(value)) {
|
|
895
|
-
return value;
|
|
896
|
-
}
|
|
897
|
-
return JSON.stringify(value);
|
|
898
|
-
}
|
|
899
|
-
if (value !== void 0) {
|
|
900
|
-
if (type === "query" || type === "header" && param.content) {
|
|
901
|
-
return formatStyle(value, param);
|
|
902
|
-
}
|
|
903
|
-
return value;
|
|
904
|
-
}
|
|
792
|
+
if (param.style) {
|
|
793
|
+
const value = getParamValue(values, param, type);
|
|
794
|
+
return formatStyle(value, param);
|
|
795
|
+
}
|
|
796
|
+
let value;
|
|
797
|
+
if (typeof getParamValue(values, param, type) !== "undefined") value = getParamValue(values, param, type);
|
|
798
|
+
else if (onlyIfExists && !param.required) value = void 0;
|
|
799
|
+
else if (param.required && param.schema && typeof param.schema === "object") {
|
|
800
|
+
const schema = isRef(param.schema) ? dereferenceRef(param.schema, api) : param.schema;
|
|
801
|
+
if (schema && typeof schema === "object" && !isRef(schema) && schema.default) value = schema.default;
|
|
802
|
+
else if (param.content) {
|
|
803
|
+
const contentType = getParameterContentType$1(param);
|
|
804
|
+
value = (contentType ? getParameterContentSchema(param, contentType, api) : null)?.default;
|
|
805
|
+
}
|
|
806
|
+
} else if (param.required && param.content) {
|
|
807
|
+
const contentType = getParameterContentType$1(param);
|
|
808
|
+
value = (contentType ? getParameterContentSchema(param, contentType, api) : null)?.default;
|
|
809
|
+
}
|
|
810
|
+
if (value === void 0 && type === "path") return param.name;
|
|
811
|
+
if (param.schema && typeof param.schema === "object" && !isRef(param.schema) && param.schema.type === "array" && param.schema.items && typeof param.schema.items === "object" && !Array.isArray(param.schema.items) && !isRef(param.schema.items) && param.schema.items.format === "binary") {
|
|
812
|
+
if (Array.isArray(value)) return value;
|
|
813
|
+
return JSON.stringify(value);
|
|
814
|
+
}
|
|
815
|
+
if (value !== void 0) {
|
|
816
|
+
if (type === "query" || type === "header" && (param.content || typeof value === "object" && value !== null)) return formatStyle(value, param);
|
|
817
|
+
return value;
|
|
818
|
+
}
|
|
905
819
|
}
|
|
906
820
|
function multipartBodyToFormatterParams(payload, oasMediaTypeObject, schema) {
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
explode: paramEncoding ? paramEncoding.explode : shouldDefaultNestedMultipartObject ? true : void 0,
|
|
927
|
-
required: schema.required && typeof schema.required === "boolean" && Boolean(schema.required) || Array.isArray(schema.required) && schema.required.includes(key),
|
|
928
|
-
schema: schema.properties[key],
|
|
929
|
-
in: "body"
|
|
930
|
-
};
|
|
931
|
-
}).filter(Boolean);
|
|
932
|
-
}
|
|
933
|
-
return [];
|
|
821
|
+
const encoding = oasMediaTypeObject.encoding;
|
|
822
|
+
if (typeof payload === "object" && payload !== null) return Object.keys(payload).map((key) => {
|
|
823
|
+
if (!schema.properties?.[key]) return false;
|
|
824
|
+
const paramEncoding = encoding ? encoding[key] : void 0;
|
|
825
|
+
const propertySchema = schema.properties[key];
|
|
826
|
+
const isObjectSchema = typeof propertySchema === "object" && propertySchema !== null && !isRef(propertySchema);
|
|
827
|
+
const hasObjectType = isObjectSchema && (Array.isArray(propertySchema.type) ? propertySchema.type.includes("object") : propertySchema.type === "object");
|
|
828
|
+
const hasObjectProperties = isObjectSchema && typeof propertySchema.properties === "object" && propertySchema.properties !== null;
|
|
829
|
+
const shouldDefaultNestedMultipartObject = !paramEncoding && (hasObjectType || hasObjectProperties);
|
|
830
|
+
return {
|
|
831
|
+
name: key,
|
|
832
|
+
style: paramEncoding ? paramEncoding.style : shouldDefaultNestedMultipartObject ? "deepObject" : void 0,
|
|
833
|
+
explode: paramEncoding ? paramEncoding.explode : shouldDefaultNestedMultipartObject ? true : void 0,
|
|
834
|
+
required: schema.required && typeof schema.required === "boolean" && Boolean(schema.required) || Array.isArray(schema.required) && schema.required.includes(key),
|
|
835
|
+
schema: schema.properties[key],
|
|
836
|
+
in: "body"
|
|
837
|
+
};
|
|
838
|
+
}).filter(Boolean);
|
|
839
|
+
return [];
|
|
934
840
|
}
|
|
935
|
-
|
|
936
|
-
|
|
841
|
+
const defaultFormDataTypes = Object.keys(jsonSchemaTypes).reduce((prev, curr) => {
|
|
842
|
+
return Object.assign(prev, { [curr]: {} });
|
|
937
843
|
}, {});
|
|
938
844
|
function getResponseContentType(content) {
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
}
|
|
947
|
-
return "application/json";
|
|
845
|
+
const types = Object.keys(content) || [];
|
|
846
|
+
if (types?.length) {
|
|
847
|
+
const jsonType = types.find((t) => matchesMimeType.json(t));
|
|
848
|
+
if (jsonType) return jsonType;
|
|
849
|
+
return types[0];
|
|
850
|
+
}
|
|
851
|
+
return "application/json";
|
|
948
852
|
}
|
|
949
853
|
function isPrimitive(val) {
|
|
950
|
-
|
|
854
|
+
return typeof val === "string" || typeof val === "number" || typeof val === "boolean";
|
|
951
855
|
}
|
|
952
856
|
function stringify(json) {
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
})
|
|
963
|
-
);
|
|
857
|
+
return JSON.stringify(removeUndefinedObjects(typeof json.RAW_BODY !== "undefined" ? json.RAW_BODY : json, {
|
|
858
|
+
preserveNullishArrays: true,
|
|
859
|
+
preserveEmptyArray: true,
|
|
860
|
+
preserveEmptyObject: {
|
|
861
|
+
arrayItem: false,
|
|
862
|
+
objectProperty: true,
|
|
863
|
+
root: true
|
|
864
|
+
}
|
|
865
|
+
}));
|
|
964
866
|
}
|
|
965
867
|
function stringifyParameter(param) {
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
return String(param);
|
|
970
|
-
}
|
|
971
|
-
return JSON.stringify(param);
|
|
868
|
+
if (param === null || isPrimitive(param)) return String(param);
|
|
869
|
+
else if (Array.isArray(param) && param.every(isPrimitive)) return String(param);
|
|
870
|
+
return JSON.stringify(param);
|
|
972
871
|
}
|
|
973
872
|
function appendHarValue(harParam, name, value, addtlData = {}) {
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
name,
|
|
987
|
-
value: String(value)
|
|
988
|
-
});
|
|
989
|
-
}
|
|
873
|
+
if (typeof value === "undefined") return;
|
|
874
|
+
if (Array.isArray(value)) value.forEach((singleValue) => {
|
|
875
|
+
appendHarValue(harParam, name, singleValue);
|
|
876
|
+
});
|
|
877
|
+
else if (typeof value === "object" && value !== null) Object.keys(value).forEach((key) => {
|
|
878
|
+
appendHarValue(harParam, key, value[key]);
|
|
879
|
+
});
|
|
880
|
+
else harParam.push({
|
|
881
|
+
...addtlData,
|
|
882
|
+
name,
|
|
883
|
+
value: String(value)
|
|
884
|
+
});
|
|
990
885
|
}
|
|
991
886
|
function encodeBodyForHAR(body) {
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
return stringify(body.RAW_BODY);
|
|
999
|
-
}
|
|
1000
|
-
return stringify(body);
|
|
887
|
+
if (isPrimitive(body)) return body;
|
|
888
|
+
else if (typeof body === "object" && body !== null && !Array.isArray(body) && typeof body.RAW_BODY !== "undefined") {
|
|
889
|
+
if (isPrimitive(body.RAW_BODY)) return body.RAW_BODY;
|
|
890
|
+
return stringify(body.RAW_BODY);
|
|
891
|
+
}
|
|
892
|
+
return stringify(body);
|
|
1001
893
|
}
|
|
1002
894
|
function oasToHar(oas, operationSchema, values = {}, auth = {}, opts = { proxyUrl: "" }) {
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
if ((har.postData?.text || requestBody?.schema && Object.keys(requestBody.schema).length) && !hasContentType) {
|
|
1274
|
-
har.headers.push({
|
|
1275
|
-
name: "content-type",
|
|
1276
|
-
value: contentType
|
|
1277
|
-
});
|
|
1278
|
-
}
|
|
1279
|
-
const securityRequirements = operation.getSecurity();
|
|
1280
|
-
if (securityRequirements?.length) {
|
|
1281
|
-
securityRequirements.forEach((schemes) => {
|
|
1282
|
-
Object.keys(schemes).forEach((security) => {
|
|
1283
|
-
const securityValue = configureSecurity(apiDefinition, auth, security);
|
|
1284
|
-
if (!securityValue) {
|
|
1285
|
-
return;
|
|
1286
|
-
}
|
|
1287
|
-
if (securityValue.value.name === "authorization") {
|
|
1288
|
-
if (har[securityValue.type].find((v) => v.name === securityValue.value.name)) {
|
|
1289
|
-
return;
|
|
1290
|
-
}
|
|
1291
|
-
}
|
|
1292
|
-
if (har[securityValue.type].find(
|
|
1293
|
-
(v) => v.name === securityValue.value.name && v.value === securityValue.value.value
|
|
1294
|
-
)) {
|
|
1295
|
-
return;
|
|
1296
|
-
}
|
|
1297
|
-
har[securityValue.type].push(securityValue.value);
|
|
1298
|
-
});
|
|
1299
|
-
});
|
|
1300
|
-
}
|
|
1301
|
-
if (Object.keys(har.postData || {}).length === 0) {
|
|
1302
|
-
delete har.postData;
|
|
1303
|
-
}
|
|
1304
|
-
return {
|
|
1305
|
-
log: {
|
|
1306
|
-
entries: [
|
|
1307
|
-
{
|
|
1308
|
-
request: har
|
|
1309
|
-
}
|
|
1310
|
-
]
|
|
1311
|
-
}
|
|
1312
|
-
};
|
|
895
|
+
let operation;
|
|
896
|
+
if (!operationSchema || typeof operationSchema.getParameters !== "function")
|
|
897
|
+
/**
|
|
898
|
+
* If `operationSchema` was supplied as a plain object instead of an instance of `Operation`
|
|
899
|
+
* then we should create a new instance of it. We're doing it with a check on `getParameters`
|
|
900
|
+
* instead of checking `instanceof Operation` because JS is very weird when it comes to
|
|
901
|
+
* checking `instanceof` against classes. One instance of `Operation` may not always match up
|
|
902
|
+
* with another if they're being loaded between two different libraries.
|
|
903
|
+
*
|
|
904
|
+
* It's weird. This is easier.
|
|
905
|
+
*/
|
|
906
|
+
operation = new Operation(oas, operationSchema?.path || "", operationSchema?.method || "", operationSchema || {
|
|
907
|
+
path: "",
|
|
908
|
+
method: ""
|
|
909
|
+
});
|
|
910
|
+
else operation = operationSchema;
|
|
911
|
+
const apiDefinition = oas.getDefinition();
|
|
912
|
+
const formData = {
|
|
913
|
+
...defaultFormDataTypes,
|
|
914
|
+
...values
|
|
915
|
+
};
|
|
916
|
+
const shouldSerializeBody = Object.prototype.hasOwnProperty.call(values, "body") && values.body !== void 0;
|
|
917
|
+
if (!formData.server) formData.server = {
|
|
918
|
+
selected: 0,
|
|
919
|
+
variables: operation.defaultVariables(0)
|
|
920
|
+
};
|
|
921
|
+
formData.server.variables = {
|
|
922
|
+
...operation.defaultVariables(formData.server.selected),
|
|
923
|
+
...formData.server.variables ? formData.server.variables : {}
|
|
924
|
+
};
|
|
925
|
+
const har = {
|
|
926
|
+
cookies: [],
|
|
927
|
+
headers: [],
|
|
928
|
+
headersSize: 0,
|
|
929
|
+
queryString: [],
|
|
930
|
+
postData: {},
|
|
931
|
+
bodySize: 0,
|
|
932
|
+
method: operation.method.toUpperCase(),
|
|
933
|
+
url: `${operation.url(formData.server.selected, formData.server.variables)}${operation.path}`.replace(/\s/g, "%20"),
|
|
934
|
+
httpVersion: "HTTP/1.1"
|
|
935
|
+
};
|
|
936
|
+
if (opts.proxyUrl) {
|
|
937
|
+
if (oas.getExtension(PROXY_ENABLED, operation)) har.url = `${opts.proxyUrl}/${har.url}`;
|
|
938
|
+
}
|
|
939
|
+
const parameters = operation.getParameters();
|
|
940
|
+
har.url = har.url.replace(/{([-_a-zA-Z0-9[\]]+)}/g, (full, key) => {
|
|
941
|
+
if (!operation || !parameters) return key;
|
|
942
|
+
const parameter = parameters.find((param) => param.name === key) || { name: key };
|
|
943
|
+
if (!parameter.style) return encodeURIComponent(formatter(formData, parameter, "path", false, oas.api));
|
|
944
|
+
return formatter(formData, parameter, "path", false, oas.api);
|
|
945
|
+
});
|
|
946
|
+
const queryStrings = parameters?.filter((param) => param.in === "query");
|
|
947
|
+
if (queryStrings?.length) queryStrings.forEach((queryString) => {
|
|
948
|
+
const value = formatter(formData, queryString, "query", true, oas.api);
|
|
949
|
+
appendHarValue(har.queryString, queryString.name, value);
|
|
950
|
+
});
|
|
951
|
+
const cookies = parameters?.filter((param) => param.in === "cookie");
|
|
952
|
+
if (cookies?.length) cookies.forEach((cookie) => {
|
|
953
|
+
const value = formatter(formData, cookie, "cookie", true, oas.api);
|
|
954
|
+
appendHarValue(har.cookies, cookie.name, value);
|
|
955
|
+
});
|
|
956
|
+
if (operation.schema.responses) Object.keys(operation.schema.responses).some((statusCode) => {
|
|
957
|
+
const response = operation.getResponseByStatusCode(statusCode);
|
|
958
|
+
if (!response) return false;
|
|
959
|
+
const content = response.content;
|
|
960
|
+
if (!content) return false;
|
|
961
|
+
if (Object.keys(formData.header || {}).find((h) => h.toLowerCase() === "accept")) return true;
|
|
962
|
+
har.headers.push({
|
|
963
|
+
name: "accept",
|
|
964
|
+
value: getResponseContentType(content)
|
|
965
|
+
});
|
|
966
|
+
return true;
|
|
967
|
+
});
|
|
968
|
+
let hasContentType = false;
|
|
969
|
+
let contentType = operation.getContentType();
|
|
970
|
+
const headers = parameters?.filter((param) => param.in === "header");
|
|
971
|
+
if (headers?.length) headers.forEach((header) => {
|
|
972
|
+
const value = formatter(formData, header, "header", true, oas.api);
|
|
973
|
+
if (typeof value === "undefined") return;
|
|
974
|
+
if (header.name.toLowerCase() === "content-type") {
|
|
975
|
+
hasContentType = true;
|
|
976
|
+
contentType = String(value);
|
|
977
|
+
}
|
|
978
|
+
appendHarValue(har.headers, header.name, value);
|
|
979
|
+
});
|
|
980
|
+
const userDefinedHeaders = oas.getExtension(HEADERS, operation);
|
|
981
|
+
if (userDefinedHeaders) userDefinedHeaders.forEach((header) => {
|
|
982
|
+
if (typeof header.key === "string" && header.key.toLowerCase() === "content-type") {
|
|
983
|
+
hasContentType = true;
|
|
984
|
+
contentType = String(header.value);
|
|
985
|
+
}
|
|
986
|
+
har.headers.push({
|
|
987
|
+
name: String(header.key),
|
|
988
|
+
value: String(header.value)
|
|
989
|
+
});
|
|
990
|
+
});
|
|
991
|
+
if (formData.header) {
|
|
992
|
+
const contentTypeFormHeader = Object.keys(formData.header).find((h) => h.toLowerCase() === "content-type");
|
|
993
|
+
if (contentTypeFormHeader && !har.headers.find((hdr) => hdr.name.toLowerCase() === "content-type")) {
|
|
994
|
+
hasContentType = true;
|
|
995
|
+
contentType = String(formData.header[contentTypeFormHeader]);
|
|
996
|
+
har.headers.push({
|
|
997
|
+
name: "content-type",
|
|
998
|
+
value: contentType
|
|
999
|
+
});
|
|
1000
|
+
}
|
|
1001
|
+
const acceptHeader = Object.keys(formData.header).find((h) => h.toLowerCase() === "accept");
|
|
1002
|
+
if (acceptHeader && !har.headers.find((hdr) => hdr.name.toLowerCase() === "accept")) har.headers.push({
|
|
1003
|
+
name: "accept",
|
|
1004
|
+
value: String(formData.header[acceptHeader])
|
|
1005
|
+
});
|
|
1006
|
+
const authorizationHeader = Object.keys(formData.header).find((h) => h.toLowerCase() === "authorization");
|
|
1007
|
+
if (authorizationHeader && !har.headers.find((hdr) => hdr.name.toLowerCase() === "authorization")) har.headers.push({
|
|
1008
|
+
name: "authorization",
|
|
1009
|
+
value: String(formData.header[authorizationHeader])
|
|
1010
|
+
});
|
|
1011
|
+
}
|
|
1012
|
+
let requestBody;
|
|
1013
|
+
if (operation.hasRequestBody()) {
|
|
1014
|
+
const isFormUrlEncoded = matchesMimeType.formUrlEncoded(contentType);
|
|
1015
|
+
const matchesRequestBodyType = (payload) => {
|
|
1016
|
+
return payload.type === (isFormUrlEncoded ? "formData" : "body");
|
|
1017
|
+
};
|
|
1018
|
+
requestBody = operation.getParametersAsJSONSchema({ contentType })?.find(matchesRequestBodyType);
|
|
1019
|
+
}
|
|
1020
|
+
if (requestBody?.schema && Object.keys(requestBody.schema).length) {
|
|
1021
|
+
const requestBodySchema = requestBody.schema;
|
|
1022
|
+
if (matchesMimeType.formUrlEncoded(contentType)) {
|
|
1023
|
+
if (Object.keys(formData.formData || {}).length) {
|
|
1024
|
+
const cleanFormData = removeUndefinedObjects(formData.formData, { preserveNullishArrays: true });
|
|
1025
|
+
if (cleanFormData !== void 0) {
|
|
1026
|
+
const postData = {
|
|
1027
|
+
params: [],
|
|
1028
|
+
mimeType: "application/x-www-form-urlencoded"
|
|
1029
|
+
};
|
|
1030
|
+
Object.keys(cleanFormData).forEach((name) => {
|
|
1031
|
+
postData.params.push({
|
|
1032
|
+
name,
|
|
1033
|
+
value: stringifyParameter(cleanFormData[name])
|
|
1034
|
+
});
|
|
1035
|
+
});
|
|
1036
|
+
har.postData = postData;
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
} else if (shouldSerializeBody) {
|
|
1040
|
+
const isMultipart = matchesMimeType.multipart(contentType);
|
|
1041
|
+
const isJSON = matchesMimeType.json(contentType);
|
|
1042
|
+
if (isMultipart || isJSON) try {
|
|
1043
|
+
let cleanBody = removeUndefinedObjects(formData.body, {
|
|
1044
|
+
preserveNullishArrays: true,
|
|
1045
|
+
preserveEmptyArray: true,
|
|
1046
|
+
preserveEmptyObject: {
|
|
1047
|
+
arrayItem: false,
|
|
1048
|
+
objectProperty: true,
|
|
1049
|
+
root: true
|
|
1050
|
+
}
|
|
1051
|
+
});
|
|
1052
|
+
if (isMultipart) {
|
|
1053
|
+
har.postData = {
|
|
1054
|
+
params: [],
|
|
1055
|
+
mimeType: "multipart/form-data"
|
|
1056
|
+
};
|
|
1057
|
+
const safeBodySchema = getSafeRequestBody(requestBodySchema, formData.body, operation.api);
|
|
1058
|
+
/**
|
|
1059
|
+
* Discover all `{ type: string, format: binary }` properties, or arrays containing the
|
|
1060
|
+
* same, within the request body. If there are any, then that means that we're dealing
|
|
1061
|
+
* with a `multipart/form-data` request and need to treat the payload as
|
|
1062
|
+
* `postData.params` and supply filenames and content types for the files (if they're
|
|
1063
|
+
* available).
|
|
1064
|
+
*
|
|
1065
|
+
* @todo It'd be nice to replace this with `getTypedFormatsInSchema` instead.
|
|
1066
|
+
* @example `{ type: string, format: binary }`
|
|
1067
|
+
* @example `{ type: array, items: { type: string, format: binary } }`
|
|
1068
|
+
*/
|
|
1069
|
+
const binaryTypes = Object.keys(safeBodySchema.properties ?? {}).filter((key) => {
|
|
1070
|
+
if (!safeBodySchema.properties?.[key] || typeof safeBodySchema.properties[key] !== "object" || safeBodySchema.properties[key] === null) return false;
|
|
1071
|
+
const propData = safeBodySchema.properties[key];
|
|
1072
|
+
if (propData.format === "binary") return true;
|
|
1073
|
+
else if (propData.type === "array" && propData.items && typeof propData.items === "object" && propData.items !== null && propData.items.format === "binary") return true;
|
|
1074
|
+
return false;
|
|
1075
|
+
});
|
|
1076
|
+
if (cleanBody !== void 0) {
|
|
1077
|
+
let multipartParams = [];
|
|
1078
|
+
const multipartContent = operation.getRequestBody(contentType);
|
|
1079
|
+
if (multipartContent) multipartParams = multipartBodyToFormatterParams(formData.body, multipartContent.mediaTypeObject, safeBodySchema);
|
|
1080
|
+
if (multipartParams.length) Object.keys(cleanBody).forEach((name) => {
|
|
1081
|
+
const param = multipartParams.find((multipartParam) => multipartParam.name === name);
|
|
1082
|
+
if (param) {
|
|
1083
|
+
const addtlData = {};
|
|
1084
|
+
let value = formatter(formData, param, "body", true, oas.api);
|
|
1085
|
+
if (!Array.isArray(value)) value = [value];
|
|
1086
|
+
value.forEach((val) => {
|
|
1087
|
+
if (binaryTypes.includes(name)) {
|
|
1088
|
+
const parsed = parse(val);
|
|
1089
|
+
if (parsed) {
|
|
1090
|
+
addtlData.fileName = "name" in parsed ? parsed.name : "unknown";
|
|
1091
|
+
if ("contentType" in parsed) addtlData.contentType = parsed.contentType;
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
appendHarValue(har.postData?.params || [], name, val, addtlData);
|
|
1095
|
+
});
|
|
1096
|
+
}
|
|
1097
|
+
});
|
|
1098
|
+
}
|
|
1099
|
+
} else {
|
|
1100
|
+
har.postData = {
|
|
1101
|
+
mimeType: contentType,
|
|
1102
|
+
text: ""
|
|
1103
|
+
};
|
|
1104
|
+
if (hasSchemaType(requestBody.schema, "string") || hasSchemaType(requestBody.schema, "integer") || hasSchemaType(requestBody.schema, "number") || hasSchemaType(requestBody.schema, "boolean")) har.postData.text = JSON.stringify(JSON.parse(cleanBody));
|
|
1105
|
+
else {
|
|
1106
|
+
/**
|
|
1107
|
+
* Handle formatted JSON objects that have properties that accept arbitrary JSON.
|
|
1108
|
+
*
|
|
1109
|
+
* Find all `{ type: string, format: json }` properties in the schema because we need
|
|
1110
|
+
* to manually `JSON.parse` them before submit, otherwise they'll be escaped instead
|
|
1111
|
+
* of actual objects. We also only want values that the user has entered, so we drop
|
|
1112
|
+
* any `undefined` `cleanBody` keys.
|
|
1113
|
+
*/
|
|
1114
|
+
const jsonTypes = getTypedFormatsInSchema("json", requestBodySchema, operation.api, { payload: cleanBody });
|
|
1115
|
+
if (Array.isArray(jsonTypes) && jsonTypes.length) try {
|
|
1116
|
+
jsonTypes.forEach((prop) => {
|
|
1117
|
+
try {
|
|
1118
|
+
set(cleanBody, String(prop), JSON.parse(get(cleanBody, String(prop))));
|
|
1119
|
+
} catch {}
|
|
1120
|
+
});
|
|
1121
|
+
if (typeof cleanBody.RAW_BODY !== "undefined") cleanBody = cleanBody.RAW_BODY;
|
|
1122
|
+
har.postData.text = JSON.stringify(cleanBody);
|
|
1123
|
+
} catch {
|
|
1124
|
+
har.postData.text = stringify(formData.body);
|
|
1125
|
+
}
|
|
1126
|
+
else try {
|
|
1127
|
+
const parsed = parseJSONStringsInBodyWithSchema(cleanBody, requestBodySchema, operation.api);
|
|
1128
|
+
if (typeof parsed?.RAW_BODY !== "undefined") har.postData.text = isPrimitive(parsed.RAW_BODY) ? String(parsed.RAW_BODY) : stringify(parsed.RAW_BODY);
|
|
1129
|
+
else har.postData.text = JSON.stringify(parsed);
|
|
1130
|
+
} catch {
|
|
1131
|
+
har.postData.text = encodeBodyForHAR(formData.body);
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
} catch {
|
|
1136
|
+
har.postData = {
|
|
1137
|
+
mimeType: contentType,
|
|
1138
|
+
text: stringify(formData.body)
|
|
1139
|
+
};
|
|
1140
|
+
}
|
|
1141
|
+
else har.postData = {
|
|
1142
|
+
mimeType: contentType,
|
|
1143
|
+
text: encodeBodyForHAR(formData.body)
|
|
1144
|
+
};
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
if ((har.postData?.text || requestBody?.schema && Object.keys(requestBody.schema).length) && !hasContentType) har.headers.push({
|
|
1148
|
+
name: "content-type",
|
|
1149
|
+
value: contentType
|
|
1150
|
+
});
|
|
1151
|
+
const securityRequirements = operation.getSecurity();
|
|
1152
|
+
if (securityRequirements?.length) securityRequirements.forEach((schemes) => {
|
|
1153
|
+
Object.keys(schemes).forEach((security) => {
|
|
1154
|
+
const securityValue = configureSecurity(apiDefinition, auth, security);
|
|
1155
|
+
if (!securityValue) return;
|
|
1156
|
+
if (securityValue.value.name === "authorization") {
|
|
1157
|
+
if (har[securityValue.type].find((v) => v.name === securityValue.value.name)) return;
|
|
1158
|
+
}
|
|
1159
|
+
if (har[securityValue.type].find((v) => v.name === securityValue.value.name && v.value === securityValue.value.value)) return;
|
|
1160
|
+
har[securityValue.type].push(securityValue.value);
|
|
1161
|
+
});
|
|
1162
|
+
});
|
|
1163
|
+
if (Object.keys(har.postData || {}).length === 0) delete har.postData;
|
|
1164
|
+
return { log: { entries: [{ request: har }] } };
|
|
1313
1165
|
}
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
};
|
|
1317
|
-
/**
|
|
1318
|
-
* This file has been extracted and modified from `swagger-client`.
|
|
1319
|
-
*
|
|
1320
|
-
* @license Apache 2.0
|
|
1321
|
-
* @link https://npm.im/swagger-client
|
|
1322
|
-
* @link https://github.com/swagger-api/swagger-js/blob/master/src/execute/oas3/style-serializer.js
|
|
1323
|
-
*/
|
|
1166
|
+
|
|
1167
|
+
export { oasToHar as default };
|
|
1324
1168
|
//# sourceMappingURL=index.js.map
|