UNPKG

@powership/server

Version:
60 lines (59 loc) 1.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.__LOCAL_DEV_USERID__ = exports.ServerRequest = void 0; var _httpErrors = _interopRequireDefault(require("http-errors")); var _BaseRequestHandler = require("./BaseRequestHandler.cjs"); var _ServerLogs = require("./ServerLogs.cjs"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } const { Unauthorized } = _httpErrors.default; let __LOCAL_DEV_USERID__ = exports.__LOCAL_DEV_USERID__ = process.env.NODE_ENV === 'development' ? '__LOCAL_DEV_USERID__' : null; class ServerRequest extends _BaseRequestHandler.BaseRequestHandler { constructor(input) { super(input); this.input = input; const { locals = {}, userId, permissions = [] } = input; this.locals = locals; this._userId = userId; this._permissions = new Set(permissions); } getPermissions = () => { return [...this._permissions.values()]; }; userIdOptional() { return this._userId || __LOCAL_DEV_USERID__ || undefined; } assertPermission(permission) { const userId = this.userId(); if (this.hasPermission(permission)) return true; _ServerLogs.ServerLogs.error('Unauthorized', { userId, permission }); throw new Unauthorized(); } hasPermission(permission) { if (__LOCAL_DEV_USERID__) return true; return this._permissions.has(permission); } userId(strict = true) { const userId = this.userIdOptional(); if (userId) return userId; if (strict) { throw new Unauthorized(); } return ''; } static create = input => { return new ServerRequest(input); }; } exports.ServerRequest = ServerRequest; //# sourceMappingURL=ServerRequest.cjs.map