Skip to content

Commit

Permalink
clarify content spec for paths
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip committed Aug 24, 2018
1 parent fec1a18 commit 9528ab6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
8 changes: 3 additions & 5 deletions packages/electrode-react-webapp/lib/hapi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,9 @@ const registerRoutes = (server, options) => {
_.each(registerOptions.paths, (pathData, path) => {
const resolveContent = () => {
if (registerOptions.serverSideRendering !== false) {
assert(
pathData.content !== undefined,
`You must define content for the webapp plugin path ${path}`
);
return ReactWebapp.resolveContent(pathData.content);
const x = ReactWebapp.resolveContent(pathData);
assert(x, `You must define content for the webapp plugin path ${path}`);
return x;
}

return {
Expand Down
37 changes: 36 additions & 1 deletion packages/electrode-react-webapp/lib/react-webapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,44 @@ const setupPathOptions = (routeOptions, path) => {
);
};

const resolveContent = (content, xrequire) => {
//
// The route path can supply:
//
// - a literal string
// - a function
// - an object
//
// If it's an object:
// -- if it doesn't contain content, then it's assume to be the content.
//
// If it contains content, then it can contain:
//
// - method: HTTP method for the route
// - config: route config (applicable for framework like Hapi)
// - content: second level field to define content
//
// content can be:
//
// - a literal string
// - a function
// - an object
//
// If content is an object, it can contain module, a path to the JS module to require
// to load the content.
//
const resolveContent = (pathData, xrequire) => {
const resolveTime = Date.now();

let content = pathData;

// If it's an object, see if contains content field
if (_.isObject(pathData) && pathData.hasOwnProperty("content")) {
content = pathData.content;
}

if (!content) return null;

// content has module field, require it.
if (!_.isString(content) && !_.isFunction(content) && content.module) {
const mod = content.module.startsWith(".") ? Path.resolve(content.module) : content.module;

Expand Down
27 changes: 27 additions & 0 deletions packages/electrode-react-webapp/test/spec/hapi.index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,33 @@ describe("hapi electrode-react-webapp", () => {
});
});

it("should fail if content is null", () => {
assign(mainRoutePathOptions, {
content: null
});

let error;

return electrodeServer(config).then(server => {
return server
.inject({
method: "GET",
url: "/"
})
.catch(err => {
error = err;
})
.then(() => {
stopServer(server);
expect(error).to.exist;
expect(error.code).to.equal("ERR_ASSERTION");
expect(error.message).to.equal(
`You must define content for the webapp plugin path /{args*}`
);
});
});
});

it("should handle 200 status @noss", () => {
assign(mainRoutePathOptions, {
content: {
Expand Down

0 comments on commit 9528ab6

Please sign in to comment.