directus-extension-api-docs 1.1.6 → 1.1.8
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/README.md +30 -0
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,6 +6,8 @@ Directus Extension to include a Swagger interface and custom endpoints definitio
|
|
|
6
6
|
|
|
7
7
|
All directus endpoints are autogenerated on runtime.
|
|
8
8
|
|
|
9
|
+
**You can enable validations middleware based on your custom definitions. See below**
|
|
10
|
+
|
|
9
11
|
## Prerequisites
|
|
10
12
|
|
|
11
13
|
Working in a Directus nodejs project
|
|
@@ -21,6 +23,7 @@ Ref: https://github.com/directus/directus
|
|
|
21
23
|
For include you custom endpoints.
|
|
22
24
|
|
|
23
25
|
Create a `oas.json` file under `/extensions/endpoints` folder.
|
|
26
|
+
|
|
24
27
|
Example below:
|
|
25
28
|
|
|
26
29
|
```
|
|
@@ -83,3 +86,30 @@ Example below:
|
|
|
83
86
|
}
|
|
84
87
|
}
|
|
85
88
|
```
|
|
89
|
+
|
|
90
|
+
## Validations (optional)
|
|
91
|
+
|
|
92
|
+
You can enable a request validations middleware based on your custom definitions.
|
|
93
|
+
|
|
94
|
+
Call `validate` function inside your custom endpoint registration.
|
|
95
|
+
Pass your `router` and a list of endpoints you want to validate.
|
|
96
|
+
|
|
97
|
+
Example below:
|
|
98
|
+
|
|
99
|
+
```
|
|
100
|
+
const { validate } = require('directus-extension-api-docs');
|
|
101
|
+
|
|
102
|
+
const id = 'my-custom-path';
|
|
103
|
+
|
|
104
|
+
module.exports = {
|
|
105
|
+
id,
|
|
106
|
+
handler: function registerEndpoint(router, { services, exceptions, logger }) {
|
|
107
|
+
|
|
108
|
+
validate(router, [`/${id}/my-endpoint`]);
|
|
109
|
+
|
|
110
|
+
router.post('/my-endpoint', async (req, res, next) => {
|
|
111
|
+
...
|
|
112
|
+
});
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
```
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const e=require("swagger-ui-express"),s=require("express-openapi-validator"),{findWorkspaceDir:
|
|
1
|
+
"use strict";const e=require("swagger-ui-express"),s=require("express-openapi-validator"),{findWorkspaceDir:o}=require("@pnpm/find-workspace-dir"),n=process.cwd(),t=require("fs"),i=require("path");const a=function(){try{const e=i.join(n,"./extensions/endpoints/oasconfig.json");return JSON.parse(t.readFileSync(e,"utf-8"))}catch(e){return{}}}();const r=(null==a?void 0:a.docsPath)||"api-docs";var c={id:r,validate:function(e,o){if(null==a?void 0:a.paths){const n={openapi:"3.0.1",info:{title:"",description:"",version:""},paths:{},components:{}};for(const e of o)n.paths[e]=a.paths[e];a.components&&(n.components=a.components),e.use(s.middleware({apiSpec:n})),e.use(((e,s,o,n)=>{o.status(e.status||500).json({message:e.message,errors:e.errors})}))}},handler:(s,{services:n,exceptions:t,logger:i})=>{const{ServiceUnavailableException:c}=t,{SpecificationService:p}=n,u={swaggerOptions:{url:`/${r}/oas`}};s.use("/",e.serve),s.get("/",e.setup({},u)),s.get("/oas",(async(e,s,n)=>{try{const n=new p({accountability:{admin:!0},schema:e.schema}),t=await n.oas.generate(),r=require(`${await o(".")}/package.json`);t.info.title=r.name,t.info.version=r.version,t.info.description=r.description;try{if(null==a?void 0:a.paths)for(const e in a.paths)t.paths[e]=a.paths[e];if(null==a?void 0:a.tags)for(const e of a.tags)t.tags.push(e)}catch(e){i.info("No custom definitions")}s.json(t)}catch(e){return n(new c(e.message||e[0].message))}}))}};module.exports=c;
|