1 | import React, {ReactNode} from 'react';
|
2 |
|
3 | import Types from '../types';
|
4 | import { IComponent } from "../types/component";
|
5 | import { IInfrastructure } from "../types";
|
6 |
|
7 | import { isMiddleware } from '../middleware/middleware-component';
|
8 | import { getChildrenArray } from '../libs';
|
9 |
|
10 |
|
11 | export const SERVICE_INSTANCE_TYPE = "ServiceComponent";
|
12 |
|
13 |
|
14 | /**
|
15 | * Specifies all the properties that a Client-Component must have
|
16 | */
|
17 | export interface IServiceArgs {
|
18 |
|
19 | /**
|
20 | * a unique id or name of the service
|
21 | */
|
22 | id: string,
|
23 |
|
24 | /**
|
25 | * the relative path of the route, e.g. "/" for the root, or "/something", or "*" for any
|
26 | * Can be a regex to filter the paths of the routes and redirects
|
27 | */
|
28 | path: string,
|
29 |
|
30 | /**
|
31 | * The http method of the route, e.g. get, post, ...
|
32 | */
|
33 | method: string,
|
34 | }
|
35 |
|
36 |
|
37 | /**
|
38 | * specifies the properties that an WebApp-Component has during runtime
|
39 | */
|
40 | export interface IServiceProps {
|
41 |
|
42 | /**
|
43 | * A Webapp component supports middlewares, defines as direct children
|
44 | */
|
45 | middlewares: Array<any>,
|
46 |
|
47 |
|
48 | /**
|
49 | * A function that the DataLayer provides, it lets the WebApp get the DataLayer Id
|
50 | */
|
51 | setDataLayerId: (dataLayerId: string) => void,
|
52 |
|
53 | /**
|
54 | * The id of the datalayer - if the webapp applies to one.
|
55 | * filled by the DataLayer
|
56 | */
|
57 | dataLayerId?: any
|
58 | }
|
59 |
|
60 | /**
|
61 | * identifies a component as a WebApp: it implements all the required fields
|
62 | *
|
63 | * @param component to be tested
|
64 | */
|
65 | export function isService(component) {
|
66 | return component !== undefined && component.instanceType === SERVICE_INSTANCE_TYPE
|
67 | }
|
68 |
|
69 | /**
|
70 | * The WebApp is a client that runs in the browser, SPA or SSR
|
71 | *
|
72 | * @param props
|
73 | */
|
74 | export default (props: IServiceArgs | any) => {
|
75 |
|
76 | //console.log ("webapp: ", props);
|
77 |
|
78 | // the ServiceComponent must have all the properties of IClient
|
79 | const componentProps: IInfrastructure & IComponent = {
|
80 | infrastructureType: Types.INFRASTRUCTURE_TYPE_COMPONENT,
|
81 | instanceType: SERVICE_INSTANCE_TYPE,
|
82 | instanceId: props.id,
|
83 |
|
84 | insulatesChildComponent: (child) => {
|
85 | // a webapp insulates (handles itself) middlewares and routes and does not privide to higher levels
|
86 | return isMiddleware(child)
|
87 | }
|
88 | };
|
89 |
|
90 | const serviceProps: IServiceProps = {
|
91 | middlewares: getChildrenArray(props.children)
|
92 | .filter(child => isMiddleware(child)),
|
93 |
|
94 | setDataLayerId: (dataLayerId: string) => {
|
95 | props.dataLayerId = dataLayerId;
|
96 | }
|
97 | }
|
98 |
|
99 | return Object.assign(props, componentProps, serviceProps);
|
100 |
|
101 | };
|