UNPKG

2.5 kBTypeScriptView Raw
1import * as React from 'react';
2
3import Types from '../types';
4import { IComponent } from "../types/component";
5import { IInfrastructure } from "../types";
6import middleware, {isMiddleware} from "../middleware/middleware-component";
7import {getChildrenArray} from "../index";
8import { listMiddleware, uploadMiddleware } from './storage-libs';
9
10export const STORAGE_INSTANCE_TYPE = "StorageComponent";
11
12
13/**
14 * Arguments provided by the user
15 */
16export interface IStorageArgs {
17 /**
18 * a unique id or name of the webapp
19 */
20 id: string,
21
22 /**
23 * the relative path of the route, e.g. "/" for the root, or "/something", or "*" for any
24 * Can be a regex to filter the paths of the routes and redirects
25 */
26 path: string
27
28}
29
30/**
31 * properties added programmatically
32 */
33export interface IStorageProps {
34
35 /**
36 * A Webapp component supports middlewares, defines as direct children
37 */
38 middlewares: Array<any>,
39
40
41 /**
42 * A function that the DataLayer provides, it lets the WebApp get the DataLayer Id
43 */
44 setDataLayerId: (dataLayerId: string) => void,
45
46 /**
47 * The id of the datalayer - if the webapp applies to one.
48 * filled by the DataLayer
49 */
50 dataLayerId?: any,
51
52 /**
53 * The http method of the route, e.g. get, post, ...
54 */
55 method: string,
56
57}
58
59
60/**
61 * identifies a component as a DataLayer
62 *
63 * @param component to be tested
64 */
65export function isStorage(component) {
66 return component !== undefined &&
67 component.instanceType === STORAGE_INSTANCE_TYPE
68}
69
70
71
72export default (props: IStorageArgs | any) => {
73
74 const componentProps: IInfrastructure & IComponent = {
75 infrastructureType: Types.INFRASTRUCTURE_TYPE_COMPONENT,
76 instanceType: STORAGE_INSTANCE_TYPE,
77 instanceId: props.id,
78
79 insulatesChildComponent: (child) => {
80 // a webapp insulates (handles itself) middlewares and routes and does not privide to higher levels
81 return isMiddleware(child)
82 }
83 };
84
85 const storageProps: IStorageProps = {
86 middlewares: getChildrenArray(props.children)
87 .filter(child => isMiddleware(child))
88 .concat([
89 listMiddleware(props.id),
90 uploadMiddleware(props.id)
91 ]),
92
93 setDataLayerId: (dataLayerId: string) => {
94 props.dataLayerId = dataLayerId;
95 },
96
97 method: "POST"
98 }
99
100 return Object.assign(props, componentProps, storageProps);
101
102
103};
\No newline at end of file