This repository has been archived by the owner on Apr 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 786
/
Copy pathserver.ts
118 lines (99 loc) · 3.59 KB
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { Children } from 'react';
import * as ReactDOM from 'react-dom/server';
import ApolloClient from 'apollo-client';
import assign = require('object-assign');
import flatten = require('lodash.flatten');
declare interface Context {
client?: ApolloClient;
store?: any;
[key: string]: any;
}
declare interface QueryTreeArgument {
component: any;
queries?: any[];
context?: Context;
}
export function getPropsFromChild(child) {
const { props, type } = child;
let ownProps = assign({}, props);
if (type && type.defaultProps) ownProps = assign({}, type.defaultProps, props);
return ownProps;
}
export function getChildFromComponent(component) {
// See if this is a class, or stateless function
if (component && component.render) return component.render();
return component;
}
let contextStore = {};
function getQueriesFromTree(
{ component, context = {}, queries = []}: QueryTreeArgument, fetch: boolean = true
) {
contextStore = assign({}, contextStore, context);
if (!component) return;
// stateless function
if (typeof component === 'function') component = { type: component };
const { type, props } = component;
if (typeof type === 'function') {
let ComponentClass = type;
let ownProps = getPropsFromChild(component);
const Component = new ComponentClass(ownProps, context);
try {
Component.props = ownProps;
Component.setState = (newState: any) => {
Component.state = assign({}, Component.state, newState);
};
} catch (e) {} // tslint:disable-line
if (Component.componentWillMount) Component.componentWillMount();
let newContext = context;
if (Component.getChildContext) newContext = assign({}, context, Component.getChildContext());
// see if there is a fetch data method
if (typeof type.fetchData === 'function' && fetch) {
const query = type.fetchData(ownProps, newContext);
if (query) queries.push({ query, component });
}
getQueriesFromTree({
component: getChildFromComponent(Component),
context: newContext,
queries,
});
} else if (props && props.children) {
Children.forEach(props.children, (child: any) => getQueriesFromTree({
component: child,
context,
queries,
}));
}
return { queries, context: contextStore };
}
// XXX component Cache
export function getDataFromTree(app, ctx: any = {}, fetch: boolean = true): Promise<any> {
// reset for next loop
contextStore = {};
let { context, queries } = getQueriesFromTree({ component: app, context: ctx }, fetch);
// reset for next loop
contextStore = {};
// no queries found, nothing to do
if (!queries.length) return Promise.resolve(context);
const mappedQueries = flatten(queries).map(y => y.query.then(x => y));
// run through all queries we can
return Promise.all(mappedQueries)
.then(trees => Promise.all(trees.filter(x => !!x).map(x => {
return getDataFromTree(x.component, context, false); // don't rerun `fetchData'
})))
.then(() => (context));
}
export function renderToStringWithData(component) {
return getDataFromTree(component)
.then(({ store, client }) => {
let markup = ReactDOM.renderToString(component);
let initialState = store.getState();
const key = client.reduxRootKey;
// XXX apollo client requires a lot in the store
// can we make this samller?
for (let queryId in initialState[key].queries) {
let fieldsToNotShip = ['minimizedQuery', 'minimizedQueryString'];
for (let field of fieldsToNotShip) delete initialState[key].queries[queryId][field];
}
return { markup, initialState };
});
}