@gitbook/react-openapi 1.4.3 → 1.5.2
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 +36 -0
- package/dist/InteractiveSection.js +59 -0
- package/dist/Markdown.js +10 -0
- package/dist/OpenAPICodeSample.js +219 -0
- package/dist/OpenAPICodeSampleInteractive.js +66 -0
- package/dist/OpenAPICodeSampleSelector.js +45 -0
- package/dist/OpenAPICopyButton.js +39 -0
- package/dist/OpenAPIDisclosure.js +30 -0
- package/dist/OpenAPIDisclosureGroup.js +75 -0
- package/dist/OpenAPIExample.js +41 -0
- package/dist/OpenAPIMediaType.js +58 -0
- package/dist/OpenAPIOperation.d.ts +12 -7
- package/dist/OpenAPIOperation.js +30 -0
- package/dist/OpenAPIOperationContext.d.ts +10 -6
- package/dist/OpenAPIOperationContext.js +30 -0
- package/dist/OpenAPIPath.js +51 -0
- package/dist/OpenAPIPrefillContextProvider.d.ts +11 -7
- package/dist/OpenAPIPrefillContextProvider.js +25 -0
- package/dist/OpenAPIRequestBody.js +28 -0
- package/dist/OpenAPIRequestBodyHeaderType.js +23 -0
- package/dist/OpenAPIResponse.js +39 -0
- package/dist/OpenAPIResponseExample.js +75 -0
- package/dist/OpenAPIResponseExampleContent.js +61 -0
- package/dist/OpenAPIResponses.js +61 -0
- package/dist/OpenAPISchema.js +373 -0
- package/dist/OpenAPISchemaName.js +45 -0
- package/dist/OpenAPISchemaServer.js +13 -0
- package/dist/OpenAPISecurities.js +124 -0
- package/dist/OpenAPISelect.js +45 -0
- package/dist/OpenAPISpec.js +73 -0
- package/dist/OpenAPIWebhook.d.ts +12 -7
- package/dist/OpenAPIWebhook.js +28 -0
- package/dist/OpenAPIWebhookExample.js +40 -0
- package/dist/ScalarApiButton.js +87 -0
- package/dist/StaticSection.js +37 -0
- package/dist/code-samples.js +267 -419
- package/dist/common/OpenAPIColumnSpec.js +23 -0
- package/dist/common/OpenAPIOperationDescription.js +18 -0
- package/dist/common/OpenAPIStability.js +17 -0
- package/dist/common/OpenAPISummary.js +27 -0
- package/dist/contentTypeChecks.js +24 -20
- package/dist/context.d.ts +68 -72
- package/dist/context.js +25 -39
- package/dist/decycle.js +39 -68
- package/dist/dereference.js +20 -64
- package/dist/generateSchemaExample.js +188 -332
- package/dist/getDisclosureLabel.js +15 -16
- package/dist/getOrCreateStoreByKey.js +20 -17
- package/dist/index.d.ts +12 -10
- package/dist/index.js +11 -8
- package/dist/json2xml.js +10 -5
- package/dist/resolveOpenAPIOperation.d.ts +11 -7
- package/dist/resolveOpenAPIOperation.js +88 -159
- package/dist/resolveOpenAPIWebhook.d.ts +11 -7
- package/dist/resolveOpenAPIWebhook.js +41 -116
- package/dist/schemas/OpenAPISchemaItem.js +26 -0
- package/dist/schemas/OpenAPISchemas.d.ts +16 -11
- package/dist/schemas/OpenAPISchemas.js +57 -0
- package/dist/schemas/resolveOpenAPISchemas.d.ts +9 -4
- package/dist/schemas/resolveOpenAPISchemas.js +15 -59
- package/dist/stringifyOpenAPI.js +12 -13
- package/dist/translate.js +43 -0
- package/dist/translations/de.js +47 -42
- package/dist/translations/en.d.ts +46 -42
- package/dist/translations/en.js +47 -42
- package/dist/translations/es.js +47 -42
- package/dist/translations/fr.js +47 -42
- package/dist/translations/index.d.ts +404 -391
- package/dist/translations/index.js +28 -24
- package/dist/translations/ja.js +47 -42
- package/dist/translations/nl.js +47 -42
- package/dist/translations/no.js +47 -42
- package/dist/translations/pt-br.js +47 -42
- package/dist/translations/types.d.ts +7 -5
- package/dist/translations/zh.js +47 -42
- package/dist/types.d.ts +30 -24
- package/dist/util/example.js +84 -0
- package/dist/util/server.js +32 -38
- package/dist/util/tryit-prefill.js +135 -121
- package/dist/utils.js +135 -196
- package/package.json +18 -11
- package/dist/InteractiveSection.d.ts +0 -33
- package/dist/InteractiveSection.jsx +0 -61
- package/dist/Markdown.d.ts +0 -4
- package/dist/Markdown.jsx +0 -5
- package/dist/OpenAPICodeSample.d.ts +0 -19
- package/dist/OpenAPICodeSample.jsx +0 -230
- package/dist/OpenAPICodeSampleInteractive.d.ts +0 -14
- package/dist/OpenAPICodeSampleInteractive.jsx +0 -73
- package/dist/OpenAPICodeSampleSelector.d.ts +0 -14
- package/dist/OpenAPICodeSampleSelector.jsx +0 -44
- package/dist/OpenAPICopyButton.d.ts +0 -13
- package/dist/OpenAPICopyButton.jsx +0 -35
- package/dist/OpenAPIDisclosure.d.ts +0 -11
- package/dist/OpenAPIDisclosure.jsx +0 -30
- package/dist/OpenAPIDisclosureGroup.d.ts +0 -23
- package/dist/OpenAPIDisclosureGroup.jsx +0 -83
- package/dist/OpenAPIExample.d.ts +0 -16
- package/dist/OpenAPIExample.jsx +0 -36
- package/dist/OpenAPIMediaType.d.ts +0 -21
- package/dist/OpenAPIMediaType.jsx +0 -61
- package/dist/OpenAPIOperation.jsx +0 -25
- package/dist/OpenAPIOperationContext.jsx +0 -26
- package/dist/OpenAPIOperationDescription.d.ts +0 -9
- package/dist/OpenAPIOperationDescription.jsx +0 -22
- package/dist/OpenAPIOperationStability.d.ts +0 -9
- package/dist/OpenAPIOperationStability.jsx +0 -27
- package/dist/OpenAPIPath.d.ts +0 -18
- package/dist/OpenAPIPath.jsx +0 -55
- package/dist/OpenAPIPrefillContextProvider.jsx +0 -19
- package/dist/OpenAPIRequestBody.d.ts +0 -11
- package/dist/OpenAPIRequestBody.jsx +0 -28
- package/dist/OpenAPIRequestBodyHeaderType.d.ts +0 -8
- package/dist/OpenAPIRequestBodyHeaderType.jsx +0 -25
- package/dist/OpenAPIResponse.d.ts +0 -10
- package/dist/OpenAPIResponse.jsx +0 -57
- package/dist/OpenAPIResponseExample.d.ts +0 -9
- package/dist/OpenAPIResponseExample.jsx +0 -105
- package/dist/OpenAPIResponseExampleContent.d.ts +0 -22
- package/dist/OpenAPIResponseExampleContent.jsx +0 -60
- package/dist/OpenAPIResponses.d.ts +0 -9
- package/dist/OpenAPIResponses.jsx +0 -77
- package/dist/OpenAPISchema.d.ts +0 -27
- package/dist/OpenAPISchema.jsx +0 -400
- package/dist/OpenAPISchemaName.d.ts +0 -16
- package/dist/OpenAPISchemaName.jsx +0 -43
- package/dist/OpenAPISchemaServer.d.ts +0 -12
- package/dist/OpenAPISchemaServer.jsx +0 -8
- package/dist/OpenAPISecurities.d.ts +0 -9
- package/dist/OpenAPISecurities.jsx +0 -114
- package/dist/OpenAPISelect.d.ts +0 -22
- package/dist/OpenAPISelect.jsx +0 -44
- package/dist/OpenAPISpec.d.ts +0 -6
- package/dist/OpenAPISpec.jsx +0 -80
- package/dist/OpenAPITabs.d.ts +0 -26
- package/dist/OpenAPITabs.jsx +0 -109
- package/dist/OpenAPIWebhook.jsx +0 -23
- package/dist/OpenAPIWebhookExample.d.ts +0 -6
- package/dist/OpenAPIWebhookExample.jsx +0 -41
- package/dist/ScalarApiButton.d.ts +0 -14
- package/dist/ScalarApiButton.jsx +0 -81
- package/dist/StaticSection.d.ts +0 -13
- package/dist/StaticSection.jsx +0 -32
- package/dist/code-samples.d.ts +0 -17
- package/dist/common/OpenAPIColumnSpec.d.ts +0 -6
- package/dist/common/OpenAPIColumnSpec.jsx +0 -20
- package/dist/common/OpenAPIOperationDescription.d.ts +0 -6
- package/dist/common/OpenAPIOperationDescription.jsx +0 -19
- package/dist/common/OpenAPIStability.d.ts +0 -4
- package/dist/common/OpenAPIStability.jsx +0 -15
- package/dist/common/OpenAPISummary.d.ts +0 -6
- package/dist/common/OpenAPISummary.jsx +0 -30
- package/dist/contentTypeChecks.d.ts +0 -10
- package/dist/decycle.d.ts +0 -2
- package/dist/dereference.d.ts +0 -5
- package/dist/generateSchemaExample.d.ts +0 -45
- package/dist/getDisclosureLabel.d.ts +0 -7
- package/dist/getOrCreateStoreByKey.d.ts +0 -10
- package/dist/json2xml.d.ts +0 -4
- package/dist/schemas/OpenAPISchemaItem.d.ts +0 -7
- package/dist/schemas/OpenAPISchemaItem.jsx +0 -16
- package/dist/schemas/OpenAPISchemas.jsx +0 -59
- package/dist/schemas/index.d.ts +0 -2
- package/dist/schemas/index.js +0 -2
- package/dist/stringifyOpenAPI.d.ts +0 -4
- package/dist/translate.d.ts +0 -10
- package/dist/translate.jsx +0 -75
- package/dist/translations/de.d.ts +0 -43
- package/dist/translations/es.d.ts +0 -43
- package/dist/translations/fr.d.ts +0 -43
- package/dist/translations/ja.d.ts +0 -43
- package/dist/translations/nl.d.ts +0 -43
- package/dist/translations/no.d.ts +0 -43
- package/dist/translations/pt-br.d.ts +0 -43
- package/dist/translations/types.js +0 -1
- package/dist/translations/zh.d.ts +0 -43
- package/dist/tsconfig.build.tsbuildinfo +0 -1
- package/dist/types.js +0 -1
- package/dist/util/example.d.ts +0 -35
- package/dist/util/example.jsx +0 -103
- package/dist/util/server.d.ts +0 -9
- package/dist/util/tryit-prefill.d.ts +0 -20
- package/dist/utils.d.ts +0 -50
- package/src/InteractiveSection.tsx +0 -147
- package/src/Markdown.tsx +0 -12
- package/src/OpenAPICodeSample.tsx +0 -330
- package/src/OpenAPICodeSampleInteractive.tsx +0 -136
- package/src/OpenAPICodeSampleSelector.tsx +0 -94
- package/src/OpenAPICopyButton.tsx +0 -72
- package/src/OpenAPIDisclosure.tsx +0 -46
- package/src/OpenAPIDisclosureGroup.tsx +0 -158
- package/src/OpenAPIExample.tsx +0 -55
- package/src/OpenAPIMediaType.tsx +0 -139
- package/src/OpenAPIOperation.tsx +0 -35
- package/src/OpenAPIOperationContext.tsx +0 -45
- package/src/OpenAPIOperationDescription.tsx +0 -34
- package/src/OpenAPIOperationStability.tsx +0 -39
- package/src/OpenAPIPath.tsx +0 -90
- package/src/OpenAPIPrefillContextProvider.tsx +0 -40
- package/src/OpenAPIRequestBody.tsx +0 -54
- package/src/OpenAPIRequestBodyHeaderType.tsx +0 -36
- package/src/OpenAPIResponse.tsx +0 -82
- package/src/OpenAPIResponseExample.tsx +0 -151
- package/src/OpenAPIResponseExampleContent.tsx +0 -125
- package/src/OpenAPIResponses.tsx +0 -125
- package/src/OpenAPISchema.test.ts +0 -172
- package/src/OpenAPISchema.tsx +0 -654
- package/src/OpenAPISchemaName.tsx +0 -80
- package/src/OpenAPISchemaServer.tsx +0 -34
- package/src/OpenAPISecurities.tsx +0 -231
- package/src/OpenAPISelect.tsx +0 -96
- package/src/OpenAPISpec.tsx +0 -138
- package/src/OpenAPITabs.tsx +0 -147
- package/src/OpenAPIWebhook.tsx +0 -33
- package/src/OpenAPIWebhookExample.tsx +0 -60
- package/src/ScalarApiButton.tsx +0 -132
- package/src/StaticSection.tsx +0 -91
- package/src/__snapshots__/json2xml.test.ts.snap +0 -18
- package/src/code-samples.test.ts +0 -714
- package/src/code-samples.ts +0 -448
- package/src/common/OpenAPIColumnSpec.tsx +0 -31
- package/src/common/OpenAPIOperationDescription.tsx +0 -31
- package/src/common/OpenAPIStability.tsx +0 -23
- package/src/common/OpenAPISummary.tsx +0 -45
- package/src/contentTypeChecks.ts +0 -39
- package/src/context.ts +0 -99
- package/src/decycle.ts +0 -68
- package/src/dereference.ts +0 -29
- package/src/generateSchemaExample.test.ts +0 -1040
- package/src/generateSchemaExample.ts +0 -530
- package/src/getDisclosureLabel.ts +0 -25
- package/src/getOrCreateStoreByKey.ts +0 -33
- package/src/index.ts +0 -10
- package/src/json2xml.test.ts +0 -46
- package/src/json2xml.ts +0 -8
- package/src/resolveOpenAPIOperation.test.ts +0 -177
- package/src/resolveOpenAPIOperation.ts +0 -151
- package/src/resolveOpenAPIWebhook.ts +0 -99
- package/src/schemas/OpenAPISchemaItem.tsx +0 -34
- package/src/schemas/OpenAPISchemas.tsx +0 -98
- package/src/schemas/index.ts +0 -2
- package/src/schemas/resolveOpenAPISchemas.test.ts +0 -174
- package/src/schemas/resolveOpenAPISchemas.ts +0 -28
- package/src/stringifyOpenAPI.ts +0 -25
- package/src/translate.tsx +0 -80
- package/src/translations/de.ts +0 -43
- package/src/translations/en.ts +0 -43
- package/src/translations/es.ts +0 -43
- package/src/translations/fr.ts +0 -43
- package/src/translations/index.ts +0 -33
- package/src/translations/ja.ts +0 -43
- package/src/translations/nl.ts +0 -43
- package/src/translations/no.ts +0 -43
- package/src/translations/pt-br.ts +0 -43
- package/src/translations/types.ts +0 -7
- package/src/translations/zh.ts +0 -43
- package/src/types.ts +0 -46
- package/src/util/example.tsx +0 -129
- package/src/util/server.test.ts +0 -58
- package/src/util/server.ts +0 -47
- package/src/util/tryit-prefill.test.ts +0 -311
- package/src/util/tryit-prefill.ts +0 -160
- package/src/utils.ts +0 -255
package/dist/code-samples.js
CHANGED
|
@@ -1,427 +1,275 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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
|
-
if (headers && Object.keys(headers).length > 0) {
|
|
105
|
-
code += indent("headers: ".concat(stringifyOpenAPI(headers, null, 2), ",\n"), 4);
|
|
106
|
-
}
|
|
107
|
-
if (body) {
|
|
108
|
-
code += indent("body: ".concat(body, "\n"), 4);
|
|
109
|
-
}
|
|
110
|
-
code += '});\n\n';
|
|
111
|
-
code += 'const data = await response.json();';
|
|
112
|
-
return code;
|
|
113
|
-
},
|
|
114
|
-
},
|
|
115
|
-
{
|
|
116
|
-
id: 'python',
|
|
117
|
-
label: 'Python',
|
|
118
|
-
syntax: 'python',
|
|
119
|
-
generate: function (_a) {
|
|
120
|
-
var method = _a.method, url = _a.url, headers = _a.headers, body = _a.body;
|
|
121
|
-
var contentType = headers === null || headers === void 0 ? void 0 : headers['Content-Type'];
|
|
122
|
-
var code = "".concat(isJSON(contentType) ? 'import json\n' : '', "import requests\n\n");
|
|
123
|
-
if (body) {
|
|
124
|
-
var lines = BodyGenerators.getPythonBody(body, headers);
|
|
125
|
-
// add the generated code to the top
|
|
126
|
-
if (lines) {
|
|
127
|
-
code += lines.code;
|
|
128
|
-
body = lines.body;
|
|
129
|
-
headers = lines.headers;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
code += "response = requests.".concat(method.toLowerCase(), "(\n");
|
|
133
|
-
code += indent("\"".concat(url, "\",\n"), 4);
|
|
134
|
-
if (headers && Object.keys(headers).length > 0) {
|
|
135
|
-
code += indent("headers=".concat(stringifyOpenAPI(headers), ",\n"), 4);
|
|
136
|
-
}
|
|
137
|
-
if (body) {
|
|
138
|
-
if (body === 'files') {
|
|
139
|
-
code += indent("files=".concat(body, "\n"), 4);
|
|
140
|
-
}
|
|
141
|
-
else if (isJSON(contentType)) {
|
|
142
|
-
code += indent("data=json.dumps(".concat(body, ")\n"), 4);
|
|
143
|
-
}
|
|
144
|
-
else {
|
|
145
|
-
code += indent("data=".concat(body, "\n"), 4);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
code += ')\n\n';
|
|
149
|
-
code += 'data = response.json()';
|
|
150
|
-
return code;
|
|
151
|
-
},
|
|
152
|
-
},
|
|
1
|
+
import { json2xml } from "./json2xml.js";
|
|
2
|
+
import { stringifyOpenAPI } from "./stringifyOpenAPI.js";
|
|
3
|
+
import { isCSV, isFormData, isFormUrlEncoded, isGraphQL, isJSON, isPDF, isPlainObject, isText, isXML, isYAML } from "./contentTypeChecks.js";
|
|
4
|
+
import yaml from "js-yaml";
|
|
5
|
+
|
|
6
|
+
//#region src/code-samples.ts
|
|
7
|
+
const codeSampleGenerators = [
|
|
8
|
+
{
|
|
9
|
+
id: "http",
|
|
10
|
+
label: "HTTP",
|
|
11
|
+
syntax: "http",
|
|
12
|
+
generate: ({ method, url: { origin, path }, headers = {}, body }) => {
|
|
13
|
+
if (body) {
|
|
14
|
+
const bodyContent = body ? stringifyOpenAPI(body) : "";
|
|
15
|
+
const encoder = new TextEncoder();
|
|
16
|
+
const bodyString$1 = BodyGenerators.getHTTPBody(body, headers);
|
|
17
|
+
if (bodyString$1) body = bodyString$1;
|
|
18
|
+
headers = {
|
|
19
|
+
...headers,
|
|
20
|
+
"Content-Length": encoder.encode(bodyContent).length.toString()
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
if (!headers.hasOwnProperty("Accept")) headers.Accept = "*/*";
|
|
24
|
+
const headerString = headers ? `${Object.entries(headers).map(([key, value]) => key.toLowerCase() !== "host" ? `${key}: ${value}` : "").join("\n")}\n` : "";
|
|
25
|
+
const bodyString = body ? `\n${body}` : "";
|
|
26
|
+
return `${method.toUpperCase()} ${decodeURI(path)} HTTP/1.1
|
|
27
|
+
Host: ${origin.replaceAll(/https*:\/\//g, "")}
|
|
28
|
+
${headerString}${bodyString}`;
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
id: "curl",
|
|
33
|
+
label: "cURL",
|
|
34
|
+
syntax: "bash",
|
|
35
|
+
generate: ({ method, url: { origin, path }, headers, body }) => {
|
|
36
|
+
const separator = " \\\n";
|
|
37
|
+
const lines = ["curl -L"];
|
|
38
|
+
if (method.toUpperCase() !== "GET") lines.push(`--request ${method.toUpperCase()}`);
|
|
39
|
+
lines.push(`--url '${origin}${path}'`);
|
|
40
|
+
if (body) {
|
|
41
|
+
const bodyContent = BodyGenerators.getCurlBody(body, headers);
|
|
42
|
+
if (bodyContent) {
|
|
43
|
+
body = bodyContent.body;
|
|
44
|
+
headers = bodyContent.headers;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (headers && Object.keys(headers).length > 0) Object.entries(headers).forEach(([key, value]) => {
|
|
48
|
+
lines.push(`--header '${key}: ${value}'`);
|
|
49
|
+
});
|
|
50
|
+
if (body) if (Array.isArray(body)) lines.push(...body);
|
|
51
|
+
else lines.push(body);
|
|
52
|
+
return lines.map((line, index) => index > 0 ? indent(line, 2) : line).join(separator);
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
id: "javascript",
|
|
57
|
+
label: "JavaScript",
|
|
58
|
+
syntax: "javascript",
|
|
59
|
+
generate: ({ method, url: { origin, path }, headers, body }) => {
|
|
60
|
+
let code = "";
|
|
61
|
+
if (body) {
|
|
62
|
+
const lines = BodyGenerators.getJavaScriptBody(body, headers);
|
|
63
|
+
if (lines) {
|
|
64
|
+
code += lines.code;
|
|
65
|
+
body = lines.body;
|
|
66
|
+
headers = lines.headers;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
code += `const response = await fetch('${origin}${path}', {
|
|
70
|
+
method: '${method.toUpperCase()}',\n`;
|
|
71
|
+
if (headers && Object.keys(headers).length > 0) code += indent(`headers: ${stringifyOpenAPI(headers, null, 2)},\n`, 4);
|
|
72
|
+
if (body) code += indent(`body: ${body}\n`, 4);
|
|
73
|
+
code += "});\n\n";
|
|
74
|
+
code += "const data = await response.json();";
|
|
75
|
+
return code;
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
id: "python",
|
|
80
|
+
label: "Python",
|
|
81
|
+
syntax: "python",
|
|
82
|
+
generate: ({ method, url: { origin, path }, headers, body }) => {
|
|
83
|
+
const contentType = headers?.["Content-Type"];
|
|
84
|
+
let code = `${isJSON(contentType) ? "import json\n" : ""}import requests\n\n`;
|
|
85
|
+
if (body) {
|
|
86
|
+
const lines = BodyGenerators.getPythonBody(body, headers);
|
|
87
|
+
if (lines) {
|
|
88
|
+
code += lines.code;
|
|
89
|
+
body = lines.body;
|
|
90
|
+
headers = lines.headers;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
code += `response = requests.${method.toLowerCase()}(\n`;
|
|
94
|
+
code += indent(`"${origin}${path}",\n`, 4);
|
|
95
|
+
if (headers && Object.keys(headers).length > 0) code += indent(`headers=${stringifyOpenAPI(headers)},\n`, 4);
|
|
96
|
+
if (body) if (body === "files") code += indent(`files=${body}\n`, 4);
|
|
97
|
+
else if (isJSON(contentType)) code += indent(`data=json.dumps(${body})\n`, 4);
|
|
98
|
+
else code += indent(`data=${body}\n`, 4);
|
|
99
|
+
code += ")\n\n";
|
|
100
|
+
code += "data = response.json()";
|
|
101
|
+
return code;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
153
104
|
];
|
|
154
105
|
function indent(code, spaces) {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
.split('\n')
|
|
158
|
-
.map(function (line) { return (line ? indent + line : ''); })
|
|
159
|
-
.join('\n');
|
|
106
|
+
const indent$1 = " ".repeat(spaces);
|
|
107
|
+
return code.split("\n").map((line) => line ? indent$1 + line : "").join("\n");
|
|
160
108
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
var path = "/".concat(parts.join('/'));
|
|
178
|
-
return { host: host, path: path };
|
|
179
|
-
}
|
|
109
|
+
function parseHostAndPath(url) {
|
|
110
|
+
try {
|
|
111
|
+
const urlObj = new URL(url);
|
|
112
|
+
const path = urlObj.pathname || "/";
|
|
113
|
+
return {
|
|
114
|
+
host: urlObj.host,
|
|
115
|
+
path
|
|
116
|
+
};
|
|
117
|
+
} catch (_e) {
|
|
118
|
+
const splitted = url.split("//");
|
|
119
|
+
const parts = (splitted[1] ? splitted[1] : url).split("/");
|
|
120
|
+
return {
|
|
121
|
+
host: parts.shift(),
|
|
122
|
+
path: `/${parts.join("/")}`
|
|
123
|
+
};
|
|
124
|
+
}
|
|
180
125
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
}
|
|
316
|
-
return { body: body, code: code, headers: headersCopy };
|
|
317
|
-
},
|
|
318
|
-
getPythonBody: function (body, headers) {
|
|
319
|
-
if (!body || !headers)
|
|
320
|
-
return;
|
|
321
|
-
var code = '';
|
|
322
|
-
var contentType = headers['Content-Type'] || '';
|
|
323
|
-
if (isFormData(contentType)) {
|
|
324
|
-
code += 'files = {\n';
|
|
325
|
-
if (isPlainObject(body)) {
|
|
326
|
-
Object.entries(body).forEach(function (_a) {
|
|
327
|
-
var key = _a[0], value = _a[1];
|
|
328
|
-
code += "".concat(indent("\"".concat(key, "\": \"").concat(String(value), "\","), 4), "\n");
|
|
329
|
-
});
|
|
330
|
-
}
|
|
331
|
-
code += '}\n\n';
|
|
332
|
-
body = 'files';
|
|
333
|
-
}
|
|
334
|
-
else if (isPDF(contentType)) {
|
|
335
|
-
code += 'files = {\n';
|
|
336
|
-
code += "".concat(indent("\"file\": \"".concat(body, "\","), 4), "\n");
|
|
337
|
-
code += '}\n\n';
|
|
338
|
-
body = 'files';
|
|
339
|
-
}
|
|
340
|
-
else if (isXML(contentType)) {
|
|
341
|
-
// Convert JSON to XML if needed
|
|
342
|
-
body = JSON.stringify(convertBodyToXML(body));
|
|
343
|
-
}
|
|
344
|
-
else if (isYAML(contentType)) {
|
|
345
|
-
code += "yamlBody = \"\"\"\n".concat(indent(yaml.dump(body), 4), "\"\"\"\n\n");
|
|
346
|
-
body = 'yamlBody';
|
|
347
|
-
}
|
|
348
|
-
else {
|
|
349
|
-
body = stringifyOpenAPI(body, function (_key, value) {
|
|
350
|
-
switch (value) {
|
|
351
|
-
case true:
|
|
352
|
-
return '$$__TRUE__$$';
|
|
353
|
-
case false:
|
|
354
|
-
return '$$__FALSE__$$';
|
|
355
|
-
case null:
|
|
356
|
-
return '$$__NULL__$$';
|
|
357
|
-
default:
|
|
358
|
-
return value;
|
|
359
|
-
}
|
|
360
|
-
}, 2)
|
|
361
|
-
.replaceAll('"$$__TRUE__$$"', 'True')
|
|
362
|
-
.replaceAll('"$$__FALSE__$$"', 'False')
|
|
363
|
-
.replaceAll('"$$__NULL__$$"', 'None');
|
|
364
|
-
}
|
|
365
|
-
return { body: body, code: code, headers: headers };
|
|
366
|
-
},
|
|
367
|
-
getHTTPBody: function (body, headers) {
|
|
368
|
-
if (!body || !headers)
|
|
369
|
-
return undefined;
|
|
370
|
-
var contentType = headers['Content-Type'] || '';
|
|
371
|
-
var typeHandlers = {
|
|
372
|
-
pdf: function () { return "".concat(stringifyOpenAPI(body, null, 2)); },
|
|
373
|
-
formUrlEncoded: function () {
|
|
374
|
-
var encoded = isPlainObject(body)
|
|
375
|
-
? Object.entries(body)
|
|
376
|
-
.map(function (_a) {
|
|
377
|
-
var key = _a[0], value = _a[1];
|
|
378
|
-
return "".concat(key, "=").concat(stringifyOpenAPI(value));
|
|
379
|
-
})
|
|
380
|
-
.join('&')
|
|
381
|
-
: stringifyOpenAPI(body);
|
|
382
|
-
return "\"".concat(encoded.replace(/"/g, "'"), "\"");
|
|
383
|
-
},
|
|
384
|
-
text: function () { return "\"".concat(String(body), "\""); },
|
|
385
|
-
xml: function () {
|
|
386
|
-
// Convert JSON to XML if needed
|
|
387
|
-
return "\"".concat(convertBodyToXML(body), "\"");
|
|
388
|
-
},
|
|
389
|
-
yaml: function () { return "\"".concat(yaml.dump(body).replace(/"/g, '\\"'), "\""); },
|
|
390
|
-
csv: function () { return "\"".concat(stringifyOpenAPI(body).replace(/"/g, ''), "\""); },
|
|
391
|
-
default: function () { return "".concat(stringifyOpenAPI(body, null, 2)); },
|
|
392
|
-
};
|
|
393
|
-
if (isPDF(contentType))
|
|
394
|
-
return typeHandlers.pdf();
|
|
395
|
-
if (isFormUrlEncoded(contentType))
|
|
396
|
-
return typeHandlers.formUrlEncoded();
|
|
397
|
-
if (isText(contentType))
|
|
398
|
-
return typeHandlers.text();
|
|
399
|
-
if (isXML(contentType))
|
|
400
|
-
return typeHandlers.xml();
|
|
401
|
-
if (isYAML(contentType))
|
|
402
|
-
return typeHandlers.yaml();
|
|
403
|
-
if (isCSV(contentType))
|
|
404
|
-
return typeHandlers.csv();
|
|
405
|
-
return typeHandlers.default();
|
|
406
|
-
},
|
|
126
|
+
const BodyGenerators = {
|
|
127
|
+
getCurlBody(body, headers) {
|
|
128
|
+
if (!body || !headers) return void 0;
|
|
129
|
+
const headersCopy = { ...headers };
|
|
130
|
+
const contentType = headersCopy["Content-Type"] || "";
|
|
131
|
+
if (isFormData(contentType)) body = isPlainObject(body) ? Object.entries(body).map(([key, value]) => `--form '${key}=${String(value)}'`) : `--form 'file=@${body}'`;
|
|
132
|
+
else if (isFormUrlEncoded(contentType)) body = isPlainObject(body) ? `--data '${Object.entries(body).map(([key, value]) => `${key}=${String(value)}`).join("&")}'` : String(body);
|
|
133
|
+
else if (isText(contentType)) body = `--data '${String(body).replace(/"/g, "")}'`;
|
|
134
|
+
else if (isXML(contentType)) body = `--data-binary $'${convertBodyToXML(body)}'`;
|
|
135
|
+
else if (isCSV(contentType)) body = `--data-binary $'${stringifyOpenAPI(body).replace(/"/g, "").replace(/\\n/g, "\n")}'`;
|
|
136
|
+
else if (isGraphQL(contentType)) {
|
|
137
|
+
body = `--data '${stringifyOpenAPI(body)}'`;
|
|
138
|
+
headersCopy["Content-Type"] = "application/json";
|
|
139
|
+
} else if (isPDF(contentType)) body = `--data-binary '@${String(body)}'`;
|
|
140
|
+
else if (isYAML(contentType)) body = `--data-binary $'${yaml.dump(body).replace(/'/g, "").replace(/\\n/g, "\n")}'`;
|
|
141
|
+
else body = `--data '${stringifyOpenAPI(body, null, 2).replace(/\\n/g, "\n")}'`;
|
|
142
|
+
return {
|
|
143
|
+
body,
|
|
144
|
+
headers: headersCopy
|
|
145
|
+
};
|
|
146
|
+
},
|
|
147
|
+
getJavaScriptBody: (body, headers) => {
|
|
148
|
+
if (!body || !headers) return;
|
|
149
|
+
let code = "";
|
|
150
|
+
const headersCopy = { ...headers };
|
|
151
|
+
const contentType = headersCopy["Content-Type"] || "";
|
|
152
|
+
if (isFormData(contentType)) {
|
|
153
|
+
code += "const formData = new FormData();\n\n";
|
|
154
|
+
if (isPlainObject(body)) Object.entries(body).forEach(([key, value]) => {
|
|
155
|
+
code += `formData.append("${key}", "${String(value)}");\n`;
|
|
156
|
+
});
|
|
157
|
+
else if (typeof body === "string") code += `formData.append("file", "${body}");\n`;
|
|
158
|
+
code += "\n";
|
|
159
|
+
body = "formData";
|
|
160
|
+
} else if (isFormUrlEncoded(contentType)) {
|
|
161
|
+
code += "const params = new URLSearchParams();\n\n";
|
|
162
|
+
if (isPlainObject(body)) Object.entries(body).forEach(([key, value]) => {
|
|
163
|
+
code += `params.append("${key}", "${String(value)}");\n`;
|
|
164
|
+
});
|
|
165
|
+
code += "\n";
|
|
166
|
+
body = "params.toString()";
|
|
167
|
+
} else if (isGraphQL(contentType)) if (isPlainObject(body)) {
|
|
168
|
+
Object.entries(body).forEach(([key, value]) => {
|
|
169
|
+
code += `const ${key} = \`\n${indent(String(value), 4)}\`;\n\n`;
|
|
170
|
+
});
|
|
171
|
+
body = `JSON.stringify({ ${Object.keys(body).join(", ")} })`;
|
|
172
|
+
headersCopy["Content-Type"] = "application/json";
|
|
173
|
+
} else {
|
|
174
|
+
code += `const query = \`\n${indent(String(body), 4)}\`;\n\n`;
|
|
175
|
+
body = "JSON.stringify(query)";
|
|
176
|
+
}
|
|
177
|
+
else if (isCSV(contentType)) {
|
|
178
|
+
code += "const csv = `\n";
|
|
179
|
+
code += indent(String(body), 4);
|
|
180
|
+
code += "`;\n\n";
|
|
181
|
+
body = "csv";
|
|
182
|
+
} else if (isPDF(contentType)) {
|
|
183
|
+
code += "const formData = new FormData();\n\n";
|
|
184
|
+
code += `formData.append("file", "${body}");\n\n`;
|
|
185
|
+
body = "formData";
|
|
186
|
+
} else if (isXML(contentType)) {
|
|
187
|
+
code += "const xml = `\n";
|
|
188
|
+
code += indent(convertBodyToXML(body), 4);
|
|
189
|
+
code += "`;\n\n";
|
|
190
|
+
body = "xml";
|
|
191
|
+
} else if (isYAML(contentType)) {
|
|
192
|
+
code += `const yamlBody = \`\n${indent(yaml.dump(body), 4)}\`;\n\n`;
|
|
193
|
+
body = "yamlBody";
|
|
194
|
+
} else if (isText(contentType)) body = stringifyOpenAPI(body, null, 2);
|
|
195
|
+
else body = `JSON.stringify(${stringifyOpenAPI(body, null, 2)})`;
|
|
196
|
+
return {
|
|
197
|
+
body,
|
|
198
|
+
code,
|
|
199
|
+
headers: headersCopy
|
|
200
|
+
};
|
|
201
|
+
},
|
|
202
|
+
getPythonBody: (body, headers) => {
|
|
203
|
+
if (!body || !headers) return;
|
|
204
|
+
let code = "";
|
|
205
|
+
const contentType = headers["Content-Type"] || "";
|
|
206
|
+
if (isFormData(contentType)) {
|
|
207
|
+
code += "files = {\n";
|
|
208
|
+
if (isPlainObject(body)) Object.entries(body).forEach(([key, value]) => {
|
|
209
|
+
code += `${indent(`"${key}": "${String(value)}",`, 4)}\n`;
|
|
210
|
+
});
|
|
211
|
+
code += "}\n\n";
|
|
212
|
+
body = "files";
|
|
213
|
+
} else if (isPDF(contentType)) {
|
|
214
|
+
code += "files = {\n";
|
|
215
|
+
code += `${indent(`"file": "${body}",`, 4)}\n`;
|
|
216
|
+
code += "}\n\n";
|
|
217
|
+
body = "files";
|
|
218
|
+
} else if (isXML(contentType)) body = JSON.stringify(convertBodyToXML(body));
|
|
219
|
+
else if (isYAML(contentType)) {
|
|
220
|
+
code += `yamlBody = \"\"\"\n${indent(yaml.dump(body), 4)}\"\"\"\n\n`;
|
|
221
|
+
body = "yamlBody";
|
|
222
|
+
} else body = stringifyOpenAPI(body, (_key, value) => {
|
|
223
|
+
switch (value) {
|
|
224
|
+
case true: return "$$__TRUE__$$";
|
|
225
|
+
case false: return "$$__FALSE__$$";
|
|
226
|
+
case null: return "$$__NULL__$$";
|
|
227
|
+
default: return value;
|
|
228
|
+
}
|
|
229
|
+
}, 2).replaceAll("\"$$__TRUE__$$\"", "True").replaceAll("\"$$__FALSE__$$\"", "False").replaceAll("\"$$__NULL__$$\"", "None");
|
|
230
|
+
return {
|
|
231
|
+
body,
|
|
232
|
+
code,
|
|
233
|
+
headers
|
|
234
|
+
};
|
|
235
|
+
},
|
|
236
|
+
getHTTPBody: (body, headers) => {
|
|
237
|
+
if (!body || !headers) return void 0;
|
|
238
|
+
const contentType = headers["Content-Type"] || "";
|
|
239
|
+
const typeHandlers = {
|
|
240
|
+
pdf: () => `${stringifyOpenAPI(body, null, 2)}`,
|
|
241
|
+
formUrlEncoded: () => {
|
|
242
|
+
return `"${(isPlainObject(body) ? Object.entries(body).map(([key, value]) => `${key}=${stringifyOpenAPI(value)}`).join("&") : stringifyOpenAPI(body)).replace(/"/g, "'")}"`;
|
|
243
|
+
},
|
|
244
|
+
text: () => `"${String(body)}"`,
|
|
245
|
+
xml: () => {
|
|
246
|
+
return `"${convertBodyToXML(body)}"`;
|
|
247
|
+
},
|
|
248
|
+
yaml: () => `"${yaml.dump(body).replace(/"/g, "\\\"")}"`,
|
|
249
|
+
csv: () => `"${stringifyOpenAPI(body).replace(/"/g, "")}"`,
|
|
250
|
+
default: () => `${stringifyOpenAPI(body, null, 2)}`
|
|
251
|
+
};
|
|
252
|
+
if (isPDF(contentType)) return typeHandlers.pdf();
|
|
253
|
+
if (isFormUrlEncoded(contentType)) return typeHandlers.formUrlEncoded();
|
|
254
|
+
if (isText(contentType)) return typeHandlers.text();
|
|
255
|
+
if (isXML(contentType)) return typeHandlers.xml();
|
|
256
|
+
if (isYAML(contentType)) return typeHandlers.yaml();
|
|
257
|
+
if (isCSV(contentType)) return typeHandlers.csv();
|
|
258
|
+
return typeHandlers.default();
|
|
259
|
+
}
|
|
407
260
|
};
|
|
408
261
|
/**
|
|
409
|
-
|
|
410
|
-
|
|
262
|
+
* Converts a body to XML format
|
|
263
|
+
*/
|
|
411
264
|
function convertBodyToXML(body) {
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
body = JSON.parse(body);
|
|
420
|
-
}
|
|
421
|
-
catch (_a) {
|
|
422
|
-
// If parsing fails, return the original body
|
|
423
|
-
return body;
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
return json2xml(body).replace(/"/g, '').replace(/\\n/g, '\n').replace(/\\t/g, '\t');
|
|
265
|
+
if (typeof body === "string" && body.trim().startsWith("<")) return body;
|
|
266
|
+
if (typeof body !== "object" || body === null) try {
|
|
267
|
+
body = JSON.parse(body);
|
|
268
|
+
} catch {
|
|
269
|
+
return body;
|
|
270
|
+
}
|
|
271
|
+
return json2xml(body).replace(/"/g, "").replace(/\\n/g, "\n").replace(/\\t/g, " ");
|
|
427
272
|
}
|
|
273
|
+
|
|
274
|
+
//#endregion
|
|
275
|
+
export { codeSampleGenerators, parseHostAndPath };
|