@trojs/openapi-server 1.1.0 → 1.2.1
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/package.json +1 -3
- package/src/express-callback.js +6 -7
- package/src/server.js +3 -3
- package/src/formdata.js +0 -19
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trojs/openapi-server",
|
|
3
3
|
"description": "OpenAPI Server",
|
|
4
|
-
"version": "1.1
|
|
4
|
+
"version": "1.2.1",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Pieter Wigboldus",
|
|
7
7
|
"url": "https://trojs.org/"
|
|
@@ -24,7 +24,6 @@
|
|
|
24
24
|
"src/error-status.js",
|
|
25
25
|
"src/types.js",
|
|
26
26
|
"src/params.js",
|
|
27
|
-
"src/formdata.js",
|
|
28
27
|
"src/express-callback.js",
|
|
29
28
|
"src/operation-ids.js",
|
|
30
29
|
"src/handlers/not-found.js",
|
|
@@ -62,7 +61,6 @@
|
|
|
62
61
|
],
|
|
63
62
|
"dependencies": {
|
|
64
63
|
"@sentry/node": "^7.112.1",
|
|
65
|
-
"@trojs/formdata-parser": "^0.2.0",
|
|
66
64
|
"ajv-formats": "^3.0.0",
|
|
67
65
|
"body-parser": "^1.20.2",
|
|
68
66
|
"compression": "^1.7.4",
|
package/src/express-callback.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import getStatusByError from './error-status.js'
|
|
2
2
|
import { parseParams } from './params.js'
|
|
3
|
-
import { parseFormData } from './formdata.js'
|
|
4
3
|
|
|
5
4
|
/**
|
|
6
5
|
* @typedef {import('express-serve-static-core').Request} Request
|
|
@@ -42,9 +41,6 @@ export const makeExpressCallback = ({
|
|
|
42
41
|
})
|
|
43
42
|
const url = `${request.protocol}://${request.get('Host')}${request.originalUrl}`
|
|
44
43
|
|
|
45
|
-
// @ts-ignore
|
|
46
|
-
const data = request?._readableState?.buffer
|
|
47
|
-
const files = parseFormData(data, request.headers)
|
|
48
44
|
const responseBody = await controller({
|
|
49
45
|
context,
|
|
50
46
|
request,
|
|
@@ -54,8 +50,7 @@ export const makeExpressCallback = ({
|
|
|
54
50
|
post: request.body,
|
|
55
51
|
url,
|
|
56
52
|
logger,
|
|
57
|
-
meta
|
|
58
|
-
files
|
|
53
|
+
meta
|
|
59
54
|
})
|
|
60
55
|
logger.debug({
|
|
61
56
|
url,
|
|
@@ -68,7 +63,11 @@ export const makeExpressCallback = ({
|
|
|
68
63
|
} catch (error) {
|
|
69
64
|
const errorCodeStatus = getStatusByError(error)
|
|
70
65
|
|
|
71
|
-
|
|
66
|
+
if (errorCodeStatus >= 500) {
|
|
67
|
+
logger.error(error)
|
|
68
|
+
} else {
|
|
69
|
+
logger.warn(error)
|
|
70
|
+
}
|
|
72
71
|
|
|
73
72
|
response.status(errorCodeStatus)
|
|
74
73
|
|
package/src/server.js
CHANGED
|
@@ -29,7 +29,6 @@ const getOriginResourcePolicy = (origin) => ({
|
|
|
29
29
|
* @typedef {import('./api.js').ApiSchema} ApiSchema
|
|
30
30
|
* @typedef {import('./api.js').Logger} Logger
|
|
31
31
|
* @typedef {import('express').Express} Express
|
|
32
|
-
* @typedef {import('@trojs/formdata-parser').FormData} FormData
|
|
33
32
|
* @typedef {object} Controller
|
|
34
33
|
* @property {Context=} context
|
|
35
34
|
* @property {Request=} request
|
|
@@ -40,7 +39,6 @@ const getOriginResourcePolicy = (origin) => ({
|
|
|
40
39
|
* @property {string=} url
|
|
41
40
|
* @property {Logger=} logger
|
|
42
41
|
* @property {object=} meta
|
|
43
|
-
* @property {FormData[]=} files
|
|
44
42
|
* @typedef {object} SentryConfig
|
|
45
43
|
* @property {string=} dsn
|
|
46
44
|
* @property {number=} tracesSampleRate
|
|
@@ -58,9 +56,10 @@ const getOriginResourcePolicy = (origin) => ({
|
|
|
58
56
|
* @param {SentryConfig=} params.sentry
|
|
59
57
|
* @param {string=} params.poweredBy
|
|
60
58
|
* @param {string=} params.version
|
|
59
|
+
* @param {any[]=} params.middleware
|
|
61
60
|
* @returns {Promise<{ app: Express }>}
|
|
62
61
|
*/
|
|
63
|
-
export const setupServer = async ({ apis, origin = '*', staticFolder, sentry, poweredBy = 'TroJS', version = '1.0.0' }) => {
|
|
62
|
+
export const setupServer = async ({ apis, origin = '*', staticFolder, sentry, poweredBy = 'TroJS', version = '1.0.0', middleware = [] }) => {
|
|
64
63
|
const corsOptions = {
|
|
65
64
|
origin
|
|
66
65
|
}
|
|
@@ -86,6 +85,7 @@ export const setupServer = async ({ apis, origin = '*', staticFolder, sentry, po
|
|
|
86
85
|
app.use(compression())
|
|
87
86
|
app.use(helmet(getOriginResourcePolicy(origin)))
|
|
88
87
|
app.use(express.json())
|
|
88
|
+
middleware.forEach((fn) => app.use(fn))
|
|
89
89
|
app.use(bodyParser.urlencoded({ extended: false }))
|
|
90
90
|
app.use((_request, response, next) => {
|
|
91
91
|
response.setHeader('X-Powered-By', poweredBy)
|
package/src/formdata.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import formDataParser from '@trojs/formdata-parser'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @typedef {import('@trojs/formdata-parser').FormData} FormData
|
|
5
|
-
* @typedef {import('node:http').IncomingHttpHeaders} IncomingHttpHeaders
|
|
6
|
-
*
|
|
7
|
-
* Parse form data
|
|
8
|
-
* @param {Buffer|string} data
|
|
9
|
-
* @param {IncomingHttpHeaders} headers
|
|
10
|
-
* @returns {FormData[]}
|
|
11
|
-
*/
|
|
12
|
-
export const parseFormData = (data, headers) => {
|
|
13
|
-
const contentType = headers?.['content-type']
|
|
14
|
-
if (contentType?.startsWith('multipart/form-data') && data) {
|
|
15
|
-
const dataString = data.toString('utf8')
|
|
16
|
-
return formDataParser(dataString, contentType)
|
|
17
|
-
}
|
|
18
|
-
return []
|
|
19
|
-
}
|