fumadocs-openapi 5.12.0 → 6.0.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.
package/dist/index.d.ts CHANGED
@@ -3,64 +3,10 @@ import Slugger from 'github-slugger';
3
3
  import { ComponentType, ReactNode } from 'react';
4
4
  import { CodeToHastOptionsCommon, CodeOptionsThemes, BuiltinTheme } from 'shiki';
5
5
 
6
- interface BaseRequestField {
7
- name: string;
8
- description?: string;
9
- }
10
- interface BaseSchema {
11
- description?: string;
12
- isRequired: boolean;
13
- }
14
- type PrimitiveRequestField = BaseRequestField & PrimitiveSchema;
15
- interface PrimitiveSchema extends BaseSchema {
16
- type: 'boolean' | 'string' | 'number';
17
- defaultValue: string;
18
- }
19
- interface ReferenceSchema extends BaseSchema {
20
- type: 'ref';
21
- schema: string;
22
- }
23
- interface ArraySchema extends BaseSchema {
24
- type: 'array';
25
- /**
26
- * Reference to item schema or the schema
27
- */
28
- items: string | RequestSchema;
29
- }
30
- interface FileSchema extends BaseSchema {
31
- type: 'file';
32
- }
33
- interface ObjectSchema extends BaseSchema {
34
- type: 'object';
35
- properties: Record<string, ReferenceSchema>;
36
- /**
37
- * Reference to schema, or true if it's `any`
38
- */
39
- additionalProperties?: boolean | string;
40
- }
41
- interface SwitcherSchema extends BaseSchema {
42
- type: 'switcher';
43
- items: Record<string, ReferenceSchema | RequestSchema>;
44
- }
45
- interface NullSchema extends BaseSchema {
46
- type: 'null';
47
- }
48
- type RequestSchema = PrimitiveSchema | ArraySchema | ObjectSchema | SwitcherSchema | NullSchema | FileSchema;
49
- interface APIPlaygroundProps {
50
- route: string;
51
- method: string;
52
- authorization?: PrimitiveRequestField & {
53
- authType: string;
54
- };
55
- path?: PrimitiveRequestField[];
56
- query?: PrimitiveRequestField[];
57
- header?: PrimitiveRequestField[];
58
- body?: RequestSchema & {
59
- mediaType: string;
60
- };
61
- schemas: Record<string, RequestSchema>;
62
- proxyUrl?: string;
63
- }
6
+ type NoReference<T> = T extends (infer I)[] ? NoReference<I>[] : T extends ReferenceObject ? Exclude<T, ReferenceObject> : T extends object ? {
7
+ [K in keyof T]: NoReference<T[K]>;
8
+ } : T;
9
+ type ParsedSchema = OpenAPIV3_1.SchemaObject;
64
10
 
65
11
  interface ResponsesProps {
66
12
  items: string[];
@@ -127,14 +73,13 @@ interface Renderer {
127
73
  */
128
74
  ObjectCollapsible: ComponentType<ObjectCollapsibleProps>;
129
75
  Property: ComponentType<PropertyProps>;
130
- APIPlayground: ComponentType<APIPlaygroundProps>;
76
+ APIPlayground: ComponentType<{
77
+ path: string;
78
+ method: MethodInformation;
79
+ ctx: RenderContext;
80
+ }>;
131
81
  }
132
82
 
133
- type NoReference<T> = T extends (infer I)[] ? NoReference<I>[] : T extends ReferenceObject ? Exclude<T, ReferenceObject> : T extends object ? {
134
- [K in keyof T]: NoReference<T[K]>;
135
- } : T;
136
- type ParsedSchema = OpenAPIV3_1.SchemaObject;
137
-
138
83
  /**
139
84
  * Sample info of endpoint
140
85
  */
@@ -196,10 +141,6 @@ type Awaitable<T> = T | Promise<T>;
196
141
  */
197
142
  type DereferenceMap = Map<unknown, string>;
198
143
  interface RenderContext {
199
- /**
200
- * Use Scalar for API Playground
201
- */
202
- useScalar: boolean;
203
144
  /**
204
145
  * The url of proxy to avoid CORS issues
205
146
  */
@@ -0,0 +1,93 @@
1
+ 'use client';
2
+ import { jsx, jsxs } from 'react/jsx-runtime';
3
+ import { cn } from 'fumadocs-ui/components/api';
4
+ import { buttonVariants } from 'fumadocs-ui/components/ui/button';
5
+ import { ApiClientModalProvider, useApiClientModal } from '@scalar/api-client-react';
6
+ import { cva } from 'class-variance-authority';
7
+ import { useTheme } from 'next-themes';
8
+ import { useState, useEffect } from 'react';
9
+
10
+ const variants = cva('font-mono font-medium', {
11
+ variants: {
12
+ color: {
13
+ green: 'text-green-600 dark:text-green-400',
14
+ yellow: 'text-yellow-600 dark:text-yellow-400',
15
+ red: 'text-red-600 dark:text-red-400',
16
+ blue: 'text-blue-600 dark:text-blue-400',
17
+ orange: 'text-orange-600 dark:text-orange-400'
18
+ }
19
+ }
20
+ });
21
+ function getBadgeColor(method) {
22
+ switch(method.toUpperCase()){
23
+ case 'PUT':
24
+ return 'yellow';
25
+ case 'PATCH':
26
+ return 'orange';
27
+ case 'POST':
28
+ return 'blue';
29
+ case 'DELETE':
30
+ return 'red';
31
+ default:
32
+ return 'green';
33
+ }
34
+ }
35
+ function MethodLabel({ children, ...props }) {
36
+ return /*#__PURE__*/ jsx("span", {
37
+ ...props,
38
+ className: cn(variants({
39
+ color: getBadgeColor(children)
40
+ }), props.className),
41
+ children: children.toUpperCase()
42
+ });
43
+ }
44
+
45
+ function ScalarPlayground({ path, method, spec }) {
46
+ const { resolvedTheme } = useTheme();
47
+ const [mounted, setMounted] = useState(false);
48
+ useEffect(()=>{
49
+ setMounted(true);
50
+ }, []);
51
+ return /*#__PURE__*/ jsxs("div", {
52
+ className: cn('flex flex-row items-center gap-2.5 p-3 rounded-xl border bg-fd-card text-fd-card-foreground not-prose', mounted ? `${resolvedTheme}-mode` : null),
53
+ children: [
54
+ /*#__PURE__*/ jsx(MethodLabel, {
55
+ className: "text-xs",
56
+ children: method
57
+ }),
58
+ /*#__PURE__*/ jsx("code", {
59
+ className: "flex-1 overflow-auto text-nowrap text-[13px] text-fd-muted-foreground",
60
+ children: path
61
+ }),
62
+ /*#__PURE__*/ jsx(ApiClientModalProvider, {
63
+ configuration: {
64
+ themeId: 'moon',
65
+ spec: {
66
+ content: spec
67
+ }
68
+ },
69
+ children: /*#__PURE__*/ jsx(Trigger, {
70
+ path: path,
71
+ method: method
72
+ })
73
+ })
74
+ ]
75
+ });
76
+ }
77
+ function Trigger({ path, method }) {
78
+ const client = useApiClientModal();
79
+ return /*#__PURE__*/ jsx("button", {
80
+ type: "submit",
81
+ className: cn(buttonVariants({
82
+ color: 'primary',
83
+ size: 'sm'
84
+ }), 'px-3 py-1.5'),
85
+ onClick: ()=>client?.open({
86
+ path,
87
+ method
88
+ }),
89
+ children: "Test"
90
+ });
91
+ }
92
+
93
+ export { ScalarPlayground as default };
@@ -0,0 +1,176 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { OpenAPIV3_1 } from 'openapi-types';
3
+ import Slugger from 'github-slugger';
4
+ import { ComponentType, ReactNode } from 'react';
5
+ import { CodeToHastOptionsCommon, CodeOptionsThemes, BuiltinTheme } from 'shiki';
6
+
7
+ type NoReference<T> = T extends (infer I)[] ? NoReference<I>[] : T extends ReferenceObject ? Exclude<T, ReferenceObject> : T extends object ? {
8
+ [K in keyof T]: NoReference<T[K]>;
9
+ } : T;
10
+ type ParsedSchema = OpenAPIV3_1.SchemaObject;
11
+
12
+ interface ResponsesProps {
13
+ items: string[];
14
+ children: ReactNode;
15
+ }
16
+ interface ResponseProps {
17
+ value: string;
18
+ children: ReactNode;
19
+ }
20
+ interface APIInfoProps {
21
+ method: string;
22
+ route: string;
23
+ head: ReactNode;
24
+ children: ReactNode;
25
+ }
26
+ interface PropertyProps {
27
+ name: string;
28
+ type: string;
29
+ required?: boolean;
30
+ deprecated?: boolean;
31
+ children?: ReactNode;
32
+ }
33
+ interface ObjectCollapsibleProps {
34
+ name: string;
35
+ children: ReactNode;
36
+ }
37
+ interface RequestProps {
38
+ language: string;
39
+ name: string;
40
+ code: string;
41
+ }
42
+ interface ResponseTypeProps {
43
+ lang: string;
44
+ code: string;
45
+ label: string;
46
+ }
47
+ interface RootProps {
48
+ baseUrl?: string;
49
+ servers: ServerObject[];
50
+ children: ReactNode;
51
+ }
52
+ interface Renderer {
53
+ Root: ComponentType<RootProps>;
54
+ API: ComponentType<{
55
+ children: ReactNode;
56
+ }>;
57
+ APIInfo: ComponentType<APIInfoProps>;
58
+ APIExample: ComponentType<{
59
+ children: ReactNode;
60
+ }>;
61
+ Responses: ComponentType<ResponsesProps>;
62
+ Response: ComponentType<ResponseProps>;
63
+ Requests: ComponentType<{
64
+ items: string[];
65
+ children: ReactNode;
66
+ }>;
67
+ Request: ComponentType<RequestProps>;
68
+ ResponseTypes: ComponentType<{
69
+ children: ReactNode;
70
+ }>;
71
+ ResponseType: ComponentType<ResponseTypeProps>;
72
+ /**
73
+ * Collapsible to show object schemas
74
+ */
75
+ ObjectCollapsible: ComponentType<ObjectCollapsibleProps>;
76
+ Property: ComponentType<PropertyProps>;
77
+ APIPlayground: ComponentType<{
78
+ path: string;
79
+ method: MethodInformation;
80
+ ctx: RenderContext;
81
+ }>;
82
+ }
83
+
84
+ /**
85
+ * Sample info of endpoint
86
+ */
87
+ interface EndpointSample {
88
+ /**
89
+ * Request URL, including path and query parameters
90
+ */
91
+ url: string;
92
+ method: string;
93
+ body?: {
94
+ schema: ParsedSchema;
95
+ mediaType: string;
96
+ sample: unknown;
97
+ };
98
+ responses: Record<string, ResponseSample>;
99
+ parameters: ParameterSample[];
100
+ }
101
+ interface ResponseSample {
102
+ mediaType: string;
103
+ sample: unknown;
104
+ schema: ParsedSchema;
105
+ }
106
+ interface ParameterSample {
107
+ name: string;
108
+ in: string;
109
+ schema: ParsedSchema;
110
+ sample: unknown;
111
+ }
112
+
113
+ interface CodeSample {
114
+ lang: string;
115
+ label: string;
116
+ source: string | ((endpoint: EndpointSample) => string | undefined) | false;
117
+ }
118
+
119
+ type ProcessedDocument = {
120
+ document: NoReference<Document>;
121
+ dereferenceMap: DereferenceMap;
122
+ downloaded: Document;
123
+ };
124
+
125
+ type Document = OpenAPIV3_1.Document;
126
+ type OperationObject = OpenAPIV3_1.OperationObject;
127
+ type ReferenceObject = OpenAPIV3_1.ReferenceObject;
128
+ type ServerObject = NoReference<OpenAPIV3_1.ServerObject>;
129
+ type MethodInformation = NoReference<OperationObject> & {
130
+ method: string;
131
+ };
132
+ type Awaitable<T> = T | Promise<T>;
133
+ /**
134
+ * Dereferenced value and its original `$ref` value
135
+ */
136
+ type DereferenceMap = Map<unknown, string>;
137
+ interface RenderContext {
138
+ /**
139
+ * The url of proxy to avoid CORS issues
140
+ */
141
+ proxyUrl?: string;
142
+ renderer: Renderer;
143
+ baseUrl: string;
144
+ servers: ServerObject[];
145
+ slugger: Slugger;
146
+ /**
147
+ * dereferenced schema
148
+ */
149
+ schema: ProcessedDocument;
150
+ /**
151
+ * Generate TypeScript definitions from response schema.
152
+ *
153
+ * Pass `false` to disable it.
154
+ *
155
+ * @param endpoint - the API endpoint
156
+ * @param code - status code
157
+ */
158
+ generateTypeScriptSchema?: ((endpoint: EndpointSample, code: string) => Awaitable<string>) | false;
159
+ /**
160
+ * Generate code samples for endpoint.
161
+ */
162
+ generateCodeSamples?: (endpoint: EndpointSample) => Awaitable<CodeSample[]>;
163
+ shikiOptions?: Omit<CodeToHastOptionsCommon, 'lang'> & CodeOptionsThemes<BuiltinTheme>;
164
+ /**
165
+ * Show full response schema instead of only example response & Typescript definitions
166
+ */
167
+ showResponseSchema?: boolean;
168
+ }
169
+
170
+ declare function APIPlayground({ path, method, ctx, }: {
171
+ path: string;
172
+ method: MethodInformation;
173
+ ctx: RenderContext;
174
+ }): react_jsx_runtime.JSX.Element;
175
+
176
+ export { APIPlayground };
@@ -0,0 +1,13 @@
1
+ import { jsx } from 'react/jsx-runtime';
2
+ import dynamic from 'next/dynamic';
3
+
4
+ const Client = dynamic(()=>import('./client-client-DgnHaOQ3.js'));
5
+ function APIPlayground({ path, method, ctx }) {
6
+ return /*#__PURE__*/ jsx(Client, {
7
+ method: method.method,
8
+ path: path,
9
+ spec: ctx.schema.downloaded
10
+ });
11
+ }
12
+
13
+ export { APIPlayground };
@@ -5,64 +5,10 @@ import { CodeToHastOptionsCommon, CodeOptionsThemes, BuiltinTheme } from 'shiki'
5
5
  import { NextRequest } from 'next/server';
6
6
  import { BuildPageTreeOptions } from 'fumadocs-core/source';
7
7
 
8
- interface BaseRequestField {
9
- name: string;
10
- description?: string;
11
- }
12
- interface BaseSchema {
13
- description?: string;
14
- isRequired: boolean;
15
- }
16
- type PrimitiveRequestField = BaseRequestField & PrimitiveSchema;
17
- interface PrimitiveSchema extends BaseSchema {
18
- type: 'boolean' | 'string' | 'number';
19
- defaultValue: string;
20
- }
21
- interface ReferenceSchema extends BaseSchema {
22
- type: 'ref';
23
- schema: string;
24
- }
25
- interface ArraySchema extends BaseSchema {
26
- type: 'array';
27
- /**
28
- * Reference to item schema or the schema
29
- */
30
- items: string | RequestSchema;
31
- }
32
- interface FileSchema extends BaseSchema {
33
- type: 'file';
34
- }
35
- interface ObjectSchema extends BaseSchema {
36
- type: 'object';
37
- properties: Record<string, ReferenceSchema>;
38
- /**
39
- * Reference to schema, or true if it's `any`
40
- */
41
- additionalProperties?: boolean | string;
42
- }
43
- interface SwitcherSchema extends BaseSchema {
44
- type: 'switcher';
45
- items: Record<string, ReferenceSchema | RequestSchema>;
46
- }
47
- interface NullSchema extends BaseSchema {
48
- type: 'null';
49
- }
50
- type RequestSchema = PrimitiveSchema | ArraySchema | ObjectSchema | SwitcherSchema | NullSchema | FileSchema;
51
- interface APIPlaygroundProps {
52
- route: string;
53
- method: string;
54
- authorization?: PrimitiveRequestField & {
55
- authType: string;
56
- };
57
- path?: PrimitiveRequestField[];
58
- query?: PrimitiveRequestField[];
59
- header?: PrimitiveRequestField[];
60
- body?: RequestSchema & {
61
- mediaType: string;
62
- };
63
- schemas: Record<string, RequestSchema>;
64
- proxyUrl?: string;
65
- }
8
+ type NoReference<T> = T extends (infer I)[] ? NoReference<I>[] : T extends ReferenceObject ? Exclude<T, ReferenceObject> : T extends object ? {
9
+ [K in keyof T]: NoReference<T[K]>;
10
+ } : T;
11
+ type ParsedSchema = OpenAPIV3_1.SchemaObject;
66
12
 
67
13
  interface ResponsesProps {
68
14
  items: string[];
@@ -129,14 +75,13 @@ interface Renderer {
129
75
  */
130
76
  ObjectCollapsible: ComponentType<ObjectCollapsibleProps>;
131
77
  Property: ComponentType<PropertyProps>;
132
- APIPlayground: ComponentType<APIPlaygroundProps>;
78
+ APIPlayground: ComponentType<{
79
+ path: string;
80
+ method: MethodInformation;
81
+ ctx: RenderContext;
82
+ }>;
133
83
  }
134
84
 
135
- type NoReference<T> = T extends (infer I)[] ? NoReference<I>[] : T extends ReferenceObject ? Exclude<T, ReferenceObject> : T extends object ? {
136
- [K in keyof T]: NoReference<T[K]>;
137
- } : T;
138
- type ParsedSchema = OpenAPIV3_1.SchemaObject;
139
-
140
85
  /**
141
86
  * Sample info of endpoint
142
87
  */
@@ -180,18 +125,18 @@ type ProcessedDocument = {
180
125
  };
181
126
 
182
127
  type Document = OpenAPIV3_1.Document;
128
+ type OperationObject = OpenAPIV3_1.OperationObject;
183
129
  type ReferenceObject = OpenAPIV3_1.ReferenceObject;
184
130
  type ServerObject = NoReference<OpenAPIV3_1.ServerObject>;
131
+ type MethodInformation = NoReference<OperationObject> & {
132
+ method: string;
133
+ };
185
134
  type Awaitable<T> = T | Promise<T>;
186
135
  /**
187
136
  * Dereferenced value and its original `$ref` value
188
137
  */
189
138
  type DereferenceMap = Map<unknown, string>;
190
139
  interface RenderContext {
191
- /**
192
- * Use Scalar for API Playground
193
- */
194
- useScalar: boolean;
195
140
  /**
196
141
  * The url of proxy to avoid CORS issues
197
142
  */
@@ -224,7 +169,7 @@ interface RenderContext {
224
169
  showResponseSchema?: boolean;
225
170
  }
226
171
 
227
- type ApiPageContextProps = Pick<Partial<RenderContext>, 'shikiOptions' | 'generateTypeScriptSchema' | 'generateCodeSamples' | 'proxyUrl' | 'showResponseSchema' | 'useScalar'>;
172
+ type ApiPageContextProps = Pick<Partial<RenderContext>, 'shikiOptions' | 'generateTypeScriptSchema' | 'generateCodeSamples' | 'proxyUrl' | 'showResponseSchema'>;
228
173
  interface ApiPageProps extends ApiPageContextProps {
229
174
  document: DocumentInput;
230
175
  hasHead: boolean;