@gitbook/react-openapi 1.5.1 → 1.5.3

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.
Files changed (87) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/dist/InteractiveSection.js +59 -0
  3. package/dist/Markdown.js +10 -0
  4. package/dist/OpenAPICodeSample.js +219 -0
  5. package/dist/OpenAPICodeSampleInteractive.js +62 -0
  6. package/dist/OpenAPICodeSampleSelector.js +45 -0
  7. package/dist/OpenAPICopyButton.js +44 -0
  8. package/dist/OpenAPIDisclosure.js +28 -0
  9. package/dist/OpenAPIDisclosureGroup.js +89 -0
  10. package/dist/OpenAPIExample.js +41 -0
  11. package/dist/OpenAPIMediaType.js +58 -0
  12. package/dist/OpenAPIOperation.d.ts +15 -0
  13. package/dist/OpenAPIOperation.js +30 -0
  14. package/dist/OpenAPIOperationContext.d.ts +20 -0
  15. package/dist/OpenAPIOperationContext.js +30 -0
  16. package/dist/OpenAPIPath.js +25 -0
  17. package/dist/OpenAPIPathItem.js +22 -0
  18. package/dist/OpenAPIPathMultipleServers.js +43 -0
  19. package/dist/OpenAPIPrefillContextProvider.d.ts +26 -0
  20. package/dist/OpenAPIPrefillContextProvider.js +25 -0
  21. package/dist/OpenAPIRequestBody.js +28 -0
  22. package/dist/OpenAPIRequestBodyHeaderType.js +23 -0
  23. package/dist/OpenAPIRequiredScopes.js +67 -0
  24. package/dist/OpenAPIResponse.js +39 -0
  25. package/dist/OpenAPIResponseExample.js +75 -0
  26. package/dist/OpenAPIResponseExampleContent.js +61 -0
  27. package/dist/OpenAPIResponses.js +61 -0
  28. package/dist/OpenAPISchema.js +373 -0
  29. package/dist/OpenAPISchemaName.js +45 -0
  30. package/dist/OpenAPISchemaServer.js +13 -0
  31. package/dist/OpenAPISecurities.js +98 -0
  32. package/dist/OpenAPISelect.js +45 -0
  33. package/dist/OpenAPISpec.js +73 -0
  34. package/dist/OpenAPITooltip.js +23 -0
  35. package/dist/OpenAPIWebhook.d.ts +15 -0
  36. package/dist/OpenAPIWebhook.js +28 -0
  37. package/dist/OpenAPIWebhookExample.js +40 -0
  38. package/dist/ScalarApiButton.js +90 -0
  39. package/dist/StaticSection.js +37 -0
  40. package/dist/code-samples.js +305 -0
  41. package/dist/common/OpenAPIColumnSpec.js +23 -0
  42. package/dist/common/OpenAPIOperationDescription.js +18 -0
  43. package/dist/common/OpenAPIStability.js +17 -0
  44. package/dist/common/OpenAPISummary.js +27 -0
  45. package/dist/contentTypeChecks.js +34 -0
  46. package/dist/context.d.ts +74 -0
  47. package/dist/context.js +29 -0
  48. package/dist/decycle.js +41 -0
  49. package/dist/dereference.js +24 -0
  50. package/dist/formatPath.js +25 -0
  51. package/dist/generateSchemaExample.js +198 -0
  52. package/dist/getDisclosureLabel.js +17 -0
  53. package/dist/getOrCreateDisclosureStoreByKey.js +31 -0
  54. package/dist/getOrCreateStoreByKey.js +22 -0
  55. package/dist/index.d.ts +11 -662
  56. package/dist/index.js +9 -3871
  57. package/dist/json2xml.js +12 -0
  58. package/dist/resolveOpenAPIOperation.d.ts +15 -0
  59. package/dist/resolveOpenAPIOperation.js +105 -0
  60. package/dist/resolveOpenAPIWebhook.d.ts +15 -0
  61. package/dist/resolveOpenAPIWebhook.js +52 -0
  62. package/dist/schemas/OpenAPISchemaItem.js +26 -0
  63. package/dist/schemas/OpenAPISchemas.d.ts +19 -0
  64. package/dist/schemas/OpenAPISchemas.js +57 -0
  65. package/dist/schemas/resolveOpenAPISchemas.d.ts +15 -0
  66. package/dist/schemas/resolveOpenAPISchemas.js +17 -0
  67. package/dist/stringifyOpenAPI.js +14 -0
  68. package/dist/translate.js +43 -0
  69. package/dist/translations/de.js +50 -0
  70. package/dist/translations/en.d.ts +49 -0
  71. package/dist/translations/en.js +50 -0
  72. package/dist/translations/es.js +50 -0
  73. package/dist/translations/fr.js +50 -0
  74. package/dist/translations/index.d.ts +426 -0
  75. package/dist/translations/index.js +31 -0
  76. package/dist/translations/ja.js +50 -0
  77. package/dist/translations/nl.js +50 -0
  78. package/dist/translations/no.js +50 -0
  79. package/dist/translations/pt-br.js +50 -0
  80. package/dist/translations/types.d.ts +7 -0
  81. package/dist/translations/zh.js +50 -0
  82. package/dist/types.d.ts +38 -0
  83. package/dist/util/example.js +84 -0
  84. package/dist/util/server.js +38 -0
  85. package/dist/util/tryit-prefill.js +143 -0
  86. package/dist/utils.js +163 -0
  87. package/package.json +28 -12
@@ -0,0 +1,50 @@
1
+ //#region src/translations/zh.ts
2
+ const zh = {
3
+ required: "必填",
4
+ deprecated: "已弃用",
5
+ deprecated_and_sunset_on: "此操作已弃用,将于 ${1} 停止使用。",
6
+ stability_experimental: "实验性",
7
+ stability_alpha: "Alpha",
8
+ stability_beta: "Beta",
9
+ discriminator: "判别器",
10
+ copy_to_clipboard: "复制到剪贴板",
11
+ copied: "已复制",
12
+ no_content: "无内容",
13
+ unresolved_reference: "未解析的引用",
14
+ circular_reference: "循环引用",
15
+ read_only: "只读",
16
+ write_only: "只写",
17
+ optional: "可选",
18
+ min: "最小值",
19
+ max: "最大值",
20
+ nullable: "可为 null",
21
+ body: "请求体",
22
+ payload: "有效载荷",
23
+ headers: "头字段",
24
+ header: "头部",
25
+ authorizations: "授权",
26
+ responses: "响应",
27
+ response: "响应",
28
+ path_parameters: "路径参数",
29
+ query_parameters: "查询参数",
30
+ header_parameters: "头参数",
31
+ attributes: "属性",
32
+ test_it: "测试一下",
33
+ information: "信息",
34
+ success: "成功",
35
+ redirect: "重定向",
36
+ error: "错误",
37
+ show: "显示${1}",
38
+ hide: "隐藏${1}",
39
+ available_items: "可用项",
40
+ required_scopes: "必需范围",
41
+ required_scopes_description: "此端点需要以下范围:",
42
+ available_scopes: "可用范围:",
43
+ properties: "属性",
44
+ or: "或",
45
+ and: "和",
46
+ possible_values: "可能的值"
47
+ };
48
+
49
+ //#endregion
50
+ export { zh };
@@ -0,0 +1,38 @@
1
+ import { OpenAPICustomOperationProperties, OpenAPICustomPrefillProperties, OpenAPICustomSpecProperties, OpenAPIV3 } from "@gitbook/openapi-parser";
2
+
3
+ //#region src/types.d.ts
4
+ type OpenAPIServerVariableWithCustomProperties = OpenAPIV3.ServerVariableObject & OpenAPICustomPrefillProperties;
5
+ /**
6
+ * OpenAPI ServerObject type extended to provide x-gitbook prefill custom properties at the variable level.
7
+ */
8
+ type OpenAPIServerWithCustomProperties = Omit<OpenAPIV3.ServerObject, 'variables'> & {
9
+ name?: string;
10
+ variables?: {
11
+ [variable: string]: OpenAPIServerVariableWithCustomProperties;
12
+ };
13
+ } & OpenAPICustomPrefillProperties;
14
+ type OpenAPISecurityScope = [string, string | undefined];
15
+ type OpenAPICustomSecurityScheme = OpenAPIV3.SecuritySchemeObject & OpenAPICustomPrefillProperties & {
16
+ required?: boolean;
17
+ scopes?: OpenAPISecurityScope[] | null;
18
+ };
19
+ interface OpenAPIOperationData extends OpenAPICustomSpecProperties {
20
+ path: string;
21
+ method: string;
22
+ /** Servers to be used for this operation */
23
+ servers: OpenAPIServerWithCustomProperties[];
24
+ /** Spec of the operation */
25
+ operation: OpenAPIV3.OperationObject<OpenAPICustomOperationProperties>;
26
+ /** Securities that should be used for this operation */
27
+ securities: [string, OpenAPICustomSecurityScheme][];
28
+ }
29
+ interface OpenAPIWebhookData extends OpenAPICustomSpecProperties {
30
+ name: string;
31
+ method: string;
32
+ /** Servers to be used for this operation */
33
+ servers: OpenAPIV3.ServerObject[];
34
+ /** Spec of the webhook */
35
+ operation: OpenAPIV3.OperationObject<OpenAPICustomOperationProperties>;
36
+ }
37
+ //#endregion
38
+ export { OpenAPIOperationData, OpenAPIWebhookData };
@@ -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 };
@@ -0,0 +1,38 @@
1
+ //#region src/util/server.ts
2
+ /**
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);
9
+ }
10
+ /**
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("");
18
+ }
19
+ function parseServerURL(url) {
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;
35
+ }
36
+
37
+ //#endregion
38
+ export { getDefaultServerURL };
@@ -0,0 +1,143 @@
1
+ import { ExpressionRuntime, parseTemplate } from "@gitbook/expr";
2
+
3
+ //#region src/util/tryit-prefill.ts
4
+ const PREFILL_CUSTOM_PROPERTY = "x-gitbook-prefill";
5
+ /**
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
+ };
28
+ }
29
+ /**
30
+ * Resolve prefill authentication configuration for the security schemes defined for an operation.
31
+ */
32
+ function resolveTryItPrefillAuthForOperationSecurities(args) {
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;
61
+ }
62
+ /**
63
+ * Resolve prefill server configuration for the servers defined for an operation.
64
+ */
65
+ function resolveTryItPrefillServersForOperationServers(args) {
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;
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 };
package/dist/utils.js ADDED
@@ -0,0 +1,163 @@
1
+ import { stringifyOpenAPI } from "./stringifyOpenAPI.js";
2
+ import { tString } from "./translate.js";
3
+
4
+ //#region src/utils.ts
5
+ function checkIsReference(input) {
6
+ return typeof input === "object" && !!input && "$ref" in input;
7
+ }
8
+ function createStateKey(key, scope) {
9
+ return scope ? `${scope}_${key}` : key;
10
+ }
11
+ /**
12
+ * Check if an object has a description. Either at the root level or in items.
13
+ */
14
+ function hasDescription(object) {
15
+ return "description" in object || "x-gitbook-description-html" in object;
16
+ }
17
+ /**
18
+ * Resolve the description of an object.
19
+ */
20
+ function resolveDescription(object) {
21
+ if (hasDescription(object)) return "x-gitbook-description-html" in object && typeof object["x-gitbook-description-html"] === "string" ? object["x-gitbook-description-html"].trim() : typeof object.description === "string" ? object.description.trim() : void 0;
22
+ if ("items" in object && typeof object.items === "object" && hasDescription(object.items)) return resolveDescription(object.items);
23
+ }
24
+ /**
25
+ * Extract descriptions from an object.
26
+ */
27
+ function extractDescriptions(object) {
28
+ return {
29
+ description: object.description,
30
+ "x-gitbook-description-html": "x-gitbook-description-html" in object ? object["x-gitbook-description-html"] : void 0
31
+ };
32
+ }
33
+ /**
34
+ * Resolve the first example from an object.
35
+ */
36
+ function resolveFirstExample(object) {
37
+ if ("examples" in object && typeof object.examples === "object" && object.examples) {
38
+ const firstKey = Object.keys(object.examples)[0];
39
+ if (firstKey && object.examples[firstKey]) return formatExample(object.examples[firstKey]);
40
+ }
41
+ if (shouldDisplayExample(object)) return formatExample(object.example);
42
+ if (object.items && typeof object.items === "object") return formatExample(object.items.example);
43
+ }
44
+ /**
45
+ * Resolve the schema of a parameter.
46
+ * Extract the description, example and deprecated from parameter.
47
+ */
48
+ function resolveParameterSchema(parameter) {
49
+ const schema = checkIsReference(parameter.schema) ? void 0 : parameter.schema;
50
+ return {
51
+ ...extractDescriptions(parameter),
52
+ example: resolveFirstExample(parameter),
53
+ deprecated: parameter.deprecated,
54
+ ...schema
55
+ };
56
+ }
57
+ /**
58
+ * Transform a parameter object to a property object.
59
+ */
60
+ function parameterToProperty(parameter) {
61
+ if (checkIsReference(parameter)) return {
62
+ propertyName: parameter.$ref ?? "Unknown ref",
63
+ schema: {},
64
+ required: void 0
65
+ };
66
+ return {
67
+ propertyName: parameter.name,
68
+ schema: resolveParameterSchema(parameter),
69
+ required: parameter.required
70
+ };
71
+ }
72
+ /**
73
+ * Format the example of a schema.
74
+ */
75
+ function formatExample(example) {
76
+ if (typeof example === "string") return example.replace(/\n/g, " ").replace(/\s+/g, " ").replace(/([\{\}:,])\s+/g, "$1 ").replace(/\s+([\{\}:,])/g, " $1").trim();
77
+ return stringifyOpenAPI(example);
78
+ }
79
+ /**
80
+ * Check if an example should be displayed.
81
+ */
82
+ function shouldDisplayExample(schema) {
83
+ return typeof schema.example === "string" && !!schema.example || typeof schema.example === "number" || typeof schema.example === "boolean" || Array.isArray(schema.example) && schema.example.length > 0 || typeof schema.example === "object" && schema.example !== null && Object.keys(schema.example).length > 0;
84
+ }
85
+ /**
86
+ * Get the class name for a status code.
87
+ * 1xx: informational
88
+ * 2xx: success
89
+ * 3xx: redirect
90
+ * 4xx, 5xx: error
91
+ */
92
+ function getStatusCodeClassName(statusCode) {
93
+ switch (getStatusCodeCategory(statusCode)) {
94
+ case 1: return "informational";
95
+ case 2: return "success";
96
+ case 3: return "redirect";
97
+ case 4:
98
+ case 5: return "error";
99
+ default: return "unknown";
100
+ }
101
+ }
102
+ /**
103
+ * Get a default label for a status code.
104
+ * This is used when there is no label provided in the OpenAPI spec.
105
+ * 1xx: Information
106
+ * 2xx: Success
107
+ * 3xx: Redirect
108
+ * 4xx, 5xx: Error
109
+ */
110
+ function getStatusCodeDefaultLabel(statusCode, context) {
111
+ switch (getStatusCodeCategory(statusCode)) {
112
+ case 1: return tString(context.translation, "information");
113
+ case 2: return tString(context.translation, "success");
114
+ case 3: return tString(context.translation, "redirect");
115
+ case 4:
116
+ case 5: return tString(context.translation, "error");
117
+ default: return "";
118
+ }
119
+ }
120
+ function getStatusCodeCategory(statusCode) {
121
+ const code = typeof statusCode === "string" ? Number.parseInt(statusCode, 10) : statusCode;
122
+ if (Number.isNaN(code) || code < 100 || code >= 600) return "unknown";
123
+ return Math.floor(code / 100);
124
+ }
125
+ function getSchemaTitle(schema) {
126
+ let type = "any";
127
+ if (schema.enum || schema["x-enumDescriptions"] || schema["x-gitbook-enum"]) type = `${schema.type} · enum`;
128
+ else if (schema.type === "array" && !!schema.items) type = `${getSchemaTitle(schema.items)}[]`;
129
+ else if (Array.isArray(schema.type)) type = schema.type.join(" | ");
130
+ else if (schema.type || schema.properties) {
131
+ type = schema.type ?? "object";
132
+ if (schema.format) type += ` · ${schema.format}`;
133
+ if (type === "object" && schema.title) type += ` · ${schema.title.replaceAll(" ", "")}`;
134
+ }
135
+ if ("anyOf" in schema) type = "any of";
136
+ else if ("oneOf" in schema) type = "one of";
137
+ else if ("allOf" in schema) type = "all of";
138
+ else if ("not" in schema) type = "not";
139
+ return type;
140
+ }
141
+ /**
142
+ * Extract security information for an operation based on its security requirements and the spec security schemes.
143
+ */
144
+ function extractOperationSecurityInfo(args) {
145
+ const { securityRequirement, securities } = args;
146
+ const securitiesMap = new Map(securities);
147
+ if (!securityRequirement || securityRequirement.length === 0) return securities.map(([key, security]) => ({
148
+ key,
149
+ label: key,
150
+ schemes: [security]
151
+ }));
152
+ return securityRequirement.map((requirement, idx) => {
153
+ const schemeKeys = Object.keys(requirement);
154
+ return {
155
+ key: `security-${idx}`,
156
+ label: schemeKeys.join(" & "),
157
+ schemes: schemeKeys.map((schemeKey) => securitiesMap.get(schemeKey)).filter((s) => s !== void 0)
158
+ };
159
+ });
160
+ }
161
+
162
+ //#endregion
163
+ export { checkIsReference, createStateKey, extractOperationSecurityInfo, getSchemaTitle, getStatusCodeClassName, getStatusCodeDefaultLabel, parameterToProperty, resolveDescription, resolveFirstExample };
package/package.json CHANGED
@@ -7,42 +7,58 @@
7
7
  "default": "./dist/index.js"
8
8
  }
9
9
  },
10
- "version": "1.5.1",
10
+ "version": "1.5.3",
11
11
  "sideEffects": false,
12
12
  "dependencies": {
13
- "@gitbook/expr": "1.2.2",
14
- "@gitbook/openapi-parser": "3.0.3",
15
- "@scalar/api-client-react": "^1.3.16",
16
- "@scalar/oas-utils": "^0.2.130",
17
- "@scalar/types": "^0.1.9",
18
- "clsx": "^2.1.1",
13
+ "@gitbook/expr": "1.2.4",
14
+ "@gitbook/openapi-parser": "3.0.5",
15
+ "@scalar/api-client-react": "^1.3.46",
16
+ "@scalar/oas-utils": "^0.6.3",
17
+ "@scalar/types": "^0.4.0",
18
+ "classnames": "^2.5.1",
19
19
  "flatted": "^3.2.9",
20
20
  "json-xml-parse": "^1.3.0",
21
- "react-aria-components": "^1.6.0",
22
- "react-aria": "^3.37.0",
23
- "usehooks-ts": "^3.1.0",
21
+ "react-aria-components": "^1.13.0",
22
+ "react-aria": "^3.44.0",
23
+ "react-stately": "^3.42.0",
24
+ "usehooks-ts": "^3.1.1",
24
25
  "zustand": "^5.0.3",
25
26
  "js-yaml": "^4.1.0"
26
27
  },
27
28
  "devDependencies": {
28
29
  "@types/js-yaml": "^4.0.9",
30
+ "@types/react": "^19.0.0",
31
+ "@types/react-dom": "^19.0.0",
29
32
  "bun-types": "^1.1.20",
33
+ "react": "^19.0.0",
34
+ "react-dom": "^19.0.0",
30
35
  "tsdown": "^0.15.6",
31
36
  "typescript": "^5.5.3"
32
37
  },
38
+ "overrides": {
39
+ "@types/react": "catalog:",
40
+ "@types/react-dom": "catalog:",
41
+ "react": "catalog:",
42
+ "react-dom": "catalog:"
43
+ },
33
44
  "peerDependencies": {
34
- "react": "*"
45
+ "react": "*",
46
+ "react-dom": "*"
35
47
  },
36
48
  "scripts": {
37
49
  "build": "tsdown",
38
50
  "typecheck": "tsc --noEmit",
39
51
  "unit": "bun test",
40
52
  "dev": "bun run build -- --watch ./src",
41
- "clean": "rm -rf ./dist"
53
+ "clean": "rm -rf ./dist",
54
+ "publish-to-npm": "../../scripts/publish-if-new.sh"
42
55
  },
43
56
  "files": ["dist", "README.md", "CHANGELOG.md"],
44
57
  "publishConfig": {
45
58
  "access": "public",
46
59
  "registry": "https://registry.npmjs.org/"
60
+ },
61
+ "repository": {
62
+ "url": "https://github.com/GitbookIO/gitbook"
47
63
  }
48
64
  }