@breautek/storm
Version:
Object-Oriented REST API framework
128 lines (125 loc) • 4.38 kB
JavaScript
"use strict";
/*
Copyright 2017-2021 Norman Breau
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Response = void 0;
const StatusCode_1 = require("./StatusCode");
const ResponseData_1 = require("./ResponseData");
const StormError_1 = require("./StormError");
const InternalError_1 = require("./InternalError");
const stream_1 = require("stream");
const TAG = 'Response';
class Response {
constructor(app, response, requestURL) {
this.$app = app;
this.$response = response;
this.$created = new Date();
this.$requestURL = requestURL;
}
setStatus(status) {
this.$response.status(status);
return this;
}
getStatus() {
return this.$response.statusCode;
}
redirect(url) {
this.$response.redirect(url);
}
$send(data, statusOverride) {
if (data === null || data === undefined) {
this.setStatus(statusOverride || StatusCode_1.StatusCode.OK_NO_CONTENT);
this.$response.send();
}
else if (typeof data === 'number') {
// Numbers needs to be toString as
// express will interpet them as a status code
this.$response.send(data.toString());
}
else if (data instanceof Buffer || ['string', 'boolean'].indexOf(typeof data) > -1) {
this.$response.send(data);
}
else if (data instanceof stream_1.Stream.Readable) {
this.pipe(data);
}
else if (data instanceof ResponseData_1.ResponseData) {
if (data.getRedirect() !== null) {
this.redirect(data.getRedirect());
return;
}
let headers = data.getHeaders();
for (let header of headers) {
this.setHeader(header[0], header[1]);
}
this.setStatus(data.getStatus());
this.$send(data.getData(), data.getStatus());
}
else if (data instanceof StormError_1.StormError) {
this.setStatus(statusOverride || data.getHTTPCode()).send(data.getErrorResponse());
}
else {
this.$response.send(data);
}
}
// public send(data?: TResponse | TErrorResponse | StormError | IErrorResponse | Buffer): void {
send(data) {
this.$send(data);
this.$app.getLogger().info(TAG, `API ${this.$requestURL} (${this.getStatus()}) responded in ${new Date().getTime() - this.$created.getTime()}ms`);
}
pipe(stream) {
stream.on('end', () => {
stream.unpipe(this.$response);
});
stream.pipe(this.$response);
}
success(data) {
if (data === undefined) {
this.setStatus(StatusCode_1.StatusCode.OK_NO_CONTENT);
}
else {
this.setStatus(StatusCode_1.StatusCode.OK);
}
this.send(data);
}
setHeader(key, value) {
this.$response.set(key, value);
}
setHeaders(keyValuePair) {
this.$response.set(keyValuePair);
}
isHeadersSent() {
return this.$response.headersSent;
}
error(error) {
if (error) {
if (error instanceof StormError_1.StormError) {
this.send(error);
}
else if (error instanceof ResponseData_1.ResponseData) {
let headers = error.getHeaders();
for (let header of headers) {
this.setHeader(header[0], header[1]);
}
this.send(error);
}
else {
this.send(new InternalError_1.InternalError(error));
}
}
else {
this.send(new InternalError_1.InternalError(error));
}
}
}
exports.Response = Response;
//# sourceMappingURL=Response.js.map