@glandjs/express
Version:
An Express adapter for Gland HTTP protocol layer, enabling classic middleware-based routing within the Gland architecture solution.
247 lines (246 loc) • 6.11 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExpressContext = void 0;
const http_1 = require("@glandjs/http");
class ExpressContext extends http_1.HttpContext {
constructor(events, req, res) {
super(events, req, res);
this.params = req.params;
this.host = req.host;
}
get body() {
return this.req.body;
}
get path() {
return this.req.path;
}
get xhr() {
return this.req.xhr;
}
get stale() {
return !this.fresh;
}
get fresh() {
return this.req.fresh;
}
get method() {
return this.req.method;
}
get hostname() {
return this.req.hostname;
}
get ip() {
return this.req.ip;
}
get protocol() {
return this.req.protocol;
}
get secure() {
return this.req.secure;
}
get url() {
return this.req.url;
}
get originalUrl() {
return this.req.originalUrl;
}
get query() {
return this.req.query;
}
get subdomains() {
return this.req.subdomains;
}
get accepts() {
return (types) => {
if (!types) {
const result = this.req.accepts() || false;
if (Array.isArray(result)) {
return result[0] || false;
}
return result;
}
if (Array.isArray(types)) {
const result = this.req.accepts(types);
if (Array.isArray(result)) {
return result[0] || false;
}
return typeof result === 'string' ? result : false;
}
const result = this.req.accepts(types);
if (Array.isArray(result)) {
return result[0] || false;
}
return typeof result === 'string' ? result : false;
};
}
get is() {
return (type) => {
return this.req.is(type);
};
}
status(code) {
this.res.status(code);
return this;
}
redirect(url) {
this.res.redirect(url);
return this;
}
setCookie(name, value, options) {
this.res.cookie(name, value, {
domain: options?.domain,
httpOnly: options?.httpOnly,
expires: options?.expires,
maxAge: options?.maxAge,
path: options?.path,
sameSite: options?.sameSite,
secure: options?.secure,
signed: options?.signed,
});
return this;
}
clearCookie(name, options) {
this.res.clearCookie(name, options);
return this;
}
getCookie(name) {
return this.req.cookies?.[name];
}
deleteCookie(name, options) {
this.res.clearCookie(name, options);
}
get cookies() {
return this.req.cookies || {};
}
get signedCookies() {
return this.req.signedCookies || {};
}
send(data, statusCode, headers) {
if (statusCode) {
this.res.status(statusCode);
}
if (headers) {
this.setHeaders(headers);
}
this.res.send(data);
return this;
}
json(data, statusCode, headers) {
if (statusCode) {
this.res.status(statusCode);
}
if (headers) {
this.setHeaders(headers);
}
this.res.json(data);
return this;
}
html(html, statusCode, headers) {
if (statusCode) {
this.res.status(statusCode);
}
if (headers) {
this.setHeaders(headers);
}
this.res.setHeader('Content-Type', 'text/html');
this.res.send(html);
return this;
}
text(text, statusCode, headers) {
if (statusCode) {
this.res.status(statusCode);
}
if (headers) {
this.setHeaders(headers);
}
this.res.setHeader('Content-Type', 'text/plain');
this.res.send(text);
return this;
}
xml(xml, statusCode, headers) {
if (statusCode) {
this.res.status(statusCode);
}
if (headers) {
this.setHeaders(headers);
}
this.res.setHeader('Content-Type', 'application/xml');
this.res.send(xml);
return this;
}
format(formatters) {
this.res.format(formatters);
return this;
}
sendFile(filePath, options = {}, fn) {
const opts = {
...options,
};
this.res.sendFile(filePath, opts, (err) => {
if (err && fn) {
fn(err);
}
});
return this;
}
jsonp(data) {
this.res.jsonp(data);
return this;
}
sse() {
return this;
}
download(filePath, filename, options) {
this.res.download(filePath, filename, options);
return this;
}
render(view, options, callback) {
if (callback) {
this.res.render(view, options, callback);
}
else {
this.res.render(view, options);
}
return this;
}
end() {
this.res.end();
return this;
}
hasHeader(name) {
return this.res.hasHeader(name);
}
getHeader(name) {
return this.res.getHeader(name);
}
setHeader(name, value) {
this.res.setHeader(name, value);
return this;
}
setHeaders(headers) {
Object.entries(headers).forEach(([key, value]) => {
this.res.setHeader(key, value);
});
return this;
}
removeHeader(name) {
this.res.removeHeader(name);
return this;
}
get headers() {
return this.req.headers;
}
vary(fields) {
this.res.vary(fields);
return this;
}
attachment(filename) {
this.res.attachment(filename);
return this;
}
location(url) {
this.res.location(url);
return this;
}
}
exports.ExpressContext = ExpressContext;