Skip to content

Commit

Permalink
[major] return context to handle route
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip committed Aug 2, 2018
1 parent 528ddaf commit 4a8151f
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 26 deletions.
4 changes: 3 additions & 1 deletion packages/electrode-react-webapp/lib/async-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ class AsyncTemplate {
await r.afterRender(context);
}

return context.isVoidStop ? context.voidResult : result;
context.result = context.isVoidStop ? context.voidResult : result;

return context;
}

/*
Expand Down
10 changes: 7 additions & 3 deletions packages/electrode-react-webapp/lib/express/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ const { htmlifyError } = require("../utils");

const DefaultHandleRoute = (request, response, handler, content, routeOptions) => {
return handler({ content, mode: request.query.__mode || "", request })
.then(data => {
const status = data.status;
.then(context => {
const data = context.result;

if (data instanceof Error) {
throw data;
} else if (status === undefined) {
}

const status = data.status;

if (status === undefined) {
response.send(data);
} else if (HttpStatus.redirect[status]) {
response.redirect(status, data.path);
Expand Down
35 changes: 24 additions & 11 deletions packages/electrode-react-webapp/lib/hapi/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

/* eslint-disable no-magic-numbers, max-params */
/* eslint-disable no-magic-numbers, max-params, max-statements */

const _ = require("lodash");
const assert = require("assert");
Expand All @@ -15,22 +15,35 @@ const DefaultHandleRoute = (request, reply, handler, content, routeOptions) => {
template: request.indexPageTemplate,
request
})
.then(data => {
const status = data.status;
.then(context => {
const data = context.result;

if (data instanceof Error) {
throw data;
} else if (status === undefined) {
reply(data);
}

let respond;
let status = data.status;

if (status === undefined) {
status = 200;
respond = reply(data);
} else if (HttpStatus.redirect[status]) {
reply.redirect(data.path);
} else if (HttpStatus.displayHtml[status]) {
reply(data.html !== undefined ? data.html : data).code(status);
} else if (status >= 200 && status < 300) {
reply(data.html !== undefined ? data.html : data);
respond = reply.redirect(data.path);
return respond;
} else if (HttpStatus.displayHtml[status] || (status >= 200 && status < 300)) {
respond = reply(data.html !== undefined ? data.html : data);
} else {
reply(data).code(status);
respond = reply(data);
}

const response = context.user && context.user.response;

if (response) {
Object.assign(respond.headers, response.headers);
}

return respond.code(status);
})
.catch(err => {
reply(htmlifyError(err, routeOptions.replyErrorStack)).code(err.status || 500);
Expand Down
10 changes: 7 additions & 3 deletions packages/electrode-react-webapp/lib/koa/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ function DefaultHandleRoute(handler, content, routeOptions) {
};

return handler({ content, mode: request.query.__mode || "", request })
.then(data => {
const status = data.status;
.then(context => {
const data = context.result;

if (data instanceof Error) {
throw data;
} else if (status === undefined) {
}

const status = data.status;

if (status === undefined) {
respond(200, data);
} else if (HttpStatus.redirect[status]) {
this.redirect(data.path);
Expand Down
3 changes: 3 additions & 0 deletions packages/electrode-react-webapp/lib/react/token-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ module.exports = function setup(handlerContext /* , asyncTemplate */) {
context.setOutputTransform(transformOutput);
context.user = {
request: options.request,
response: {
headers: {}
},
routeOptions,
routeData,
content,
Expand Down
1 change: 1 addition & 0 deletions packages/electrode-react-webapp/test/data/index-1.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<!--%{PAGE_TITLE}-->
<!--%{CRITICAL_CSS}-->
<!--%{WEBAPP_HEADER_BUNDLES}-->
<!--%{ user-header-token }-->
</head>

<body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ module.exports = () => {
});
},

"user-header-token": context => {
context.user.response.headers = {
"x-foo-bar": "hello-world"
};
},

PAGE_TITLE: () => {
return "<title>user-handler-title</title>";
}
Expand Down
5 changes: 3 additions & 2 deletions packages/electrode-react-webapp/test/spec/hapi.index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ describe("hapi electrode-react-webapp", () => {
});
});

it("should use top level htmlFile", () => {
it("should use top level htmlFile and return response headers", () => {
configOptions.prodBundleBase = "http://awesome-cdn.com/myapp/";
configOptions.stats = "test/data/stats-test-one-bundle.json";
configOptions.htmlFile = "test/data/index-1.html";
Expand All @@ -569,7 +569,6 @@ describe("hapi electrode-react-webapp", () => {
url: "/"
})
.then(res => {
expect(res.statusCode).to.equal(200);
expect(res.result).includes(`<title>user-handler-title</title>`);
expect(res.result).includes(`</script><div>user-promise-token</div><script`);
expect(res.result).includes(
Expand All @@ -578,6 +577,8 @@ describe("hapi electrode-react-webapp", () => {
expect(res.result).includes(
`</script><div>from custom-1</div><div>user-token-1</div><div>user-token-2</div><noscript>` // eslint-disable-line
);
expect(res.headers["x-foo-bar"]).to.equal("hello-world");
expect(res.statusCode).to.equal(200);
stopServer(server);
})
.catch(err => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe("react-webapp", function() {
const promise = handleRoute({ request: {}, content: { status: 200, html: "" } });

return promise
.then(result => {
.then(context => {
intercept.restore();
const expected = `
from wants next module
Expand All @@ -76,7 +76,7 @@ from wants next module
not found
from string only module
from async ok module`;
expect(result).to.equal(expected);
expect(context.result).to.equal(expected);
})
.catch(err => {
intercept.restore();
Expand Down Expand Up @@ -110,7 +110,7 @@ from async ok module`;
const promise = handleRoute({ request: {}, content: { status: 200, html: "" } });

return promise
.then(result => {
.then(context => {
intercept.restore();
const expected = `<!-- unhandled token INITIALIZE -->
from wants next module
Expand All @@ -123,7 +123,7 @@ from wants next module
not found
from string only module
from async ok module`;
expect(result).to.equal(expected);
expect(context.result).to.equal(expected);
})
.catch(err => {
intercept.restore();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ from string only module
from async ok module`;
const intercept = xstdout.intercept(false);
return Promise.try(() => asyncTemplate.render({}))
.then(result => {
.then(context => {
intercept.restore();
expect(
internalHandler.name,
Expand All @@ -74,7 +74,7 @@ from async ok module`;
"error from test/fixtures/async-error",
"error from test/fixtures/async-error"
]);
expect(result).to.equal("");
expect(context.result).to.equal("");
expect(receivedResult).to.equal(expected);
})
.catch(err => {
Expand Down

0 comments on commit 4a8151f

Please sign in to comment.