mirror of
https://github.com/Wessel/kirbe.git
synced 2026-07-14 12:33:39 +02:00
44 lines
1.1 KiB
JavaScript
44 lines
1.1 KiB
JavaScript
module.exports = class KirbeResponse {
|
|
constructor(res) {
|
|
this.coreRes = res;
|
|
this.headers = {};
|
|
|
|
this.statusCode = 200;
|
|
this.statusMessage = null;
|
|
|
|
this.data = Buffer.alloc(0);
|
|
}
|
|
|
|
body(body) {
|
|
if (typeof body === 'object' && !Buffer.isBuffer(body)) {
|
|
if (!this.headers['content-type']) this.headers['content-type'] = 'application/json'; this.data = JSON.stringify(body);
|
|
} else this.data = body;
|
|
return this;
|
|
}
|
|
|
|
header(a, b) {
|
|
if (typeof a === 'object') Object.keys(a).forEach((v) => this.headers[v.toLowerCase()] = a[v]);
|
|
else this.headers[a.toLowerCase()] = b;
|
|
return this;
|
|
}
|
|
|
|
status(code, message) {
|
|
this.statusCode = code;
|
|
this.statusMessage = message;
|
|
return this;
|
|
}
|
|
|
|
writeHead(status, headers) {
|
|
if (status) this.status(status);
|
|
if (headers) this.header(headers);
|
|
}
|
|
|
|
end(data) {
|
|
if (data) this.body(data);
|
|
if (this.statusMessage) this.coreRes.writeHead(this.statusCode, this.statusMessage, this.headers);
|
|
else this.coreRes.writeHead(this.statusCode, this.headers);
|
|
this.coreRes.end(this.data);
|
|
return this;
|
|
}
|
|
};
|