1 | import React, {ReactNode} from 'react';
|
2 |
|
3 |
|
4 | import uuidv4 from 'uuid/v4';
|
5 |
|
6 | import {IComponent} from "../types/component";
|
7 | import Types, { IInfrastructure } from "../types";
|
8 |
|
9 | import createMiddleware, { isMiddleware } from '../middleware/middleware-component';
|
10 | import createWebApp, { isWebApp } from '../webapp/webapp-component';
|
11 |
|
12 | import { getChildrenArray, findComponentRecursively } from '../libs';
|
13 | import cookiesMiddleware from 'universal-cookie-express';
|
14 |
|
15 | export const IDENTITY_INSTANCE_TYPE = "IdentityComponent";
|
16 |
|
17 | export const getBrowserId = (req, key=IDENTITY_KEY) => {
|
18 | const browserId = req.universalCookies.get(key);
|
19 |
|
20 | if (browserId !== undefined) {
|
21 | return browserId;
|
22 |
|
23 | } else {
|
24 | const newId = uuidv4();
|
25 | req.universalCookies.set(key, newId);
|
26 | return newId;
|
27 | }
|
28 | }
|
29 |
|
30 |
|
31 |
|
32 | export const IDENTITY_KEY ="IC_IDENTITY_KEY";
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | export interface IIdentityArgs {
|
38 |
|
39 |
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | export interface IIdentityProps {
|
47 |
|
48 | |
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | setStoreData: (
|
55 | storeData: (pkEntity: string, pkVal: any, skEntity: string, skVal: any, jsonData: any) => void
|
56 | ) => void
|
57 |
|
58 | storeData?: (pkEntity: string, pkVal: any, skEntity: string, skVal: any, jsonData: any) => void,
|
59 |
|
60 | setGetData: (
|
61 | storeData: (pkEntity: string, pkVal: any, skEntity: string, skVal: any) => any
|
62 | ) => void
|
63 |
|
64 | getData?: (pkEntity: string, pkVal: any, skEntity: string, skVal: any) => any
|
65 | }
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | export default (props: IIdentityArgs | any) => {
|
74 |
|
75 |
|
76 |
|
77 | const componentProps: IInfrastructure & IComponent = {
|
78 | infrastructureType: Types.INFRASTRUCTURE_TYPE_COMPONENT,
|
79 | instanceType: IDENTITY_INSTANCE_TYPE,
|
80 | instanceId: undefined,
|
81 | };
|
82 |
|
83 |
|
84 | const identityProps: IIdentityProps = {
|
85 | setStoreData: (storeData: (pkEntity: string, pkVal: any, skEntity: string, skVal: any, jsonData: any) => void) => {
|
86 |
|
87 | props.storeData = storeData;
|
88 | },
|
89 |
|
90 | setGetData: (getData: (pkEntity: string, pkVal: any, skEntity: string, skVal: any) => void) => {
|
91 |
|
92 | props.getData = getData;
|
93 | }
|
94 | }
|
95 |
|
96 |
|
97 | findComponentRecursively(props.children, (child) => child.setStoreIdentityData !== undefined).forEach( child => {
|
98 |
|
99 | child.setStoreIdentityData(
|
100 |
|
101 | async function (request: any, secondaryKey: string, val: any, jsonData: any) {
|
102 |
|
103 | return await props.storeData(
|
104 | IDENTITY_KEY,
|
105 | getBrowserId(request, IDENTITY_KEY),
|
106 | secondaryKey,
|
107 | val,
|
108 | jsonData
|
109 | )
|
110 | }
|
111 | );
|
112 |
|
113 | child.setGetIdentityData(
|
114 |
|
115 | async function (request: any, matchBrowserIdentity: boolean, secondaryKey: string, val: any) {
|
116 |
|
117 | return await props.getData(
|
118 | IDENTITY_KEY,
|
119 | matchBrowserIdentity ? getBrowserId(request, IDENTITY_KEY) : undefined,
|
120 | secondaryKey,
|
121 | val
|
122 | )
|
123 | }
|
124 | );
|
125 |
|
126 | });
|
127 |
|
128 |
|
129 | |
130 |
|
131 |
|
132 | const mappedChildren = {
|
133 |
|
134 | children: [
|
135 |
|
136 |
|
137 | createMiddleware({ callback: cookiesMiddleware() }),
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | createMiddleware({ callback: (req, res, next) => {
|
143 |
|
144 |
|
145 | findComponentRecursively(props.children, (child) => child.setIdentity !== undefined).forEach( child => {
|
146 | child.setIdentity(getBrowserId(req, IDENTITY_KEY));
|
147 | });
|
148 |
|
149 | return next();
|
150 | }})
|
151 |
|
152 | ].concat(props.children)
|
153 | };
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | return Object.assign(props, componentProps, identityProps, mappedChildren);
|
161 |
|
162 |
|
163 | };
|
164 |
|
165 | export const isIdentity = (component) => {
|
166 |
|
167 | return component !== undefined &&
|
168 | component.instanceType === IDENTITY_INSTANCE_TYPE;
|
169 | }; |
\ | No newline at end of file |