UNPKG

3.31 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6const koa_send_1 = __importDefault(require("koa-send"));
7const path_1 = __importDefault(require("path"));
8const ResponseStrategies_1 = require("./ResponseStrategies");
9class Response {
10 constructor({ message, data, meta, statusCode }) {
11 this.headers = {};
12 this.strategy = ResponseStrategies_1.ResponseStrategies.Json;
13 this.data = data;
14 this.meta = meta;
15 this.message = message;
16 this.statusCode = statusCode;
17 }
18 async patchContext(ctx) {
19 this.ctx = ctx;
20 // Basic stuff
21 this.applyStatusCode(this.statusCode).applyHeaders();
22 if (this.strategy === ResponseStrategies_1.ResponseStrategies.File) {
23 await this.sendFile();
24 }
25 else if (this.strategy === ResponseStrategies_1.ResponseStrategies.Html) {
26 ctx.type = 'html';
27 this.applyBody(this.data);
28 }
29 else {
30 // Default to json.
31 this.applyBody(this.format());
32 }
33 // Allow hooks.
34 if (typeof this.apply === 'function') {
35 await this.apply();
36 }
37 }
38 async sendFile() {
39 const { location, options = {} } = this.meta;
40 const { dir, base } = path_1.default.parse(location);
41 if (options.root) {
42 return await koa_send_1.default(this.ctx, location, options);
43 }
44 await koa_send_1.default(this.ctx, base, Object.assign({ root: dir }, options));
45 }
46 applyStatusCode(statusCode) {
47 this.ctx.status = statusCode;
48 return this;
49 }
50 setStatusCode(statusCode) {
51 this.statusCode = statusCode;
52 return this;
53 }
54 getStatusCode() {
55 return this.statusCode;
56 }
57 applyBody(body) {
58 if (body) {
59 this.ctx.body = body;
60 }
61 return this;
62 }
63 setHeaders(headers) {
64 this.headers = headers;
65 return this;
66 }
67 addHeaders(headers) {
68 Reflect.ownKeys(headers).forEach((header) => this.setHeader(header, headers[header]));
69 return this;
70 }
71 setHeader(header, value) {
72 this.headers[header] = value;
73 return this;
74 }
75 appendHeader(header, value) {
76 this.headers[header] = [].concat(this.headers[header], value);
77 }
78 removeHeader(header) {
79 delete this.headers[header];
80 return this;
81 }
82 applyHeaders() {
83 Reflect.ownKeys(this.headers).forEach((header) => this.ctx.set(header, this.headers[header]));
84 return this;
85 }
86 file(location, options) {
87 this.strategy = ResponseStrategies_1.ResponseStrategies.File;
88 this.meta = { location, options };
89 return this;
90 }
91 json(data) {
92 this.strategy = ResponseStrategies_1.ResponseStrategies.Json;
93 this.data = data;
94 return this;
95 }
96 html(data) {
97 this.strategy = ResponseStrategies_1.ResponseStrategies.Html;
98 this.data = data;
99 return this;
100 }
101 apply() {
102 return;
103 }
104 format() {
105 return;
106 }
107}
108exports.Response = Response;
109//# sourceMappingURL=Response.js.map
\No newline at end of file