fumadocs-openapi 5.0.1 → 5.0.2

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.
@@ -28,7 +28,28 @@ function getPreferredType(body) {
28
28
  return typeof value === 'string' ? value : JSON.stringify(value, null, 2);
29
29
  }
30
30
 
31
- function generateSample(path, method, baseUrl) {
31
+ function getSecurities(requirement, document) {
32
+ const results = [];
33
+ const schemas = document.components?.securitySchemes ?? {};
34
+ for (const [key, scopes] of Object.entries(requirement)){
35
+ if (!(key in schemas)) return [];
36
+ const schema = noRef(schemas[key]);
37
+ results.push({
38
+ ...schema,
39
+ scopes
40
+ });
41
+ }
42
+ return results;
43
+ }
44
+ function getSecurityPrefix(security) {
45
+ if (security.type === 'http') return ({
46
+ basic: 'Basic',
47
+ bearer: 'Bearer'
48
+ })[security.scheme];
49
+ if (security.type === 'oauth2') return 'Bearer';
50
+ }
51
+
52
+ function generateSample(path, method, { baseUrl, document }) {
32
53
  const params = [];
33
54
  const responses = {};
34
55
  for (const param of method.parameters){
@@ -51,6 +72,20 @@ function generateSample(path, method, baseUrl) {
51
72
  });
52
73
  }
53
74
  }
75
+ const requirements = method.security ?? document.security;
76
+ if (requirements && requirements.length > 0) {
77
+ for (const security of getSecurities(requirements[0], document)){
78
+ const prefix = getSecurityPrefix(security);
79
+ params.push({
80
+ name: 'Authorization',
81
+ schema: {
82
+ type: 'string'
83
+ },
84
+ sample: prefix ? `${prefix} <token>` : '<token>',
85
+ in: 'header'
86
+ });
87
+ }
88
+ }
54
89
  let bodyOutput;
55
90
  if (method.requestBody) {
56
91
  const body = noRef(method.requestBody).content;
@@ -199,7 +234,7 @@ func main() {
199
234
  ${Array.from(variables.entries()).map(([k, v])=>` ${k} := ${v}`).join('\n')}
200
235
  ${additional.join('\n ')}
201
236
  req, _ := http.NewRequest("${endpoint.method}", url, ${variables.has('payload') ? 'payload' : 'nil'})
202
- ${Array.from(headers.entries()).map(([key, value])=>`req.Header.Add("${key}", ${value})`).join('\n')}
237
+ ${Array.from(headers.entries()).map(([key, value])=>`req.Header.Add("${key}", ${value})`).join('\n ')}
203
238
  res, _ := http.DefaultClient.Do(req)
204
239
  defer res.Body.Close()
205
240
  body, _ := ioutil.ReadAll(res.Body)
@@ -220,20 +255,6 @@ async function getTypescriptSchema(endpoint, code) {
220
255
  }
221
256
  }
222
257
 
223
- function getScheme(requirement, document) {
224
- const results = [];
225
- const schemas = document.components?.securitySchemes ?? {};
226
- for (const [key, scopes] of Object.entries(requirement)){
227
- if (!(key in schemas)) return [];
228
- const schema = noRef(schemas[key]);
229
- results.push({
230
- ...schema,
231
- scopes
232
- });
233
- }
234
- return results;
235
- }
236
-
237
258
  function Playground({ path, method, ctx }) {
238
259
  let currentId = 0;
239
260
  const bodyContent = noRef(method.requestBody)?.content;
@@ -266,7 +287,7 @@ function getAuthorizationField(method, ctx) {
266
287
  if (security.length === 0) return;
267
288
  const singular = security.find((requirements)=>Object.keys(requirements).length === 1);
268
289
  if (!singular) return;
269
- const scheme = getScheme(singular, ctx.document)[0];
290
+ const scheme = getSecurities(singular, ctx.document)[0];
270
291
  return {
271
292
  type: 'string',
272
293
  name: 'Authorization',
@@ -676,7 +697,7 @@ function Operation({ path, method, ctx, hasHead }) {
676
697
  }, "body"));
677
698
  }
678
699
  const parameterGroups = new Map();
679
- const endpoint = generateSample(path, method, ctx.baseUrl);
700
+ const endpoint = generateSample(path, method, ctx);
680
701
  for (const param of method.parameters){
681
702
  const pInfo = endpoint.parameters.find((item)=>item.name === param.name && item.in === param.in);
682
703
  if (!pInfo) continue;
@@ -778,15 +799,12 @@ function AuthSection({ ctx: { document, renderer }, requirements }) {
778
799
  let id = 0;
779
800
  const info = [];
780
801
  for (const requirement of requirements){
781
- if (info.length > 0) info.push(`---`);
782
- for (const schema of getScheme(requirement, document)){
802
+ for (const schema of getSecurities(requirement, document)){
803
+ const prefix = getSecurityPrefix(schema);
783
804
  if (schema.type === 'http') {
784
805
  info.push(/*#__PURE__*/ jsxs(renderer.Property, {
785
806
  name: "Authorization",
786
- type: {
787
- basic: 'Basic <token>',
788
- bearer: 'Bearer <token>'
789
- }[schema.scheme] ?? '<token>',
807
+ type: prefix ? `${prefix} <token>` : '<token>',
790
808
  required: true,
791
809
  children: [
792
810
  schema.description ? /*#__PURE__*/ jsx(Markdown, {
@@ -806,7 +824,7 @@ function AuthSection({ ctx: { document, renderer }, requirements }) {
806
824
  if (schema.type === 'oauth2') {
807
825
  info.push(/*#__PURE__*/ jsxs(renderer.Property, {
808
826
  name: "Authorization",
809
- type: "Bearer <token>",
827
+ type: prefix ? `${prefix} <token>` : '<token>',
810
828
  required: true,
811
829
  children: [
812
830
  schema.description ? /*#__PURE__*/ jsx(Markdown, {
@@ -820,15 +838,14 @@ function AuthSection({ ctx: { document, renderer }, requirements }) {
820
838
  })
821
839
  ]
822
840
  }),
823
- /*#__PURE__*/ jsxs("p", {
841
+ schema.scopes.length > 0 ? /*#__PURE__*/ jsxs("p", {
824
842
  children: [
825
- "Scope:",
826
- ' ',
843
+ "Scope: ",
827
844
  /*#__PURE__*/ jsx("code", {
828
- children: schema.scopes.length > 0 ? schema.scopes.join(', ') : 'none'
845
+ children: schema.scopes.join(', ')
829
846
  })
830
847
  ]
831
- })
848
+ }) : null
832
849
  ]
833
850
  }, id++));
834
851
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fumadocs-openapi",
3
- "version": "5.0.1",
3
+ "version": "5.0.2",
4
4
  "description": "Generate MDX docs for your OpenAPI spec",
5
5
  "keywords": [
6
6
  "NextJs",