function createError (code, msg) { const err = new Error(msg || HttpStatus.getStatusText(code)) err.statusCode = code return err }
res .status(status) .send(status === 500 ? HttpStatus.getStatusText(status) : err.message) })
const sendErrorResponse = res => { res.writeHead(httpStatus.NOT_FOUND, { "Content-Type": "text/html" }); res.write("<h1>File Not Found!</h1>"); res.end(); }
/** * * @param request * @param reply */ const keepAlive = async (request, reply) => { reply.code(HttpStatus.OK).send('API is alive'); }
/** * Create challenge type timeline template. * @param {Object} req the request * @param {Object} res the response */ async function createChallengeTimelineTemplate (req, res) { const result = await service.createChallengeTimelineTemplate(req.body) res.status(HttpStatus.CREATED).send(result) }
static getDefaulterResponseHandlerData (res) { return { onSuccess : function ( data, message, code){ ResponseManager.respondWithSuccess(res, code || ResponseManager.HTTP_STATUS.OK, data, message); }, onError :function (error) { console.log('getDefaulterResponseHandlerData onError'); ResponseManager.respondWithErrorData(res, error.status, error.message, error.data); } } }
const guardMiddleware = (request, reply, next) => { const msg = 'apiKey is required to access the endpoint'; if (request.headers['apikey'] && request.headers['apikey'] !== 'api-authorization-key') { reply.writeHead(HttpStatus.FORBIDDEN); reply.write(JSON.stringify({ msg })); reply.end(); } next(); }
test('find student [GET `/v1/student/:id`]', async () => { const res = await fastify.inject({ method: 'GET', url: '/v1/student/5e456a4628309eb0e1c53d9a', }); expect(res.statusCode).toEqual(HttpStatus.OK); expect(JSON.parse(res.payload)) .toEqual(expect.objectContaining({ _id: '5e456a4628309eb0e1c53d9a', firstName: 'Danish' })); });
/** * Create challenge * @param {Object} req the request * @param {Object} res the response */ async function createChallenge (req, res) { const result = await service.createChallenge(req.authUser, req.body, req.userToken) res.status(HttpStatus.CREATED).send(result) }
static getDefaultResponseHandlerData(res) { return { onSuccess: function (data, message, code) { ResponseManager.respondWithSuccess(res, code || ResponseManager.HTTP_STATUS.OK, data, message); }, onError: function (error) { ResponseManager.respondWithErrorData(res, error.status, error.message, error.data); } }; }
static getDefaultResponseHandlerSuccess (res, errorCallback) { return { onSuccess : function (data, message, code){ ResponseManager.respondWithSuccess(res, code || ResponseManager.HTTP_STATUS.OK, data, message); }, onError: function(error){ errorCallback(error); } } }
/** * Create challenge type * @param {Object} req the request * @param {Object} res the response */ async function createChallengeType (req, res) { const result = await service.createChallengeType(req.body) res.status(HttpStatus.CREATED).send(result) }
/** * Create timeline template. * @param {Object} req the request * @param {Object} res the response */ async function createTimelineTemplate (req, res) { const result = await service.createTimelineTemplate(req.body) res.status(HttpStatus.CREATED).send(result) }
test('update student [PUT `/v1/student/:id`]', async () => { const res = await fastify.inject({ method: 'PUT', url: '/v1/student/5e456a4628309eb0e1c53d9a?firstName=Danish 1&lastName=Siddiq 1', }); expect(res.statusCode).toEqual(HttpStatus.OK); expect(JSON.parse(res.payload)) .toEqual({ status: 'ok' }); });
/** * Create challenge type * @param {Object} req the request * @param {Object} res the response */ async function createChallengeTrack (req, res) { const result = await service.createChallengeTrack(req.body) res.status(HttpStatus.CREATED).send(result) }