@gitbook/react-openapi 0.7.1 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/CHANGELOG.md +49 -0
  2. package/dist/InteractiveSection.d.ts +4 -8
  3. package/dist/InteractiveSection.jsx +60 -0
  4. package/dist/Markdown.d.ts +1 -2
  5. package/dist/Markdown.jsx +5 -0
  6. package/dist/OpenAPICodeSample.d.ts +2 -4
  7. package/dist/OpenAPICodeSample.jsx +141 -0
  8. package/dist/OpenAPIDisclosure.d.ts +12 -0
  9. package/dist/OpenAPIDisclosure.jsx +32 -0
  10. package/dist/OpenAPIDisclosureGroup.d.ts +19 -0
  11. package/dist/OpenAPIDisclosureGroup.jsx +81 -0
  12. package/dist/OpenAPIOperation.d.ts +2 -4
  13. package/dist/OpenAPIOperation.jsx +51 -0
  14. package/dist/OpenAPIOperationContext.d.ts +16 -0
  15. package/dist/OpenAPIOperationContext.jsx +26 -0
  16. package/dist/OpenAPIPath.d.ts +8 -0
  17. package/dist/OpenAPIPath.jsx +54 -0
  18. package/dist/OpenAPIRequestBody.d.ts +4 -5
  19. package/dist/OpenAPIRequestBody.jsx +22 -0
  20. package/dist/OpenAPIResponse.d.ts +4 -4
  21. package/dist/OpenAPIResponse.jsx +39 -0
  22. package/dist/OpenAPIResponseExample.d.ts +2 -4
  23. package/dist/OpenAPIResponseExample.jsx +108 -0
  24. package/dist/OpenAPIResponses.d.ts +3 -4
  25. package/dist/OpenAPIResponses.jsx +35 -0
  26. package/dist/OpenAPISchema.d.ts +11 -8
  27. package/dist/OpenAPISchema.jsx +285 -0
  28. package/dist/OpenAPISchemaName.d.ts +12 -0
  29. package/dist/OpenAPISchemaName.jsx +15 -0
  30. package/dist/OpenAPISecurities.d.ts +2 -4
  31. package/dist/OpenAPISecurities.jsx +55 -0
  32. package/dist/OpenAPIServerURL.d.ts +2 -3
  33. package/dist/OpenAPIServerURL.jsx +67 -0
  34. package/dist/OpenAPIServerURLVariable.d.ts +2 -3
  35. package/dist/OpenAPIServerURLVariable.jsx +8 -0
  36. package/dist/OpenAPISpec.d.ts +3 -4
  37. package/dist/OpenAPISpec.jsx +91 -0
  38. package/dist/OpenAPITabs.d.ts +26 -0
  39. package/dist/OpenAPITabs.jsx +103 -0
  40. package/dist/ScalarApiButton.d.ts +3 -3
  41. package/dist/ScalarApiButton.jsx +51 -0
  42. package/dist/code-samples.d.ts +4 -0
  43. package/dist/code-samples.js +103 -38
  44. package/dist/generateSchemaExample.d.ts +2 -2
  45. package/dist/generateSchemaExample.js +29 -102
  46. package/dist/index.d.ts +3 -2
  47. package/dist/index.js +2 -1
  48. package/dist/resolveOpenAPIOperation.d.ts +11 -0
  49. package/dist/resolveOpenAPIOperation.js +194 -0
  50. package/dist/stringifyOpenAPI.d.ts +4 -0
  51. package/dist/stringifyOpenAPI.js +6 -0
  52. package/dist/tsconfig.build.tsbuildinfo +1 -0
  53. package/dist/types.d.ts +11 -12
  54. package/dist/useSyncedTabsGlobalState.d.ts +1 -0
  55. package/dist/useSyncedTabsGlobalState.js +16 -0
  56. package/dist/utils.d.ts +6 -2
  57. package/dist/utils.js +13 -6
  58. package/package.json +12 -10
  59. package/src/InteractiveSection.tsx +90 -86
  60. package/src/Markdown.tsx +2 -3
  61. package/src/OpenAPICodeSample.tsx +43 -31
  62. package/src/OpenAPIDisclosure.tsx +50 -0
  63. package/src/OpenAPIDisclosureGroup.tsx +136 -0
  64. package/src/OpenAPIOperation.tsx +36 -42
  65. package/src/OpenAPIOperationContext.tsx +45 -0
  66. package/src/OpenAPIPath.tsx +65 -0
  67. package/src/OpenAPIRequestBody.tsx +10 -17
  68. package/src/OpenAPIResponse.tsx +27 -45
  69. package/src/OpenAPIResponseExample.tsx +89 -31
  70. package/src/OpenAPIResponses.tsx +48 -17
  71. package/src/OpenAPISchema.test.ts +1 -1
  72. package/src/OpenAPISchema.tsx +129 -108
  73. package/src/OpenAPISchemaName.tsx +27 -0
  74. package/src/OpenAPISecurities.tsx +45 -24
  75. package/src/OpenAPIServerURL.tsx +17 -10
  76. package/src/OpenAPIServerURLVariable.tsx +2 -4
  77. package/src/OpenAPISpec.tsx +58 -58
  78. package/src/OpenAPITabs.tsx +153 -0
  79. package/src/ScalarApiButton.tsx +84 -7
  80. package/src/code-samples.test.ts +51 -0
  81. package/src/code-samples.ts +95 -31
  82. package/src/generateSchemaExample.ts +26 -153
  83. package/src/index.ts +3 -2
  84. package/src/resolveOpenAPIOperation.test.ts +177 -0
  85. package/src/resolveOpenAPIOperation.ts +164 -0
  86. package/src/stringifyOpenAPI.ts +6 -0
  87. package/src/types.ts +17 -10
  88. package/src/useSyncedTabsGlobalState.ts +23 -0
  89. package/src/utils.ts +14 -7
  90. package/dist/InteractiveSection.js +0 -47
  91. package/dist/Markdown.js +0 -6
  92. package/dist/OpenAPICodeSample.js +0 -110
  93. package/dist/OpenAPIOperation.js +0 -38
  94. package/dist/OpenAPIRequestBody.js +0 -18
  95. package/dist/OpenAPIResponse.js +0 -32
  96. package/dist/OpenAPIResponseExample.js +0 -54
  97. package/dist/OpenAPIResponses.js +0 -18
  98. package/dist/OpenAPISchema.js +0 -235
  99. package/dist/OpenAPISchema.test.d.ts +0 -1
  100. package/dist/OpenAPISchema.test.js +0 -91
  101. package/dist/OpenAPISecurities.js +0 -42
  102. package/dist/OpenAPIServerURL.js +0 -51
  103. package/dist/OpenAPIServerURLVariable.js +0 -10
  104. package/dist/OpenAPISpec.js +0 -70
  105. package/dist/ScalarApiButton.js +0 -14
  106. package/dist/fetchOpenAPIOperation.d.ts +0 -72
  107. package/dist/fetchOpenAPIOperation.js +0 -124
  108. package/dist/fetchOpenAPIOperation.test.d.ts +0 -1
  109. package/dist/fetchOpenAPIOperation.test.js +0 -152
  110. package/dist/resolveOpenAPIPath.d.ts +0 -7
  111. package/dist/resolveOpenAPIPath.js +0 -112
  112. package/dist/resolveOpenAPIPath.test.d.ts +0 -1
  113. package/dist/resolveOpenAPIPath.test.js +0 -39
  114. package/dist/tsconfig.tsbuildinfo +0 -1
  115. package/src/fetchOpenAPIOperation.test.ts +0 -185
  116. package/src/fetchOpenAPIOperation.ts +0 -230
  117. package/src/resolveOpenAPIPath.test.ts +0 -60
  118. package/src/resolveOpenAPIPath.ts +0 -145
@@ -1,60 +0,0 @@
1
- import { it, expect } from 'bun:test';
2
-
3
- import { resolveOpenAPIPath } from './resolveOpenAPIPath';
4
- import { OpenAPIFetcher } from './types';
5
-
6
- const createFetcherForSchema = (schema: any): OpenAPIFetcher => {
7
- return {
8
- fetch: async (url) => {
9
- return schema;
10
- },
11
- };
12
- };
13
-
14
- it('should resolve a simple path through objects', async () => {
15
- const resolved = await resolveOpenAPIPath(
16
- 'https://test.com',
17
- ['a', 'b', 'c'],
18
- createFetcherForSchema({
19
- a: {
20
- b: {
21
- c: 'hello',
22
- },
23
- },
24
- }),
25
- );
26
-
27
- expect(resolved).toBe('hello');
28
- });
29
-
30
- it('should return undefined if the last part of the path does not exists', async () => {
31
- const resolved = await resolveOpenAPIPath(
32
- 'https://test.com',
33
- ['a', 'b', 'c'],
34
- createFetcherForSchema({
35
- a: {
36
- b: {
37
- d: 'hello',
38
- },
39
- },
40
- }),
41
- );
42
-
43
- expect(resolved).toBe(undefined);
44
- });
45
-
46
- it('should return undefined if a middle part of the path does not exists', async () => {
47
- const resolved = await resolveOpenAPIPath(
48
- 'https://test.com',
49
- ['a', 'x', 'c'],
50
- createFetcherForSchema({
51
- a: {
52
- b: {
53
- c: 'hello',
54
- },
55
- },
56
- }),
57
- );
58
-
59
- expect(resolved).toBe(undefined);
60
- });
@@ -1,145 +0,0 @@
1
- import { OpenAPIFetcher } from './types';
2
-
3
- const SYMBOL_MARKDOWN_PARSED = '__$markdownParsed';
4
- export const SYMBOL_REF_RESOLVED = '__$refResolved';
5
-
6
- /**
7
- * Resolve a path in a OpenAPI file.
8
- * It resolves any reference needed to resolve the path, ignoring other references outside the path.
9
- */
10
- export async function resolveOpenAPIPath<T>(
11
- url: string,
12
- dataPath: string[],
13
- fetcher: OpenAPIFetcher,
14
- ): Promise<T | undefined> {
15
- const data = await fetcher.fetch(url);
16
- let value: unknown = data;
17
-
18
- if (!value) {
19
- return undefined;
20
- }
21
-
22
- const lastKey = dataPath[dataPath.length - 1];
23
- dataPath = dataPath.slice(0, -1);
24
-
25
- for (const part of dataPath) {
26
- // @ts-ignore
27
- if (isRef(value[part])) {
28
- await transformAll(url, value, part, fetcher);
29
- }
30
-
31
- // @ts-ignore
32
- value = value[part];
33
-
34
- // If any part along the path is undefined, return undefined.
35
- if (typeof value !== 'object' || value === null) {
36
- return undefined;
37
- }
38
- }
39
-
40
- await transformAll(url, value, lastKey, fetcher);
41
-
42
- // @ts-expect-error
43
- return value[lastKey] as T;
44
- }
45
-
46
- /**
47
- * Recursively process a part of the OpenAPI spec to resolve all references.
48
- */
49
- async function transformAll(
50
- url: string,
51
- data: any,
52
- key: string | number,
53
- fetcher: OpenAPIFetcher,
54
- ): Promise<void> {
55
- const value = data[key];
56
-
57
- if (
58
- typeof value === 'string' &&
59
- key === 'description' &&
60
- fetcher.parseMarkdown &&
61
- !data[SYMBOL_MARKDOWN_PARSED]
62
- ) {
63
- // Parse markdown
64
- data[SYMBOL_MARKDOWN_PARSED] = true;
65
- data[key] = await fetcher.parseMarkdown(value);
66
- } else if (
67
- typeof value === 'string' ||
68
- typeof value === 'number' ||
69
- typeof value === 'boolean' ||
70
- value === null
71
- ) {
72
- // Primitives
73
- } else if (typeof value === 'object' && value !== null && SYMBOL_REF_RESOLVED in value) {
74
- // Ref was already resolved
75
- } else if (isRef(value)) {
76
- const ref = value.$ref;
77
-
78
- // Delete the ref to avoid infinite loop with circular references
79
- // @ts-ignore
80
- delete value.$ref;
81
-
82
- data[key] = await resolveReference(url, ref, fetcher);
83
- if (data[key]) {
84
- data[key][SYMBOL_REF_RESOLVED] = extractRefName(ref);
85
- }
86
- } else if (Array.isArray(value)) {
87
- // Recursively resolve all references in the array
88
- await Promise.all(value.map((item, index) => transformAll(url, value, index, fetcher)));
89
- } else if (typeof value === 'object' && value !== null) {
90
- // Recursively resolve all references in the object
91
- const keys = Object.keys(value);
92
- for (const key of keys) {
93
- await transformAll(url, value, key, fetcher);
94
- }
95
- }
96
- }
97
-
98
- async function resolveReference(
99
- origin: string,
100
- ref: string,
101
- fetcher: OpenAPIFetcher,
102
- ): Promise<any> {
103
- const parsed = parseReference(origin, ref);
104
- return resolveOpenAPIPath(parsed.url, parsed.dataPath, fetcher);
105
- }
106
-
107
- function parseReference(origin: string, ref: string): { url: string; dataPath: string[] } {
108
- if (!ref) {
109
- return {
110
- url: origin,
111
- dataPath: [],
112
- };
113
- }
114
-
115
- if (ref.startsWith('#')) {
116
- // Local references
117
- const dataPath = ref.split('/').filter(Boolean).slice(1);
118
- return {
119
- url: origin,
120
- dataPath,
121
- };
122
- }
123
-
124
- // Absolute references
125
- const url = new URL(ref, origin);
126
- if (url.hash) {
127
- const hash = url.hash;
128
- url.hash = '';
129
- return parseReference(url.toString(), hash);
130
- }
131
-
132
- return {
133
- url: url.toString(),
134
- dataPath: [],
135
- };
136
- }
137
-
138
- function extractRefName(ref: string): string {
139
- const parts = ref.split('/');
140
- return parts[parts.length - 1];
141
- }
142
-
143
- function isRef(ref: any): ref is { $ref: string } {
144
- return typeof ref === 'object' && ref !== null && '$ref' in ref && ref.$ref;
145
- }