UNPKG

infrastructure-components

Version:

Infrastructure-Components configure the infrastructure of your React-App as part of your React-Components.

106 lines 5.15 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const v4_1 = __importDefault(require("uuid/v4")); const types_1 = __importDefault(require("../types")); const middleware_component_1 = __importDefault(require("../middleware/middleware-component")); const libs_1 = require("../libs"); const universal_cookie_express_1 = __importDefault(require("universal-cookie-express")); exports.IDENTITY_INSTANCE_TYPE = "IdentityComponent"; exports.getBrowserId = (req, key = exports.IDENTITY_KEY) => { const browserId = req.universalCookies.get(key); if (browserId !== undefined) { return browserId; } else { const newId = v4_1.default(); req.universalCookies.set(key, newId); return newId; } }; exports.IDENTITY_KEY = "IC_IDENTITY_KEY"; /** * The Identity-Component uses cookies to uniquely identify a browser * * @param props */ exports.default = (props) => { console.log("Identity: ", props); const componentProps = { infrastructureType: types_1.default.INFRASTRUCTURE_TYPE_COMPONENT, instanceType: exports.IDENTITY_INSTANCE_TYPE, instanceId: undefined, }; const identityProps = { setStoreData: (storeData) => { //console.log("setStoreData: ",storeData) props.storeData = storeData; }, setGetData: (getData) => { //console.log("setStoreData: ",storeData) props.getData = getData; } }; // if the child needs to store data that belongs to the user, provide a function to do so! libs_1.findComponentRecursively(props.children, (child) => child.setStoreIdentityData !== undefined).forEach(child => { child.setStoreIdentityData(function (request, secondaryKey, val, jsonData) { return __awaiter(this, void 0, void 0, function* () { //console.log("identity: storeData: ", props); return yield props.storeData(exports.IDENTITY_KEY, //pkEntity: string, exports.getBrowserId(request, exports.IDENTITY_KEY), //pkVal: any, secondaryKey, //skEntity: string, val, //skVal: any, jsonData //: any ); }); }); child.setGetIdentityData(function (request, matchBrowserIdentity, secondaryKey, val) { return __awaiter(this, void 0, void 0, function* () { //console.log("identity: storeData: ", props); return yield props.getData(exports.IDENTITY_KEY, //pkEntity: string, matchBrowserIdentity ? exports.getBrowserId(request, exports.IDENTITY_KEY) : undefined, //pkVal: any, secondaryKey, //skEntity: string, val //skVal: any, ); }); }); }); /** * The Identity requires cookies to store an uuid */ const mappedChildren = { // we provide the middlewares that we require children: [ // we need to use cookies in order to verify whether a user is logged in middleware_component_1.default({ callback: universal_cookie_express_1.default() }), // here we provide all interested children with the identity - on server side only! // but for the browser, we provide the cookie middleware_component_1.default({ callback: (req, res, next) => { console.log("this it the identity-mw"); libs_1.findComponentRecursively(props.children, (child) => child.setIdentity !== undefined).forEach(child => { child.setIdentity(exports.getBrowserId(req, exports.IDENTITY_KEY)); }); return next(); } }) ].concat(props.children) }; //console.log("mapped children: ", mappedChildren.children.filter(c=> isWebApp(c)).map(c=> c.routes.map(r=>r.middlewares))); //console.log("identity children: ", findComponentRecursively(props.children, isWebApp)); //console.log("identity mapped children: ", findComponentRecursively(mappedChildren.children, isWebApp)); return Object.assign(props, componentProps, identityProps, mappedChildren); }; exports.isIdentity = (component) => { return component !== undefined && component.instanceType === exports.IDENTITY_INSTANCE_TYPE; }; //# sourceMappingURL=identity-component.js.map