UNPKG

@midware/mauth

Version:

A simple auth middleware for Node.js

75 lines 2.55 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getPath = exports.genPath = exports.addPath = exports.request = void 0; const utils_1 = require("./utils"); const jsonWebToken_1 = __importDefault(require("./jsonWebToken")); const crud = { post: 'create', put: 'update', patch: 'update', get: 'read', delete: 'delete', }; const genPath = (name, value) => { return value !== undefined && value !== null ? Array.isArray(value) ? name + '%5B%5D=' + value.map((av) => encodeURI(av.toString())).join('&' + name + '%5B%5D=') : name + '=' + encodeURI(value.toString()) : undefined; }; exports.genPath = genPath; const getPath = (oldPath) => { return oldPath !== undefined && oldPath !== null && oldPath?.length > 0 ? `${oldPath}&` : '?'; }; exports.getPath = getPath; const addPath = (oldPath, addPath) => { return addPath !== undefined && addPath !== null ? getPath(oldPath) + addPath : oldPath; }; exports.addPath = addPath; const request = async (method, host, domain, service, query, input, endpoint, config = {}, out) => { try { const user = { id: '000000000000000000000000', givenName: process.env.SERVICE_NAME, familyName: process.env.INSTANCE, identification: process.env.SERVICE_NAME, type: 'API', permissions: { auth: { signIn: ['all'], }, }, instances: ['all'], }; user.permissions[domain] = {}; user.permissions[domain][service] = [crud[method]]; const token = await jsonWebToken_1.default.getInstance().sign(user); config.headers = { Type: 'application/json', Accept: 'application/json', Authorization: `Bearer ${token}`, ...(config.headers || {}), }; if (host && host.length > 0) { const response = await (0, utils_1.request)(method, host, endpoint || `/${domain}/${service}${query || ''}`, input, config, out); return response; } else throw new Error('Missing Params'); } catch (error) { error.data = out; throw error; } }; exports.request = request; //# sourceMappingURL=smartRequest.js.map