@trojs/openapi-server 1.0.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/LICENSE +21 -0
- package/README.md +107 -0
- package/package.json +79 -0
- package/src/api.js +76 -0
- package/src/error-status.js +24 -0
- package/src/express-callback.js +91 -0
- package/src/handlers/not-found.js +8 -0
- package/src/handlers/request-validation.js +9 -0
- package/src/handlers/response-validation.js +30 -0
- package/src/handlers/unauthorized.js +8 -0
- package/src/openapi.js +15 -0
- package/src/operation-ids.js +14 -0
- package/src/params.js +34 -0
- package/src/router.js +79 -0
- package/src/server.js +111 -0
- package/src/types.js +12 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 TroJS
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# OpenAPI server
|
|
2
|
+
|
|
3
|
+
[![NPM version][npm-image]][npm-url] [](https://sonarcloud.io/summary/new_code?id=hckrnews_openapi-server) [](https://sonarcloud.io/summary/new_code?id=hckrnews_openapi-server)
|
|
4
|
+
[](https://sonarcloud.io/summary/new_code?id=hckrnews_openapi-server) [](https://sonarcloud.io/summary/new_code?id=hckrnews_openapi-server) [](https://sonarcloud.io/summary/new_code?id=hckrnews_openapi-server) [](https://sonarcloud.io/summary/new_code?id=hckrnews_openapi-server)
|
|
5
|
+
[](https://sonarcloud.io/summary/new_code?id=hckrnews_openapi-server) [](https://sonarcloud.io/summary/new_code?id=hckrnews_openapi-server) [](https://sonarcloud.io/summary/new_code?id=hckrnews_openapi-server)
|
|
6
|
+
|
|
7
|
+
Create easy a webserver API first with a OpenAPI spec.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
`npm install @trojs/openapi-server`
|
|
12
|
+
or
|
|
13
|
+
`yarn add @trojs/openapi-server`
|
|
14
|
+
|
|
15
|
+
## Test the package
|
|
16
|
+
|
|
17
|
+
`npm run test`
|
|
18
|
+
or
|
|
19
|
+
`yarn test`
|
|
20
|
+
|
|
21
|
+
## How to use
|
|
22
|
+
|
|
23
|
+
```javascript
|
|
24
|
+
|
|
25
|
+
const controllers = {
|
|
26
|
+
// connect to a openationId in the OpenAPI spec with the same name
|
|
27
|
+
getTest: ({
|
|
28
|
+
context,
|
|
29
|
+
request,
|
|
30
|
+
response,
|
|
31
|
+
parameters,
|
|
32
|
+
specification,
|
|
33
|
+
url
|
|
34
|
+
}) => ({ //response an object
|
|
35
|
+
test: 'ok'
|
|
36
|
+
})
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const { openAPISpecification, Api } = await openAPI({ file: './openapi-spec.json', base })
|
|
40
|
+
const api = new Api({
|
|
41
|
+
version: 'v1',
|
|
42
|
+
specification: openAPISpecification,
|
|
43
|
+
controllers,
|
|
44
|
+
secret: 'test',
|
|
45
|
+
logger: console
|
|
46
|
+
})
|
|
47
|
+
const { app } = await setupServer({
|
|
48
|
+
env: process.env,
|
|
49
|
+
apis: [api]
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
If you create a controller, you can easy connect it to the operationId in the OpenAPI spec.
|
|
55
|
+
Check also the examples in the test files.
|
|
56
|
+
In your controller you can use e.g. context, request and response, from express.
|
|
57
|
+
It isn neccesary to define it in your controller, if you don't use it, you can remove it.
|
|
58
|
+
e.g.
|
|
59
|
+
```javascript
|
|
60
|
+
getTest: ({ parameters }) =>
|
|
61
|
+
{
|
|
62
|
+
return {
|
|
63
|
+
test: 'ok'
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
parameters are query param's from the url of a get request, parsed by the type defined in the OpenAPI spec.
|
|
69
|
+
|
|
70
|
+
Specifications is the OpenAPI spec.
|
|
71
|
+
|
|
72
|
+
Url is the current url.
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
## Add custom security handlers like JWT
|
|
76
|
+
```javascript
|
|
77
|
+
import jwt from 'jsonwebtoken'
|
|
78
|
+
|
|
79
|
+
function jwtHandler(context, request, response) {
|
|
80
|
+
const authHeader = context.request.headers.authorization;
|
|
81
|
+
if (!authHeader) {
|
|
82
|
+
throw new Error('Missing authorization header');
|
|
83
|
+
}
|
|
84
|
+
const token = authHeader.replace('Bearer ', '');
|
|
85
|
+
return jwt.verify(token, 'secret');
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const securityHandlers = [
|
|
89
|
+
{
|
|
90
|
+
name: 'jwt',
|
|
91
|
+
handler: jwtHandler
|
|
92
|
+
}
|
|
93
|
+
]
|
|
94
|
+
|
|
95
|
+
const api = new Api({
|
|
96
|
+
version: 'v1',
|
|
97
|
+
specification: openAPISpecification,
|
|
98
|
+
controllers,
|
|
99
|
+
securityHandlers
|
|
100
|
+
})
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
See also: https://openapistack.co/docs/openapi-backend/security-handlers/#security-handlers
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
[npm-url]: https://www.npmjs.com/package/@trojs/openapi-server
|
|
107
|
+
[npm-image]: https://img.shields.io/npm/v/@trojs/openapi-server.svg
|
package/package.json
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@trojs/openapi-server",
|
|
3
|
+
"description": "OpenAPI Server",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "Pieter Wigboldus",
|
|
7
|
+
"url": "https://trojs.org/"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"lint": "eslint src/*.js --config .eslintrc",
|
|
12
|
+
"lint:report": "eslint src/*.js --config .eslintrc -f json -o report.json",
|
|
13
|
+
"lint:fix": "eslint src/*.js --config .eslintrc --fix",
|
|
14
|
+
"test": "c8 node --test src/*.test.js",
|
|
15
|
+
"cpd": "node_modules/jscpd/bin/jscpd src",
|
|
16
|
+
"vulnerabilities": "npm audit --omit=dev"
|
|
17
|
+
},
|
|
18
|
+
"type": "module",
|
|
19
|
+
"files": [
|
|
20
|
+
"src/api.js",
|
|
21
|
+
"src/openapi.js",
|
|
22
|
+
"src/router.js",
|
|
23
|
+
"src/server.js",
|
|
24
|
+
"src/error-status.js",
|
|
25
|
+
"src/types.js",
|
|
26
|
+
"src/params.js",
|
|
27
|
+
"src/express-callback.js",
|
|
28
|
+
"src/operation-ids.js",
|
|
29
|
+
"src/handlers/not-found.js",
|
|
30
|
+
"src/handlers/request-validation.js",
|
|
31
|
+
"src/handlers/response-validation.js",
|
|
32
|
+
"src/handlers/unauthorized.js"
|
|
33
|
+
],
|
|
34
|
+
"main": "src/server.js",
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@hckrnews/eslint-config": "^3.0.0",
|
|
37
|
+
"@types/express-serve-static-core": "^4.17.41",
|
|
38
|
+
"c8": "^9.0.0",
|
|
39
|
+
"eslint": "^8.23.0",
|
|
40
|
+
"eslint-config-standard": "^17.1.0",
|
|
41
|
+
"eslint-plugin-import": "^2.26.0",
|
|
42
|
+
"eslint-plugin-jsdoc": "^48.0.0",
|
|
43
|
+
"eslint-plugin-n": "^16.0.0",
|
|
44
|
+
"eslint-plugin-promise": "^6.0.1",
|
|
45
|
+
"eslint-plugin-sonarjs": "^0.25.1",
|
|
46
|
+
"jscpd": "^3.2.1",
|
|
47
|
+
"supertest": "^6.3.3"
|
|
48
|
+
},
|
|
49
|
+
"repository": {
|
|
50
|
+
"type": "git",
|
|
51
|
+
"url": "https://github.com/hckrnews/openapi-server"
|
|
52
|
+
},
|
|
53
|
+
"engines": {
|
|
54
|
+
"node": ">= 18.13"
|
|
55
|
+
},
|
|
56
|
+
"keywords": [
|
|
57
|
+
"openapi",
|
|
58
|
+
"server",
|
|
59
|
+
"express"
|
|
60
|
+
],
|
|
61
|
+
"dependencies": {
|
|
62
|
+
"@sentry/node": "^7.86.0",
|
|
63
|
+
"ajv-formats": "^3.0.0",
|
|
64
|
+
"body-parser": "^1.20.2",
|
|
65
|
+
"compression": "^1.7.4",
|
|
66
|
+
"cors": "^2.8.5",
|
|
67
|
+
"express": "^4.19.2",
|
|
68
|
+
"helmet": "^7.0.0",
|
|
69
|
+
"openapi-backend": "^5.9.2",
|
|
70
|
+
"swagger-ui-express": "^5.0.0"
|
|
71
|
+
},
|
|
72
|
+
"funding": {
|
|
73
|
+
"type": "github",
|
|
74
|
+
"url": "https://github.com/sponsors/w3nl"
|
|
75
|
+
},
|
|
76
|
+
"overrides": {
|
|
77
|
+
"semver": "^7.5.3"
|
|
78
|
+
}
|
|
79
|
+
}
|
package/src/api.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import express from 'express'
|
|
2
|
+
import swaggerUi from 'swagger-ui-express'
|
|
3
|
+
import { setupRouter } from './router.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {import('openapi-backend').Handler} Handler
|
|
7
|
+
* @typedef {object} Logger
|
|
8
|
+
* @property {Function} error
|
|
9
|
+
* @property {Function} warn
|
|
10
|
+
* @property {Function} info
|
|
11
|
+
* @property {Function} debug
|
|
12
|
+
* @typedef {object} SecurityHandler
|
|
13
|
+
* @property {string} name
|
|
14
|
+
* @property {Handler} handler
|
|
15
|
+
* @typedef {object} ApiSchema
|
|
16
|
+
* @property {string} version
|
|
17
|
+
* @property {object} specification
|
|
18
|
+
* @property {object} controllers
|
|
19
|
+
* @property {string=} secret
|
|
20
|
+
* @property {string=} apiRoot
|
|
21
|
+
* @property {boolean=} strictSpecification
|
|
22
|
+
* @property {boolean=} errorDetails
|
|
23
|
+
* @property {Logger=} logger
|
|
24
|
+
* @property {object=} meta
|
|
25
|
+
* @property {SecurityHandler[]=} securityHandlers
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Setup the server for a specific API, so every server can run multiple instances of the API, like different versions, for e.g. different clients
|
|
30
|
+
* @class
|
|
31
|
+
*/
|
|
32
|
+
export class Api {
|
|
33
|
+
/**
|
|
34
|
+
* @param {ApiSchema} params
|
|
35
|
+
*/
|
|
36
|
+
constructor ({ version, specification, controllers, secret, apiRoot, strictSpecification, errorDetails, logger, meta, securityHandlers }) {
|
|
37
|
+
this.version = version
|
|
38
|
+
this.specification = specification
|
|
39
|
+
this.controllers = controllers
|
|
40
|
+
this.secret = secret
|
|
41
|
+
this.apiRoot = apiRoot
|
|
42
|
+
this.strictSpecification = strictSpecification
|
|
43
|
+
this.errorDetails = errorDetails || false
|
|
44
|
+
this.logger = logger || console
|
|
45
|
+
this.meta = meta || {}
|
|
46
|
+
this.securityHandlers = securityHandlers || []
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
setup () {
|
|
50
|
+
const router = express.Router()
|
|
51
|
+
|
|
52
|
+
router.use('/swagger', swaggerUi.serveFiles(this.specification, {}), swaggerUi.setup(this.specification))
|
|
53
|
+
router.get('/api-docs', (_request, response) =>
|
|
54
|
+
response.json(this.specification)
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
const { api } = setupRouter({
|
|
58
|
+
secret: this.secret,
|
|
59
|
+
openAPISpecification: this.specification,
|
|
60
|
+
controllers: this.controllers,
|
|
61
|
+
apiRoot: this.apiRoot,
|
|
62
|
+
strictSpecification: this.strictSpecification,
|
|
63
|
+
errorDetails: this.errorDetails,
|
|
64
|
+
logger: this.logger,
|
|
65
|
+
meta: this.meta,
|
|
66
|
+
securityHandlers: this.securityHandlers
|
|
67
|
+
})
|
|
68
|
+
api.init()
|
|
69
|
+
|
|
70
|
+
router.use((request, response) =>
|
|
71
|
+
api.handleRequest(request, request, response)
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
return router
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const errorCodesStatus = [
|
|
2
|
+
{
|
|
3
|
+
type: TypeError,
|
|
4
|
+
status: 422
|
|
5
|
+
},
|
|
6
|
+
{
|
|
7
|
+
type: RangeError,
|
|
8
|
+
status: 404
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
type: Error,
|
|
12
|
+
status: 500
|
|
13
|
+
}
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Get a http status when you send an error.
|
|
18
|
+
* When it is a error, throw back the error.
|
|
19
|
+
* @param {Error} error
|
|
20
|
+
* @returns {number}
|
|
21
|
+
*/
|
|
22
|
+
export default (error) =>
|
|
23
|
+
errorCodesStatus.find((errorCode) => error instanceof errorCode.type)
|
|
24
|
+
.status
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import getStatusByError from './error-status.js'
|
|
2
|
+
import { parseParams } from './params.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @typedef {import('express-serve-static-core').Request} Request
|
|
6
|
+
* @typedef {import('express-serve-static-core').Response} Response
|
|
7
|
+
* @typedef {import('openapi-backend').Context} Context
|
|
8
|
+
* @typedef {import('./api.js').Logger} Logger
|
|
9
|
+
* @param {object} params
|
|
10
|
+
* @param {Function} params.controller
|
|
11
|
+
* @param {object} params.specification
|
|
12
|
+
* @param {boolean=} params.errorDetails
|
|
13
|
+
* @param {Logger=} params.logger
|
|
14
|
+
* @param {object=} params.meta
|
|
15
|
+
* @returns {(context: object, request: object, response: object) => Promise<any>}
|
|
16
|
+
*/
|
|
17
|
+
export const makeExpressCallback = ({
|
|
18
|
+
controller,
|
|
19
|
+
specification,
|
|
20
|
+
errorDetails,
|
|
21
|
+
logger,
|
|
22
|
+
meta
|
|
23
|
+
}) =>
|
|
24
|
+
/**
|
|
25
|
+
* Handle controller
|
|
26
|
+
* @async
|
|
27
|
+
* @param {Context} context
|
|
28
|
+
* @param {Request} request
|
|
29
|
+
* @param {Response} response
|
|
30
|
+
* @returns {Promise<any>}
|
|
31
|
+
*/
|
|
32
|
+
async (context, request, response) => {
|
|
33
|
+
try {
|
|
34
|
+
const allParameters = {
|
|
35
|
+
...(context.request?.params || {}),
|
|
36
|
+
...(context.request?.query || {})
|
|
37
|
+
}
|
|
38
|
+
const parameters = parseParams({
|
|
39
|
+
query: allParameters,
|
|
40
|
+
spec: context.operation.parameters
|
|
41
|
+
})
|
|
42
|
+
const url = `${request.protocol}://${request.get('Host')}${request.originalUrl}`
|
|
43
|
+
|
|
44
|
+
const responseBody = await controller({
|
|
45
|
+
context,
|
|
46
|
+
request,
|
|
47
|
+
response,
|
|
48
|
+
parameters,
|
|
49
|
+
specification,
|
|
50
|
+
post: request.body,
|
|
51
|
+
url,
|
|
52
|
+
logger,
|
|
53
|
+
meta
|
|
54
|
+
})
|
|
55
|
+
logger.debug({
|
|
56
|
+
url,
|
|
57
|
+
parameters,
|
|
58
|
+
post: request.body,
|
|
59
|
+
response: responseBody
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
return responseBody
|
|
63
|
+
} catch (error) {
|
|
64
|
+
const errorCodeStatus = getStatusByError(error)
|
|
65
|
+
|
|
66
|
+
logger.error(error)
|
|
67
|
+
|
|
68
|
+
response.status(errorCodeStatus)
|
|
69
|
+
|
|
70
|
+
if (errorDetails) {
|
|
71
|
+
return {
|
|
72
|
+
errors: [
|
|
73
|
+
{
|
|
74
|
+
message: error.message,
|
|
75
|
+
value: error.valueOf(),
|
|
76
|
+
type: error.constructor.name
|
|
77
|
+
}
|
|
78
|
+
],
|
|
79
|
+
status: errorCodeStatus,
|
|
80
|
+
timestamp: new Date(),
|
|
81
|
+
message: error.message
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
status: errorCodeStatus,
|
|
87
|
+
timestamp: new Date(),
|
|
88
|
+
message: error.message
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export const responseValidation = (context, request, response) => {
|
|
2
|
+
const responseDoesntNeedValidation = response.statusCode >= 400
|
|
3
|
+
if (responseDoesntNeedValidation) {
|
|
4
|
+
return response.json(context.response)
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
const valid = context.api.validateResponse(
|
|
8
|
+
context.response,
|
|
9
|
+
context.operation
|
|
10
|
+
)
|
|
11
|
+
if (valid?.errors) {
|
|
12
|
+
return response.status(502).json({
|
|
13
|
+
errors: valid.errors,
|
|
14
|
+
status: 502,
|
|
15
|
+
timestamp: new Date(),
|
|
16
|
+
message: 'Bad response'
|
|
17
|
+
})
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (!context.response) {
|
|
21
|
+
return response.end()
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const contentType = request?.headers?.accept ?? 'application/json'
|
|
25
|
+
if (contentType === 'application/json') {
|
|
26
|
+
return response.json(context.response)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return response.send(context.response)
|
|
30
|
+
}
|
package/src/openapi.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Get the OpenAPI specification from the file.
|
|
5
|
+
* @async
|
|
6
|
+
* @param {object} params
|
|
7
|
+
* @param {string} params.file
|
|
8
|
+
* @param {string=} params.base
|
|
9
|
+
* @returns {Promise<{ openAPISpecification: object; }>}
|
|
10
|
+
*/
|
|
11
|
+
export const openAPI = async ({ file, base = import.meta.url }) => {
|
|
12
|
+
const fileUrl = new URL(file, base)
|
|
13
|
+
const openAPISpecification = JSON.parse(await readFile(fileUrl, 'utf8'))
|
|
14
|
+
return { openAPISpecification }
|
|
15
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const operations = ['get', 'put', 'patch', 'post', 'delete']
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Get all operation ID's from the specification.
|
|
5
|
+
* @param {object} params
|
|
6
|
+
* @param {object} params.specification
|
|
7
|
+
* @returns {string[]}
|
|
8
|
+
*/
|
|
9
|
+
export const operationIds = ({ specification }) => Object.values(specification.paths)
|
|
10
|
+
.map((path) => Object.entries(path)
|
|
11
|
+
.map(([operation, data]) => operations.includes(operation)
|
|
12
|
+
? data.operationId
|
|
13
|
+
: null))
|
|
14
|
+
.flat()
|
package/src/params.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { types } from './types.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Parse params to the types defined in the spec
|
|
5
|
+
* @param {object} params
|
|
6
|
+
* @param {object} params.query
|
|
7
|
+
* @param {object} params.spec
|
|
8
|
+
* @returns {object}
|
|
9
|
+
*/
|
|
10
|
+
export const parseParams = ({ query, spec }) =>
|
|
11
|
+
spec.map(parameter => {
|
|
12
|
+
const { name, schema } = parameter
|
|
13
|
+
const { type, default: defaultValue, example: exampleValue } = schema
|
|
14
|
+
const Type = types[type]
|
|
15
|
+
const paramName = query?.[name]
|
|
16
|
+
|
|
17
|
+
if (!paramName) {
|
|
18
|
+
return { name, value: defaultValue ?? exampleValue }
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (Type === Boolean) {
|
|
22
|
+
return {
|
|
23
|
+
name,
|
|
24
|
+
value: JSON.parse(paramName.toLowerCase())
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const value = new Type(paramName).valueOf()
|
|
29
|
+
return { name, value }
|
|
30
|
+
})
|
|
31
|
+
.reduce((acc, { name, value }) => {
|
|
32
|
+
acc[name] = value
|
|
33
|
+
return acc
|
|
34
|
+
}, {})
|
package/src/router.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { OpenAPIBackend } from 'openapi-backend'
|
|
2
|
+
import addFormats from 'ajv-formats'
|
|
3
|
+
import { makeExpressCallback } from './express-callback.js'
|
|
4
|
+
import { operationIds } from './operation-ids.js'
|
|
5
|
+
import { notFound } from './handlers/not-found.js'
|
|
6
|
+
import { requestValidation } from './handlers/request-validation.js'
|
|
7
|
+
import { responseValidation } from './handlers/response-validation.js'
|
|
8
|
+
import { unauthorized } from './handlers/unauthorized.js'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @typedef {import('./api.js').Logger} Logger
|
|
12
|
+
* @typedef {import('./api.js').SecurityHandler} SecurityHandler
|
|
13
|
+
* Setup the router
|
|
14
|
+
* @param {object} params
|
|
15
|
+
* @param {string=} params.secret
|
|
16
|
+
* @param {object} params.openAPISpecification
|
|
17
|
+
* @param {object} params.controllers
|
|
18
|
+
* @param {string=} params.apiRoot
|
|
19
|
+
* @param {boolean=} params.strictSpecification
|
|
20
|
+
* @param {boolean=} params.errorDetails
|
|
21
|
+
* @param {Logger=} params.logger
|
|
22
|
+
* @param {object=} params.meta
|
|
23
|
+
* @param {SecurityHandler[]=} params.securityHandlers
|
|
24
|
+
* @returns {{ api, openAPISpecification: object }}
|
|
25
|
+
*/
|
|
26
|
+
export const setupRouter = ({ secret, openAPISpecification, controllers, apiRoot, strictSpecification, errorDetails, logger, meta, securityHandlers = [] }) => {
|
|
27
|
+
const api = new OpenAPIBackend({
|
|
28
|
+
definition: openAPISpecification,
|
|
29
|
+
apiRoot,
|
|
30
|
+
strict: strictSpecification,
|
|
31
|
+
customizeAjv: (originalAjv) => {
|
|
32
|
+
addFormats(originalAjv)
|
|
33
|
+
return originalAjv
|
|
34
|
+
}
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
api.register({
|
|
38
|
+
unauthorizedHandler: unauthorized,
|
|
39
|
+
validationFail: requestValidation,
|
|
40
|
+
notFound,
|
|
41
|
+
postResponseHandler: responseValidation
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
operationIds({ specification: openAPISpecification }).forEach((operationId) => {
|
|
45
|
+
if (!Object.hasOwn(controllers, operationId)) {
|
|
46
|
+
return
|
|
47
|
+
}
|
|
48
|
+
api.register(
|
|
49
|
+
operationId,
|
|
50
|
+
makeExpressCallback({
|
|
51
|
+
controller: controllers[operationId],
|
|
52
|
+
specification: openAPISpecification,
|
|
53
|
+
errorDetails,
|
|
54
|
+
logger,
|
|
55
|
+
meta
|
|
56
|
+
})
|
|
57
|
+
)
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
api.register('notImplemented', (context, request, response) => {
|
|
61
|
+
const { mock } = context.api.mockResponseForOperation(
|
|
62
|
+
context.operation.operationId
|
|
63
|
+
)
|
|
64
|
+
return mock
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
if (secret) {
|
|
68
|
+
api.registerSecurityHandler(
|
|
69
|
+
'apiKey',
|
|
70
|
+
(context) => context.request.headers['x-api-key'] === secret
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
securityHandlers.forEach((securityHandler) => {
|
|
75
|
+
api.registerSecurityHandler(securityHandler.name, securityHandler.handler)
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
return { api, openAPISpecification }
|
|
79
|
+
}
|
package/src/server.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import express from 'express'
|
|
2
|
+
import cors from 'cors'
|
|
3
|
+
import compression from 'compression'
|
|
4
|
+
import helmet from 'helmet'
|
|
5
|
+
import * as Sentry from '@sentry/node'
|
|
6
|
+
import bodyParser from 'body-parser'
|
|
7
|
+
import { openAPI } from './openapi.js'
|
|
8
|
+
import { Api } from './api.js'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Get the origin resource policy
|
|
12
|
+
* @param {string} origin
|
|
13
|
+
* @returns {{ crossOriginResourcePolicy: { policy: string, directives: object } }}
|
|
14
|
+
*/
|
|
15
|
+
const getOriginResourcePolicy = (origin) => ({
|
|
16
|
+
crossOriginResourcePolicy: {
|
|
17
|
+
policy: origin === '*' ? 'cross-origin' : 'same-origin',
|
|
18
|
+
directives: {
|
|
19
|
+
// ...
|
|
20
|
+
'require-trusted-types-for': ["'script'"]
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @typedef {import('express-serve-static-core').Request} Request
|
|
27
|
+
* @typedef {import('express-serve-static-core').Response} Response
|
|
28
|
+
* @typedef {import('openapi-backend').Context} Context
|
|
29
|
+
* @typedef {import('./api.js').ApiSchema} ApiSchema
|
|
30
|
+
* @typedef {import('./api.js').Logger} Logger
|
|
31
|
+
* @typedef {import('express').Express} Express
|
|
32
|
+
* @typedef {object} Controller
|
|
33
|
+
* @property {Context=} context
|
|
34
|
+
* @property {Request=} request
|
|
35
|
+
* @property {Response=} response
|
|
36
|
+
* @property {object=} parameters
|
|
37
|
+
* @property {object=} specification
|
|
38
|
+
* @property {object=} post
|
|
39
|
+
* @property {string=} url
|
|
40
|
+
* @property {Logger=} logger
|
|
41
|
+
* @property {object=} meta
|
|
42
|
+
* @typedef {object} SentryConfig
|
|
43
|
+
* @property {string=} dsn
|
|
44
|
+
* @property {number=} tracesSampleRate
|
|
45
|
+
* @property {number=} profilesSampleRate
|
|
46
|
+
* @property {string=} release
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Setup the server
|
|
51
|
+
* @async
|
|
52
|
+
* @param {object} params
|
|
53
|
+
* @param {ApiSchema[]} params.apis
|
|
54
|
+
* @param {string=} params.origin
|
|
55
|
+
* @param {string=} params.staticFolder
|
|
56
|
+
* @param {SentryConfig=} params.sentry
|
|
57
|
+
* @param {string=} params.poweredBy
|
|
58
|
+
* @param {string=} params.version
|
|
59
|
+
* @returns {Promise<{ app: Express }>}
|
|
60
|
+
*/
|
|
61
|
+
export const setupServer = async ({ apis, origin = '*', staticFolder, sentry, poweredBy = 'TroJS', version = '1.0.0' }) => {
|
|
62
|
+
const corsOptions = {
|
|
63
|
+
origin
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const app = express()
|
|
67
|
+
|
|
68
|
+
if (sentry) {
|
|
69
|
+
Sentry.init({
|
|
70
|
+
dsn: sentry.dsn,
|
|
71
|
+
integrations: [
|
|
72
|
+
new Sentry.Integrations.Http({ tracing: true }),
|
|
73
|
+
new Sentry.Integrations.Express({ app })
|
|
74
|
+
],
|
|
75
|
+
tracesSampleRate: sentry.tracesSampleRate || 1.0,
|
|
76
|
+
profilesSampleRate: sentry.profilesSampleRate || 1.0,
|
|
77
|
+
release: sentry.release
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
app.use(Sentry.Handlers.requestHandler())
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
app.use(cors(corsOptions))
|
|
84
|
+
app.use(compression())
|
|
85
|
+
app.use(helmet(getOriginResourcePolicy(origin)))
|
|
86
|
+
app.use(express.json())
|
|
87
|
+
app.use(bodyParser.urlencoded({ extended: false }))
|
|
88
|
+
app.use((_request, response, next) => {
|
|
89
|
+
response.setHeader('X-Powered-By', poweredBy)
|
|
90
|
+
response.setHeader('X-Version', version)
|
|
91
|
+
next()
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
if (staticFolder) {
|
|
95
|
+
app.use(express.static(staticFolder))
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
apis.forEach((api) => {
|
|
99
|
+
const apiRoutes = new Api(api)
|
|
100
|
+
const routes = apiRoutes.setup()
|
|
101
|
+
app.use(`/${api.version}`, routes)
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
if (sentry) {
|
|
105
|
+
app.use(Sentry.Handlers.errorHandler())
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return { app }
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export { openAPI, Api }
|