ng-apimock-with-presets
Version:
An ng-apimock fork with preset functionality
63 lines • 2.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var http_1 = require("../../http");
var UpdateMockHandler = (function () {
function UpdateMockHandler() {
}
UpdateMockHandler.prototype.handleRequest = function (request, response, next, registry, ngApimockId) {
var _this = this;
var requestDataChunks = [];
request.on('data', function (rawData) {
requestDataChunks.push(rawData);
});
request.on('end', function () {
var data = JSON.parse(Buffer.concat(requestDataChunks).toString());
try {
var match = registry.mocks.filter(function (_mock) { return _mock.identifier === data.identifier; })[0];
if (match !== undefined) {
if (_this.isScenarioSelectionRequest(data)) {
if (_this.isPassThroughScenario(data.scenario)) {
_this.handlePassThroughScenario(registry, data.identifier, ngApimockId);
}
else if (match.responses[data.scenario]) {
_this.handleScenarioSelection(registry, data.identifier, data.scenario, ngApimockId);
}
else {
throw new Error('No scenario matching name [' + data.scenario + '] found');
}
}
else if (_this.isEchoRequest(data)) {
_this.handleEcho(registry, data.identifier, data.echo, ngApimockId);
}
else if (_this.isDelayResponseRequest(data)) {
_this.handleDelay(registry, data.identifier, data.delay, ngApimockId);
}
}
else {
throw new Error('No mock matching identifier [' + data.identifier + '] found');
}
response.writeHead(200, http_1.httpHeaders.CONTENT_TYPE_APPLICATION_JSON);
response.end();
}
catch (e) {
response.writeHead(409, http_1.httpHeaders.CONTENT_TYPE_APPLICATION_JSON);
response.end(JSON.stringify(e, ['message']));
}
});
};
UpdateMockHandler.prototype.isScenarioSelectionRequest = function (data) {
return data.scenario !== undefined;
};
UpdateMockHandler.prototype.isEchoRequest = function (data) {
return data.echo !== undefined;
};
UpdateMockHandler.prototype.isDelayResponseRequest = function (data) {
return data.delay !== undefined;
};
UpdateMockHandler.prototype.isPassThroughScenario = function (scenario) {
return scenario === null || scenario === 'passThrough';
};
return UpdateMockHandler;
}());
exports.default = UpdateMockHandler;
//# sourceMappingURL=updateMockHandler.js.map