UNPKG

ckn.backend

Version:

CKN Framework for Backend

73 lines (63 loc) 1.9 kB
import {Log} from 'ckn.core'; class Session { #request = null; get request() { return this.#request; } #response = null; get response() { return this.#response; } constructor(req, res) { this.#request = req; this.#response = res; } async send(data, status = null) { if (status != null && typeof status === "number") { this.#response.status(status); } if (data != null) { this.#response.send(data); } } redirect(url) { this.#response.redirect(url); } get fullUrl() { let fullUrl = this.#request.protocol + '://' + this.#request.get('host') + this.#request.originalUrl; return fullUrl; } get url() { let fullUrl = this.#request.protocol + '://' + this.#request.get('host'); return fullUrl; } cookies = { set: (key, value) => { this.#response.cookie(key, value, { maxAge: 60*1000*60, httpOnly: true, secure: false//process.env.NODE_ENV === 'production' ? true : false } ); }, get: (key) => { // We extract the raw cookies from the request headers let rawCookies = this.#request.headers.cookie; if (rawCookies != null) { rawCookies = rawCookies.split('; '); const parsedCookies = {}; rawCookies.forEach(rawCookie => { const parsedCookie = rawCookie.split('='); parsedCookies[parsedCookie[0]] = parsedCookie[1]; }); return parsedCookies[key]; } return null; }, remove: (key) => { this.#response.clearCookie(key); } } } export {Session}