UNPKG

@ords/modules

Version:

Modules for ords-core microservices based upon proposals

257 lines (256 loc) 8.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const core_1 = require("@ords/core"); const rxjs_1 = require("rxjs"); const express = require("express"); const bodyParser = require("body-parser"); /** * Express REST map connector */ class MapExpress { /** * Construct new instance on the specific set of services */ constructor(mserver, connector) { /** * Maps to instance of db */ this.root = 'auth'; // create router on instance should it map differently connector.maps.push(express.Router().use(this.validate.bind(this))); connector.maps.push(express.Router().post('/auth/signin/', bodyParser.urlencoded({ extended: true }), bodyParser.json(), this.signIn.bind(this))); connector.maps.push(express.Router().post('/auth/signup/', bodyParser.urlencoded({ extended: true }), bodyParser.json(), this.signUp.bind(this))); connector.maps.push(express.Router().get('/auth/signout/', this.signOut.bind(this))); connector.maps.push(express.Router().get('/auth/remove/', this.remove.bind(this))); connector.maps.push(express.Router().get('/auth/patch/', this.patch.bind(this))); // save instance ref this.msever = mserver; } /** * Log in a user based upon password and username */ signIn(req, res, next) { // check if fields exists in body if (req.body.meta === undefined) { res.send(400); // if everything is a okay then processed } else { // create an observable request let request = { package: rxjs_1.Observable.pairs(req.body.meta), auth: req.auth }; // perform the action core_1.ShortenAct.tryCatch(this.msever, this.root, 'signin', request, (err, out, meta) => { if (err) { res.status(404).send(err.toString()); } else { // set header from meta Object.keys(meta).forEach((key) => { // bind key values of meta res.header(key, meta[key]); }); // check datatype of results if (Number.isInteger(out)) { res.send(out.toString()); } else { res.send(out); } } }); } } /** * Sign up a user based upon password and username */ signUp(req, res, next) { // check if fields exists in body if (req.body.meta === undefined) { res.send(400); // if everything is a okay then processed } else { // create an observable request let request = { package: rxjs_1.Observable.pairs(req.body), auth: req.auth }; // perform the action core_1.ShortenAct.tryCatch(this.msever, this.root, 'signup', request, (err, out, meta) => { if (err) { res.status(404).send(err.toString()); } else { // set header from meta Object.keys(meta).forEach((key) => { // bind key values of meta res.header(key, meta[key]); }); // check datatype of results if (Number.isInteger(out)) { res.send(out.toString()); } else { res.send(out); } } }); } } /** * Log in a user based upon password and username */ signOut(req, res, next) { // check session if (req.headers.authorization !== undefined) { // split into parts var parts = req.headers.authorization; parts = parts.split(' '); // create an observable request let request = { package: rxjs_1.Observable.pairs({ session: parts[1] }), auth: req.auth }; // perform the action core_1.ShortenAct.tryCatch(this.msever, this.root, 'signout', request, (err, out, meta) => { if (err) { res.status(404).send(err.toString()); } else { // set header from meta Object.keys(meta).forEach((key) => { // bind key values of meta res.header(key, meta[key]); }); // check datatype of results if (Number.isInteger(out)) { res.send(out.toString()); } else { res.send(out); } } }); // could not sign out since no session specified } else { res.send(400); } } /** * Remove the current user */ remove(req, res, next) { // create an observable request let request = { package: rxjs_1.Observable.pairs({ account: req.auth }), auth: req.auth }; // perform the action core_1.ShortenAct.tryCatch(this.msever, this.root, 'remove', request, (err, out, meta) => { if (err) { res.status(404).send(err.toString()); } else { // set header from meta Object.keys(meta).forEach((key) => { // bind key values of meta res.header(key, meta[key]); }); // check datatype of results if (Number.isInteger(out)) { res.send(out.toString()); } else { res.send(out); } } }); } /** * Patch the current user */ patch(req, res, next) { // check if fields exists in body if (req.body.meta === undefined) { res.send(400); // if everything is a okay then processed } else { // create an observable request let request = { package: rxjs_1.Observable.pairs({ user: req.auth, meta: req.body.meta }), auth: req.auth }; // perform the action core_1.ShortenAct.tryCatch(this.msever, this.root, 'patch', request, (err, out, meta) => { if (err) { res.status(404).send(err.toString()); } else { // set header from meta Object.keys(meta).forEach((key) => { // bind key values of meta res.header(key, meta[key]); }); // check datatype of results if (Number.isInteger(out)) { res.send(out.toString()); } else { res.send(out); } } }); } } /** * Remove the current user */ validate(req, res, next) { // if no header is set go next as auth is undefined if (req.headers.authorization === undefined) { next(); // if everything is a okay then processed } else { // split into parts var parts = req.headers.authorization; parts = parts.split(' '); // create an observable request let request = { package: rxjs_1.Observable.pairs({ session: parts[1], }), auth: req.auth }; // perform the action core_1.ShortenAct.tryCatch(this.msever, this.root, 'validate', request, (err, out, meta) => { if (err) { res.status(404).send(err.toString()); } else { // set header from meta Object.keys(meta).forEach((key) => { // bind key values of meta res.header(key, meta[key]); }); // set auth req.auth = out; next(); } }); } } } exports.MapExpress = MapExpress;