fumadocs-openapi 6.3.0 → 7.0.0-beta.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 (77) hide show
  1. package/dist/playground/client.d.ts +4 -3
  2. package/dist/playground/client.d.ts.map +1 -1
  3. package/dist/playground/client.js +109 -102
  4. package/dist/playground/fetcher.d.ts +6 -22
  5. package/dist/playground/fetcher.d.ts.map +1 -1
  6. package/dist/playground/fetcher.js +25 -85
  7. package/dist/playground/get-default-values.d.ts +2 -3
  8. package/dist/playground/get-default-values.d.ts.map +1 -1
  9. package/dist/playground/get-default-values.js +8 -4
  10. package/dist/playground/index.d.ts +0 -1
  11. package/dist/playground/index.d.ts.map +1 -1
  12. package/dist/playground/index.js +0 -1
  13. package/dist/playground/inputs.d.ts.map +1 -1
  14. package/dist/playground/inputs.js +46 -32
  15. package/dist/render/operation/api-example.d.ts +18 -4
  16. package/dist/render/operation/api-example.d.ts.map +1 -1
  17. package/dist/render/operation/api-example.js +68 -72
  18. package/dist/render/operation/get-request-data.d.ts +5 -0
  19. package/dist/render/operation/get-request-data.d.ts.map +1 -0
  20. package/dist/render/operation/get-request-data.js +71 -0
  21. package/dist/render/{operation.d.ts → operation/index.d.ts} +4 -4
  22. package/dist/render/operation/index.d.ts.map +1 -0
  23. package/dist/render/{operation.js → operation/index.js} +14 -15
  24. package/dist/render/renderer.d.ts +1 -9
  25. package/dist/render/renderer.d.ts.map +1 -1
  26. package/dist/render/renderer.js +3 -5
  27. package/dist/requests/_shared.d.ts +11 -0
  28. package/dist/requests/_shared.d.ts.map +1 -0
  29. package/dist/requests/_shared.js +4 -0
  30. package/dist/requests/curl.d.ts +2 -2
  31. package/dist/requests/curl.d.ts.map +1 -1
  32. package/dist/requests/curl.js +17 -18
  33. package/dist/requests/go.d.ts +2 -2
  34. package/dist/requests/go.d.ts.map +1 -1
  35. package/dist/requests/go.js +15 -14
  36. package/dist/requests/index.d.ts +3 -0
  37. package/dist/requests/index.d.ts.map +1 -0
  38. package/dist/requests/index.js +26 -0
  39. package/dist/requests/javascript.d.ts +2 -2
  40. package/dist/requests/javascript.d.ts.map +1 -1
  41. package/dist/requests/javascript.js +20 -27
  42. package/dist/requests/python.d.ts +2 -2
  43. package/dist/requests/python.d.ts.map +1 -1
  44. package/dist/requests/python.js +18 -22
  45. package/dist/server/api-page.js +1 -1
  46. package/dist/types.d.ts +5 -6
  47. package/dist/types.d.ts.map +1 -1
  48. package/dist/ui/client.d.ts +1 -2
  49. package/dist/ui/client.d.ts.map +1 -1
  50. package/dist/ui/client.js +1 -3
  51. package/dist/ui/components/method-label.d.ts +1 -1
  52. package/dist/ui/contexts/api.d.ts +3 -5
  53. package/dist/ui/contexts/api.d.ts.map +1 -1
  54. package/dist/ui/contexts/api.js +2 -7
  55. package/dist/ui/contexts/code-example.d.ts +25 -0
  56. package/dist/ui/contexts/code-example.d.ts.map +1 -0
  57. package/dist/ui/contexts/code-example.js +104 -0
  58. package/dist/ui/contexts/code-example.lazy.d.ts +12 -0
  59. package/dist/ui/contexts/code-example.lazy.d.ts.map +1 -0
  60. package/dist/ui/contexts/code-example.lazy.js +5 -0
  61. package/dist/ui/server-select.d.ts +6 -1
  62. package/dist/ui/server-select.d.ts.map +1 -1
  63. package/dist/ui/server-select.js +6 -7
  64. package/dist/utils/get-pathname-from-input.d.ts +2 -0
  65. package/dist/utils/get-pathname-from-input.d.ts.map +1 -0
  66. package/dist/utils/get-pathname-from-input.js +15 -0
  67. package/dist/utils/get-typescript-schema.d.ts +2 -3
  68. package/dist/utils/get-typescript-schema.d.ts.map +1 -1
  69. package/dist/utils/get-typescript-schema.js +13 -16
  70. package/package.json +6 -6
  71. package/dist/render/operation.d.ts.map +0 -1
  72. package/dist/ui/sample-select.d.ts +0 -4
  73. package/dist/ui/sample-select.d.ts.map +0 -1
  74. package/dist/ui/sample-select.js +0 -19
  75. package/dist/utils/generate-sample.d.ts +0 -40
  76. package/dist/utils/generate-sample.d.ts.map +0 -1
  77. package/dist/utils/generate-sample.js +0 -109
@@ -1,3 +1,3 @@
1
- import { type EndpointSample } from '../utils/generate-sample.js';
2
- export declare function getSampleRequest(endpoint: EndpointSample, sampleKey: string): string;
1
+ import { type RequestData } from '../requests/_shared.js';
2
+ export declare function getSampleRequest(url: string, data: RequestData): string;
3
3
  //# sourceMappingURL=curl.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"curl.d.ts","sourceRoot":"","sources":["../../src/requests/curl.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAG9D,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,cAAc,EACxB,SAAS,EAAE,MAAM,GAChB,MAAM,CA0CR"}
1
+ {"version":3,"file":"curl.d.ts","sourceRoot":"","sources":["../../src/requests/curl.ts"],"names":[],"mappings":"AAEA,OAAO,EAAU,KAAK,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAE9D,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,GAAG,MAAM,CAqCvE"}
@@ -1,28 +1,27 @@
1
+ 'use client';
1
2
  import { inputToString } from '../utils/input-to-string.js';
2
- export function getSampleRequest(endpoint, sampleKey) {
3
+ import { getUrl } from '../requests/_shared.js';
4
+ export function getSampleRequest(url, data) {
3
5
  const s = [];
4
- s.push(`curl -X ${endpoint.method} "${endpoint.url}"`);
5
- for (const param of endpoint.parameters) {
6
- if (param.in === 'header') {
7
- const header = `${param.name}: ${param.sample}`;
8
- s.push(`-H "${header}"`);
9
- }
10
- if (param.in === 'cookie') {
11
- const cookie = JSON.stringify(`${param.name}=${param.sample}`);
12
- s.push(`--cookie ${cookie}`);
13
- }
6
+ s.push(`curl -X ${data.method} "${getUrl(url, data)}"`);
7
+ for (const header in data.header) {
8
+ const value = `${header}: ${data.header[header]}`;
9
+ s.push(`-H "${value}"`);
10
+ }
11
+ for (const cookie in data.cookie) {
12
+ const value = JSON.stringify(`${cookie}=${data.cookie[cookie]}`);
13
+ s.push(`--cookie ${value}`);
14
14
  }
15
- if (endpoint.body?.mediaType === 'multipart/form-data') {
16
- const sample = endpoint.body.samples[sampleKey]?.value;
17
- if (sample && typeof sample === 'object') {
18
- for (const [key, value] of Object.entries(sample)) {
15
+ if (data.bodyMediaType === 'multipart/form-data') {
16
+ if (data.body && typeof data.body === 'object') {
17
+ for (const [key, value] of Object.entries(data.body)) {
19
18
  s.push(`-F ${key}=${inputToString(value)}`);
20
19
  }
21
20
  }
22
21
  }
23
- else if (endpoint.body) {
24
- s.push(`-H "Content-Type: ${endpoint.body.mediaType}"`);
25
- s.push(`-d ${inputToString(endpoint.body.samples[sampleKey]?.value ?? '', endpoint.body.mediaType, 'single-quote')}`);
22
+ else if (data.body) {
23
+ s.push(`-H "Content-Type: ${data.bodyMediaType}"`);
24
+ s.push(`-d ${inputToString(data.body, data.bodyMediaType, 'single-quote')}`);
26
25
  }
27
26
  return s
28
27
  .flatMap((v, i) => v
@@ -1,3 +1,3 @@
1
- import { type EndpointSample } from '../utils/generate-sample.js';
2
- export declare function getSampleRequest(endpoint: EndpointSample, sampleKey: string): string;
1
+ import { type RequestData } from '../requests/_shared.js';
2
+ export declare function getSampleRequest(url: string, data: RequestData): string;
3
3
  //# sourceMappingURL=go.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"go.d.ts","sourceRoot":"","sources":["../../src/requests/go.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAG9D,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,cAAc,EACxB,SAAS,EAAE,MAAM,GAChB,MAAM,CA4ER"}
1
+ {"version":3,"file":"go.d.ts","sourceRoot":"","sources":["../../src/requests/go.ts"],"names":[],"mappings":"AAEA,OAAO,EAAU,KAAK,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAE9D,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,GAAG,MAAM,CAwEvE"}
@@ -1,32 +1,33 @@
1
+ 'use client';
1
2
  import { inputToString } from '../utils/input-to-string.js';
2
- export function getSampleRequest(endpoint, sampleKey) {
3
+ import { getUrl } from '../requests/_shared.js';
4
+ export function getSampleRequest(url, data) {
3
5
  const imports = ['fmt', 'net/http', 'io/ioutil'];
4
6
  const headers = new Map();
5
7
  const variables = new Map();
8
+ variables.set('url', JSON.stringify(getUrl(url, data)));
6
9
  // additional lines before initializing request
7
10
  const additional = [];
8
- for (const p of endpoint.parameters) {
9
- if (p.in === 'header')
10
- headers.set(p.name, JSON.stringify(p.sample));
11
+ for (const header in data.header) {
12
+ headers.set(header, JSON.stringify(data.header[header]));
11
13
  }
12
- const cookies = endpoint.parameters.filter((p) => p.in === 'cookie');
13
- variables.set('url', JSON.stringify(endpoint.url));
14
+ const cookies = Object.keys(data.cookie);
14
15
  if (cookies.length > 0)
15
- headers.set('Cookie', JSON.stringify(cookies.map((p) => `${p.name}=${p.sample}`).join('; ')));
16
- if (endpoint.body) {
17
- headers.set('Content-Type', `"${endpoint.body.mediaType}"`);
18
- if (endpoint.body.mediaType === 'multipart/form-data' &&
19
- typeof endpoint.body.samples[sampleKey]?.value === 'object') {
16
+ headers.set('Cookie', JSON.stringify(cookies.map((p) => `${p}=${data.cookie[p]}`).join('; ')));
17
+ if (data.body) {
18
+ headers.set('Content-Type', `"${data.bodyMediaType}"`);
19
+ if (data.bodyMediaType === 'multipart/form-data' &&
20
+ typeof data.body === 'object') {
20
21
  imports.push('mime/multipart', 'bytes');
21
22
  variables.set('payload', `new(bytes.Buffer)`);
22
23
  variables.set('mp', 'multipart.NewWriter(payload)');
23
- for (const [key, value] of Object.entries(endpoint.body.samples[sampleKey]?.value ?? {})) {
24
+ for (const [key, value] of Object.entries(data.body)) {
24
25
  additional.push(`mp.WriteField("${key}", ${inputToString(value, undefined, 'backtick')})`);
25
26
  }
26
27
  }
27
28
  else {
28
29
  imports.push('strings');
29
- variables.set('payload', `strings.NewReader(${inputToString(endpoint.body.samples[sampleKey]?.value ?? '', endpoint.body.mediaType, 'backtick').replaceAll('\n', '\n ')})`);
30
+ variables.set('payload', `strings.NewReader(${inputToString(data.body, data.bodyMediaType, 'backtick').replaceAll('\n', '\n ')})`);
30
31
  }
31
32
  }
32
33
  return `package main
@@ -40,7 +41,7 @@ ${Array.from(variables.entries())
40
41
  .map(([k, v]) => ` ${k} := ${v}`)
41
42
  .join('\n')}
42
43
  ${additional.join('\n ')}
43
- req, _ := http.NewRequest("${endpoint.method}", url, ${variables.has('payload') ? 'payload' : 'nil'})
44
+ req, _ := http.NewRequest("${data.method}", url, ${variables.has('payload') ? 'payload' : 'nil'})
44
45
  ${Array.from(headers.entries())
45
46
  .map(([key, value]) => `req.Header.Add("${key}", ${value})`)
46
47
  .join('\n ')}
@@ -0,0 +1,3 @@
1
+ import type { CodeSample } from '../render/operation/index.js';
2
+ export declare const defaultSamples: CodeSample[];
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/requests/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAMrD,eAAO,MAAM,cAAc,EAAE,UAAU,EAqBtC,CAAC"}
@@ -0,0 +1,26 @@
1
+ import * as CURL from '../requests/curl.js';
2
+ import * as JS from '../requests/javascript.js';
3
+ import * as Go from '../requests/go.js';
4
+ import * as Python from '../requests/python.js';
5
+ export const defaultSamples = [
6
+ {
7
+ label: 'cURL',
8
+ source: CURL.getSampleRequest,
9
+ lang: 'bash',
10
+ },
11
+ {
12
+ label: 'JavaScript',
13
+ source: JS.getSampleRequest,
14
+ lang: 'js',
15
+ },
16
+ {
17
+ label: 'Go',
18
+ source: Go.getSampleRequest,
19
+ lang: 'go',
20
+ },
21
+ {
22
+ label: 'Python',
23
+ source: Python.getSampleRequest,
24
+ lang: 'python',
25
+ },
26
+ ];
@@ -1,3 +1,3 @@
1
- import { type EndpointSample } from '../utils/generate-sample.js';
2
- export declare function getSampleRequest(endpoint: EndpointSample, sampleKey: string): string;
1
+ import { type RequestData } from '../requests/_shared.js';
2
+ export declare function getSampleRequest(url: string, data: RequestData): string;
3
3
  //# sourceMappingURL=javascript.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"javascript.d.ts","sourceRoot":"","sources":["../../src/requests/javascript.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAG9D,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,cAAc,EACxB,SAAS,EAAE,MAAM,GAChB,MAAM,CA8ER"}
1
+ {"version":3,"file":"javascript.d.ts","sourceRoot":"","sources":["../../src/requests/javascript.ts"],"names":[],"mappings":"AAEA,OAAO,EAAU,KAAK,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAE9D,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,GAAG,MAAM,CAmDvE"}
@@ -1,49 +1,42 @@
1
+ 'use client';
1
2
  import { inputToString } from '../utils/input-to-string.js';
2
- export function getSampleRequest(endpoint, sampleKey) {
3
+ import { getUrl } from '../requests/_shared.js';
4
+ export function getSampleRequest(url, data) {
3
5
  const s = [];
4
6
  const options = new Map();
5
- const headers = new Map();
6
- const cookies = new Map();
7
- for (const param of endpoint.parameters) {
8
- if (param.in === 'header') {
9
- headers.set(param.name, param.sample);
10
- }
11
- if (param.in === 'cookie') {
12
- cookies.set(param.name, param.sample);
13
- }
14
- }
15
- if (cookies.size > 0) {
16
- headers.set('cookie', Array.from(cookies.entries())
7
+ const headers = { ...data.header };
8
+ if (Object.keys(data.cookie).length > 0) {
9
+ headers['cookie'] = Object.entries(data.cookie)
17
10
  .map(([key, value]) => `${key}=${value}`)
18
- .join('; '));
11
+ .join('; ');
19
12
  }
20
- if (headers.size > 0) {
21
- options.set('headers', JSON.stringify(Object.fromEntries(headers.entries()), null, 2).replaceAll('\n', '\n '));
13
+ if (Object.keys(headers).length > 0) {
14
+ options.set('headers', JSON.stringify(headers, null, 2).replaceAll('\n', '\n '));
22
15
  }
23
- if (endpoint.body?.mediaType === 'multipart/form-data' &&
24
- typeof endpoint.body.samples[sampleKey]?.value === 'object' &&
25
- endpoint.body.samples[sampleKey]?.value) {
16
+ if (data.bodyMediaType === 'multipart/form-data' &&
17
+ typeof data.body === 'object' &&
18
+ data.body) {
26
19
  s.push(`const formData = new FormData();`);
27
- for (const [key, value] of Object.entries(endpoint.body.samples[sampleKey]?.value))
20
+ for (const [key, value] of Object.entries(data.body))
28
21
  s.push(`formData.set(${key}, ${inputToString(value)})`);
29
22
  options.set('body', 'formData');
30
23
  }
31
- else if (endpoint.body) {
24
+ else if (data.body) {
32
25
  let code;
33
- if (endpoint.body.mediaType === 'application/json') {
26
+ if (data.bodyMediaType === 'application/json') {
34
27
  code =
35
- typeof endpoint.body.samples[sampleKey]?.value === 'string'
36
- ? inputToString(endpoint.body.samples[sampleKey]?.value, endpoint.body.mediaType, 'backtick')
37
- : `JSON.stringify(${JSON.stringify(endpoint.body.samples[sampleKey]?.value, null, 2)})`;
28
+ typeof data.body === 'string'
29
+ ? inputToString(data.body, data.bodyMediaType, 'backtick')
30
+ : `JSON.stringify(${JSON.stringify(data.body, null, 2)})`;
38
31
  }
39
32
  else {
40
- code = inputToString(endpoint.body.samples[sampleKey]?.value ?? '', endpoint.body.mediaType, 'backtick');
33
+ code = inputToString(data.body, data.bodyMediaType, 'backtick');
41
34
  }
42
35
  options.set('body', code.replaceAll('\n', '\n '));
43
36
  }
44
37
  const optionsStr = Array.from(options.entries())
45
38
  .map(([k, v]) => ` ${k}: ${v}`)
46
39
  .join(',\n');
47
- s.push(`fetch(${JSON.stringify(endpoint.url)}, {\n${optionsStr}\n});`);
40
+ s.push(`fetch(${JSON.stringify(getUrl(url, data))}, {\n${optionsStr}\n});`);
48
41
  return s.join('\n\n');
49
42
  }
@@ -1,3 +1,3 @@
1
- import { type EndpointSample } from '../utils/generate-sample.js';
2
- export declare function getSampleRequest(endpoint: EndpointSample, sampleKey: string): string;
1
+ import { type RequestData } from '../requests/_shared.js';
2
+ export declare function getSampleRequest(url: string, data: RequestData): string;
3
3
  //# sourceMappingURL=python.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"python.d.ts","sourceRoot":"","sources":["../../src/requests/python.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAG9D,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,cAAc,EACxB,SAAS,EAAE,MAAM,GAChB,MAAM,CA4ER"}
1
+ {"version":3,"file":"python.d.ts","sourceRoot":"","sources":["../../src/requests/python.ts"],"names":[],"mappings":"AAEA,OAAO,EAAU,KAAK,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAE9D,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,GAAG,MAAM,CA8CvE"}
@@ -1,41 +1,37 @@
1
+ 'use client';
1
2
  import { inputToString } from '../utils/input-to-string.js';
2
- export function getSampleRequest(endpoint, sampleKey) {
3
- const headers = new Map();
4
- const cookies = new Map();
3
+ import { getUrl } from '../requests/_shared.js';
4
+ export function getSampleRequest(url, data) {
5
5
  const variables = new Map();
6
- for (const param of endpoint.parameters) {
7
- if (param.in === 'header')
8
- headers.set(param.name, param.sample);
9
- if (param.in === 'cookie')
10
- cookies.set(param.name, param.sample);
11
- }
12
- if (endpoint.body) {
13
- switch (endpoint.body.mediaType) {
6
+ const headers = { ...data.header };
7
+ if (data.body) {
8
+ switch (data.bodyMediaType) {
14
9
  case 'application/json':
15
- variables.set('json', JSON.stringify(endpoint.body.samples[sampleKey]?.value ?? {}, null, 2));
10
+ variables.set('json', JSON.stringify(data.body, null, 2));
16
11
  break;
17
12
  case 'multipart/form-data':
18
- headers.set('Content-Type', endpoint.body.mediaType);
19
- variables.set('data', JSON.stringify(endpoint.body.samples[sampleKey]?.value ?? {}, null, 2));
13
+ headers['Content-Type'] = data.bodyMediaType;
14
+ variables.set('data', JSON.stringify(data.body, null, 2));
20
15
  break;
21
16
  default:
22
- headers.set('Content-Type', endpoint.body.mediaType);
23
- variables.set('data', inputToString(endpoint.body.samples[sampleKey]?.value ?? '', endpoint.body.mediaType, 'python'));
17
+ if (data.bodyMediaType)
18
+ headers['Content-Type'] = data.bodyMediaType;
19
+ variables.set('data', inputToString(data.body, data.bodyMediaType, 'python'));
24
20
  }
25
21
  }
26
- if (headers.size > 0) {
27
- variables.set('headers', JSON.stringify(Object.fromEntries(headers.entries()), null, 2));
22
+ if (Object.keys(headers).length > 0) {
23
+ variables.set('headers', JSON.stringify(headers, null, 2));
28
24
  }
29
- if (cookies.size > 0) {
30
- variables.set('cookies', JSON.stringify(Object.fromEntries(cookies.entries()), null, 2));
25
+ if (Object.keys(data.cookie).length > 0) {
26
+ variables.set('cookies', JSON.stringify(data.cookie, null, 2));
31
27
  }
32
28
  return `import requests
33
29
 
34
- url = ${JSON.stringify(endpoint.url)}
30
+ url = ${JSON.stringify(getUrl(url, data))}
35
31
  ${Array.from(variables.entries())
36
32
  .map(([k, v]) => `${k} = ${v}`)
37
33
  .join('\n')}
38
- response = requests.request("${endpoint.method}", url${variables.size > 0
34
+ response = requests.request("${data.method}", url${variables.size > 0
39
35
  ? `, ${Array.from(variables.keys())
40
36
  .map((k) => `${k}=${k}`)
41
37
  .join(', ')}`
@@ -1,6 +1,6 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import Slugger from 'github-slugger';
3
- import { Operation } from '../render/operation.js';
3
+ import { Operation } from '../render/operation/index.js';
4
4
  import { createMethod } from '../server/create-method.js';
5
5
  import { createRenders } from '../render/renderer.js';
6
6
  import { processDocument, } from '../utils/process-document.js';
package/dist/types.d.ts CHANGED
@@ -1,8 +1,7 @@
1
1
  import type { OpenAPIV3_1 as V3_1 } from 'openapi-types';
2
2
  import type { default as Slugger } from 'github-slugger';
3
3
  import { type Renderer } from './render/renderer.js';
4
- import type { EndpointSample } from './utils/generate-sample.js';
5
- import type { CodeSample } from './render/operation.js';
4
+ import type { CodeSample } from './render/operation/index.js';
6
5
  import type { BuiltinTheme, CodeOptionsThemes, CodeToHastOptionsCommon } from 'shiki';
7
6
  import type { NoReference } from './utils/schema.js';
8
7
  import type { ProcessedDocument } from './utils/process-document.js';
@@ -46,14 +45,14 @@ export interface RenderContext {
46
45
  *
47
46
  * Pass `false` to disable it.
48
47
  *
49
- * @param endpoint - the API endpoint
50
- * @param code - status code
48
+ * @param method - the operation object
49
+ * @param statusCode - status code
51
50
  */
52
- generateTypeScriptSchema?: ((endpoint: EndpointSample, code: string) => Awaitable<string>) | false;
51
+ generateTypeScriptSchema?: ((method: NoReference<MethodInformation>, statusCode: string) => Awaitable<string>) | false;
53
52
  /**
54
53
  * Generate code samples for endpoint.
55
54
  */
56
- generateCodeSamples?: (endpoint: EndpointSample) => Awaitable<CodeSample[]>;
55
+ generateCodeSamples?: (method: NoReference<MethodInformation>) => Awaitable<CodeSample[]>;
57
56
  shikiOptions?: Omit<CodeToHastOptionsCommon, 'lang'> & CodeOptionsThemes<BuiltinTheme>;
58
57
  /**
59
58
  * Show full response schema instead of only example response & Typescript definitions
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,IAAI,IAAI,EAAE,MAAM,eAAe,CAAC;AACzD,OAAO,KAAK,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAClD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAC9D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO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B,CAAC,KACnC,SAAS,CAAC,UAAU,EAAE,CAAC,CAAC;IAE7B,YAAY,CAAC,EAAE,IAAI,CAAC,uBAAuB,EAAE,MAAM,CAAC,GAClD,iBAAiB,CAAC,YAAY,CAAC,CAAC;IAElC;;OAEG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAC9B"}
@@ -1,3 +1,2 @@
1
- export declare const Samples: import("react").ComponentType<import("../render/renderer.js").SamplesProps>;
2
- export declare const Sample: import("react").ComponentType<import("../render/renderer.js").SampleProps>;
1
+ export { ClientLazy as APIPlayground } from '../playground/client.lazy.js';
3
2
  //# sourceMappingURL=client.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/ui/client.tsx"],"names":[],"mappings":"AAGA,eAAO,MAAM,OAAO,0EAEnB,CAAC;AAEF,eAAO,MAAM,MAAM,yEAElB,CAAC"}
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/ui/client.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,IAAI,aAAa,EAAE,MAAM,0BAA0B,CAAC"}
package/dist/ui/client.js CHANGED
@@ -1,4 +1,2 @@
1
1
  'use client';
2
- import dynamic from 'next/dynamic';
3
- export const Samples = dynamic(() => import('./sample-select.js').then((mod) => mod.Samples));
4
- export const Sample = dynamic(() => import('./sample-select.js').then((mod) => mod.Sample));
2
+ export { ClientLazy as APIPlayground } from '../playground/client.lazy.js';
@@ -1,7 +1,7 @@
1
1
  import { type VariantProps } from 'class-variance-authority';
2
2
  import type { HTMLAttributes } from 'react';
3
3
  export declare const badgeVariants: (props?: ({
4
- color?: "red" | "green" | "yellow" | "blue" | "orange" | null | undefined;
4
+ color?: "green" | "yellow" | "red" | "blue" | "orange" | null | undefined;
5
5
  } & import("class-variance-authority/types").ClassProp) | undefined) => string;
6
6
  export declare function Badge({ className, color, ...props }: Omit<HTMLAttributes<HTMLSpanElement>, 'color'> & VariantProps<typeof badgeVariants>): import("react/jsx-runtime").JSX.Element;
7
7
  export declare function MethodLabel({ children, ...props }: Omit<HTMLAttributes<HTMLSpanElement>, 'children'> & {
@@ -1,4 +1,4 @@
1
- import { type ReactNode, type RefObject } from 'react';
1
+ import { type ReactNode } from 'react';
2
2
  import type { RenderContext, ServerObject } from '../../types.js';
3
3
  export interface ApiProviderProps {
4
4
  /**
@@ -9,13 +9,11 @@ export interface ApiProviderProps {
9
9
  shikiOptions: RenderContext['shikiOptions'];
10
10
  children?: ReactNode;
11
11
  }
12
- interface SelectedServer {
12
+ export interface SelectedServer {
13
13
  url: string;
14
14
  variables: Record<string, string>;
15
15
  }
16
- interface ApiContextType extends Omit<ApiProviderProps, 'children' | 'defaultBaseUrl'> {
17
- serverRef: RefObject<SelectedServer | null>;
18
- }
16
+ type ApiContextType = Omit<ApiProviderProps, 'children' | 'defaultBaseUrl'>;
19
17
  interface ServerSelectType {
20
18
  server: SelectedServer | null;
21
19
  setServer: (value: string) => void;
@@ -1 +1 @@
1
- {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../../src/ui/contexts/api.tsx"],"names":[],"mappings":"AACA,OAAO,EAEL,KAAK,SAAS,EACd,KAAK,SAAS,EAMf,MAAM,OAAO,CAAC;AACf,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAE3D,MAAM,WAAW,gBAAgB;IAC/B;;OAEG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IAExB,OAAO,EAAE,YAAY,EAAE,CAAC;IACxB,YAAY,EAAE,aAAa,CAAC,cAAc,CAAC,CAAC;IAE5C,QAAQ,CAAC,EAAE,SAAS,CAAC;CACtB;AAED,UAAU,cAAc;IACtB,GAAG,EAAE,MAAM,CAAC;IACZ,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACnC;AAED,UAAU,cACR,SAAQ,IAAI,CAAC,gBAAgB,EAAE,UAAU,GAAG,gBAAgB,CAAC;IAC7D,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MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,KAAK,IAAI,CAAC;CAC7D;AAOD,wBAAgB,aAAa,IAAI,cAAc,CAK9C;AAED,wBAAgB,sBAAsB,IAAI,gBAAgB,CAKzD;AAED,wBAAgB,WAAW,CAAC,EAC1B,cAAc,EACd,QAAQ,EACR,GAAG,KAAK,EACT,EAAE,gBAAgB,2CA+DlB"}
@@ -1,6 +1,6 @@
1
1
  'use client';
2
2
  import { jsx as _jsx } from "react/jsx-runtime";
3
- import { createContext, useContext, useEffect, useMemo, useRef, useState, } from 'react';
3
+ import { createContext, useContext, useEffect, useMemo, useState, } from 'react';
4
4
  const ApiContext = createContext(undefined);
5
5
  const ServerSelectContext = createContext(undefined);
6
6
  export function useApiContext() {
@@ -27,8 +27,6 @@ export function ApiProvider({ defaultBaseUrl, children, ...props }) {
27
27
  }
28
28
  : null;
29
29
  });
30
- const serverRef = useRef(server);
31
- serverRef.current = server;
32
30
  useEffect(() => {
33
31
  const cached = localStorage.getItem('apiBaseUrl');
34
32
  if (!cached)
@@ -43,10 +41,7 @@ export function ApiProvider({ defaultBaseUrl, children, ...props }) {
43
41
  // ignore
44
42
  }
45
43
  }, []);
46
- return (_jsx(ApiContext.Provider, { value: useMemo(() => ({
47
- ...props,
48
- serverRef,
49
- }), [props]), children: _jsx(ServerSelectContext.Provider, { value: useMemo(() => ({
44
+ return (_jsx(ApiContext.Provider, { value: props, children: _jsx(ServerSelectContext.Provider, { value: useMemo(() => ({
50
45
  server,
51
46
  setServerVariables(variables) {
52
47
  setServer((prev) => {
@@ -0,0 +1,25 @@
1
+ import type { CodeSample } from '../../render/operation/index.js';
2
+ import type { SamplesProps } from '../../render/renderer.js';
3
+ import type { RequestData } from '../../requests/_shared.js';
4
+ export declare function CodeExampleProvider({ route, examples, initialKey, children, }: {
5
+ route: string;
6
+ examples: {
7
+ key: string;
8
+ data: RequestData;
9
+ }[];
10
+ initialKey?: string;
11
+ children: React.ReactNode;
12
+ }): import("react/jsx-runtime").JSX.Element;
13
+ export declare function CodeExample(props: CodeSample): import("react/jsx-runtime").JSX.Element | null;
14
+ export declare function CodeExampleSelector({ items }: SamplesProps): import("react/jsx-runtime").JSX.Element;
15
+ export declare function useRequestData(): {
16
+ /**
17
+ * initial request data
18
+ */
19
+ data: RequestData;
20
+ /**
21
+ * Save changes to request data, it won't trigger re-render on the component itself, which makes it safe to call in an effect with `data` as dep
22
+ */
23
+ saveData: (data: RequestData) => void;
24
+ };
25
+ //# sourceMappingURL=code-example.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"code-example.d.ts","sourceRoot":"","sources":["../../../src/ui/contexts/code-example.tsx"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAUtD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAqBtD,wBAAgB,mBAAmB,CAAC,EAClC,KAAK,EACL,QAAQ,EACR,UAAU,EACV,QAAQ,GACT,EAAE;IACD,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE;QACR,GAAG,EAAE,MAAM,CAAC;QACZ,IAAI,EAAE,WAAW,CAAC;KACnB,EAAE,CAAC;IACJ,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC3B,2CAqDA;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,UAAU,kDAsC5C;AAED,wBAAgB,mBAAmB,CAAC,EAAE,KAAK,EAAE,EAAE,YAAY,2CAoB1D;AAgBD,wBAAgB,cAAc;IAaxB;;OAEG;;IAEH;;OAEG;qBAZgC,WAAW;EAiBnD"}
@@ -0,0 +1,104 @@
1
+ 'use client';
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { createContext, useContext, useEffect, useMemo, useRef, useState, } from 'react';
4
+ import { useApiContext, useServerSelectContext } from '../../ui/contexts/api.js';
5
+ import { DynamicCodeBlock } from 'fumadocs-ui/components/dynamic-codeblock';
6
+ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '../../ui/components/select.js';
7
+ import { useEffectEvent } from 'fumadocs-core/utils/use-effect-event';
8
+ import { getUrl } from '../../utils/server-url.js';
9
+ import { defaultSamples } from '../../requests/index.js';
10
+ const CodeExampleContext = createContext(null);
11
+ export function CodeExampleProvider({ route, examples, initialKey, children, }) {
12
+ const [key, setKey] = useState(initialKey ?? examples[0].key);
13
+ const listeners = useRef([]);
14
+ const setData = useEffectEvent((newData) => {
15
+ for (const example of examples) {
16
+ if (example.key === key) {
17
+ // persistent changes
18
+ example.data = newData;
19
+ }
20
+ }
21
+ for (const listener of listeners.current) {
22
+ listener(newData);
23
+ }
24
+ });
25
+ const updateKey = useEffectEvent((newKey) => {
26
+ const data = examples.find((example) => example.key === newKey)?.data;
27
+ if (!data)
28
+ return;
29
+ setKey(newKey);
30
+ for (const listener of listeners.current) {
31
+ listener(data);
32
+ }
33
+ });
34
+ const addListener = useEffectEvent((listener) => {
35
+ listeners.current.push(listener);
36
+ });
37
+ const removeListener = useEffectEvent((listener) => {
38
+ listeners.current = listeners.current.filter((item) => item !== listener);
39
+ });
40
+ return (_jsx(CodeExampleContext, { value: useMemo(() => ({
41
+ key,
42
+ route,
43
+ setKey: updateKey,
44
+ examples,
45
+ setData,
46
+ removeListener,
47
+ addListener,
48
+ }), [addListener, examples, key, removeListener, route, setData, updateKey]), children: children }));
49
+ }
50
+ export function CodeExample(props) {
51
+ const { shikiOptions } = useApiContext();
52
+ const { examples, key, route, addListener, removeListener } = useContext(CodeExampleContext);
53
+ const { server } = useServerSelectContext();
54
+ const [data, setData] = useState(() => {
55
+ return examples.find((example) => example.key === key).data;
56
+ });
57
+ const sample = useMemo(() => {
58
+ if (props.source)
59
+ return props;
60
+ return defaultSamples.find((item) => item.label === props.label);
61
+ }, [props]);
62
+ useEffect(() => {
63
+ const listener = setData;
64
+ addListener(listener);
65
+ return () => {
66
+ removeListener(listener);
67
+ };
68
+ }, [addListener, removeListener]);
69
+ const code = useMemo(() => {
70
+ if (!sample?.source)
71
+ return;
72
+ if (typeof sample.source === 'string')
73
+ return sample.source;
74
+ return sample.source(`${server ? getUrl(server.url, server.variables) : '/'}${route}`, data);
75
+ }, [sample, server, route, data]);
76
+ if (!code || !sample)
77
+ return null;
78
+ return (_jsx(DynamicCodeBlock, { lang: sample.lang, code: code, options: shikiOptions }));
79
+ }
80
+ export function CodeExampleSelector({ items }) {
81
+ const { key, setKey } = useContext(CodeExampleContext);
82
+ const item = items.find((item) => item.value === key);
83
+ return (_jsxs(Select, { value: key, onValueChange: setKey, children: [_jsx(SelectTrigger, { className: "not-prose mb-2", children: _jsx(SelectValue, { asChild: true, children: item ? _jsx(SelectDisplay, { item: item }) : null }) }), _jsx(SelectContent, { children: items.map((item) => (_jsx(SelectItem, { value: item.value, children: _jsx(SelectDisplay, { item: item }) }, item.value))) })] }));
84
+ }
85
+ function SelectDisplay({ item, ...props }) {
86
+ return (_jsxs("div", { ...props, children: [_jsx("span", { className: "font-medium text-sm", children: item.title }), _jsx("span", { className: "text-fd-muted-foreground", children: item.description })] }));
87
+ }
88
+ export function useRequestData() {
89
+ const { examples, key, setData } = useContext(CodeExampleContext);
90
+ const data = useMemo(() => examples.find((example) => example.key === key).data, [examples, key]);
91
+ const saveData = useEffectEvent((data) => {
92
+ setData(data);
93
+ });
94
+ return useMemo(() => ({
95
+ /**
96
+ * initial request data
97
+ */
98
+ data,
99
+ /**
100
+ * Save changes to request data, it won't trigger re-render on the component itself, which makes it safe to call in an effect with `data` as dep
101
+ */
102
+ saveData,
103
+ }), [data, saveData]);
104
+ }
@@ -0,0 +1,12 @@
1
+ export declare const CodeExampleProvider: import("react").ComponentType<{
2
+ route: string;
3
+ examples: {
4
+ key: string;
5
+ data: import("../../requests/_shared.js").RequestData;
6
+ }[];
7
+ initialKey?: string;
8
+ children: React.ReactNode;
9
+ }>;
10
+ export declare const CodeExample: import("react").ComponentType<import("../../render/operation/index.js").CodeSample>;
11
+ export declare const CodeExampleSelector: import("react").ComponentType<import("../../render/renderer.js").SamplesProps>;
12
+ //# sourceMappingURL=code-example.lazy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"code-example.lazy.d.ts","sourceRoot":"","sources":["../../../src/ui/contexts/code-example.lazy.tsx"],"names":[],"mappings":"AAGA,eAAO,MAAM,mBAAmB;;;;;;;cASi4B,MAAO,SAAS;EAPh7B,CAAC;AACF,eAAO,MAAM,WAAW,4EAEvB,CAAC;AACF,eAAO,MAAM,mBAAmB,6EAE/B,CAAC"}