@scalar/oas-utils 0.0.4 → 0.1.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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @scalar/oas-utils
2
2
 
3
+ ## 0.1.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 31aae5e: chore: moved shared types and methods into oas-utils
8
+
9
+ ## 0.1.0
10
+
11
+ ### Minor Changes
12
+
13
+ - 7fb8273: Migrate to @scalar/openapi-parser
14
+
3
15
  ## 0.0.4
4
16
 
5
17
  ### Patch Changes
@@ -0,0 +1,21 @@
1
+ /**
2
+ * This function takes a properties object and generates an example response content.
3
+ */
4
+ export declare const getExampleFromSchema: (schema: Record<string, any>, options?: {
5
+ /**
6
+ * The fallback string for empty string values.
7
+ * @default ''
8
+ **/
9
+ emptyString?: string;
10
+ /**
11
+ * Whether to use the XML tag names as keys
12
+ * @default false
13
+ */
14
+ xml?: boolean;
15
+ /**
16
+ * Whether to show read-only/write-only properties. Otherwise all properties are shown.
17
+ * @default undefined
18
+ */
19
+ mode?: 'read' | 'write';
20
+ }, level?: number) => any;
21
+ //# sourceMappingURL=getExampleFromSchema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getExampleFromSchema.d.ts","sourceRoot":"","sources":["../src/getExampleFromSchema.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,eAAO,MAAM,oBAAoB,WACvB,OAAO,MAAM,EAAE,GAAG,CAAC,YACjB;IACR;;;QAGI;IACJ,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB;;;OAGG;IACH,GAAG,CAAC,EAAE,OAAO,CAAA;IACb;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,GAAG,OAAO,CAAA;CACxB,UACM,MAAM,KACZ,GA+NF,CAAA"}
@@ -0,0 +1,4 @@
1
+ import type { HarRequest } from 'httpsnippet-lite';
2
+ import type { HarRequestWithPath } from './types';
3
+ export declare const getHarRequest: (...requests: Partial<HarRequestWithPath>[]) => HarRequest;
4
+ //# sourceMappingURL=getHarRequest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getHarRequest.d.ts","sourceRoot":"","sources":["../src/getHarRequest.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAElD,OAAO,KAAK,EAAU,kBAAkB,EAAiB,MAAM,SAAS,CAAA;AAExE,eAAO,MAAM,aAAa,gBACX,QAAQ,kBAAkB,CAAC,EAAE,KACzC,UAsDF,CAAA"}
@@ -0,0 +1,11 @@
1
+ import type { BaseParameter, TransformedOperation } from './types';
2
+ /**
3
+ * Get the query parameters from an operation.
4
+ *
5
+ * Example: [ { name: 'foobar', value: '' } ]
6
+ *
7
+ * - OpenAPI 3.x: Possible values are “query”, “header”, “path” or “cookie”.
8
+ * - Swagger 2.0: Possible values are "query", "header", "path", "formData" or "body".
9
+ */
10
+ export declare function getParametersFromOperation(operation: TransformedOperation, where: 'query' | 'header' | 'path' | 'cookie' | 'formData' | 'body', requiredOnly?: boolean): BaseParameter[];
11
+ //# sourceMappingURL=getParametersFromOperation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getParametersFromOperation.d.ts","sourceRoot":"","sources":["../src/getParametersFromOperation.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAA;AAElE;;;;;;;GAOG;AACH,wBAAgB,0BAA0B,CACxC,SAAS,EAAE,oBAAoB,EAC/B,KAAK,EAAE,OAAO,GAAG,QAAQ,GAAG,MAAM,GAAG,QAAQ,GAAG,UAAU,GAAG,MAAM,EACnE,YAAY,GAAE,OAAc,GAC3B,aAAa,EAAE,CAkCjB"}
@@ -0,0 +1,86 @@
1
+ import type { ContentType, TransformedOperation } from './types';
2
+ /**
3
+ * Get the request body from the operation.
4
+ */
5
+ export declare function getRequestBodyFromOperation(operation: TransformedOperation, selectedExampleKey?: string | number): {
6
+ postData: {
7
+ mimeType: string;
8
+ text: any;
9
+ params?: undefined;
10
+ };
11
+ headers?: undefined;
12
+ } | {
13
+ postData: {
14
+ mimeType: string;
15
+ params: {
16
+ name: string;
17
+ value: string | number | Record<string, any>;
18
+ }[];
19
+ text?: undefined;
20
+ };
21
+ headers?: undefined;
22
+ } | {
23
+ postData: undefined;
24
+ headers?: undefined;
25
+ } | {
26
+ headers: {
27
+ name: string;
28
+ value: ContentType;
29
+ }[];
30
+ postData: {
31
+ mimeType: "application/json";
32
+ text: string;
33
+ params?: undefined;
34
+ };
35
+ } | {
36
+ headers: {
37
+ name: string;
38
+ value: ContentType;
39
+ }[];
40
+ postData: {
41
+ mimeType: "application/xml";
42
+ text: any;
43
+ params?: undefined;
44
+ };
45
+ } | {
46
+ headers: {
47
+ name: string;
48
+ value: ContentType;
49
+ }[];
50
+ postData: {
51
+ mimeType: "application/octet-stream";
52
+ text: string;
53
+ params?: undefined;
54
+ };
55
+ } | {
56
+ headers: {
57
+ name: string;
58
+ value: ContentType;
59
+ }[];
60
+ postData: {
61
+ mimeType: "text/plain";
62
+ text: any;
63
+ params?: undefined;
64
+ };
65
+ } | {
66
+ headers: {
67
+ name: string;
68
+ value: ContentType;
69
+ }[];
70
+ postData: {
71
+ mimeType: "application/x-www-form-urlencoded";
72
+ text?: undefined;
73
+ params?: undefined;
74
+ };
75
+ } | {
76
+ headers: {
77
+ name: string;
78
+ value: ContentType;
79
+ }[];
80
+ postData: {
81
+ mimeType: "multipart/form-data";
82
+ text?: undefined;
83
+ params?: undefined;
84
+ };
85
+ } | undefined;
86
+ //# sourceMappingURL=getRequestBodyFromOperation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getRequestBodyFromOperation.d.ts","sourceRoot":"","sources":["../src/getRequestBodyFromOperation.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,WAAW,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAA;AAEhE;;GAEG;AACH,wBAAgB,2BAA2B,CACzC,SAAS,EAAE,oBAAoB,EAC/B,kBAAkB,CAAC,EAAE,MAAM,GAAG,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAuNrC"}
@@ -0,0 +1,6 @@
1
+ import type { HarRequestWithPath, TransformedOperation } from './types';
2
+ export declare const getRequestFromOperation: (operation: TransformedOperation, options?: {
3
+ replaceVariables?: boolean;
4
+ requiredOnly?: boolean;
5
+ }, selectedExampleKey?: string | number) => Partial<HarRequestWithPath>;
6
+ //# sourceMappingURL=getRequestFromOperation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getRequestFromOperation.d.ts","sourceRoot":"","sources":["../src/getRequestFromOperation.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAEV,kBAAkB,EAGlB,oBAAoB,EACrB,MAAM,SAAS,CAAA;AAEhB,eAAO,MAAM,uBAAuB,cACvB,oBAAoB,YACrB;IACR,gBAAgB,CAAC,EAAE,OAAO,CAAA;IAC1B,YAAY,CAAC,EAAE,OAAO,CAAA;CACvB,uBACoB,MAAM,GAAG,MAAM,KACnC,QAAQ,kBAAkB,CAsC5B,CAAA"}
package/dist/index.d.ts CHANGED
@@ -1,4 +1,11 @@
1
1
  export { fetchSpecFromUrl } from './fetch-spec';
2
- export * from './parse';
2
+ export { getExampleFromSchema } from './getExampleFromSchema';
3
+ export { getHarRequest } from './getHarRequest';
4
+ export { getParametersFromOperation } from './getParametersFromOperation';
5
+ export { getRequestBodyFromOperation } from './getRequestBodyFromOperation';
6
+ export { getRequestFromOperation } from './getRequestFromOperation';
7
+ export { json2xml } from './json2xml';
8
+ export { formatJsonOrYamlString, isJsonString, json, parseJsonOrYaml, transformToJson, yaml, } from './parse';
9
+ export { prettyPrintJson } from './prettyPrintJson';
3
10
  export * from './types';
4
11
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAA;AAC/C,cAAc,SAAS,CAAA;AACvB,cAAc,SAAS,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAA;AAC/C,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAA;AAC7D,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,0BAA0B,EAAE,MAAM,8BAA8B,CAAA;AACzE,OAAO,EAAE,2BAA2B,EAAE,MAAM,+BAA+B,CAAA;AAC3E,OAAO,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAA;AACnE,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AACrC,OAAO,EACL,sBAAsB,EACtB,YAAY,EACZ,IAAI,EACJ,eAAe,EACf,eAAe,EACf,IAAI,GACL,MAAM,SAAS,CAAA;AAChB,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,cAAc,SAAS,CAAA"}
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { parse, stringify } from 'yaml';
2
+ import { AxiosHeaders } from 'axios';
2
3
 
3
4
  const yaml = {
4
5
  /** Parse and throw if the return value is not an object */
@@ -89,4 +90,453 @@ async function fetchSpecFromUrl(url, proxy) {
89
90
  return formatJsonOrYamlString(payload);
90
91
  }
91
92
 
92
- export { fetchSpecFromUrl, formatJsonOrYamlString, isJsonString, json, parseJsonOrYaml, transformToJson, yaml };
93
+ const getExampleFromSchema = (schema, options, level = 0) => {
94
+ if (level > 5) {
95
+ return null;
96
+ }
97
+ if (options?.mode === "write" && schema.readOnly) {
98
+ return void 0;
99
+ }
100
+ if (options?.mode === "read" && schema.writeOnly) {
101
+ return void 0;
102
+ }
103
+ if (Array.isArray(schema.examples) && schema.examples.length > 0) {
104
+ return schema.examples[0];
105
+ }
106
+ if (schema.example !== void 0) {
107
+ return schema.example;
108
+ }
109
+ if (schema.default !== void 0) {
110
+ return schema.default;
111
+ }
112
+ if (schema.enum !== void 0) {
113
+ return schema.enum[0];
114
+ }
115
+ if (schema.type === "object" || schema.properties !== void 0) {
116
+ const response = {};
117
+ if (schema.properties !== void 0) {
118
+ Object.keys(schema.properties).forEach((name) => {
119
+ const property = schema.properties[name];
120
+ const propertyXmlTagName = options?.xml ? property.xml?.name : void 0;
121
+ response[propertyXmlTagName ?? name] = getExampleFromSchema(
122
+ property,
123
+ options,
124
+ level + 1
125
+ );
126
+ });
127
+ }
128
+ if (schema.anyOf !== void 0) {
129
+ Object.assign(
130
+ response,
131
+ getExampleFromSchema(schema.anyOf[0]),
132
+ options,
133
+ level + 1
134
+ );
135
+ } else if (schema.oneOf !== void 0) {
136
+ Object.assign(
137
+ response,
138
+ getExampleFromSchema(schema.oneOf[0]),
139
+ options,
140
+ level + 1
141
+ );
142
+ } else if (schema.allOf !== void 0) {
143
+ Object.assign(
144
+ response,
145
+ ...schema.allOf.map(
146
+ (item) => getExampleFromSchema(item, options, level + 1)
147
+ )
148
+ );
149
+ }
150
+ if (schema.additionalProperties !== void 0 && schema.additionalProperties !== false) {
151
+ const additionalSchema = getExampleFromSchema(
152
+ schema.additionalProperties,
153
+ options,
154
+ level + 1
155
+ );
156
+ if (additionalSchema && typeof additionalSchema === "object" && !Array.isArray(additionalSchema)) {
157
+ return {
158
+ ...response,
159
+ ...getExampleFromSchema(
160
+ schema.additionalProperties,
161
+ options,
162
+ level + 1
163
+ )
164
+ };
165
+ }
166
+ if (additionalSchema === null) {
167
+ return null;
168
+ }
169
+ return {
170
+ ...response,
171
+ someKey: getExampleFromSchema(
172
+ schema.additionalProperties,
173
+ options,
174
+ level + 1
175
+ )
176
+ };
177
+ }
178
+ return response;
179
+ }
180
+ if (schema.type === "array" || schema.items !== void 0) {
181
+ const itemsXmlTagName = schema?.items?.xml?.name;
182
+ const wrapItems = !!(options?.xml && schema.xml?.wrapped && itemsXmlTagName);
183
+ if (schema.example !== void 0) {
184
+ return wrapItems ? { [itemsXmlTagName]: schema.example } : schema.example;
185
+ }
186
+ if (schema.items) {
187
+ const rules = ["anyOf", "oneOf", "allOf"];
188
+ for (const rule of rules) {
189
+ if (!schema.items[rule]) {
190
+ continue;
191
+ }
192
+ const schemas = ["anyOf", "oneOf"].includes(rule) ? (
193
+ // Use the first item only
194
+ schema.items[rule].slice(0, 1)
195
+ ) : (
196
+ // Use all items
197
+ schema.items[rule]
198
+ );
199
+ const exampleFromRule = schemas.map(
200
+ (item) => getExampleFromSchema(item, options, level + 1)
201
+ );
202
+ return wrapItems ? [{ [itemsXmlTagName]: exampleFromRule }] : exampleFromRule;
203
+ }
204
+ }
205
+ if (schema.items?.type) {
206
+ const exampleFromSchema = getExampleFromSchema(
207
+ schema.items,
208
+ options,
209
+ level + 1
210
+ );
211
+ return wrapItems ? [{ [itemsXmlTagName]: exampleFromSchema }] : [exampleFromSchema];
212
+ }
213
+ return [];
214
+ }
215
+ const exampleValues = {
216
+ string: options?.emptyString ?? "",
217
+ boolean: true,
218
+ integer: schema.min ?? 1,
219
+ number: schema.min ?? 1,
220
+ array: []
221
+ };
222
+ if (schema.type !== void 0 && exampleValues[schema.type] !== void 0) {
223
+ return exampleValues[schema.type];
224
+ }
225
+ if (Array.isArray(schema.oneOf) && schema.oneOf.length > 0) {
226
+ const firstOneOfItem = schema.oneOf[0];
227
+ return getExampleFromSchema(firstOneOfItem, options, level + 1);
228
+ }
229
+ if (Array.isArray(schema.allOf)) {
230
+ let example = null;
231
+ schema.allOf.forEach((allOfItem) => {
232
+ const newExample = getExampleFromSchema(allOfItem, options, level + 1);
233
+ example = typeof newExample === "object" && typeof example === "object" ? {
234
+ ...example ?? {},
235
+ ...newExample
236
+ } : Array.isArray(newExample) && Array.isArray(example) ? [...example ?? {}, ...newExample] : newExample;
237
+ });
238
+ return example;
239
+ }
240
+ console.warn(`[getExampleFromSchema] Unknown property type "${schema.type}".`);
241
+ return null;
242
+ };
243
+
244
+ const getHarRequest = (...requests) => {
245
+ let mergedRequests = {
246
+ httpVersion: "1.1",
247
+ method: "GET",
248
+ url: "",
249
+ path: "",
250
+ headers: [],
251
+ headersSize: -1,
252
+ queryString: [],
253
+ cookies: [],
254
+ bodySize: -1
255
+ };
256
+ requests.forEach((request) => {
257
+ mergedRequests = {
258
+ ...mergedRequests,
259
+ ...request,
260
+ headers: [...mergedRequests.headers, ...request.headers ?? []],
261
+ queryString: [
262
+ ...mergedRequests.queryString,
263
+ ...request.queryString ?? []
264
+ ],
265
+ cookies: [...mergedRequests.cookies, ...request.cookies ?? []]
266
+ };
267
+ });
268
+ const headersObj = mergedRequests.headers.reduce(
269
+ (obj, { name, value }) => {
270
+ obj[name] = value;
271
+ return obj;
272
+ },
273
+ {}
274
+ );
275
+ const normalizedAxiosHeaders = AxiosHeaders.from(headersObj).normalize(true);
276
+ mergedRequests.headers = Object.entries(normalizedAxiosHeaders).map(
277
+ ([name, value]) => ({ name, value })
278
+ );
279
+ const { path, ...result } = mergedRequests;
280
+ if (path) {
281
+ return {
282
+ ...result,
283
+ url: `${mergedRequests.url}${path}`
284
+ };
285
+ }
286
+ return result;
287
+ };
288
+
289
+ function getParametersFromOperation(operation, where, requiredOnly = true) {
290
+ const parameters = [
291
+ ...operation.pathParameters || [],
292
+ ...operation.information?.parameters || []
293
+ ];
294
+ const params = parameters.filter((parameter) => parameter.in === where).filter(
295
+ (parameter) => requiredOnly && parameter.required || !requiredOnly
296
+ ).map((parameter) => ({
297
+ name: parameter.name,
298
+ description: parameter.description ?? null,
299
+ value: parameter.example ? parameter.example : parameter.schema ? getExampleFromSchema(parameter.schema, { mode: "write" }) : "",
300
+ required: parameter.required ?? false,
301
+ enabled: parameter.required ?? false
302
+ }));
303
+ return params.sort((a, b) => {
304
+ if (a.required && !b.required) {
305
+ return -1;
306
+ } else if (!a.required && b.required) {
307
+ return 1;
308
+ }
309
+ return 0;
310
+ });
311
+ }
312
+
313
+ function json2xml(data, tab) {
314
+ const toXml = function(value, key, indentation) {
315
+ let xml2 = "";
316
+ if (value instanceof Array) {
317
+ for (let i = 0, n = value.length; i < n; i++) {
318
+ xml2 += indentation + toXml(value[i], key, indentation + " ") + "\n";
319
+ }
320
+ } else if (typeof value == "object") {
321
+ let hasChild = false;
322
+ xml2 += indentation + "<" + key;
323
+ for (const m in value) {
324
+ if (m.charAt(0) == "@")
325
+ xml2 += " " + m.substr(1) + '="' + value[m].toString() + '"';
326
+ else
327
+ hasChild = true;
328
+ }
329
+ xml2 += hasChild ? ">" : "/>";
330
+ if (hasChild) {
331
+ for (const m in value) {
332
+ if (m == "#text")
333
+ xml2 += value[m];
334
+ else if (m == "#cdata")
335
+ xml2 += "<![CDATA[" + value[m] + "]]>";
336
+ else if (m.charAt(0) != "@")
337
+ xml2 += toXml(value[m], m, indentation + " ");
338
+ }
339
+ xml2 += (xml2.charAt(xml2.length - 1) == "\n" ? indentation : "") + "</" + key + ">";
340
+ }
341
+ } else {
342
+ xml2 += indentation + "<" + key + ">" + value.toString() + "</" + key + ">";
343
+ }
344
+ return xml2;
345
+ };
346
+ let xml = "";
347
+ for (const key in data) {
348
+ xml += toXml(data[key], key, "");
349
+ }
350
+ return tab ? xml.replace(/\t/g, tab) : xml.replace(/\t|\n/g, "");
351
+ }
352
+
353
+ const prettyPrintJson = (value) => {
354
+ try {
355
+ if (typeof value === "string") {
356
+ return JSON.stringify(JSON.parse(value), null, 2);
357
+ } else {
358
+ return JSON.stringify(value, null, 2);
359
+ }
360
+ } catch {
361
+ console.log("[prettyPrintJson] Error parsing JSON", value);
362
+ return value;
363
+ }
364
+ };
365
+
366
+ function getRequestBodyFromOperation(operation, selectedExampleKey) {
367
+ const mimeTypes = [
368
+ "application/json",
369
+ "application/octet-stream",
370
+ "application/x-www-form-urlencoded",
371
+ "application/xml",
372
+ "multipart/form-data",
373
+ "text/plain"
374
+ ];
375
+ const mimeType = mimeTypes.find(
376
+ (currentMimeType) => !!operation.information?.requestBody?.content?.[currentMimeType]
377
+ );
378
+ const examples = operation.information?.requestBody?.content?.["application/json"]?.examples;
379
+ const selectedExample = (examples ?? {})?.[selectedExampleKey ?? Object.keys(examples ?? {})[0]];
380
+ if (selectedExample) {
381
+ return {
382
+ postData: {
383
+ mimeType: "application/json",
384
+ text: prettyPrintJson(selectedExample?.value)
385
+ }
386
+ };
387
+ }
388
+ const bodyParameters = getParametersFromOperation(operation, "body", false);
389
+ if (bodyParameters.length > 0) {
390
+ return {
391
+ postData: {
392
+ mimeType: "application/json",
393
+ text: prettyPrintJson(bodyParameters[0].value)
394
+ }
395
+ };
396
+ }
397
+ const formDataParameters = getParametersFromOperation(
398
+ operation,
399
+ "formData",
400
+ false
401
+ );
402
+ if (formDataParameters.length > 0) {
403
+ return {
404
+ postData: {
405
+ mimeType: "application/x-www-form-urlencoded",
406
+ params: formDataParameters.map((parameter) => ({
407
+ name: parameter.name,
408
+ value: parameter.value
409
+ }))
410
+ }
411
+ };
412
+ }
413
+ if (!mimeType) {
414
+ return {
415
+ postData: void 0
416
+ };
417
+ }
418
+ const requestBodyObject = operation.information?.requestBody?.content?.[mimeType];
419
+ const headers = [
420
+ {
421
+ name: "Content-Type",
422
+ value: mimeType
423
+ }
424
+ ];
425
+ const example = requestBodyObject?.example ? requestBodyObject?.example : void 0;
426
+ if (mimeType === "application/json") {
427
+ const exampleFromSchema = requestBodyObject?.schema ? getExampleFromSchema(requestBodyObject?.schema, { mode: "write" }) : null;
428
+ const body = example ?? exampleFromSchema;
429
+ return {
430
+ headers,
431
+ postData: {
432
+ mimeType,
433
+ text: typeof body === "string" ? body : JSON.stringify(body, null, 2)
434
+ }
435
+ };
436
+ }
437
+ if (mimeType === "application/xml") {
438
+ const exampleFromSchema = requestBodyObject?.schema ? getExampleFromSchema(requestBodyObject?.schema, {
439
+ xml: true,
440
+ mode: "write"
441
+ }) : null;
442
+ return {
443
+ headers,
444
+ postData: {
445
+ mimeType,
446
+ text: example ?? json2xml(exampleFromSchema, " ")
447
+ }
448
+ };
449
+ }
450
+ if (mimeType === "application/octet-stream") {
451
+ return {
452
+ headers,
453
+ postData: {
454
+ mimeType,
455
+ text: "BINARY"
456
+ }
457
+ };
458
+ }
459
+ if (mimeType === "text/plain") {
460
+ const exampleFromSchema = requestBodyObject?.schema ? getExampleFromSchema(requestBodyObject?.schema, {
461
+ xml: true,
462
+ mode: "write"
463
+ }) : null;
464
+ return {
465
+ headers,
466
+ postData: {
467
+ mimeType,
468
+ text: example ?? exampleFromSchema ?? ""
469
+ }
470
+ };
471
+ }
472
+ if (mimeType === "application/x-www-form-urlencoded") {
473
+ return {
474
+ headers,
475
+ postData: {
476
+ mimeType
477
+ // TODO: We have an object, but how do we get that kind of array from the object?
478
+ // Don’t forget to include nested properties … :|
479
+ // params: [
480
+ // {
481
+ // name: 'foo',
482
+ // value: 'bar',
483
+ // },
484
+ // ],
485
+ }
486
+ };
487
+ }
488
+ if (mimeType === "multipart/form-data") {
489
+ return {
490
+ headers,
491
+ postData: {
492
+ mimeType
493
+ // TODO: We have an object, but how do we get that kind of array from the object?
494
+ // Don’t forget to include nested properties … :|
495
+ // params: [
496
+ // {
497
+ // name: 'foo',
498
+ // value: 'bar',
499
+ // },
500
+ // ],
501
+ }
502
+ };
503
+ }
504
+ return void 0;
505
+ }
506
+
507
+ const getRequestFromOperation = (operation, options, selectedExampleKey) => {
508
+ let path = operation.path;
509
+ if (options?.replaceVariables === true) {
510
+ const pathVariables = path.match(/{(.*?)}/g);
511
+ if (pathVariables) {
512
+ pathVariables.forEach((variable) => {
513
+ const variableName = variable.replace(/{|}/g, "");
514
+ path = path.replace(variable, `__${variableName.toUpperCase()}__`);
515
+ });
516
+ }
517
+ }
518
+ const requestBody = getRequestBodyFromOperation(operation, selectedExampleKey);
519
+ return {
520
+ method: operation.httpVerb.toUpperCase(),
521
+ path,
522
+ headers: [
523
+ ...getParametersFromOperation(operation, "header", options?.requiredOnly),
524
+ ...requestBody?.headers ?? []
525
+ ],
526
+ // TODO: Sorry, something is off here and I don’t get it.
527
+ // @ts-ignore
528
+ postData: requestBody?.postData,
529
+ queryString: getParametersFromOperation(
530
+ operation,
531
+ "query",
532
+ options?.requiredOnly
533
+ ),
534
+ cookies: getParametersFromOperation(
535
+ operation,
536
+ "cookie",
537
+ options?.requiredOnly
538
+ )
539
+ };
540
+ };
541
+
542
+ export { fetchSpecFromUrl, formatJsonOrYamlString, getExampleFromSchema, getHarRequest, getParametersFromOperation, getRequestBodyFromOperation, getRequestFromOperation, isJsonString, json, json2xml, parseJsonOrYaml, prettyPrintJson, transformToJson, yaml };
@@ -0,0 +1,5 @@
1
+ /**
2
+ * This function converts an object to XML.
3
+ */
4
+ export declare function json2xml(data: Record<string, any>, tab?: string): string;
5
+ //# sourceMappingURL=json2xml.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json2xml.d.ts","sourceRoot":"","sources":["../src/json2xml.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,GAAG,CAAC,EAAE,MAAM,UA+C/D"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Takes JSON and formats it.
3
+ **/
4
+ export declare const prettyPrintJson: (value: any) => any;
5
+ //# sourceMappingURL=prettyPrintJson.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prettyPrintJson.d.ts","sourceRoot":"","sources":["../src/prettyPrintJson.ts"],"names":[],"mappings":"AAAA;;IAEI;AACJ,eAAO,MAAM,eAAe,UAAW,GAAG,QAYzC,CAAA"}
package/dist/types.d.ts CHANGED
@@ -1,3 +1,106 @@
1
+ import { type OpenAPIV3 } from '@scalar/openapi-parser';
2
+ import type { HarRequest } from 'httpsnippet-lite';
1
3
  export type AnyObject = Record<string, any>;
2
4
  export type AnyStringOrObject = string | Record<string, any>;
5
+ export type BaseParameter = {
6
+ name: string;
7
+ description?: string | null;
8
+ value: string | number | Record<string, any>;
9
+ required?: boolean;
10
+ enabled: boolean;
11
+ };
12
+ export type ContentType = 'application/json' | 'application/xml' | 'text/plain' | 'text/html' | 'application/octet-stream' | 'application/x-www-form-urlencoded' | 'multipart/form-data';
13
+ export type Cookie = {
14
+ name: string;
15
+ value: string;
16
+ };
17
+ export type CustomRequestExample = {
18
+ lang: string;
19
+ label: string;
20
+ source: string;
21
+ };
22
+ export type HarRequestWithPath = HarRequest & {
23
+ path: string;
24
+ };
25
+ export type Header = {
26
+ name: string;
27
+ value: string;
28
+ };
29
+ export type Information = {
30
+ 'description'?: string;
31
+ 'operationId'?: string | number;
32
+ 'parameters'?: Parameters[];
33
+ 'responses'?: Record<string, ScalarResponse>;
34
+ 'security'?: OpenAPIV3.SecurityRequirementObject[];
35
+ 'requestBody'?: RequestBody;
36
+ 'summary'?: string;
37
+ 'tags'?: string[];
38
+ 'deprecated'?: boolean;
39
+ /**
40
+ * Scalar
41
+ **/
42
+ 'x-custom-examples'?: CustomRequestExample[];
43
+ /**
44
+ * Redocly, current
45
+ **/
46
+ 'x-codeSamples'?: CustomRequestExample[];
47
+ /**
48
+ * Redocly, deprecated
49
+ **/
50
+ 'x-code-samples'?: CustomRequestExample[];
51
+ };
52
+ export type Operation = {
53
+ httpVerb: string;
54
+ path: string;
55
+ operationId?: string;
56
+ name?: string;
57
+ description?: string;
58
+ information?: Information;
59
+ };
60
+ export type Parameters = {
61
+ name: string;
62
+ in?: string;
63
+ description?: string;
64
+ required?: boolean;
65
+ deprecated?: boolean;
66
+ allowEmptyValue?: boolean;
67
+ style?: 'form' | 'simple';
68
+ explode?: boolean;
69
+ allowReserved?: boolean;
70
+ schema?: Schema;
71
+ example?: any;
72
+ examples?: Map<string, any>;
73
+ };
74
+ export type Query = {
75
+ name: string;
76
+ value: string;
77
+ };
78
+ export type RequestBodyMimeTypes = {
79
+ [K in ContentType]?: {
80
+ schema?: any;
81
+ example?: any;
82
+ examples?: any;
83
+ };
84
+ };
85
+ export type ScalarResponse = {
86
+ description: string;
87
+ content: any;
88
+ };
89
+ export type RequestBody = {
90
+ description?: string;
91
+ required?: boolean;
92
+ content?: RequestBodyMimeTypes;
93
+ };
94
+ export type Schema = {
95
+ type: string;
96
+ name?: string;
97
+ example?: any;
98
+ default?: any;
99
+ format?: string;
100
+ description?: string;
101
+ properties?: Record<string, Schema>;
102
+ };
103
+ export type TransformedOperation = Operation & {
104
+ pathParameters?: Parameters[];
105
+ };
3
106
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;AAE3C,MAAM,MAAM,iBAAiB,GAAG,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,wBAAwB,CAAA;AACvD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAElD,MAAM,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;AAE3C,MAAM,MAAM,iBAAiB,GAAG,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;AAE5D,MAAM,MAAM,aAAa,GAAG;IAC1B,IAAI,EAAE,MAAM,CAAA;IACZ,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IAC3B,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IAC5C,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,OAAO,EAAE,OAAO,CAAA;C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E,oBAAoB,CAAA;CAC/B,CAAA;AAED,MAAM,MAAM,MAAM,GAAG;IACnB,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,OAAO,CAAC,EAAE,GAAG,CAAA;IACb,OAAO,CAAC,EAAE,GAAG,CAAA;IACb,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CACpC,CAAA;AAED,MAAM,MAAM,oBAAoB,GAAG,SAAS,GAAG;IAC7C,cAAc,CAAC,EAAE,UAAU,EAAE,CAAA;CAC9B,CAAA"}
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "specification",
12
12
  "yaml"
13
13
  ],
14
- "version": "0.0.4",
14
+ "version": "0.1.1",
15
15
  "engines": {
16
16
  "node": ">=18"
17
17
  },
@@ -45,10 +45,13 @@
45
45
  "yaml": "^2.4.1"
46
46
  },
47
47
  "devDependencies": {
48
+ "@scalar/openapi-parser": "^0.3.2",
49
+ "axios": "^1.6.7",
50
+ "httpsnippet-lite": "^3.0.5",
48
51
  "tsc-alias": "^1.8.8",
49
52
  "vite": "^5.1.1",
50
53
  "vitest": "^1.2.2",
51
- "@scalar/build-tooling": "0.0.4"
54
+ "@scalar/build-tooling": "0.1.1"
52
55
  },
53
56
  "scripts": {
54
57
  "build": "vite build && pnpm types:build && tsc-alias -p tsconfig.build.json",