@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
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { tString } from "../translate.js";
|
|
2
|
+
import { OpenAPIExample } from "../OpenAPIExample.js";
|
|
3
|
+
import { checkIsReference } from "../utils.js";
|
|
4
|
+
import { generateSchemaExample } from "../generateSchemaExample.js";
|
|
5
|
+
|
|
6
|
+
//#region src/util/example.tsx
|
|
7
|
+
/**
|
|
8
|
+
* Generate an example from a reference object.
|
|
9
|
+
*/
|
|
10
|
+
function getExampleFromReference(ref, context) {
|
|
11
|
+
return {
|
|
12
|
+
summary: tString(context.translation, "unresolved_reference"),
|
|
13
|
+
value: { $ref: ref.$ref }
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Get examples from a media type object.
|
|
18
|
+
*/
|
|
19
|
+
function getExamplesFromMediaTypeObject(args) {
|
|
20
|
+
const { mediaTypeObject, mediaType, context } = args;
|
|
21
|
+
if (mediaTypeObject.examples) return Object.entries(mediaTypeObject.examples).map(([key, example]) => {
|
|
22
|
+
return {
|
|
23
|
+
key,
|
|
24
|
+
example: checkIsReference(example) ? getExampleFromReference(example, context) : example
|
|
25
|
+
};
|
|
26
|
+
});
|
|
27
|
+
if (mediaTypeObject.example) return [{
|
|
28
|
+
key: "default",
|
|
29
|
+
example: { value: mediaTypeObject.example }
|
|
30
|
+
}];
|
|
31
|
+
if (mediaTypeObject.schema) {
|
|
32
|
+
if (mediaType === "application/xml") return [{
|
|
33
|
+
key: "default",
|
|
34
|
+
example: { value: { [mediaTypeObject.schema.xml?.name ?? "object"]: generateSchemaExample(mediaTypeObject.schema, {
|
|
35
|
+
xml: mediaType === "application/xml",
|
|
36
|
+
mode: "read"
|
|
37
|
+
}) } }
|
|
38
|
+
}];
|
|
39
|
+
return [{
|
|
40
|
+
key: "default",
|
|
41
|
+
example: { value: generateSchemaExample(mediaTypeObject.schema, { mode: "read" }) }
|
|
42
|
+
}];
|
|
43
|
+
}
|
|
44
|
+
return [];
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Get example from a schema object.
|
|
48
|
+
*/
|
|
49
|
+
function getExampleFromSchema(args) {
|
|
50
|
+
const { schema } = args;
|
|
51
|
+
if (schema.example) return { value: schema.example };
|
|
52
|
+
return { value: generateSchemaExample(schema, { mode: "read" }) };
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Get the examples from a media type object.
|
|
56
|
+
*/
|
|
57
|
+
function getExamples(props) {
|
|
58
|
+
const { mediaTypeObject, mediaType, context } = props;
|
|
59
|
+
const examples = getExamplesFromMediaTypeObject({
|
|
60
|
+
mediaTypeObject,
|
|
61
|
+
mediaType,
|
|
62
|
+
context
|
|
63
|
+
});
|
|
64
|
+
const syntax = getSyntaxFromMediaType(mediaType);
|
|
65
|
+
return examples.map((example) => {
|
|
66
|
+
return {
|
|
67
|
+
key: example.key,
|
|
68
|
+
label: example.example.summary || example.key,
|
|
69
|
+
body: <OpenAPIExample example={example.example} context={props.context} syntax={syntax} />
|
|
70
|
+
};
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Get the syntax from a media type.
|
|
75
|
+
*/
|
|
76
|
+
function getSyntaxFromMediaType(mediaType) {
|
|
77
|
+
if (mediaType.includes("json")) return "json";
|
|
78
|
+
if (mediaType.includes("yaml")) return "yaml";
|
|
79
|
+
if (mediaType === "application/xml") return "xml";
|
|
80
|
+
return "text";
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
//#endregion
|
|
84
|
+
export { getExampleFromReference, getExampleFromSchema, getExamples };
|
package/dist/util/server.js
CHANGED
|
@@ -1,44 +1,38 @@
|
|
|
1
|
+
//#region src/util/server.ts
|
|
1
2
|
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
return '';
|
|
9
|
-
}
|
|
10
|
-
return interpolateServerURL(server);
|
|
3
|
+
* Get the default URL for the server.
|
|
4
|
+
*/
|
|
5
|
+
function getDefaultServerURL(servers) {
|
|
6
|
+
const server = servers[0];
|
|
7
|
+
if (!server) return "";
|
|
8
|
+
return interpolateServerURL(server);
|
|
11
9
|
}
|
|
12
10
|
/**
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
var _a, _b, _c;
|
|
21
|
-
if (part.kind === 'text') {
|
|
22
|
-
return part.text;
|
|
23
|
-
}
|
|
24
|
-
return (_c = (_b = (_a = server.variables) === null || _a === void 0 ? void 0 : _a[part.name]) === null || _b === void 0 ? void 0 : _b.default) !== null && _c !== void 0 ? _c : "{".concat(part.name, "}");
|
|
25
|
-
})
|
|
26
|
-
.join('');
|
|
11
|
+
* Interpolate the server URL with the default values of the variables.
|
|
12
|
+
*/
|
|
13
|
+
function interpolateServerURL(server) {
|
|
14
|
+
return parseServerURL(server?.url ?? "").map((part) => {
|
|
15
|
+
if (part.kind === "text") return part.text;
|
|
16
|
+
return server.variables?.[part.name]?.default ?? `{${part.name}}`;
|
|
17
|
+
}).join("");
|
|
27
18
|
}
|
|
28
19
|
function parseServerURL(url) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
20
|
+
const parts = url.split(/{([^}]+)}/g);
|
|
21
|
+
const result = [];
|
|
22
|
+
for (let i = 0; i < parts.length; i++) {
|
|
23
|
+
const part = parts[i];
|
|
24
|
+
if (!part) continue;
|
|
25
|
+
if (i % 2 === 0) result.push({
|
|
26
|
+
kind: "text",
|
|
27
|
+
text: part
|
|
28
|
+
});
|
|
29
|
+
else result.push({
|
|
30
|
+
kind: "variable",
|
|
31
|
+
name: part
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
return result;
|
|
44
35
|
}
|
|
36
|
+
|
|
37
|
+
//#endregion
|
|
38
|
+
export { getDefaultServerURL };
|
|
@@ -1,129 +1,143 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
-
t[p] = s[p];
|
|
7
|
-
}
|
|
8
|
-
return t;
|
|
9
|
-
};
|
|
10
|
-
return __assign.apply(this, arguments);
|
|
11
|
-
};
|
|
12
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
13
|
-
var t = {};
|
|
14
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
15
|
-
t[p] = s[p];
|
|
16
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
17
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
18
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
19
|
-
t[p[i]] = s[p[i]];
|
|
20
|
-
}
|
|
21
|
-
return t;
|
|
22
|
-
};
|
|
23
|
-
import { ExpressionRuntime, parseTemplate } from '@gitbook/expr';
|
|
1
|
+
import { ExpressionRuntime, parseTemplate } from "@gitbook/expr";
|
|
2
|
+
|
|
3
|
+
//#region src/util/tryit-prefill.ts
|
|
4
|
+
const PREFILL_CUSTOM_PROPERTY = "x-gitbook-prefill";
|
|
24
5
|
/**
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
? resolveTryItPrefillServersForOperationServers({ servers: servers, resolveTryItPrefillExpression: resolveTryItPrefillExpression })
|
|
48
|
-
: [];
|
|
49
|
-
return __assign(__assign({}, (prefillAuth ? { authentication: prefillAuth } : {})), (prefillServers ? { servers: prefillServers } : {}));
|
|
6
|
+
* Resolve the Scalar API client prefill configuration for a given OpenAPI operation.
|
|
7
|
+
*/
|
|
8
|
+
function resolveTryItPrefillForOperation(args) {
|
|
9
|
+
const { operation: { securities, servers }, prefillInputContext } = args;
|
|
10
|
+
if (!prefillInputContext) return {};
|
|
11
|
+
const runtime = new ExpressionRuntime();
|
|
12
|
+
const resolveTryItPrefillExpression = (expr) => {
|
|
13
|
+
if (!parseTemplate(expr).length) return;
|
|
14
|
+
return runtime.evaluateTemplate(expr, prefillInputContext);
|
|
15
|
+
};
|
|
16
|
+
const prefillAuth = securities ? resolveTryItPrefillAuthForOperationSecurities({
|
|
17
|
+
securities,
|
|
18
|
+
resolveTryItPrefillExpression
|
|
19
|
+
}) : void 0;
|
|
20
|
+
const prefillServers = servers ? resolveTryItPrefillServersForOperationServers({
|
|
21
|
+
servers,
|
|
22
|
+
resolveTryItPrefillExpression
|
|
23
|
+
}) : [];
|
|
24
|
+
return {
|
|
25
|
+
...prefillAuth ? { authentication: prefillAuth } : {},
|
|
26
|
+
...prefillServers ? { servers: prefillServers } : {}
|
|
27
|
+
};
|
|
50
28
|
}
|
|
51
29
|
/**
|
|
52
|
-
|
|
53
|
-
|
|
30
|
+
* Resolve prefill authentication configuration for the security schemes defined for an operation.
|
|
31
|
+
*/
|
|
54
32
|
function resolveTryItPrefillAuthForOperationSecurities(args) {
|
|
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
|
-
break;
|
|
85
|
-
}
|
|
86
|
-
case 'oauth2':
|
|
87
|
-
case 'openIdConnect': {
|
|
88
|
-
break;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
return Object.keys(prefillAuthConfig).length > 0
|
|
93
|
-
? { securitySchemes: prefillAuthConfig }
|
|
94
|
-
: undefined;
|
|
33
|
+
const { securities, resolveTryItPrefillExpression } = args;
|
|
34
|
+
const prefillAuthConfig = {};
|
|
35
|
+
for (const [schemeName, security] of Object.values(securities)) {
|
|
36
|
+
const tryitPrefillAuthValue = security[PREFILL_CUSTOM_PROPERTY] ? resolveTryItPrefillExpression(security[PREFILL_CUSTOM_PROPERTY]) : void 0;
|
|
37
|
+
if (!tryitPrefillAuthValue) continue;
|
|
38
|
+
switch (security.type) {
|
|
39
|
+
case "http":
|
|
40
|
+
if (security.scheme?.includes("bearer")) prefillAuthConfig[schemeName] = { token: tryitPrefillAuthValue };
|
|
41
|
+
else if (security.scheme?.includes("basic") && tryitPrefillAuthValue.includes(":")) {
|
|
42
|
+
const [username, password] = tryitPrefillAuthValue.split(":", 2);
|
|
43
|
+
prefillAuthConfig[schemeName] = {
|
|
44
|
+
username,
|
|
45
|
+
password
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
break;
|
|
49
|
+
case "apiKey":
|
|
50
|
+
prefillAuthConfig[schemeName] = {
|
|
51
|
+
name: security.name,
|
|
52
|
+
in: security.in,
|
|
53
|
+
value: tryitPrefillAuthValue
|
|
54
|
+
};
|
|
55
|
+
break;
|
|
56
|
+
case "oauth2":
|
|
57
|
+
case "openIdConnect": break;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return Object.keys(prefillAuthConfig).length > 0 ? { securitySchemes: prefillAuthConfig } : void 0;
|
|
95
61
|
}
|
|
96
62
|
/**
|
|
97
|
-
|
|
98
|
-
|
|
63
|
+
* Resolve prefill server configuration for the servers defined for an operation.
|
|
64
|
+
*/
|
|
99
65
|
function resolveTryItPrefillServersForOperationServers(args) {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
resolvedServers.push(resolvedServer);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
return resolvedServers.length > 0 ? resolvedServers : undefined;
|
|
66
|
+
const { servers, resolveTryItPrefillExpression } = args;
|
|
67
|
+
const resolvedServers = [];
|
|
68
|
+
for (const server of servers) {
|
|
69
|
+
const tryItPrefillServerUrlExpr = server[PREFILL_CUSTOM_PROPERTY];
|
|
70
|
+
const tryItPrefillServerUrlValue = tryItPrefillServerUrlExpr ? resolveTryItPrefillExpression(tryItPrefillServerUrlExpr) : void 0;
|
|
71
|
+
const variables = server.variables ? { ...server.variables } : {};
|
|
72
|
+
if (server.variables) for (const [varName, variable] of Object.entries(server.variables)) {
|
|
73
|
+
const { [PREFILL_CUSTOM_PROPERTY]: tryItPrefillVarExpr,...variableProps } = variable;
|
|
74
|
+
const tryItPrefillVarValue = tryItPrefillVarExpr ? resolveTryItPrefillExpression(tryItPrefillVarExpr) : void 0;
|
|
75
|
+
variables[varName] = {
|
|
76
|
+
...variableProps,
|
|
77
|
+
...tryItPrefillVarValue ? { default: String(tryItPrefillVarValue) } : {}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
const hasServerVariables = Object.keys(variables).length > 0;
|
|
81
|
+
if (server.url && (tryItPrefillServerUrlValue || hasServerVariables)) {
|
|
82
|
+
const resolvedServer = {
|
|
83
|
+
url: tryItPrefillServerUrlValue ?? server.url,
|
|
84
|
+
...server.description ? { description: server.description } : {},
|
|
85
|
+
...hasServerVariables ? { variables } : {}
|
|
86
|
+
};
|
|
87
|
+
resolvedServers.push(resolvedServer);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return resolvedServers.length > 0 ? resolvedServers : void 0;
|
|
129
91
|
}
|
|
92
|
+
/**
|
|
93
|
+
* Return a X-GITBOOK-PREFILL placeholder based on the prefill custom property in the provided security scheme.
|
|
94
|
+
*/
|
|
95
|
+
function resolvePrefillCodePlaceholderFromSecurityScheme(args) {
|
|
96
|
+
const { security, defaultPlaceholderValue } = args;
|
|
97
|
+
const prefillExprParts = extractPrefillExpressionPartsFromSecurityScheme(security);
|
|
98
|
+
if (prefillExprParts.length === 0) return defaultPlaceholderValue ?? "";
|
|
99
|
+
return toPrefillCodePlaceholder(templatePartsToExpression(prefillExprParts), defaultPlaceholderValue);
|
|
100
|
+
}
|
|
101
|
+
function extractPrefillExpressionPartsFromSecurityScheme(security) {
|
|
102
|
+
const expression = security[PREFILL_CUSTOM_PROPERTY];
|
|
103
|
+
if (!expression || expression.length === 0) return [];
|
|
104
|
+
return parseTemplate(expression);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Return a server URL with X-GITBOOK-PREFILL placeholders based on the prefill custom properties in the provided security scheme.
|
|
108
|
+
*/
|
|
109
|
+
function resolveURLWithPrefillCodePlaceholdersFromServer(server, defaultServerUrl) {
|
|
110
|
+
const serverVariables = server.variables ?? {};
|
|
111
|
+
const variableExprs = {};
|
|
112
|
+
let hasVariablePrefill = false;
|
|
113
|
+
for (const [name, variable] of Object.entries(serverVariables ?? {})) if (variable[PREFILL_CUSTOM_PROPERTY]) {
|
|
114
|
+
hasVariablePrefill = true;
|
|
115
|
+
variableExprs[name] = `(${templatePartsToExpression(parseTemplate(variable[PREFILL_CUSTOM_PROPERTY]))} ?? '${variable.default ?? ""}')`;
|
|
116
|
+
} else variableExprs[name] = String(variable.default) ?? "";
|
|
117
|
+
let interpolatedUrl = server.url ?? "";
|
|
118
|
+
interpolatedUrl = interpolatedUrl.replace(/{([^}]+)}/g, (_, varName) => {
|
|
119
|
+
const expr = variableExprs[varName];
|
|
120
|
+
if (serverVariables[varName]?.[PREFILL_CUSTOM_PROPERTY]) return `\${${expr ?? `'${varName}'`}}`;
|
|
121
|
+
return expr ?? `{${varName}}`;
|
|
122
|
+
});
|
|
123
|
+
const interpolatedUrlTemplate = hasVariablePrefill ? `\`${interpolatedUrl}\`` : interpolatedUrl;
|
|
124
|
+
const urlLevelExpr = server[PREFILL_CUSTOM_PROPERTY];
|
|
125
|
+
if (urlLevelExpr) return toPrefillCodePlaceholder(`${templatePartsToExpression(parseTemplate(urlLevelExpr))} ?? ${hasVariablePrefill ? interpolatedUrlTemplate : `'${interpolatedUrlTemplate}'`}`, defaultServerUrl);
|
|
126
|
+
if (hasVariablePrefill) return toPrefillCodePlaceholder(interpolatedUrlTemplate, defaultServerUrl);
|
|
127
|
+
return interpolatedUrl;
|
|
128
|
+
}
|
|
129
|
+
function templatePartsToExpression(parts) {
|
|
130
|
+
return parts.map((part) => {
|
|
131
|
+
switch (part.type) {
|
|
132
|
+
case "text": return `"${part.value}"`;
|
|
133
|
+
case "expression": return part.value;
|
|
134
|
+
default: return "";
|
|
135
|
+
}
|
|
136
|
+
}).join(" + ");
|
|
137
|
+
}
|
|
138
|
+
function toPrefillCodePlaceholder(expression, defaultValue) {
|
|
139
|
+
return `$$__X-GITBOOK-PREFILL[(${expression})${defaultValue ? ` ?? '${defaultValue}'` : ""}]__$$`;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
//#endregion
|
|
143
|
+
export { resolvePrefillCodePlaceholderFromSecurityScheme, resolveTryItPrefillForOperation, resolveURLWithPrefillCodePlaceholdersFromServer };
|