@gitbook/react-openapi 1.0.4 → 1.1.0

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 (58) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/InteractiveSection.jsx +10 -9
  3. package/dist/OpenAPICodeSample.jsx +3 -3
  4. package/dist/OpenAPIDisclosure.d.ts +5 -9
  5. package/dist/OpenAPIDisclosure.jsx +25 -27
  6. package/dist/OpenAPIDisclosureGroup.d.ts +2 -2
  7. package/dist/OpenAPIDisclosureGroup.jsx +6 -5
  8. package/dist/OpenAPIPath.jsx +5 -1
  9. package/dist/OpenAPIResponseExample.jsx +8 -8
  10. package/dist/OpenAPIResponses.jsx +3 -3
  11. package/dist/OpenAPISchema.d.ts +3 -26
  12. package/dist/OpenAPISchema.jsx +80 -131
  13. package/dist/OpenAPISpec.jsx +3 -4
  14. package/dist/OpenAPITabs.jsx +51 -47
  15. package/dist/ScalarApiButton.d.ts +3 -2
  16. package/dist/ScalarApiButton.jsx +22 -18
  17. package/dist/StaticSection.d.ts +10 -0
  18. package/dist/StaticSection.jsx +23 -0
  19. package/dist/dereference.d.ts +5 -0
  20. package/dist/dereference.js +68 -0
  21. package/dist/index.d.ts +3 -2
  22. package/dist/index.js +2 -1
  23. package/dist/models/OpenAPIModels.d.ts +9 -0
  24. package/dist/models/OpenAPIModels.jsx +62 -0
  25. package/dist/models/index.d.ts +2 -0
  26. package/dist/models/index.js +2 -0
  27. package/dist/models/resolveOpenAPIModels.d.ts +7 -0
  28. package/dist/models/resolveOpenAPIModels.js +73 -0
  29. package/dist/resolveOpenAPIOperation.d.ts +2 -2
  30. package/dist/resolveOpenAPIOperation.js +3 -34
  31. package/dist/tsconfig.build.tsbuildinfo +1 -1
  32. package/dist/types.d.ts +8 -0
  33. package/dist/useSyncedTabsGlobalState.d.ts +10 -1
  34. package/dist/useSyncedTabsGlobalState.js +19 -15
  35. package/dist/utils.js +42 -3
  36. package/package.json +3 -3
  37. package/src/InteractiveSection.tsx +10 -18
  38. package/src/OpenAPICodeSample.tsx +3 -3
  39. package/src/OpenAPIDisclosure.tsx +35 -42
  40. package/src/OpenAPIDisclosureGroup.tsx +13 -11
  41. package/src/OpenAPIPath.tsx +7 -1
  42. package/src/OpenAPIResponseExample.tsx +8 -15
  43. package/src/OpenAPIResponses.tsx +3 -3
  44. package/src/OpenAPISchema.test.ts +26 -35
  45. package/src/OpenAPISchema.tsx +138 -227
  46. package/src/OpenAPISpec.tsx +3 -5
  47. package/src/OpenAPITabs.tsx +52 -63
  48. package/src/ScalarApiButton.tsx +26 -28
  49. package/src/StaticSection.tsx +59 -0
  50. package/src/dereference.ts +29 -0
  51. package/src/index.ts +3 -2
  52. package/src/models/OpenAPIModels.tsx +89 -0
  53. package/src/models/index.ts +2 -0
  54. package/src/models/resolveOpenAPIModels.ts +35 -0
  55. package/src/resolveOpenAPIOperation.ts +8 -36
  56. package/src/types.ts +10 -0
  57. package/src/useSyncedTabsGlobalState.ts +33 -21
  58. package/src/utils.ts +51 -3
@@ -1,57 +1,43 @@
1
- import clsx from 'clsx';
2
1
  import { useId } from 'react';
3
- import { InteractiveSection } from './InteractiveSection';
2
+ import clsx from 'clsx';
4
3
  import { Markdown } from './Markdown';
5
4
  import { OpenAPIDisclosure } from './OpenAPIDisclosure';
6
5
  import { OpenAPISchemaName } from './OpenAPISchemaName';
7
- import { stringifyOpenAPI } from './stringifyOpenAPI';
8
- import { checkIsReference, resolveDescription } from './utils';
6
+ import { checkIsReference, resolveDescription, resolveFirstExample } from './utils';
9
7
  /**
10
8
  * Render a property of an OpenAPI schema.
11
9
  */
12
- export function OpenAPISchemaProperty(props) {
13
- var _a;
14
- var schema = props.schema, _b = props.circularRefs, parentCircularRefs = _b === void 0 ? new Map() : _b, context = props.context, className = props.className;
10
+ function OpenAPISchemaProperty(props) {
11
+ var property = props.property, _a = props.circularRefs, parentCircularRefs = _a === void 0 ? new Map() : _a, context = props.context, className = props.className;
12
+ var schema = property.schema;
15
13
  var id = useId();
16
- var parentCircularRef = parentCircularRefs.get(schema);
17
- var circularRefs = new Map(parentCircularRefs).set(schema, id);
18
- // Avoid recursing infinitely, and instead render a link to the parent schema
19
- var properties = parentCircularRef ? null : getSchemaProperties(schema);
20
- var alternatives = parentCircularRef
21
- ? null
22
- : getSchemaAlternatives(schema, new Set(circularRefs.keys()));
23
- if ((_a = alternatives === null || alternatives === void 0 ? void 0 : alternatives[0]) === null || _a === void 0 ? void 0 : _a.length) {
24
- return (<OpenAPISchemaAlternativesItem {...props} circularRefs={circularRefs} context={context} alternatives={alternatives} parentCircularRef={parentCircularRef}/>);
25
- }
26
- if ((properties && properties.length > 0) || schema.type === 'object') {
27
- return (<InteractiveSection id={id} className={clsx('openapi-schema', className)}>
28
- <OpenAPISchemaPresentation {...props}/>
29
- {properties && properties.length > 0 ? (<OpenAPIDisclosure context={context} label={getDisclosureLabel(schema)}>
30
- <OpenAPISchemaProperties properties={properties} circularRefs={circularRefs} context={context}/>
31
- </OpenAPIDisclosure>) : null}
32
- {parentCircularRef ? (<OpenAPISchemaCircularRef id={parentCircularRef} schema={schema}/>) : null}
33
- </InteractiveSection>);
34
- }
35
- return (<InteractiveSection id={id} className={clsx('openapi-schema', className)}>
36
- <OpenAPISchemaPresentation {...props}/>
37
- {(properties && properties.length > 0) ||
38
- (schema.enum && schema.enum.length > 0) ||
39
- parentCircularRef ? (<>
40
- {(properties === null || properties === void 0 ? void 0 : properties.length) ? (<OpenAPISchemaProperties properties={properties} circularRefs={circularRefs} context={context}/>) : null}
41
- {parentCircularRef ? (<OpenAPISchemaCircularRef id={parentCircularRef} schema={schema}/>) : null}
42
- </>) : null}
43
- </InteractiveSection>);
14
+ return (<div id={id} className={clsx('openapi-schema', className)}>
15
+ <OpenAPISchemaPresentation property={property}/>
16
+ {(function () {
17
+ var parentCircularRef = parentCircularRefs.get(schema);
18
+ // Avoid recursing infinitely, and instead render a link to the parent schema
19
+ if (parentCircularRef) {
20
+ return <OpenAPISchemaCircularRef id={parentCircularRef} schema={schema}/>;
21
+ }
22
+ var circularRefs = parentCircularRefs.set(schema, id);
23
+ var properties = getSchemaProperties(schema);
24
+ var alternatives = getSchemaAlternatives(schema, new Set(circularRefs.keys()));
25
+ return (<>
26
+ {alternatives === null || alternatives === void 0 ? void 0 : alternatives.map(function (schema, index) { return (<OpenAPISchemaAlternative key={index} schema={schema} circularRefs={circularRefs} context={context}/>); })}
27
+ {(properties === null || properties === void 0 ? void 0 : properties.length) ? (<OpenAPIDisclosure context={context} label={getDisclosureLabel(schema)}>
28
+ <OpenAPISchemaProperties properties={properties} circularRefs={circularRefs} context={context}/>
29
+ </OpenAPIDisclosure>) : null}
30
+ </>);
31
+ })()}
32
+ </div>);
44
33
  }
45
34
  /**
46
35
  * Render a set of properties of an OpenAPI schema.
47
36
  */
48
37
  export function OpenAPISchemaProperties(props) {
49
38
  var id = props.id, properties = props.properties, circularRefs = props.circularRefs, context = props.context;
50
- if (!properties.length) {
51
- return null;
52
- }
53
39
  return (<div id={id} className="openapi-schema-properties">
54
- {properties.map(function (property) { return (<OpenAPISchemaProperty key={property.propertyName} circularRefs={circularRefs} {...property} context={context}/>); })}
40
+ {properties.map(function (property, index) { return (<OpenAPISchemaProperty key={index} circularRefs={circularRefs} property={property} context={context}/>); })}
55
41
  </div>);
56
42
  }
57
43
  /**
@@ -59,12 +45,11 @@ export function OpenAPISchemaProperties(props) {
59
45
  */
60
46
  export function OpenAPIRootSchema(props) {
61
47
  var schema = props.schema, context = props.context;
62
- // Avoid recursing infinitely, and instead render a link to the parent schema
63
48
  var properties = getSchemaProperties(schema);
64
- if (properties && properties.length > 0) {
49
+ if (properties === null || properties === void 0 ? void 0 : properties.length) {
65
50
  return <OpenAPISchemaProperties properties={properties} context={context}/>;
66
51
  }
67
- return (<OpenAPISchemaProperty schema={schema} context={context} className="openapi-schema-root"/>);
52
+ return (<OpenAPISchemaProperty className="openapi-schema-root" property={{ schema: schema }} context={context}/>);
68
53
  }
69
54
  /**
70
55
  * Render a tab for an alternative schema.
@@ -72,36 +57,16 @@ export function OpenAPIRootSchema(props) {
72
57
  * for primitives, it renders the schema itself.
73
58
  */
74
59
  function OpenAPISchemaAlternative(props) {
75
- var _a;
76
60
  var schema = props.schema, circularRefs = props.circularRefs, context = props.context;
77
- var id = useId();
78
- var subProperties = getSchemaProperties(schema);
79
61
  var description = resolveDescription(schema);
80
- var alternatives = getSchemaAlternatives(schema, new Set(circularRefs === null || circularRefs === void 0 ? void 0 : circularRefs.keys()));
81
- if (((_a = alternatives === null || alternatives === void 0 ? void 0 : alternatives[0]) === null || _a === void 0 ? void 0 : _a.length) && !(subProperties === null || subProperties === void 0 ? void 0 : subProperties.length)) {
82
- return (<>
83
- {description ? (<Markdown source={description} className="openapi-schema-description"/>) : null}
84
- <OpenAPIDisclosure context={context} label={getDisclosureLabel(schema)}>
85
- <OpenAPISchemaAlternativesItem schema={schema} circularRefs={circularRefs} context={context} alternatives={alternatives}/>
86
- </OpenAPIDisclosure>
87
- </>);
88
- }
62
+ var properties = getSchemaProperties(schema);
89
63
  return (<>
90
64
  {description ? (<Markdown source={description} className="openapi-schema-description"/>) : null}
91
65
  <OpenAPIDisclosure context={context} label={getDisclosureLabel(schema)}>
92
- <OpenAPISchemaProperties id={id} properties={subProperties !== null && subProperties !== void 0 ? subProperties : [{ schema: schema }]} circularRefs={subProperties ? new Map(circularRefs).set(schema, id) : circularRefs} context={context}/>
66
+ {(properties === null || properties === void 0 ? void 0 : properties.length) ? (<OpenAPISchemaProperties properties={properties} circularRefs={circularRefs} context={context}/>) : (<OpenAPISchemaProperty property={{ schema: schema }} circularRefs={circularRefs} context={context}/>)}
93
67
  </OpenAPIDisclosure>
94
68
  </>);
95
69
  }
96
- function OpenAPISchemaAlternativesItem(props) {
97
- var id = useId();
98
- var schema = props.schema, circularRefs = props.circularRefs, context = props.context, alternatives = props.alternatives, parentCircularRef = props.parentCircularRef;
99
- return (<InteractiveSection id={id} className={clsx('openapi-schema')}>
100
- <OpenAPISchemaPresentation {...props}/>
101
- {alternatives[0].map(function (alternative, index) { return (<OpenAPISchemaAlternative key={"alternative-".concat(index)} schema={alternative} circularRefs={circularRefs} context={context}/>); })}
102
- {parentCircularRef ? (<OpenAPISchemaCircularRef id={parentCircularRef} schema={schema}/>) : null}
103
- </InteractiveSection>);
104
- }
105
70
  /**
106
71
  * Render a circular reference to a schema.
107
72
  */
@@ -115,7 +80,7 @@ function OpenAPISchemaCircularRef(props) {
115
80
  /**
116
81
  * Render the enum value for a schema.
117
82
  */
118
- export function OpenAPISchemaEnum(props) {
83
+ function OpenAPISchemaEnum(props) {
119
84
  var enumValues = props.enumValues;
120
85
  return (<div className="openapi-schema-enum">
121
86
  <span>
@@ -127,18 +92,13 @@ export function OpenAPISchemaEnum(props) {
127
92
  </span>
128
93
  </div>);
129
94
  }
130
- export function OpenAPISchemaPresentation(props) {
131
- var schema = props.schema, propertyName = props.propertyName, required = props.required;
132
- var shouldDisplayExample = function (schema) {
133
- return ((typeof schema.example === 'string' && !!schema.example) ||
134
- typeof schema.example === 'number' ||
135
- typeof schema.example === 'boolean' ||
136
- (Array.isArray(schema.example) && schema.example.length > 0) ||
137
- (typeof schema.example === 'object' &&
138
- schema.example !== null &&
139
- Object.keys(schema.example).length > 0));
140
- };
95
+ /**
96
+ * Render the top row of a schema. e.g: name, type, and required status.
97
+ */
98
+ function OpenAPISchemaPresentation(props) {
99
+ var _a = props.property, schema = _a.schema, propertyName = _a.propertyName, required = _a.required;
141
100
  var description = resolveDescription(schema);
101
+ var example = resolveFirstExample(schema);
142
102
  return (<div className="openapi-schema-presentation">
143
103
  <OpenAPISchemaName schema={schema} type={getSchemaTitle(schema)} propertyName={propertyName} required={required}/>
144
104
  {schema['x-deprecated-sunset'] ? (<div className="openapi-deprecated-sunset openapi-schema-description openapi-markdown">
@@ -148,8 +108,8 @@ export function OpenAPISchemaPresentation(props) {
148
108
  </span>
149
109
  </div>) : null}
150
110
  {description ? (<Markdown source={description} className="openapi-schema-description"/>) : null}
151
- {shouldDisplayExample(schema) ? (<div className="openapi-schema-example">
152
- Example: <code>{formatExample(schema.example)}</code>
111
+ {example ? (<div className="openapi-schema-example">
112
+ Example: <code>{example}</code>
153
113
  </div>) : null}
154
114
  {schema.pattern ? (<div className="openapi-schema-pattern">
155
115
  Pattern: <code>{schema.pattern}</code>
@@ -162,28 +122,30 @@ export function OpenAPISchemaPresentation(props) {
162
122
  */
163
123
  function getSchemaProperties(schema) {
164
124
  // check array AND schema.items as this is sometimes null despite what the type indicates
165
- if (schema.type === 'array' && !!schema.items) {
125
+ if (schema.type === 'array' && schema.items && !checkIsReference(schema.items)) {
166
126
  var items = schema.items;
167
127
  var itemProperties = getSchemaProperties(items);
168
128
  if (itemProperties) {
169
129
  return itemProperties;
170
130
  }
171
131
  // If the items are a primitive type, we don't need to display them
172
- if (['string', 'number', 'boolean', 'integer'].includes(items.type) && !items.enum) {
132
+ if ((items.type === 'string' ||
133
+ items.type === 'number' ||
134
+ items.type === 'boolean' ||
135
+ items.type === 'integer') &&
136
+ !items.enum) {
173
137
  return null;
174
138
  }
175
- return [
176
- {
177
- propertyName: 'items',
178
- schema: items,
179
- },
180
- ];
139
+ return [{ propertyName: 'items', schema: items }];
181
140
  }
182
141
  if (schema.type === 'object' || schema.properties) {
183
142
  var result_1 = [];
184
143
  if (schema.properties) {
185
144
  Object.entries(schema.properties).forEach(function (_a) {
186
145
  var propertyName = _a[0], propertySchema = _a[1];
146
+ if (checkIsReference(propertySchema)) {
147
+ return;
148
+ }
187
149
  result_1.push({
188
150
  propertyName: propertyName,
189
151
  required: Array.isArray(schema.required)
@@ -193,11 +155,10 @@ function getSchemaProperties(schema) {
193
155
  });
194
156
  });
195
157
  }
196
- if (schema.additionalProperties) {
197
- var additionalProperties = schema.additionalProperties;
158
+ if (schema.additionalProperties && !checkIsReference(schema.additionalProperties)) {
198
159
  result_1.push({
199
160
  propertyName: 'Other properties',
200
- schema: additionalProperties === true ? {} : additionalProperties,
161
+ schema: schema.additionalProperties === true ? {} : schema.additionalProperties,
201
162
  });
202
163
  }
203
164
  return result_1;
@@ -209,43 +170,42 @@ function getSchemaProperties(schema) {
209
170
  */
210
171
  export function getSchemaAlternatives(schema, ancestors) {
211
172
  if (ancestors === void 0) { ancestors = new Set(); }
212
- var downAncestors = new Set(ancestors).add(schema);
213
- if (schema.anyOf) {
214
- return [flattenAlternatives('anyOf', schema.anyOf, downAncestors), schema.discriminator];
215
- }
216
- if (schema.oneOf) {
217
- return [flattenAlternatives('oneOf', schema.oneOf, downAncestors), schema.discriminator];
218
- }
219
- if (schema.allOf) {
220
- return [flattenAlternatives('allOf', schema.allOf, downAncestors), schema.discriminator];
173
+ var alternatives = (function () {
174
+ if (schema.anyOf) {
175
+ return ['anyOf', schema.anyOf];
176
+ }
177
+ if (schema.oneOf) {
178
+ return ['oneOf', schema.oneOf];
179
+ }
180
+ if (schema.allOf) {
181
+ return ['allOf', schema.allOf];
182
+ }
183
+ return null;
184
+ })();
185
+ if (!alternatives) {
186
+ return null;
221
187
  }
222
- return null;
188
+ var type = alternatives[0], schemas = alternatives[1];
189
+ return flattenAlternatives(type, schemas, new Set(ancestors).add(schema));
223
190
  }
224
- function flattenAlternatives(alternativeType, alternatives, ancestors) {
225
- return alternatives.reduce(function (acc, alternative) {
226
- var _a;
227
- if (!!alternative[alternativeType] && !ancestors.has(alternative)) {
228
- acc.push.apply(acc, (((_a = getSchemaAlternatives(alternative, ancestors)) === null || _a === void 0 ? void 0 : _a[0]) || []));
191
+ function flattenAlternatives(alternativeType, schemasOrRefs, ancestors) {
192
+ return schemasOrRefs.reduce(function (acc, schemaOrRef) {
193
+ if (checkIsReference(schemaOrRef)) {
194
+ return acc;
229
195
  }
230
- else {
231
- acc.push(alternative);
196
+ if (schemaOrRef[alternativeType] && !ancestors.has(schemaOrRef)) {
197
+ var schemas = getSchemaAlternatives(schemaOrRef, ancestors);
198
+ if (schemas) {
199
+ acc.push.apply(acc, schemas);
200
+ }
201
+ return acc;
232
202
  }
203
+ acc.push(schemaOrRef);
233
204
  return acc;
234
205
  }, []);
235
206
  }
236
- export function getSchemaTitle(schema,
237
- /** If the title is inferred in a oneOf with discriminator, we can use it to optimize the title */
238
- discriminator) {
207
+ function getSchemaTitle(schema) {
239
208
  var _a;
240
- // Try using the discriminator
241
- if ((discriminator === null || discriminator === void 0 ? void 0 : discriminator.propertyName) && schema.properties) {
242
- var discriminatorProperty = schema.properties[discriminator.propertyName];
243
- if (discriminatorProperty && !checkIsReference(discriminatorProperty)) {
244
- if (discriminatorProperty.enum) {
245
- return discriminatorProperty.enum.map(function (value) { return value.toString(); }).join(' | ');
246
- }
247
- }
248
- }
249
209
  // Otherwise try to infer a nice title
250
210
  var type = 'any';
251
211
  if (schema.enum) {
@@ -286,20 +246,9 @@ function getDisclosureLabel(schema) {
286
246
  }
287
247
  // Fallback to "child attributes" for enums and objects
288
248
  if (schema.items.enum || schema.items.type === 'object') {
289
- return;
249
+ return 'child attributes';
290
250
  }
291
251
  return (_b = (_a = schema.items.title) !== null && _a !== void 0 ? _a : schema.title) !== null && _b !== void 0 ? _b : getSchemaTitle(schema.items);
292
252
  }
293
- return schema.title;
294
- }
295
- function formatExample(example) {
296
- if (typeof example === 'string') {
297
- return example
298
- .replace(/\n/g, ' ') // Replace newlines with spaces
299
- .replace(/\s+/g, ' ') // Collapse multiple spaces/newlines into a single space
300
- .replace(/([\{\}:,])\s+/g, '$1 ') // Ensure a space after {, }, :, and ,
301
- .replace(/\s+([\{\}:,])/g, ' $1') // Ensure a space before {, }, :, and ,
302
- .trim();
303
- }
304
- return stringifyOpenAPI(example);
253
+ return schema.title || 'child attributes';
305
254
  }
@@ -1,9 +1,8 @@
1
- 'use client';
2
- import { InteractiveSection } from './InteractiveSection';
3
1
  import { OpenAPIRequestBody } from './OpenAPIRequestBody';
4
2
  import { OpenAPIResponses } from './OpenAPIResponses';
5
3
  import { OpenAPISchemaProperties } from './OpenAPISchema';
6
4
  import { OpenAPISecurities } from './OpenAPISecurities';
5
+ import { StaticSection } from './StaticSection';
7
6
  import { parameterToProperty } from './utils';
8
7
  /**
9
8
  * Client component to render the spec for the request and response.
@@ -21,9 +20,9 @@ export function OpenAPISpec(props) {
21
20
  {securities.length > 0 ? (<OpenAPISecurities securities={securities} context={context}/>) : null}
22
21
 
23
22
  {parameterGroups.map(function (group) {
24
- return (<InteractiveSection key={group.key} className="openapi-parameters" header={group.label}>
23
+ return (<StaticSection key={group.key} className="openapi-parameters" header={group.label}>
25
24
  <OpenAPISchemaProperties properties={group.parameters.map(parameterToProperty)} context={context}/>
26
- </InteractiveSection>);
25
+ </StaticSection>);
27
26
  })}
28
27
 
29
28
  {operation.requestBody ? (<OpenAPIRequestBody requestBody={operation.requestBody} context={context}/>) : null}
@@ -1,9 +1,9 @@
1
1
  'use client';
2
- import { createContext, useContext, useEffect, useMemo, useState } from 'react';
2
+ import { createContext, useContext, useEffect, useMemo, useRef, useState } from 'react';
3
3
  import { Tab, TabList, TabPanel, Tabs } from 'react-aria-components';
4
- import { useIntersectionObserver } from 'usehooks-ts';
4
+ import { useEventCallback } from 'usehooks-ts';
5
5
  import { Markdown } from './Markdown';
6
- import { useSyncedTabsGlobalState } from './useSyncedTabsGlobalState';
6
+ import { getOrCreateTabStoreByKey } from './useSyncedTabsGlobalState';
7
7
  var OpenAPITabsContext = createContext(null);
8
8
  function useOpenAPITabsContext() {
9
9
  var context = useContext(OpenAPITabsContext);
@@ -16,59 +16,62 @@ function useOpenAPITabsContext() {
16
16
  * The OpenAPI Tabs wrapper component.
17
17
  */
18
18
  export function OpenAPITabs(props) {
19
+ var _a, _b;
19
20
  var children = props.children, items = props.items, stateKey = props.stateKey;
20
- var _a = useIntersectionObserver({
21
- threshold: 0.1,
22
- rootMargin: '200px',
23
- }), ref = _a[0], isIntersectionVisible = _a[1];
24
- var isVisible = stateKey ? isIntersectionVisible : true;
25
- var defaultTab = items[0];
26
- var _b = useSyncedTabsGlobalState(), syncedTabs = _b[0], setSyncedTabs = _b[1];
27
21
  var _c = useState(function () {
28
- var _a, _b, _c;
29
- if (isVisible && stateKey && syncedTabs && syncedTabs.has(stateKey)) {
30
- var tabFromState = syncedTabs.get(stateKey);
31
- return (_a = tabFromState === null || tabFromState === void 0 ? void 0 : tabFromState.key) !== null && _a !== void 0 ? _a : (_b = items[0]) === null || _b === void 0 ? void 0 : _b.key;
32
- }
33
- return (_c = items[0]) === null || _c === void 0 ? void 0 : _c.key;
34
- }), selectedTabKey = _c[0], setSelectedTabKey = _c[1];
35
- var _d = useState(defaultTab), selectedTab = _d[0], setSelectedTab = _d[1];
36
- var handleSelectionChange = function (key) {
37
- setSelectedTabKey(key);
38
- if (stateKey) {
39
- var tab_1 = items.find(function (item) { return item.key === key; });
40
- if (!tab_1) {
41
- return;
22
+ var _a, _b;
23
+ if (stateKey && typeof window !== 'undefined') {
24
+ var store = getOrCreateTabStoreByKey(stateKey);
25
+ var tabKey_1 = store.getState().tabKey;
26
+ if (tabKey_1) {
27
+ return tabKey_1;
42
28
  }
43
- setSyncedTabs(function (state) {
44
- var newState = new Map(state);
45
- newState.set(stateKey, tab_1);
46
- return newState;
47
- });
48
29
  }
49
- };
30
+ return (_b = (_a = items[0]) === null || _a === void 0 ? void 0 : _a.key) !== null && _b !== void 0 ? _b : null;
31
+ }), tabKey = _c[0], setTabKey = _c[1];
32
+ var selectTab = useEventCallback(function (key) {
33
+ if (!key || key === tabKey) {
34
+ return;
35
+ }
36
+ var tab = items.find(function (item) { return item.key === key; });
37
+ if (!tab) {
38
+ return;
39
+ }
40
+ setTabKey(key);
41
+ });
42
+ var selectedTab = (_b = (_a = items.find(function (item) { return item.key === tabKey; })) !== null && _a !== void 0 ? _a : items[0]) !== null && _b !== void 0 ? _b : null;
43
+ var cancelDeferRef = useRef(null);
50
44
  useEffect(function () {
51
- if (isVisible && stateKey && syncedTabs && syncedTabs.has(stateKey)) {
52
- var tabFromState_1 = syncedTabs.get(stateKey);
53
- if (!items.some(function (item) { return item.key === (tabFromState_1 === null || tabFromState_1 === void 0 ? void 0 : tabFromState_1.key); })) {
54
- return setSelectedTab(defaultTab);
55
- }
56
- if (tabFromState_1 && (tabFromState_1 === null || tabFromState_1 === void 0 ? void 0 : tabFromState_1.key) !== (selectedTab === null || selectedTab === void 0 ? void 0 : selectedTab.key)) {
57
- var tabFromItems = items.find(function (item) { return item.key === tabFromState_1.key; });
58
- if (!tabFromItems) {
59
- return;
60
- }
61
- setSelectedTab(tabFromItems);
62
- }
45
+ if (!stateKey) {
46
+ return undefined;
63
47
  }
64
- }, [isVisible, stateKey, syncedTabs, selectedTabKey]);
48
+ var store = getOrCreateTabStoreByKey(stateKey);
49
+ return store.subscribe(function (state) {
50
+ var _a;
51
+ (_a = cancelDeferRef.current) === null || _a === void 0 ? void 0 : _a.call(cancelDeferRef);
52
+ cancelDeferRef.current = defer(function () { return selectTab(state.tabKey); });
53
+ });
54
+ }, [stateKey, selectTab]);
55
+ useEffect(function () {
56
+ return function () { var _a; return (_a = cancelDeferRef.current) === null || _a === void 0 ? void 0 : _a.call(cancelDeferRef); };
57
+ }, []);
65
58
  var contextValue = useMemo(function () { return ({ items: items, selectedTab: selectedTab }); }, [items, selectedTab]);
66
59
  return (<OpenAPITabsContext.Provider value={contextValue}>
67
- <Tabs ref={ref} className="openapi-tabs" onSelectionChange={handleSelectionChange} selectedKey={selectedTab === null || selectedTab === void 0 ? void 0 : selectedTab.key}>
60
+ <Tabs className="openapi-tabs" onSelectionChange={function (tabKey) {
61
+ selectTab(tabKey);
62
+ if (stateKey) {
63
+ var store = getOrCreateTabStoreByKey(stateKey);
64
+ store.setState({ tabKey: tabKey });
65
+ }
66
+ }} selectedKey={tabKey}>
68
67
  {children}
69
68
  </Tabs>
70
69
  </OpenAPITabsContext.Provider>);
71
70
  }
71
+ var defer = function (fn) {
72
+ var id = setTimeout(fn, 0);
73
+ return function () { return clearTimeout(id); };
74
+ };
72
75
  /**
73
76
  * The OpenAPI Tabs list component.
74
77
  * This component should be used as a child of the OpenAPITabs component.
@@ -77,14 +80,14 @@ export function OpenAPITabs(props) {
77
80
  export function OpenAPITabsList() {
78
81
  var items = useOpenAPITabsContext().items;
79
82
  return (<TabList className="openapi-tabs-list">
80
- {items.map(function (tab) { return (<Tab style={function (_a) {
83
+ {items.map(function (tab) { return (<Tab key={tab.key} id={tab.key} style={function (_a) {
81
84
  var isFocusVisible = _a.isFocusVisible;
82
85
  return ({
83
86
  outline: isFocusVisible
84
87
  ? '2px solid rgb(var(--primary-color-500)/0.4)'
85
88
  : 'none',
86
89
  });
87
- }} className="openapi-tabs-tab" key={"Tab-".concat(tab.key)} id={tab.key}>
90
+ }} className="openapi-tabs-tab">
88
91
  {tab.label}
89
92
  </Tab>); })}
90
93
  </TabList>);
@@ -99,7 +102,8 @@ export function OpenAPITabsPanels() {
99
102
  if (!selectedTab) {
100
103
  return null;
101
104
  }
102
- return (<TabPanel key={"TabPanel-".concat(selectedTab.key)} id={selectedTab.key.toString()} className="openapi-tabs-panel">
105
+ var key = selectedTab.key.toString();
106
+ return (<TabPanel key={key} id={key} className="openapi-tabs-panel">
103
107
  {selectedTab.body}
104
108
  {selectedTab.description ? (<Markdown source={selectedTab.description} className="openapi-tabs-footer"/>) : null}
105
109
  </TabPanel>);
@@ -1,8 +1,9 @@
1
+ import type { OpenAPIV3_1 } from '@gitbook/openapi-parser';
1
2
  /**
2
3
  * Button which launches the Scalar API Client
3
4
  */
4
- export declare function ScalarApiButton({ method, path, specUrl, }: {
5
- method: string;
5
+ export declare function ScalarApiButton(props: {
6
+ method: OpenAPIV3_1.HttpMethods;
6
7
  path: string;
7
8
  specUrl: string;
8
9
  }): import("react").JSX.Element;
@@ -1,15 +1,14 @@
1
1
  'use client';
2
2
  import { ApiClientModalProvider, useApiClientModal } from '@scalar/api-client-react';
3
- import { useEffect, useImperativeHandle, useRef, useState } from 'react';
3
+ import { useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react';
4
4
  import { createPortal } from 'react-dom';
5
- import { useEventCallback } from 'usehooks-ts';
6
5
  import { useOpenAPIOperationContext } from './OpenAPIOperationContext';
7
6
  /**
8
7
  * Button which launches the Scalar API Client
9
8
  */
10
- export function ScalarApiButton(_a) {
11
- var method = _a.method, path = _a.path, specUrl = _a.specUrl;
12
- var _b = useState(false), isOpen = _b[0], setIsOpen = _b[1];
9
+ export function ScalarApiButton(props) {
10
+ var method = props.method, path = props.path, specUrl = props.specUrl;
11
+ var _a = useState(false), isOpen = _a[0], setIsOpen = _a[1];
13
12
  var controllerRef = useRef(null);
14
13
  return (<div className="scalar scalar-activate">
15
14
  <button className="scalar-activate-button button" onClick={function () {
@@ -28,24 +27,29 @@ export function ScalarApiButton(_a) {
28
27
  </div>);
29
28
  }
30
29
  function ScalarModal(props) {
31
- return (<ApiClientModalProvider configuration={{ spec: { url: props.specUrl } }} initialRequest={{ path: props.path, method: props.method }}>
32
- <ScalarModalController method={props.method} path={props.path} controllerRef={props.controllerRef}/>
30
+ var method = props.method, path = props.path, specUrl = props.specUrl, controllerRef = props.controllerRef;
31
+ return (<ApiClientModalProvider configuration={{ spec: { url: specUrl } }} initialRequest={{ method: method, path: path }}>
32
+ <ScalarModalController method={method} path={path} controllerRef={controllerRef}/>
33
33
  </ApiClientModalProvider>);
34
34
  }
35
35
  function ScalarModalController(props) {
36
+ var method = props.method, path = props.path, controllerRef = props.controllerRef;
36
37
  var client = useApiClientModal();
37
- var openClient = client === null || client === void 0 ? void 0 : client.open;
38
- useImperativeHandle(props.controllerRef, function () { return ({ openClient: openClient ? function () { return openClient(); } : undefined }); }, [openClient]);
39
- // Open the client when the component is mounted.
40
- var onOpenClient = useOpenAPIOperationContext().onOpenClient;
41
- var trackOpening = useEventCallback(function () {
42
- onOpenClient({ method: props.method, path: props.path });
43
- });
44
- useEffect(function () {
45
- if (openClient) {
46
- openClient();
47
- trackOpening();
38
+ var openScalarClient = client === null || client === void 0 ? void 0 : client.open;
39
+ var trackClientOpening = useOpenAPIOperationContext().onOpenClient;
40
+ var openClient = useMemo(function () {
41
+ if (openScalarClient) {
42
+ return function () {
43
+ openScalarClient({ method: method, path: path, _source: 'gitbook' });
44
+ trackClientOpening({ method: method, path: path });
45
+ };
48
46
  }
47
+ return null;
48
+ }, [openScalarClient, method, path, trackClientOpening]);
49
+ useImperativeHandle(controllerRef, function () { return ({ openClient: openClient ? function () { return openClient(); } : undefined }); }, [openClient]);
50
+ // Open at mount
51
+ useEffect(function () {
52
+ openClient === null || openClient === void 0 ? void 0 : openClient();
49
53
  }, [openClient]);
50
54
  return null;
51
55
  }
@@ -0,0 +1,10 @@
1
+ import { type ComponentPropsWithoutRef } from 'react';
2
+ export declare function Section(props: ComponentPropsWithoutRef<'div'>): import("react").JSX.Element;
3
+ export declare function SectionHeader(props: ComponentPropsWithoutRef<'div'>): import("react").JSX.Element;
4
+ export declare function SectionHeaderContent(props: ComponentPropsWithoutRef<'div'>): import("react").JSX.Element;
5
+ export declare const SectionBody: import("react").ForwardRefExoticComponent<Omit<import("react").DetailedHTMLProps<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref"> & import("react").RefAttributes<HTMLDivElement>>;
6
+ export declare function StaticSection(props: {
7
+ className: string;
8
+ header: React.ReactNode;
9
+ children: React.ReactNode;
10
+ }): import("react").JSX.Element;
@@ -0,0 +1,23 @@
1
+ import clsx from 'clsx';
2
+ import { forwardRef } from 'react';
3
+ export function Section(props) {
4
+ return <div {...props} className={clsx('openapi-section', props.className)}/>;
5
+ }
6
+ export function SectionHeader(props) {
7
+ return (<div {...props} className={clsx('openapi-section-header', props.className && "".concat(props.className, "-header"))}/>);
8
+ }
9
+ export function SectionHeaderContent(props) {
10
+ return (<div {...props} className={clsx('openapi-section-header-content', props.className && "".concat(props.className, "-header-content"))}/>);
11
+ }
12
+ export var SectionBody = forwardRef(function SectionBody(props, ref) {
13
+ return (<div ref={ref} {...props} className={clsx('openapi-section-body', props.className && "".concat(props.className, "-body"))}/>);
14
+ });
15
+ export function StaticSection(props) {
16
+ var className = props.className, header = props.header, children = props.children;
17
+ return (<Section className={className}>
18
+ <SectionHeader className={className}>
19
+ <SectionHeaderContent className={className}>{header}</SectionHeaderContent>
20
+ </SectionHeader>
21
+ <SectionBody className={className}>{children}</SectionBody>
22
+ </Section>);
23
+ }
@@ -0,0 +1,5 @@
1
+ import { type Filesystem, type OpenAPIV3xDocument } from '@gitbook/openapi-parser';
2
+ /**
3
+ * Memoized version of `dereferenceSchema`.
4
+ */
5
+ export declare function dereferenceFilesystem(filesystem: Filesystem): Promise<OpenAPIV3xDocument>;