UNPKG

1.81 kBPlain TextView Raw
1import React, {ReactNode} from 'react';
2
3import {IComponent} from "../types/component";
4import Types, { IInfrastructure } from "../types";
5
6import { isMiddleware } from '../middleware/middleware-component';
7import {getChildrenArray, findComponentRecursively} from '../libs';
8
9
10export const ROUTE_INSTANCE_TYPE = "RouteComponent";
11
12
13/**
14 * Specifies all the properties that a Route-Component must have
15 */
16export interface IRouteArgs {
17
18 path: string,
19
20 name: string,
21
22 render?: any,
23
24 component?: any,
25
26 exact?: boolean,
27}
28
29
30/**
31 * specifies the properties that an Route-Component has during runtime
32 */
33export interface IRouteProps {
34
35 /**
36 * A route component supports middlewares, defines as direct children
37 */
38 middlewares: Array<any>,
39
40
41
42 isSecured?: boolean
43}
44
45
46/**
47 * The WebApp is a client that runs in the browser, SPA or SSR
48 *
49 * @param props
50 */
51export default (props: IRouteArgs | any) => {
52
53 //console.log ("route: ",props );
54
55 const componentProps: IInfrastructure & IComponent = {
56 infrastructureType: Types.INFRASTRUCTURE_TYPE_COMPONENT,
57 instanceType: ROUTE_INSTANCE_TYPE,
58 instanceId: undefined, // middlewares cannot be found programmatically!
59
60 insulatesChildComponent: (child) => {
61 // a route insulates (handles itself) middlewares and does not privide to higher levels
62 return isMiddleware(child)
63 }
64 };
65
66 const routeProps: IRouteProps = {
67 middlewares:findComponentRecursively(props.children, isMiddleware),
68 }
69
70 return Object.assign({}, props, componentProps, routeProps, {exact: props.exact === undefined ? true : props.exact});
71
72
73};
74
75export const isRoute = (component) => {
76
77 return component !== undefined &&
78 component.instanceType === ROUTE_INSTANCE_TYPE;
79};
\No newline at end of file