Skip to content

Commit

Permalink
Fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterwigboldus-visma committed Nov 27, 2024
1 parent db5dddc commit c0e1999
Show file tree
Hide file tree
Showing 20 changed files with 730 additions and 727 deletions.
392 changes: 196 additions & 196 deletions src/__fixtures__/spec.js

Large diffs are not rendered by default.

53 changes: 27 additions & 26 deletions src/api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import express from 'express';
import swaggerUi from 'swagger-ui-express';
import { setupRouter } from './router.js';
import express from 'express'
import swaggerUi from 'swagger-ui-express'
import { setupRouter } from './router.js'

/**
* @typedef {import('openapi-backend').Handler} Handler
Expand Down Expand Up @@ -29,13 +29,14 @@ import { setupRouter } from './router.js';
*/

/**
* 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
* 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
*/

export class Api {
/**
* Create a new instance of the API
* @constructor
* @class
* @param {ApiSchema} params
*/
constructor({
Expand All @@ -50,36 +51,36 @@ export class Api {
securityHandlers,
swagger,
apiDocs,
ajvOptions,
ajvOptions
}) {
this.version = version;
this.specification = specification;
this.controllers = controllers;
this.apiRoot = apiRoot;
this.strictSpecification = strictSpecification;
this.errorDetails = errorDetails || false;
this.logger = logger || console;
this.meta = meta || {};
this.securityHandlers = securityHandlers || [];
this.swagger = swagger ?? true;
this.apiDocs = apiDocs ?? true;
this.ajvOptions = ajvOptions ?? { allErrors: false };
this.version = version
this.specification = specification
this.controllers = controllers
this.apiRoot = apiRoot
this.strictSpecification = strictSpecification
this.errorDetails = errorDetails || false
this.logger = logger || console
this.meta = meta || {}
this.securityHandlers = securityHandlers || []
this.swagger = swagger ?? true
this.apiDocs = apiDocs ?? true
this.ajvOptions = ajvOptions ?? { allErrors: false }
}

setup() {
const router = express.Router();
const router = express.Router()

if (this.swagger) {
router.use(
'/swagger',
swaggerUi.serveFiles(this.specification, {}),
swaggerUi.setup(this.specification)
);
)
}
if (this.apiDocs) {
router.get('/api-docs', (_request, response) =>
response.json(this.specification)
);
)
}

const { api } = setupRouter({
Expand All @@ -91,14 +92,14 @@ export class Api {
logger: this.logger,
meta: this.meta,
securityHandlers: this.securityHandlers,
ajvOptions: this.ajvOptions,
});
api.init();
ajvOptions: this.ajvOptions
})
api.init()

router.use((request, response) =>
api.handleRequest(request, request, response)
);
)

return router;
return router
}
}
12 changes: 6 additions & 6 deletions src/error-status.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
const errorCodesStatus = [
{
type: TypeError,
status: 422,
status: 422
},
{
type: RangeError,
status: 404,
status: 404
},
{
type: Error,
status: 500,
},
];
status: 500
}
]

/**
* Get a http status when you send an error.
Expand All @@ -21,4 +21,4 @@ const errorCodesStatus = [
*/
export default (error) =>
errorCodesStatus.find((errorCode) => error instanceof errorCode.type)
.status;
.status
24 changes: 12 additions & 12 deletions src/error-status.test.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import test from 'node:test';
import assert from 'node:assert';
import getStatusByError from './error-status.js';
import test from 'node:test'
import assert from 'node:assert'
import getStatusByError from './error-status.js'

const TestCases = [
{
description: 'A normal error should return status 500',
error: new Error('test'),
expectedResult: 500,
expectedResult: 500
},
{
description: 'A type error should return status 422',
error: new TypeError('test'),
expectedResult: 422,
expectedResult: 422
},
{
description: 'A range error should return status 404',
error: new RangeError('test'),
expectedResult: 404,
},
];
expectedResult: 404
}
]

test('Error status entity', async (t) => {
await Promise.all(
TestCases.map(async ({ description, error, expectedResult }) => {
await t.test(description, () => {
assert.deepEqual(getStatusByError(error), expectedResult);
});
assert.deepEqual(getStatusByError(error), expectedResult)
})
})
);
});
)
})
114 changes: 57 additions & 57 deletions src/express-callback.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import getStatusByError from './error-status.js';
import { parseParams } from './params.js';
import getStatusByError from './error-status.js'
import { parseParams } from './params.js'

/**
* @typedef {import('express-serve-static-core').Request} Request
Expand Down Expand Up @@ -29,68 +29,68 @@ export const makeExpressCallback =
* @param {Response} response
* @returns {Promise<any>}
*/
async (context, request, response) => {
try {
const allParameters = {
...(context.request?.params || {}),
...(context.request?.query || {}),
};
const parameters = parseParams({
query: allParameters,
spec: context.operation.parameters,
mock,
});
const url = `${request.protocol}://${request.get('Host')}${request.originalUrl}`;
async (context, request, response) => {
try {
const allParameters = {
...(context.request?.params || {}),
...(context.request?.query || {})
}
const parameters = parseParams({
query: allParameters,
spec: context.operation.parameters,
mock
})
const url = `${request.protocol}://${request.get('Host')}${request.originalUrl}`

const responseBody = await controller({
context,
request,
response,
parameters,
specification,
post: request.body,
url,
logger,
meta,
});
logger.debug({
url,
parameters,
post: request.body,
response: responseBody,
});
const responseBody = await controller({
context,
request,
response,
parameters,
specification,
post: request.body,
url,
logger,
meta
})
logger.debug({
url,
parameters,
post: request.body,
response: responseBody
})

return responseBody;
} catch (error) {
const errorCodeStatus = getStatusByError(error);
return responseBody
} catch (error) {
const errorCodeStatus = getStatusByError(error)

if (errorCodeStatus >= 500) {
logger.error(error);
} else {
logger.warn(error);
}
if (errorCodeStatus >= 500) {
logger.error(error)
} else {
logger.warn(error)
}

response.status(errorCodeStatus)

response.status(errorCodeStatus);
if (errorDetails) {
return {
errors: [
{
message: error.message,
value: error.valueOf(),
type: error.constructor.name
}
],
status: errorCodeStatus,
timestamp: new Date(),
message: error.message
}
}

if (errorDetails) {
return {
errors: [
{
message: error.message,
value: error.valueOf(),
type: error.constructor.name,
},
],
status: errorCodeStatus,
timestamp: new Date(),
message: error.message,
};
message: error.message
}
}

return {
status: errorCodeStatus,
timestamp: new Date(),
message: error.message,
};
}
};
Loading

0 comments on commit c0e1999

Please sign in to comment.