Skip to content

Commit 9c0f612

Browse files
authored
Merge pull request #2 from gigantz/ojr/components-types
Renderer, Components moved to lib and propTypes added
2 parents 333a2d4 + bab88f6 commit 9c0f612

File tree

15 files changed

+138
-36
lines changed

15 files changed

+138
-36
lines changed

lib/components/App.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from "react";
2+
import PropTypes from "prop-types";
3+
4+
export const App = ({ children, port }) => <app port={port}>{children}</app>;
5+
6+
App.propTypes = {
7+
port: PropTypes.number,
8+
children: PropTypes.node,
9+
};

lib/components/Router.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React from "react";
2+
import PropTypes from "prop-types";
3+
4+
export const Router = ({
5+
path,
6+
caseSensitive,
7+
mergeParams,
8+
strict,
9+
children,
10+
}) => (
11+
<router
12+
path={path}
13+
caseSensitive={!!caseSensitive}
14+
mergeParams={!!mergeParams}
15+
strict={!!strict}
16+
>
17+
{children}
18+
</router>
19+
);
20+
21+
Router.propTypes = {
22+
path: PropTypes.string.isRequired,
23+
caseSensitive: PropTypes.bool,
24+
mergeParams: PropTypes.bool,
25+
strict: PropTypes.bool,
26+
children: PropTypes.node,
27+
};

lib/components/Routes.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import React from "react";
2+
import PropTypes from "prop-types";
3+
import { METHODS } from "../renderer/helpers";
4+
5+
const BaseRoute = (method) => {
6+
const RouteComponent = ({ path, content, children }) => (
7+
<route method={method} path={path} content={content}>
8+
{children}
9+
</route>
10+
);
11+
12+
RouteComponent.propTypes = {
13+
path: PropTypes.string,
14+
content: PropTypes.any,
15+
handler: PropTypes.func,
16+
};
17+
18+
return RouteComponent;
19+
};
20+
21+
export const Get = BaseRoute("get");
22+
export const Post = BaseRoute("post");
23+
export const Put = BaseRoute("put");
24+
export const Head = BaseRoute("head");
25+
export const Delete = BaseRoute("delete");
26+
export const Options = BaseRoute("options");
27+
export const Trace = BaseRoute("trace");
28+
export const Copy = BaseRoute("copy");
29+
export const Lock = BaseRoute("lock");
30+
export const Mkcol = BaseRoute("mkcol");
31+
export const Move = BaseRoute("move");
32+
export const Purge = BaseRoute("purge");
33+
export const Propfind = BaseRoute("propfind");
34+
export const Proppatch = BaseRoute("proppatch");
35+
export const Unlock = BaseRoute("unlock");
36+
export const Report = BaseRoute("report");
37+
export const Mkactivity = BaseRoute("mkactivity");
38+
export const Checkout = BaseRoute("checkout");
39+
export const Merge = BaseRoute("merge");
40+
export const Msearch = BaseRoute("m-search");
41+
export const Notify = BaseRoute("notify");
42+
export const Subscribe = BaseRoute("subscribe");
43+
export const Unsubscribe = BaseRoute("unsubscribe");
44+
export const Patch = BaseRoute("patch");
45+
export const Search = BaseRoute("search");
46+
export const Connect = BaseRoute("connect");

lib/components/Static.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React from "react";
2+
import PropTypes from "prop-types";
3+
4+
export const Static = ({ publicPath, path, options }) => (
5+
<static publicPath={publicPath} path={path} options={options} />
6+
);
7+
8+
Static.propTypes = {
9+
publicPath: PropTypes.string.isRequired,
10+
path: PropTypes.string,
11+
options: PropTypes.object,
12+
};

lib/components/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from "./App";
2+
export * from "./Static";
3+
export * from "./Router";
4+
export * from "./Routes";

lib/context.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { createContext } from "react";
2+
3+
export const ReqResContext = createContext({ req: null, res: null });

lib/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from "./renderer";
2+
export * from "./components";
3+
export * from "./context";
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ import React from "react";
22
import { renderToString } from "react-dom/server";
33
import { ServerStyleSheet } from "styled-components";
44
import { Helmet } from "react-helmet";
5-
import { context } from "../context";
5+
import { ReqResContext } from "../context";
66

77
const sheet = new ServerStyleSheet();
88

99
function renderPage(Component, req, res) {
1010
const app = renderToString(
1111
sheet.collectStyles(
12-
<context.Provider value={{ req, res }}>
12+
<ReqResContext.Provider value={{ req, res }}>
1313
<Component />
14-
</context.Provider>
14+
</ReqResContext.Provider>
1515
)
1616
);
1717
const styleTags = sheet.getStyleTags();
@@ -37,8 +37,8 @@ function renderPage(Component, req, res) {
3737
res.send(html);
3838
}
3939

40-
export function generateRoute(parentInstance, method, props) {
41-
parentInstance.routerInstance[method](
40+
export function generateRoute(parentInstance, props) {
41+
parentInstance.routerInstance[props.method](
4242
props.path || "/",
4343
...[
4444
...(props.middlewares || []),
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ export const colors = {
3434
export function log(type, msg) {
3535
switch (type) {
3636
case "success":
37-
console.log(`${colors.fg.green}${colors.bright}${msg}${colors.reset}`);
37+
console.log(
38+
`${colors.fg.green}${colors.bright}[ReactXpress] ${msg}${colors.reset}`
39+
);
3840
break;
3941
case "warn":
40-
console.log(`${colors.fg.yellow}${msg}${colors.reset}`);
42+
console.log(`${colors.fg.yellow}[ReactXpress] ${msg}${colors.reset}`);
4143
break;
4244
default:
43-
console.log(msg);
45+
console.log(`[ReactXpress] ${msg}`);
4446
break;
4547
}
4648
}
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ let reconciler = ReactReconciler({
4242
return { routerInstance: router, path: props.path };
4343
}
4444

45-
if (METHODS.includes(type)) {
45+
if (type === "route") {
4646
return {
47-
method: type,
47+
type,
4848
props,
4949
};
5050
}
@@ -73,8 +73,8 @@ let reconciler = ReactReconciler({
7373
return;
7474
}
7575

76-
if (child.method) {
77-
generateRoute(parentInstance, child.method, child.props);
76+
if (child.type === "route") {
77+
generateRoute(parentInstance, child.props);
7878
return;
7979
}
8080

@@ -158,12 +158,10 @@ let reconciler = ReactReconciler({
158158
clearContainer(container, child) {},
159159
});
160160

161-
let ReactExpress = {
161+
export const ReactXpress = {
162162
render(app) {
163163
log("success", `starting...`);
164164
let container = reconciler.createContainer(null, false, false);
165165
reconciler.updateContainer(app, container, null, null);
166166
},
167167
};
168-
169-
export default ReactExpress;

0 commit comments

Comments
 (0)