UNPKG

hors

Version:
128 lines (127 loc) 3.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class Transaction { constructor(request, response, correlationId, onEndCallback) { this.send = null; this.correlationId = null; this.request = null; this.response = null; this.nextFunction = null; this.user = null; this.request = request; this.response = response; this.correlationId = correlationId; this.send = new Send(this.response, onEndCallback); } getBody() { return this.request.body; } getCookies() { return this.request.cookies || {}; } getExpressNextFunction() { return this.nextFunction; } getExpressRequest() { return this.request; } getExpressResponse() { return this.response; } getParams() { return this.request.params; } getQuery() { return this.request.query; } getRequestInfo() { return { correlationId: this.correlationId, url: this.request.url, ipAddress: this.request.ip }; } getUser() { return this.user; } redirect(url) { this.response.redirect(url); } setCookie(name, value, options) { this.response.cookie(name, value, options); } setExpressNextFunction(next) { this.nextFunction = next; } setUser(user) { this.user = user; } } exports.Transaction = Transaction; class Send { constructor(response, onEndCallback) { this.response = null; this.onEndCallback = null; this.response = response; this.onEndCallback = onEndCallback; } /* Successful */ ok(payload) { this.send(200, payload); } created(payload) { this.send(201, payload); } accepted(payload) { this.send(202, payload); } noContent() { this.send(204); } partialContent(payload) { this.send(206, payload); } /* Redirection */ movedPermanently(payload) { this.send(301, payload); } found(payload) { this.send(302, payload); } notModified(payload) { this.send(304, payload); } /* Client error */ badRequest(payload) { this.send(400, payload); } unauthorized(payload) { this.send(401, payload); } forbidden(payload) { this.send(403, payload); } notFound(payload) { this.send(404, payload); } methodNotAllowed(payload) { this.send(405, payload); } /* Server error */ internalServerError(payload) { this.send(500, payload); } notImplemented(payload) { this.send(501, payload); } /* Tools */ send(statusCode, payload) { this.response .status(statusCode) // Send automatically sets the correct Content-Type if // data is Buffer, string or an normal object (...or array) .send(payload); this.onEndCallback(); } } exports.default = Transaction;