directus-extension-api-docs 1.3.4 → 1.4.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/README.md +130 -125
- package/dist/index.js +1 -1
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -1,125 +1,130 @@
|
|
|
1
|
-
# directus-extension-api-docs
|
|
2
|
-
|
|
3
|
-
Directus Extension to include a Swagger interface and custom endpoints definitions
|
|
4
|
-
|
|
5
|
-

|
|
6
|
-
|
|
7
|
-
All directus endpoints are autogenerated on runtime.
|
|
8
|
-
|
|
9
|
-
**You can enable validations middleware based on your custom definitions. See below**
|
|
10
|
-
|
|
11
|
-
## Prerequisites
|
|
12
|
-
|
|
13
|
-
Working in a Directus nodejs project
|
|
14
|
-
|
|
15
|
-
Ref: https://github.com/directus/directus
|
|
16
|
-
|
|
17
|
-
## Installation
|
|
18
|
-
|
|
19
|
-
npm install directus-extension-api-docs
|
|
20
|
-
|
|
21
|
-
## Configuration (optional)
|
|
22
|
-
|
|
23
|
-
For include you custom endpoints.
|
|
24
|
-
|
|
25
|
-
Create a `oasconfig.
|
|
26
|
-
|
|
27
|
-
Options:
|
|
28
|
-
|
|
29
|
-
- `docsPath` _optional_ path where the interface will be (default 'api-docs')
|
|
30
|
-
- `tags` _optional_ openapi custom tags
|
|
31
|
-
- `paths` _optional_ openapi custom paths
|
|
32
|
-
- `components` _optional_ openapi custom components (
|
|
33
|
-
|
|
34
|
-
Example below:
|
|
35
|
-
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
1
|
+
# directus-extension-api-docs
|
|
2
|
+
|
|
3
|
+
Directus Extension to include a Swagger interface and custom endpoints definitions
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
All directus endpoints are autogenerated on runtime.
|
|
8
|
+
|
|
9
|
+
**You can enable validations middleware based on your custom definitions. See below**
|
|
10
|
+
|
|
11
|
+
## Prerequisites
|
|
12
|
+
|
|
13
|
+
Working in a Directus nodejs project
|
|
14
|
+
|
|
15
|
+
Ref: https://github.com/directus/directus
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
npm install directus-extension-api-docs
|
|
20
|
+
|
|
21
|
+
## Configuration (optional)
|
|
22
|
+
|
|
23
|
+
For include you custom endpoints.
|
|
24
|
+
|
|
25
|
+
Create a `oasconfig.yaml` file under `/extensions/endpoints` folder.
|
|
26
|
+
|
|
27
|
+
Options:
|
|
28
|
+
|
|
29
|
+
- `docsPath` _optional_ path where the interface will be (default 'api-docs')
|
|
30
|
+
- `tags` _optional_ openapi custom tags (will be merged with all standard and all customs tags)
|
|
31
|
+
- `paths` _optional_ openapi custom paths (will be merged with all standard and all customs paths)
|
|
32
|
+
- `components` _optional_ openapi custom components (will be merged with all standard and all customs tags)
|
|
33
|
+
|
|
34
|
+
Example below:
|
|
35
|
+
|
|
36
|
+
```
|
|
37
|
+
docsPath: 'api-docs'
|
|
38
|
+
tags:
|
|
39
|
+
- name: MyCustomTag
|
|
40
|
+
description: MyCustomTag description
|
|
41
|
+
components:
|
|
42
|
+
schemas:
|
|
43
|
+
UserId:
|
|
44
|
+
type: object
|
|
45
|
+
required:
|
|
46
|
+
- user_id
|
|
47
|
+
x-collection: directus_users
|
|
48
|
+
properties:
|
|
49
|
+
user_id:
|
|
50
|
+
description: Unique identifier for the user.
|
|
51
|
+
example: 63716273-0f29-4648-8a2a-2af2948f6f78
|
|
52
|
+
type: string
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Definitions (optional)
|
|
57
|
+
|
|
58
|
+
For each custom endpoints group, you can define openapi including a file `oas.yaml` on root of your group folder.
|
|
59
|
+
|
|
60
|
+
Properties:
|
|
61
|
+
|
|
62
|
+
- `tags` _optional_ openapi custom tags
|
|
63
|
+
- `paths` _optional_ openapi custom paths
|
|
64
|
+
- `components` _optional_ openapi custom components
|
|
65
|
+
|
|
66
|
+
Exemple below (`./extensions/endpoints/my-custom-path/oas.yaml`) :
|
|
67
|
+
|
|
68
|
+
```
|
|
69
|
+
tags:
|
|
70
|
+
- name: MyCustomTag2
|
|
71
|
+
description: MyCustomTag description2
|
|
72
|
+
paths:
|
|
73
|
+
"/my-custom-path/my-endpoint":
|
|
74
|
+
post:
|
|
75
|
+
summary: Validate email
|
|
76
|
+
description: Validate email
|
|
77
|
+
tags:
|
|
78
|
+
- MyCustomTag2
|
|
79
|
+
- MyCustomTag
|
|
80
|
+
requestBody:
|
|
81
|
+
content:
|
|
82
|
+
application/json:
|
|
83
|
+
schema:
|
|
84
|
+
"$ref": "#/components/schemas/Users"
|
|
85
|
+
responses:
|
|
86
|
+
'200':
|
|
87
|
+
description: Successful request
|
|
88
|
+
content:
|
|
89
|
+
application/json:
|
|
90
|
+
schema:
|
|
91
|
+
"$ref": "#/components/schemas/User" // you can ref to standard components
|
|
92
|
+
'401':
|
|
93
|
+
description: Unauthorized
|
|
94
|
+
content: {}
|
|
95
|
+
'422':
|
|
96
|
+
description: Unprocessable Entity
|
|
97
|
+
content: {}
|
|
98
|
+
'500':
|
|
99
|
+
description: Server Error
|
|
100
|
+
content: {}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Validations (optional)
|
|
104
|
+
|
|
105
|
+
You can enable a request validations middleware based on your custom definitions.
|
|
106
|
+
|
|
107
|
+
Call `validate` function inside your custom endpoint registration.
|
|
108
|
+
|
|
109
|
+
Pass your `router`, `services`, `schema` and a list (_optional_) of endpoints you want to validate.
|
|
110
|
+
|
|
111
|
+
Example below:
|
|
112
|
+
|
|
113
|
+
```
|
|
114
|
+
const { validate } = require('directus-extension-api-docs');
|
|
115
|
+
|
|
116
|
+
const id = 'my-custom-path';
|
|
117
|
+
|
|
118
|
+
module.exports = {
|
|
119
|
+
id,
|
|
120
|
+
handler: async function registerEndpoint(router, { services, getSchema }) {
|
|
121
|
+
|
|
122
|
+
const schema = await getSchema();
|
|
123
|
+
await validate(router, services, schema); // Enable validator for custom endpoints
|
|
124
|
+
|
|
125
|
+
router.post('/my-endpoint', async (req, res, next) => {
|
|
126
|
+
...
|
|
127
|
+
});
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
```
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const e=require("
|
|
1
|
+
"use strict";const e=require("js-yaml"),s=require("path"),n=require("fs"),t=process.cwd();let o;async function i(e,s){if(o)return JSON.parse(o);const{SpecificationService:n}=e,t=new n({accountability:{admin:!0},schema:s});return o=JSON.stringify(await t.oas.generate()),JSON.parse(o)}function a(e,s){return Object.entries(s).reduce(((e,[s,n])=>(e[s]=n&&"object"==typeof n?a(e[s]=e[s]||(Array.isArray(n)?[]:{}),n):n,e)),e)}const r=require("swagger-ui-express"),c=require("express-openapi-validator"),{findWorkspaceDir:p}=require("@pnpm/find-workspace-dir"),u=function(){try{const o=s.join(t,"./extensions/endpoints/oasconfig.json"),i=e.load(n.readFileSync(o,{encoding:"utf-8"})),a=s.join(t,"./extensions/endpoints"),r=n.readdirSync(a,{withFileTypes:!0});for(const s of r){const t=`${a}/${s.name}/oas.yaml`;if(s.isDirectory()&&n.existsSync(t)){const s=e.load(n.readFileSync(t,{encoding:"utf-8"}));i.tags=[...i.tags,...s.tags],i.paths={...i.paths,...s.paths},i.components={...i.components,...s.components}}}return i}catch(e){return{}}}(),d=(null==u?void 0:u.docsPath)||"api-docs";var f={id:d,validate:async function(e,s,n,t){if(null==u?void 0:u.paths){const o=await i(s,n);if(t)for(const e of t)o.paths[e]=u.paths[e];else o.paths=u.paths;u.components?o.components=u.components:(delete o.components.definitions,delete o.components.schemas),e.use(c.middleware({apiSpec:o})),e.use(((e,s,n,t)=>{n.status(e.status||500).json({message:e.message,errors:e.errors})}))}return e},handler:(e,{services:s,exceptions:n,logger:t,getSchema:o})=>{const{ServiceUnavailableException:c}=n,f={swaggerOptions:{url:`/${d}/oas`}};e.use("/",r.serve),e.get("/",r.setup({},f)),e.get("/oas",(async(e,n,r)=>{try{const e=await o(),r=await i(s,e),c=require(`${await p(".")}/package.json`);r.info.title=c.name,r.info.version=c.version,r.info.description=c.description;try{if(null==u?void 0:u.paths)for(const e in u.paths)r.paths[e]=u.paths[e];if(null==u?void 0:u.tags)for(const e of u.tags)r.tags.push(e);(null==u?void 0:u.components)&&(r.components=a(u.components,r.components))}catch(e){t.info("No custom definitions")}n.json(r)}catch(e){return r(new c(e.message||e[0].message))}}))}};module.exports=f;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "directus-extension-api-docs",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.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",
|
|
@@ -35,13 +35,14 @@
|
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"@pnpm/find-workspace-dir": "^5.0.0",
|
|
37
37
|
"express-openapi-validator": "^4.13.8",
|
|
38
|
+
"js-yaml": "^4.1.0",
|
|
38
39
|
"swagger-ui-express": "^4.6.0"
|
|
39
40
|
},
|
|
40
41
|
"devDependencies": {
|
|
41
42
|
"@babel/preset-env": "^7.20.2",
|
|
42
43
|
"@directus/extensions-sdk": "^9.20.4",
|
|
43
44
|
"@types/express": "^4.17.14",
|
|
44
|
-
"@types/jest": "^29.2.
|
|
45
|
+
"@types/jest": "^29.2.3",
|
|
45
46
|
"@types/node": "^18.11.9",
|
|
46
47
|
"@typescript-eslint/eslint-plugin": "^5.43.0",
|
|
47
48
|
"@typescript-eslint/parser": "^5.43.0",
|
|
@@ -57,6 +58,6 @@
|
|
|
57
58
|
"prettier": "^2.7.1",
|
|
58
59
|
"ts-jest": "^29.0.3",
|
|
59
60
|
"ts-node": "^10.9.1",
|
|
60
|
-
"typescript": "^4.
|
|
61
|
+
"typescript": "^4.9.3"
|
|
61
62
|
}
|
|
62
63
|
}
|