1 | import React, { ReactNode } from 'react';
|
2 |
|
3 | import { StaticRouter, matchPath } from 'react-router';
|
4 | import { Switch, Route, BrowserRouter, Link } from 'react-router-dom';
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | import RedirectWithStatus from './redirect-w-status';
|
10 | import AttachRequest from '../dist/components/attach-request';
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | export interface IRoute {
|
18 |
|
19 | |
20 |
|
21 |
|
22 | path: string,
|
23 |
|
24 | |
25 |
|
26 |
|
27 | method: string,
|
28 |
|
29 | |
30 |
|
31 |
|
32 | middlewares: Array<any>,
|
33 |
|
34 |
|
35 | |
36 |
|
37 |
|
38 |
|
39 |
|
40 | render?: (props: any) => ReactNode,
|
41 |
|
42 | component?: any,
|
43 |
|
44 | |
45 |
|
46 |
|
47 | name: string,
|
48 |
|
49 | |
50 |
|
51 |
|
52 | exact: boolean,
|
53 |
|
54 | |
55 |
|
56 |
|
57 | customType?: any
|
58 |
|
59 | }
|
60 |
|
61 | export interface IRedirect {
|
62 | |
63 |
|
64 |
|
65 | from: string,
|
66 |
|
67 | |
68 |
|
69 |
|
70 | to: string,
|
71 |
|
72 | |
73 |
|
74 |
|
75 | status: number
|
76 | }
|
77 |
|
78 | interface RoutedAppProps {
|
79 | routes: Array<IRoute>,
|
80 | redirects: Array<IRedirect>
|
81 | };
|
82 |
|
83 |
|
84 | const RoutedApp: React.SFC<RoutedAppProps> = (props) => {
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | const routes = props.routes.map(({ path, exact, component, render }, i) => {
|
90 | console.log("routepath: ", path)
|
91 |
|
92 | return render !== undefined ? <Route key={'ROUTE_'+i} exact={exact} path={path} render={render} />:
|
93 | <Route key={'ROUTE_'+i} exact={exact} path={path} component={component} />
|
94 | });
|
95 |
|
96 | const redirects = props.redirects.map(({ from, to, status }, i) =>
|
97 | <RedirectWithStatus key={'REDIRECT_'+i} from={from} to={to} status={status} />
|
98 | );
|
99 |
|
100 |
|
101 | return <Switch>
|
102 | {routes}
|
103 | {redirects}
|
104 | </Switch>;
|
105 |
|
106 | };
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | export const createClientApp = (routes: Array<IRoute>, redirects: Array<IRedirect>, basename: string) => {
|
117 | return <BrowserRouter basename={basename}>
|
118 | <AttachRequest>
|
119 | <RoutedApp routes={routes} redirects={redirects}/>
|
120 | </AttachRequest>
|
121 | </BrowserRouter>;
|
122 | };
|
123 |
|
124 | export const createServerApp = (
|
125 | routes: Array<IRoute>,
|
126 | redirects: Array<IRedirect>,
|
127 | basename: string,
|
128 | url: string,
|
129 | context: any,
|
130 | request: any) => {
|
131 |
|
132 | return <StaticRouter context={context} location={url} basename={basename}>
|
133 | <AttachRequest request={request}>
|
134 | <RoutedApp routes={routes} redirects={redirects}/>
|
135 | </AttachRequest>
|
136 | </StaticRouter>;
|
137 | }; |
\ | No newline at end of file |