UNPKG

@n1k1t/mock-server

Version:

Powerful util to setup mocks over HTTP APIs

63 lines 2.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InternalHttpReply = void 0; const models_1 = require("../../../models"); const utils_1 = require("../utils"); const headers = { 'Content-type': 'application/json', }; class InternalHttpReply extends models_1.Reply { ok(payload) { const outgoing = { headers, type: 'plain', status: 200, dataRaw: Buffer.from(JSON.stringify((0, utils_1.buildEndpointResponse)('OK', payload))), }; this.context.response.writeHead(outgoing.status, outgoing.headers); this.context.response.write(outgoing.dataRaw); this.context.response.end(); this.context.assign({ outgoing }).complete(); } internalError(message = 'Something went wrong') { const outgoing = { headers, type: 'plain', status: 500, dataRaw: Buffer.from(JSON.stringify((0, utils_1.buildEndpointResponse)('INTERNAL_ERROR', { message }))), }; this.context.response.writeHead(outgoing.status, outgoing.headers); this.context.response.write(outgoing.dataRaw); this.context.response.end(); this.context.assign({ outgoing }).complete(); } validationError(reasons = ['Payload is not valid']) { const outgoing = { headers, type: 'plain', status: 400, dataRaw: Buffer.from(JSON.stringify((0, utils_1.buildEndpointResponse)('VALIDATION_ERROR', { reasons }))), }; this.context.response.writeHead(outgoing.status, outgoing.headers); this.context.response.write(outgoing.dataRaw); this.context.response.end(); this.context.assign({ outgoing }).complete(); } notFound() { const outgoing = { headers, type: 'plain', status: 404, dataRaw: Buffer.from(JSON.stringify((0, utils_1.buildEndpointResponse)('NOT_FOUND', null))), }; this.context.response.writeHead(outgoing.status, outgoing.headers); this.context.response.write(outgoing.dataRaw); this.context.response.end(); this.context.assign({ outgoing }).complete(); } static build(context) { return new InternalHttpReply(context); } } exports.InternalHttpReply = InternalHttpReply; //# sourceMappingURL=reply.js.map