Skip to content

Commit

Permalink
Check props is promise (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
okoala authored Dec 6, 2017
1 parent 97623ee commit 25f6a20
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions packages/avet-utils/lib/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,32 @@ function getDisplayName(Component) {
return Component.displayName || Component.name || 'UnKnown';
}

function isPromise(obj) {
return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
}

function loadGetInitialProps(Component, ctx) {
return new Promise(function(resolve, reject) {
if (!Component.getInitialProps) return resolve({});

Component.getInitialProps(ctx).then(function(props) {
if (!props && (!ctx.res || !ctx.res.finished)) {
const compName = getDisplayName(Component);
const message = `"${
compName
}.getInitialProps()" should resolve to an object. But found "${
props
}" instead.`;
return reject(message);
}
const props = Component.getInitialProps(ctx)
if (isPromise(props)) {
props.then(function(p) {
if (!p && (!ctx.res || !ctx.res.finished)) {
const compName = getDisplayName(Component);
const message = `"${
compName
}.getInitialProps()" should resolve to an object. But found "${
p
}" instead.`;
return reject(message);
}

return resolve(props);
})
return resolve(p);
});
} else {
resolve(props);
}
})
}

Expand Down

0 comments on commit 25f6a20

Please sign in to comment.