@trojs/openapi-server 1.5.2 → 1.6.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 CHANGED
@@ -92,11 +92,21 @@ const securityHandlers = [
92
92
  }
93
93
  ]
94
94
 
95
+ const unauthorizedHandler = async (context, request, response) => {
96
+ response.status(401)
97
+ return {
98
+ status: 401,
99
+ timestamp: new Date(),
100
+ message: 'Unauthorized'
101
+ }
102
+ }
103
+
95
104
  const api = new Api({
96
105
  version: 'v1',
97
106
  specification: openAPISpecification,
98
107
  controllers,
99
- securityHandlers
108
+ securityHandlers,
109
+ unauthorizedHandler
100
110
  })
101
111
  ```
102
112
 
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "@trojs/openapi-server",
3
3
  "description": "OpenAPI Server",
4
- "version": "1.5.2",
4
+ "version": "1.6.0",
5
5
  "author": {
6
6
  "name": "Pieter Wigboldus",
7
7
  "url": "https://trojs.org/"
8
8
  },
9
9
  "license": "MIT",
10
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",
11
+ "lint": "eslint",
12
+ "lint:report": "eslint src/*.js -f json -o report.json",
13
+ "lint:fix": "eslint --fix",
14
14
  "test": "c8 node --test src/*.test.js",
15
15
  "cpd": "node_modules/jscpd/bin/jscpd src",
16
16
  "vulnerabilities": "npm audit --omit=dev"
@@ -33,22 +33,21 @@
33
33
  ],
34
34
  "main": "src/server.js",
35
35
  "devDependencies": {
36
- "@hckrnews/eslint-code-quality": "^0.2.1",
37
- "@hckrnews/eslint-config": "^3.1.0",
36
+ "@eslint/js": "^9.15.0",
37
+ "@stylistic/eslint-plugin": "^2.11.0",
38
+ "@stylistic/eslint-plugin-js": "^2.11.0",
38
39
  "@types/express-serve-static-core": "^4.17.41",
39
- "@types/node": "^20.14.9",
40
+ "@types/node": "^22.0.0",
40
41
  "c8": "^10.0.0",
41
- "eslint": "^8.23.0",
42
- "eslint-config-airbnb-base": "^15.0.0",
42
+ "eslint": "^9.15.0",
43
43
  "eslint-config-prettier": "^9.1.0",
44
- "eslint-plugin-html": "^8.1.1",
45
- "eslint-plugin-import": "^2.26.0",
46
- "eslint-plugin-jsdoc": "^50.0.0",
47
- "eslint-plugin-jsx-a11y": "^6.9.0",
48
- "eslint-plugin-n": "^17.0.0",
49
- "eslint-plugin-prettier": "^5.1.3",
50
- "eslint-plugin-promise": "^7.0.0",
51
- "eslint-plugin-sonarjs": "^0.25.1",
44
+ "eslint-plugin-import": "^2.31.0",
45
+ "eslint-plugin-jsdoc": "^50.5.0",
46
+ "eslint-plugin-n": "^17.14.0",
47
+ "eslint-plugin-prettier": "^5.2.1",
48
+ "eslint-plugin-promise": "^7.1.0",
49
+ "eslint-plugin-sonarjs": "^2.0.4",
50
+ "globals": "^15.12.0",
52
51
  "jscpd": "^4.0.0",
53
52
  "prettier": "^3.3.3",
54
53
  "supertest": "^7.0.0"
@@ -58,7 +57,7 @@
58
57
  "url": "https://github.com/trojs/openapi-server"
59
58
  },
60
59
  "engines": {
61
- "node": ">= 18.13"
60
+ "node": ">= 20"
62
61
  },
63
62
  "keywords": [
64
63
  "openapi",
@@ -83,6 +82,13 @@
83
82
  "overrides": {
84
83
  "semver": "^7.5.3",
85
84
  "send": "^0.19.0",
86
- "cookie@<=0.7.0": "0.7.0"
85
+ "cookie@<=0.7.0": "0.7.0",
86
+ "eslint-plugin-sonarjs": {
87
+ "eslint": "^9.15.0",
88
+ "@typescript-eslint/eslint-plugin": "^8.0.0",
89
+ "@typescript-eslint/utils": "^8.0.0",
90
+ "eslint-plugin-import": "^2.31.0",
91
+ "eslint-plugin-react-hooks": "^5.0.0"
92
+ }
87
93
  }
88
94
  }
package/src/api.js CHANGED
@@ -1,6 +1,6 @@
1
- import express from 'express';
2
- import swaggerUi from 'swagger-ui-express';
3
- import { setupRouter } from './router.js';
1
+ import express from 'express'
2
+ import swaggerUi from 'swagger-ui-express'
3
+ import { setupRouter } from './router.js'
4
4
 
5
5
  /**
6
6
  * @typedef {import('openapi-backend').Handler} Handler
@@ -23,19 +23,21 @@ import { setupRouter } from './router.js';
23
23
  * @property {Logger=} logger
24
24
  * @property {object=} meta
25
25
  * @property {SecurityHandler[]=} securityHandlers
26
+ * @property {Handler=} unauthorizedHandler
26
27
  * @property {boolean=} swagger
27
28
  * @property {boolean=} apiDocs
28
29
  * @property {AjvOpts=} ajvOptions
29
30
  */
30
31
 
31
32
  /**
32
- * 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
33
+ * Setup the server for a specific API, so every server can run multiple instances of the API,
34
+ * like different versions, for e.g. different clients
33
35
  */
34
36
 
35
37
  export class Api {
36
38
  /**
37
39
  * Create a new instance of the API
38
- * @constructor
40
+ * @class
39
41
  * @param {ApiSchema} params
40
42
  */
41
43
  constructor({
@@ -48,38 +50,40 @@ export class Api {
48
50
  logger,
49
51
  meta,
50
52
  securityHandlers,
53
+ unauthorizedHandler,
51
54
  swagger,
52
55
  apiDocs,
53
- ajvOptions,
56
+ ajvOptions
54
57
  }) {
55
- this.version = version;
56
- this.specification = specification;
57
- this.controllers = controllers;
58
- this.apiRoot = apiRoot;
59
- this.strictSpecification = strictSpecification;
60
- this.errorDetails = errorDetails || false;
61
- this.logger = logger || console;
62
- this.meta = meta || {};
63
- this.securityHandlers = securityHandlers || [];
64
- this.swagger = swagger ?? true;
65
- this.apiDocs = apiDocs ?? true;
66
- this.ajvOptions = ajvOptions ?? { allErrors: false };
58
+ this.version = version
59
+ this.specification = specification
60
+ this.controllers = controllers
61
+ this.apiRoot = apiRoot
62
+ this.strictSpecification = strictSpecification
63
+ this.errorDetails = errorDetails || false
64
+ this.logger = logger || console
65
+ this.meta = meta || {}
66
+ this.securityHandlers = securityHandlers || []
67
+ this.unauthorizedHandler = unauthorizedHandler || undefined
68
+ this.swagger = swagger ?? true
69
+ this.apiDocs = apiDocs ?? true
70
+ this.ajvOptions = ajvOptions ?? { allErrors: false }
67
71
  }
68
72
 
69
73
  setup() {
70
- const router = express.Router();
74
+ const router = express.Router()
71
75
 
72
76
  if (this.swagger) {
73
77
  router.use(
74
78
  '/swagger',
75
79
  swaggerUi.serveFiles(this.specification, {}),
76
80
  swaggerUi.setup(this.specification)
77
- );
81
+ )
78
82
  }
79
83
  if (this.apiDocs) {
80
84
  router.get('/api-docs', (_request, response) =>
81
85
  response.json(this.specification)
82
- );
86
+ )
83
87
  }
84
88
 
85
89
  const { api } = setupRouter({
@@ -91,14 +95,15 @@ export class Api {
91
95
  logger: this.logger,
92
96
  meta: this.meta,
93
97
  securityHandlers: this.securityHandlers,
94
- ajvOptions: this.ajvOptions,
95
- });
96
- api.init();
98
+ unauthorizedHandler: this.unauthorizedHandler,
99
+ ajvOptions: this.ajvOptions
100
+ })
101
+ api.init()
97
102
 
98
103
  router.use((request, response) =>
99
104
  api.handleRequest(request, request, response)
100
- );
105
+ )
101
106
 
102
- return router;
107
+ return router
103
108
  }
104
109
  }
@@ -1,17 +1,17 @@
1
1
  const errorCodesStatus = [
2
2
  {
3
3
  type: TypeError,
4
- status: 422,
4
+ status: 422
5
5
  },
6
6
  {
7
7
  type: RangeError,
8
- status: 404,
8
+ status: 404
9
9
  },
10
10
  {
11
11
  type: Error,
12
- status: 500,
13
- },
14
- ];
12
+ status: 500
13
+ }
14
+ ]
15
15
 
16
16
  /**
17
17
  * Get a http status when you send an error.
@@ -21,4 +21,4 @@ const errorCodesStatus = [
21
21
  */
22
22
  export default (error) =>
23
23
  errorCodesStatus.find((errorCode) => error instanceof errorCode.type)
24
- .status;
24
+ .status
@@ -1,5 +1,5 @@
1
- import getStatusByError from './error-status.js';
2
- import { parseParams } from './params.js';
1
+ import getStatusByError from './error-status.js'
2
+ import { parseParams } from './params.js'
3
3
 
4
4
  /**
5
5
  * @typedef {import('express-serve-static-core').Request} Request
@@ -29,68 +29,68 @@ export const makeExpressCallback =
29
29
  * @param {Response} response
30
30
  * @returns {Promise<any>}
31
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
- mock,
42
- });
43
- const url = `${request.protocol}://${request.get('Host')}${request.originalUrl}`;
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
+ mock
42
+ })
43
+ const url = `${request.protocol}://${request.get('Host')}${request.originalUrl}`
44
44
 
45
- const responseBody = await controller({
46
- context,
47
- request,
48
- response,
49
- parameters,
50
- specification,
51
- post: request.body,
52
- url,
53
- logger,
54
- meta,
55
- });
56
- logger.debug({
57
- url,
58
- parameters,
59
- post: request.body,
60
- response: responseBody,
61
- });
45
+ const responseBody = await controller({
46
+ context,
47
+ request,
48
+ response,
49
+ parameters,
50
+ specification,
51
+ post: request.body,
52
+ url,
53
+ logger,
54
+ meta
55
+ })
56
+ logger.debug({
57
+ url,
58
+ parameters,
59
+ post: request.body,
60
+ response: responseBody
61
+ })
62
62
 
63
- return responseBody;
64
- } catch (error) {
65
- const errorCodeStatus = getStatusByError(error);
63
+ return responseBody
64
+ } catch (error) {
65
+ const errorCodeStatus = getStatusByError(error)
66
66
 
67
- if (errorCodeStatus >= 500) {
68
- logger.error(error);
69
- } else {
70
- logger.warn(error);
71
- }
67
+ if (errorCodeStatus >= 500) {
68
+ logger.error(error)
69
+ } else {
70
+ logger.warn(error)
71
+ }
72
+
73
+ response.status(errorCodeStatus)
72
74
 
73
- response.status(errorCodeStatus);
75
+ if (errorDetails) {
76
+ return {
77
+ errors: [
78
+ {
79
+ message: error.message,
80
+ value: error.valueOf(),
81
+ type: error.constructor.name
82
+ }
83
+ ],
84
+ status: errorCodeStatus,
85
+ timestamp: new Date(),
86
+ message: error.message
87
+ }
88
+ }
74
89
 
75
- if (errorDetails) {
76
90
  return {
77
- errors: [
78
- {
79
- message: error.message,
80
- value: error.valueOf(),
81
- type: error.constructor.name,
82
- },
83
- ],
84
91
  status: errorCodeStatus,
85
92
  timestamp: new Date(),
86
- message: error.message,
87
- };
93
+ message: error.message
94
+ }
88
95
  }
89
-
90
- return {
91
- status: errorCodeStatus,
92
- timestamp: new Date(),
93
- message: error.message,
94
- };
95
96
  }
96
- };
@@ -1,8 +1,8 @@
1
1
  export const notFound = (_context, request, response) => {
2
- response.status(404)
3
- return {
4
- status: 404,
5
- timestamp: new Date(),
6
- message: 'Not found'
7
- }
2
+ response.status(404)
3
+ return {
4
+ status: 404,
5
+ timestamp: new Date(),
6
+ message: 'Not found'
7
+ }
8
8
  }
@@ -1,9 +1,9 @@
1
1
  export const requestValidation = (context, request, response) => {
2
- response.status(400)
3
- return {
4
- errors: context.validation.errors,
5
- status: 400,
6
- timestamp: new Date(),
7
- message: 'Bad Request'
8
- }
2
+ response.status(400)
3
+ return {
4
+ errors: context.validation.errors,
5
+ status: 400,
6
+ timestamp: new Date(),
7
+ message: 'Bad Request'
8
+ }
9
9
  }
@@ -1,30 +1,30 @@
1
1
  export const responseValidation = (context, request, response) => {
2
- const responseDoesntNeedValidation = response.statusCode >= 400
3
- if (responseDoesntNeedValidation) {
4
- return response.json(context.response)
5
- }
2
+ const responseDoesntNeedValidation = response.statusCode >= 400
3
+ if (responseDoesntNeedValidation) {
4
+ return response.json(context.response)
5
+ }
6
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
- }
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
19
 
20
- if (!context.response) {
21
- return response.end()
22
- }
20
+ if (!context.response) {
21
+ return response.end()
22
+ }
23
23
 
24
- const contentType = request?.headers?.accept ?? 'application/json'
25
- if (contentType === 'application/json') {
26
- return response.json(context.response)
27
- }
24
+ const contentType = request?.headers?.accept ?? 'application/json'
25
+ if (contentType === 'application/json') {
26
+ return response.json(context.response)
27
+ }
28
28
 
29
- return response.send(context.response)
29
+ return response.send(context.response)
30
30
  }
@@ -1,8 +1,8 @@
1
1
  export const unauthorized = async (context, request, response) => {
2
- response.status(401)
3
- return {
4
- status: 401,
5
- timestamp: new Date(),
6
- message: 'Unauthorized'
7
- }
2
+ response.status(401)
3
+ return {
4
+ status: 401,
5
+ timestamp: new Date(),
6
+ message: 'Unauthorized'
7
+ }
8
8
  }
package/src/openapi.js CHANGED
@@ -1,4 +1,4 @@
1
- import { readFile } from 'node:fs/promises';
1
+ import { readFile } from 'node:fs/promises'
2
2
 
3
3
  /**
4
4
  * Get the OpenAPI specification from the file.
@@ -9,7 +9,7 @@ import { readFile } from 'node:fs/promises';
9
9
  * @returns {Promise<{ openAPISpecification: object; }>}
10
10
  */
11
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
- };
12
+ const fileUrl = new URL(file, base)
13
+ const openAPISpecification = JSON.parse(await readFile(fileUrl, 'utf8'))
14
+ return { openAPISpecification }
15
+ }
@@ -1,4 +1,4 @@
1
- const operations = ['get', 'put', 'patch', 'post', 'delete'];
1
+ const operations = ['get', 'put', 'patch', 'post', 'delete']
2
2
 
3
3
  /**
4
4
  * Get all operation ID's from the specification.
@@ -13,4 +13,4 @@ export const operationIds = ({ specification }) =>
13
13
  operations.includes(operation) ? data.operationId : null
14
14
  )
15
15
  )
16
- .flat();
16
+ .flat()
package/src/params.js CHANGED
@@ -1,4 +1,4 @@
1
- import { types } from './types.js';
1
+ import { types } from './types.js'
2
2
 
3
3
  /**
4
4
  * Parse params to the types defined in the spec
@@ -11,39 +11,39 @@ import { types } from './types.js';
11
11
  export const parseParams = ({ query, spec, mock = false }) =>
12
12
  spec
13
13
  .map((parameter) => {
14
- const { name, schema } = parameter;
14
+ const { name, schema } = parameter
15
15
  const {
16
16
  type,
17
17
  default: defaultValue,
18
- example: exampleValue,
19
- } = schema;
20
- const Type = types[type];
21
- const paramName = query?.[name];
18
+ example: exampleValue
19
+ } = schema
20
+ const Type = types[type]
21
+ const paramName = query?.[name]
22
22
 
23
23
  if (!paramName && defaultValue !== undefined) {
24
- return { name, value: defaultValue };
24
+ return { name, value: defaultValue }
25
25
  }
26
26
 
27
27
  if (!paramName && mock && exampleValue !== undefined) {
28
- return { name, value: exampleValue };
28
+ return { name, value: exampleValue }
29
29
  }
30
30
 
31
31
  if (!paramName) {
32
- return undefined;
32
+ return undefined
33
33
  }
34
34
 
35
35
  if (Type === Boolean) {
36
36
  return {
37
37
  name,
38
- value: JSON.parse(paramName.toLowerCase()),
39
- };
38
+ value: JSON.parse(paramName.toLowerCase())
39
+ }
40
40
  }
41
41
 
42
- const value = new Type(paramName).valueOf();
43
- return { name, value };
42
+ const value = new Type(paramName).valueOf()
43
+ return { name, value }
44
44
  })
45
45
  .filter(Boolean)
46
46
  .reduce((acc, { name, value }) => {
47
- acc[name] = value;
48
- return acc;
49
- }, {});
47
+ acc[name] = value
48
+ return acc
49
+ }, {})
package/src/router.js CHANGED
@@ -1,15 +1,16 @@
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';
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
9
 
10
10
  /**
11
11
  * @typedef {import('./api.js').Logger} Logger
12
12
  * @typedef {import('./api.js').SecurityHandler} SecurityHandler
13
+ * @typedef {import('./api.js').Handler} Handler
13
14
  * @typedef {import('ajv').Options} AjvOpts
14
15
  */
15
16
 
@@ -24,6 +25,7 @@ import { unauthorized } from './handlers/unauthorized.js';
24
25
  * @param {Logger=} params.logger
25
26
  * @param {object=} params.meta
26
27
  * @param {SecurityHandler[]=} params.securityHandlers
28
+ * @param {Handler=} params.unauthorizedHandler
27
29
  * @param {AjvOpts=} params.ajvOptions
28
30
  * @param {boolean=} params.mock
29
31
  * @returns {{ api: OpenAPIBackend<any>, openAPISpecification: object }}
@@ -37,8 +39,9 @@ export const setupRouter = ({
37
39
  logger,
38
40
  meta,
39
41
  securityHandlers = [],
42
+ unauthorizedHandler,
40
43
  ajvOptions = {},
41
- mock,
44
+ mock
42
45
  }) => {
43
46
  const api = new OpenAPIBackend({
44
47
  definition: openAPISpecification,
@@ -46,22 +49,22 @@ export const setupRouter = ({
46
49
  strict: strictSpecification,
47
50
  ajvOpts: ajvOptions,
48
51
  customizeAjv: (originalAjv) => {
49
- addFormats(originalAjv);
50
- return originalAjv;
51
- },
52
- });
52
+ addFormats(originalAjv)
53
+ return originalAjv
54
+ }
55
+ })
53
56
 
54
57
  api.register({
55
- unauthorizedHandler: unauthorized,
58
+ unauthorizedHandler: unauthorizedHandler || unauthorized,
56
59
  validationFail: requestValidation,
57
60
  notFound,
58
- postResponseHandler: responseValidation,
59
- });
61
+ postResponseHandler: responseValidation
62
+ })
60
63
 
61
64
  operationIds({ specification: openAPISpecification }).forEach(
62
65
  (operationId) => {
63
66
  if (!Object.hasOwn(controllers, operationId)) {
64
- return;
67
+ return
65
68
  }
66
69
  api.register(
67
70
  operationId,
@@ -71,24 +74,24 @@ export const setupRouter = ({
71
74
  errorDetails,
72
75
  logger,
73
76
  meta,
74
- mock,
77
+ mock
75
78
  })
76
- );
79
+ )
77
80
  }
78
- );
81
+ )
79
82
 
80
83
  api.register('notImplemented', (context) => {
81
84
  const { mock: mockImplementation } =
82
- context.api.mockResponseForOperation(context.operation.operationId);
83
- return mockImplementation;
84
- });
85
+ context.api.mockResponseForOperation(context.operation.operationId)
86
+ return mockImplementation
87
+ })
85
88
 
86
89
  securityHandlers.forEach((securityHandler) => {
87
90
  api.registerSecurityHandler(
88
91
  securityHandler.name,
89
92
  securityHandler.handler
90
- );
91
- });
93
+ )
94
+ })
92
95
 
93
- return { api, openAPISpecification };
94
- };
96
+ return { api, openAPISpecification }
97
+ }
package/src/server.js CHANGED
@@ -1,11 +1,13 @@
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';
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
+ /* eslint-disable sonarjs/cors */
9
11
 
10
12
  /**
11
13
  * Get the origin resource policy
@@ -17,10 +19,10 @@ const getOriginResourcePolicy = (origin) => ({
17
19
  policy: origin === '*' ? 'cross-origin' : 'same-origin',
18
20
  directives: {
19
21
  // ...
20
- 'require-trusted-types-for': ["'script'"],
21
- },
22
- },
23
- });
22
+ 'require-trusted-types-for': ["'script'"]
23
+ }
24
+ }
25
+ })
24
26
 
25
27
  /**
26
28
  * @typedef {import('express-serve-static-core').Request} Request
@@ -75,56 +77,56 @@ export const setupServer = async ({
75
77
  poweredBy = 'TroJS',
76
78
  version = '1.0.0',
77
79
  middleware = [],
78
- maximumBodySize = undefined,
80
+ maximumBodySize = undefined
79
81
  }) => {
80
82
  const corsOptions = {
81
- origin,
82
- };
83
+ origin
84
+ }
83
85
 
84
- const app = express();
86
+ const app = express()
85
87
 
86
88
  if (sentry) {
87
89
  Sentry.init({
88
90
  dsn: sentry.dsn,
89
91
  integrations: [
90
92
  new Sentry.Integrations.Http({ tracing: true }),
91
- new Sentry.Integrations.Express({ app }),
93
+ new Sentry.Integrations.Express({ app })
92
94
  ],
93
95
  tracesSampleRate: sentry.tracesSampleRate || 1.0,
94
96
  profilesSampleRate: sentry.profilesSampleRate || 1.0,
95
- release: sentry.release,
96
- });
97
+ release: sentry.release
98
+ })
97
99
 
98
- app.use(Sentry.Handlers.requestHandler());
100
+ app.use(Sentry.Handlers.requestHandler())
99
101
  }
100
102
 
101
- app.use(cors(corsOptions));
102
- app.use(compression());
103
- app.use(helmet(getOriginResourcePolicy(origin)));
104
- app.use(express.json({ limit: maximumBodySize }));
105
- middleware.forEach((fn) => app.use(fn));
106
- app.use(bodyParser.urlencoded({ extended: false, limit: maximumBodySize }));
103
+ app.use(cors(corsOptions))
104
+ app.use(compression())
105
+ app.use(helmet(getOriginResourcePolicy(origin)))
106
+ app.use(express.json({ limit: maximumBodySize }))
107
+ middleware.forEach((fn) => app.use(fn))
108
+ app.use(bodyParser.urlencoded({ extended: false, limit: maximumBodySize }))
107
109
  app.use((_request, response, next) => {
108
- response.setHeader('X-Powered-By', poweredBy);
109
- response.setHeader('X-Version', version);
110
- next();
111
- });
110
+ response.setHeader('X-Powered-By', poweredBy)
111
+ response.setHeader('X-Version', version)
112
+ next()
113
+ })
112
114
 
113
115
  if (staticFolder) {
114
- app.use(express.static(staticFolder));
116
+ app.use(express.static(staticFolder))
115
117
  }
116
118
 
117
119
  apis.forEach((api) => {
118
- const apiRoutes = new Api(api);
119
- const routes = apiRoutes.setup();
120
- app.use(`/${api.version}`, routes);
121
- });
120
+ const apiRoutes = new Api(api)
121
+ const routes = apiRoutes.setup()
122
+ app.use(`/${api.version}`, routes)
123
+ })
122
124
 
123
125
  if (sentry) {
124
- app.use(Sentry.Handlers.errorHandler());
126
+ app.use(Sentry.Handlers.errorHandler())
125
127
  }
126
128
 
127
- return { app };
128
- };
129
+ return { app }
130
+ }
129
131
 
130
- export { openAPI, Api };
132
+ export { openAPI, Api }
package/src/types.js CHANGED
@@ -6,7 +6,7 @@ const types = {
6
6
  integer: Number,
7
7
  boolean: Boolean,
8
8
  url: URL,
9
- date: Date,
10
- };
9
+ date: Date
10
+ }
11
11
 
12
- export { types };
12
+ export { types }