@n1k1t/mock-server
Version:
The ultimate toolkit to intercept, transform, and simulate HTTP/WS traffic with type-safe expectations
71 lines • 2.62 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SystemHttpReply = void 0;
const models_1 = require("../../../models");
const utils_1 = require("../utils");
const headers = {
'content-type': 'application/json',
};
class SystemHttpReply extends models_1.Reply {
ok(payload) {
const outgoing = {
headers,
type: 'plain',
status: 200,
raw: {
data: Buffer.from(JSON.stringify((0, utils_1.buildEndpointResponse)('OK', payload))),
},
};
this.context.response.writeHead(outgoing.status, outgoing.headers);
this.context.response.write(outgoing.raw.data ?? Buffer.from(''));
this.context.response.end();
this.context.assign({ outgoing }).complete();
}
internalError(message = 'Something went wrong') {
const outgoing = {
headers,
type: 'plain',
status: 500,
raw: {
data: Buffer.from(JSON.stringify((0, utils_1.buildEndpointResponse)('INTERNAL_ERROR', { message }))),
},
};
this.context.response.writeHead(outgoing.status, outgoing.headers);
this.context.response.write(outgoing.raw.data ?? Buffer.from(''));
this.context.response.end();
this.context.assign({ outgoing }).complete();
}
validationError(reasons = ['Payload is not valid']) {
const outgoing = {
headers,
type: 'plain',
status: 400,
raw: {
data: Buffer.from(JSON.stringify((0, utils_1.buildEndpointResponse)('VALIDATION_ERROR', { reasons }))),
},
};
this.context.response.writeHead(outgoing.status, outgoing.headers);
this.context.response.write(outgoing.raw.data ?? Buffer.from(''));
this.context.response.end();
this.context.assign({ outgoing }).complete();
}
notFound() {
const outgoing = {
headers,
type: 'plain',
status: 404,
raw: {
data: Buffer.from(JSON.stringify((0, utils_1.buildEndpointResponse)('NOT_FOUND', null))),
},
};
this.context.response.writeHead(outgoing.status, outgoing.headers);
this.context.response.write(outgoing.raw.data ?? Buffer.from(''));
this.context.response.end();
this.context.assign({ outgoing }).complete();
}
static build(context) {
return new SystemHttpReply(context);
}
}
exports.SystemHttpReply = SystemHttpReply;
//# sourceMappingURL=reply.js.map