infrastructure-components
Version:
Infrastructure-Components configure the infrastructure of your React-App as part of your React-Components.
78 lines (52 loc) • 1.77 kB
text/typescript
import React, {ReactNode} from 'react';
import {IComponent} from "../types/component";
import Types, { IInfrastructure } from "../types";
import { isMiddleware } from '../middleware/middleware-component';
import {getChildrenArray, findComponentRecursively} from '../libs';
export const ROUTE_INSTANCE_TYPE = "RouteComponent";
/**
* Specifies all the properties that a Route-Component must have
*/
export interface IRouteArgs {
path: string,
name: string,
render?: any,
component?: any
}
/**
* specifies the properties that an Route-Component has during runtime
*/
export interface IRouteProps {
/**
* A route component supports middlewares, defines as direct children
*/
middlewares: Array<any>,
exact: boolean,
isSecured?: boolean
}
/**
* The WebApp is a client that runs in the browser, SPA or SSR
*
* @param props
*/
export default (props: IRouteArgs | any) => {
//console.log ("route: ",props );
const componentProps: IInfrastructure & IComponent = {
infrastructureType: Types.INFRASTRUCTURE_TYPE_COMPONENT,
instanceType: ROUTE_INSTANCE_TYPE,
instanceId: undefined, // middlewares cannot be found programmatically!
insulatesChildComponent: (child) => {
// a route insulates (handles itself) middlewares and does not privide to higher levels
return isMiddleware(child)
}
};
const routeProps: IRouteProps = {
middlewares:findComponentRecursively(props.children, isMiddleware),
exact: true
}
return Object.assign(props, componentProps, routeProps);
};
export const isRoute = (component) => {
return component !== undefined &&
component.instanceType === ROUTE_INSTANCE_TYPE;
};