directus-extension-api-docs 1.2.10 → 1.3.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 (3) hide show
  1. package/README.md +63 -58
  2. package/dist/index.js +1 -1
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -22,73 +22,77 @@ Ref: https://github.com/directus/directus
22
22
 
23
23
  For include you custom endpoints.
24
24
 
25
- Create a `oasconfig.json` file under `/extensions/endpoints` folder.
25
+ Create a `oasconfig.js` file under `/extensions/endpoints` folder.
26
+
27
+ Options:
28
+
29
+ - `docsPath` path where the interface will be
30
+ - `tags` openapi custom tags
31
+ - `paths` openapi custom paths
32
+ - `components` openapi custom components (you can ref to directus standard components declaring them empty)
26
33
 
27
34
  Example below:
28
35
 
29
36
  ```
30
- {
31
- "docsPath": "api-docs",
32
- "tags": [
37
+ module.exports = {
38
+ docsPath: 'api-docs', // optional default 'api-docs'
39
+ tags: [
33
40
  {
34
- "name":"MyCustomTag",
35
- "description":"MyCustomTag description"
41
+ name: 'MyCustomTag',
42
+ description: 'MyCustomTag description',
36
43
  },
37
44
  ],
38
- "paths": {
39
- "/my-custom-path/my-endpoint": {
40
- "post": {
41
- "summary": "do something",
42
- "description": "do something",
43
- "requestBody": {
44
- "content": {
45
- "application/json": {
46
- "schema": {
47
- "type": "object",
48
- "required": [
49
- "field"
50
- ],
51
- "properties": {
52
- "field": {
53
- "type": "string"
54
- }
55
- }
56
- }
57
- }
58
- },
59
- "responses": {
60
- "200": {
61
- "description": "Successful request",
62
- "content": {
63
- "application/json": {
64
- "schema": {
65
- "type": "object",
66
- "properties": {
67
- "field": {
68
- "type": "string"
69
- }
70
- }
71
- }
72
- }
73
- }
45
+ paths: {
46
+ '/my-custom-path/my-endpoint': {
47
+ post: {
48
+ summary: 'do something',
49
+ description: 'do something',
50
+ requestBody: {
51
+ content: {
52
+ 'application/json': {
53
+ schema: {
54
+ type: 'object',
55
+ required: ['field'],
56
+ properties: {
57
+ field: {
58
+ type: 'string',
59
+ },
60
+ },
61
+ },
62
+ },
74
63
  },
75
- "401": {
76
- "description": "Unauthorized",
77
- "content": {}
64
+ responses: {
65
+ '200': {
66
+ description: 'Successful request',
67
+ content: {
68
+ 'application/json': {
69
+ schema: {
70
+ type: 'object',
71
+ properties: {
72
+ field: {
73
+ type: 'string',
74
+ },
75
+ },
76
+ },
77
+ },
78
+ },
79
+ },
80
+ '401': {
81
+ description: 'Unauthorized',
82
+ content: {},
83
+ },
84
+ '404': {
85
+ description: 'Not Found',
86
+ content: {},
87
+ },
78
88
  },
79
- "404": {
80
- "description": "Not Found",
81
- "content": {}
82
- }
89
+ tags: ['MyCustomTag', 'Assets'],
83
90
  },
84
- "tags": [
85
- "MyCustomTag",
86
- "Assets"
87
- ]
88
- }
89
- }
90
- }
91
- }
91
+ },
92
+ },
93
+ },
94
+ components: {},
95
+ };
92
96
  ```
93
97
 
94
98
  ## Validations (optional)
@@ -96,7 +100,8 @@ Example below:
96
100
  You can enable a request validations middleware based on your custom definitions.
97
101
 
98
102
  Call `validate` function inside your custom endpoint registration.
99
- Pass your `router` and a list of endpoints you want to validate.
103
+
104
+ Pass your `router`, `services`, `schema` and (_optional_) a list of endpoints you want to validate.
100
105
 
101
106
  Example below:
102
107
 
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";const e=require("swagger-ui-express"),s=require("express-openapi-validator"),{findWorkspaceDir:n}=require("@pnpm/find-workspace-dir"),t=require("fs"),o=require("path"),i=process.cwd();let a;const r=function(){try{const e=o.join(i,"./extensions/endpoints/oasconfig.json");return JSON.parse(t.readFileSync(e,"utf-8"))}catch(e){return{}}}();async function c(e,s){if(a)return JSON.parse(a);const{SpecificationService:n}=e,t=new n({accountability:{admin:!0},schema:s});return a=JSON.stringify(await t.oas.generate()),JSON.parse(a)}function p(e,s){for(const n of Object.keys(s))s[n]instanceof Object&&Object.assign(s[n],p(e[n],s[n]));return Object.assign(e||{},s),e}const u=(null==r?void 0:r.docsPath)||"api-docs";var f={id:u,validate:async function(e,n,t,o){if(null==r?void 0:r.paths){const i=await c(n,t);if(o)for(const e of o)i.paths[e]=r.paths[e];else i.paths=r.paths;r.components?i.components=r.components:(delete i.components.definitions,delete i.components.schemas),e.use(s.middleware({apiSpec:i})),e.use(((e,s,n,t)=>{n.status(e.status||500).json({message:e.message,errors:e.errors})}))}return e},handler:(s,{services:t,exceptions:o,logger:i,getSchema:a})=>{const{ServiceUnavailableException:f}=o,d={swaggerOptions:{url:`/${u}/oas`}};s.use("/",e.serve),s.get("/",e.setup({},d)),s.get("/oas",(async(e,s,o)=>{try{const e=await a(),o=await c(t,e),u=require(`${await n(".")}/package.json`);o.info.title=u.name,o.info.version=u.version,o.info.description=u.description;try{if(null==r?void 0:r.paths)for(const e in r.paths)o.paths[e]=r.paths[e];if(null==r?void 0:r.tags)for(const e of r.tags)o.tags.push(e);(null==r?void 0:r.components)&&(o.components=p(r.components,o.components))}catch(e){i.info("No custom definitions")}s.json(o)}catch(e){return o(new f(e.message||e[0].message))}}))}};module.exports=f;
1
+ "use strict";const e=require("swagger-ui-express"),s=require("express-openapi-validator"),{findWorkspaceDir:n}=require("@pnpm/find-workspace-dir"),t=require("path"),o=process.cwd();let i;const a=function(){try{return require(t.join(o,"./extensions/endpoints/oasconfig.json"))}catch(e){return{}}}();async function r(e,s){if(i)return JSON.parse(i);const{SpecificationService:n}=e,t=new n({accountability:{admin:!0},schema:s});return i=JSON.stringify(await t.oas.generate()),JSON.parse(i)}function c(e,s){for(const n of Object.keys(s))s[n]instanceof Object&&Object.assign(s[n],c(e[n],s[n]));return Object.assign(e||{},s),e}const p=(null==a?void 0:a.docsPath)||"api-docs";var u={id:p,validate:async function(e,n,t,o){if(null==a?void 0:a.paths){const i=await r(n,t);if(o)for(const e of o)i.paths[e]=a.paths[e];else i.paths=a.paths;a.components?i.components=a.components:(delete i.components.definitions,delete i.components.schemas),e.use(s.middleware({apiSpec:i})),e.use(((e,s,n,t)=>{n.status(e.status||500).json({message:e.message,errors:e.errors})}))}return e},handler:(s,{services:t,exceptions:o,logger:i,getSchema:u})=>{const{ServiceUnavailableException:f}=o,d={swaggerOptions:{url:`/${p}/oas`}};s.use("/",e.serve),s.get("/",e.setup({},d)),s.get("/oas",(async(e,s,o)=>{try{const e=await u(),o=await r(t,e),p=require(`${await n(".")}/package.json`);o.info.title=p.name,o.info.version=p.version,o.info.description=p.description;try{if(null==a?void 0:a.paths)for(const e in a.paths)o.paths[e]=a.paths[e];if(null==a?void 0:a.tags)for(const e of a.tags)o.tags.push(e);(null==a?void 0:a.components)&&(o.components=c(a.components,o.components))}catch(e){i.info("No custom definitions")}s.json(o)}catch(e){return o(new f(e.message||e[0].message))}}))}};module.exports=u;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "directus-extension-api-docs",
3
- "version": "1.2.10",
3
+ "version": "1.3.0",
4
4
  "description": "directus extension for swagger interface and custom endpoints definitions",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",