@anansi/core 0.13.0 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/dist/client.js +45 -46
- package/dist/client.js.map +1 -1
- package/dist/server.js +139 -145
- package/dist/server.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +3 -2
- package/lib/index.server.d.ts +1 -0
- package/lib/index.server.d.ts.map +1 -1
- package/lib/index.server.js +3 -2
- package/lib/spouts/document.d.ts +2 -8
- package/lib/spouts/document.d.ts.map +1 -1
- package/lib/spouts/document.js +4 -6
- package/lib/spouts/document.server.d.ts +3 -5
- package/lib/spouts/document.server.d.ts.map +1 -1
- package/lib/spouts/document.server.js +40 -42
- package/lib/spouts/json.d.ts +3 -3
- package/lib/spouts/json.d.ts.map +1 -1
- package/lib/spouts/json.js +6 -6
- package/lib/spouts/json.server.d.ts +3 -5
- package/lib/spouts/json.server.d.ts.map +1 -1
- package/lib/spouts/json.server.js +32 -34
- package/lib/spouts/restHooks.d.ts +2 -4
- package/lib/spouts/restHooks.d.ts.map +1 -1
- package/lib/spouts/restHooks.js +9 -11
- package/lib/spouts/restHooks.server.d.ts +4 -81
- package/lib/spouts/restHooks.server.d.ts.map +1 -1
- package/lib/spouts/restHooks.server.js +15 -17
- package/lib/spouts/router.d.ts +3 -4
- package/lib/spouts/router.d.ts.map +1 -1
- package/lib/spouts/router.js +14 -13
- package/lib/spouts/router.server.d.ts +2 -6
- package/lib/spouts/router.server.d.ts.map +1 -1
- package/lib/spouts/router.server.js +17 -19
- package/lib/spouts/types.d.ts +2 -0
- package/lib/spouts/types.d.ts.map +1 -1
- package/lib/spouts/types.js +1 -1
- package/package.json +1 -1
- package/src/index.server.ts +1 -0
- package/src/index.ts +1 -0
- package/src/spouts/document.server.tsx +63 -67
- package/src/spouts/document.tsx +5 -14
- package/src/spouts/json.server.tsx +40 -40
- package/src/spouts/json.tsx +8 -11
- package/src/spouts/restHooks.server.tsx +25 -29
- package/src/spouts/restHooks.tsx +12 -18
- package/src/spouts/router.server.tsx +29 -34
- package/src/spouts/router.tsx +19 -19
- package/src/spouts/types.ts +18 -0
|
@@ -1,23 +1,24 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
|
|
3
|
-
import type {
|
|
3
|
+
import type { ServerSpout } from './types';
|
|
4
4
|
|
|
5
5
|
type NeededNext = {
|
|
6
6
|
initData?: Record<string, () => unknown>;
|
|
7
7
|
scripts?: React.ReactNode[];
|
|
8
|
-
}
|
|
8
|
+
};
|
|
9
9
|
|
|
10
10
|
export default function JSONSpout({
|
|
11
11
|
id = 'anansi-json',
|
|
12
|
-
}: { id?: string } = {})
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
}: { id?: string } = {}): ServerSpout<
|
|
13
|
+
Record<string, unknown>,
|
|
14
|
+
Record<string, unknown>,
|
|
15
|
+
NeededNext
|
|
16
|
+
> {
|
|
17
|
+
return next => async props => {
|
|
18
|
+
const nextProps = await next(props);
|
|
18
19
|
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
const scripts: React.ReactNode[] = nextProps.scripts ?? [];
|
|
21
|
+
/*
|
|
21
22
|
Object.entries(nextProps.initData ?? {}).forEach(([key, data]) => {
|
|
22
23
|
try {
|
|
23
24
|
const encoded = JSON.stringify(data);
|
|
@@ -37,37 +38,36 @@ export default function JSONSpout({
|
|
|
37
38
|
console.error(e);
|
|
38
39
|
}
|
|
39
40
|
});*/
|
|
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
|
-
|
|
41
|
+
const Script = () => {
|
|
42
|
+
try {
|
|
43
|
+
const data: any = {};
|
|
44
|
+
Object.entries(nextProps.initData ?? {}).forEach(([key, getData]) => {
|
|
45
|
+
data[key] = getData();
|
|
46
|
+
});
|
|
47
|
+
const encoded = JSON.stringify(data);
|
|
48
|
+
return (
|
|
49
|
+
<script
|
|
50
|
+
key={id}
|
|
51
|
+
id={id}
|
|
52
|
+
type="application/json"
|
|
53
|
+
dangerouslySetInnerHTML={{
|
|
54
|
+
__html: encoded,
|
|
55
|
+
}}
|
|
56
|
+
nonce={props.nonce}
|
|
57
|
+
/>
|
|
58
|
+
);
|
|
59
|
+
} catch (e) {
|
|
60
|
+
// TODO: Use unified logging
|
|
61
|
+
console.error('Error serializing json');
|
|
62
|
+
console.error(e);
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
scripts.push(<Script />);
|
|
66
67
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
};
|
|
68
|
+
return {
|
|
69
|
+
...nextProps,
|
|
70
|
+
scripts,
|
|
71
71
|
};
|
|
72
72
|
};
|
|
73
73
|
}
|
package/src/spouts/json.tsx
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
type NeededNext = ResolveProps;
|
|
1
|
+
import type { ClientSpout } from './types';
|
|
4
2
|
|
|
5
3
|
export default function JSONSpout({
|
|
6
4
|
id = 'anansi-json',
|
|
7
|
-
}: { id?: string } = {})
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
};
|
|
5
|
+
}: { id?: string } = {}): ClientSpout<
|
|
6
|
+
Record<string, unknown>,
|
|
7
|
+
{ initData: Record<string, unknown> }
|
|
8
|
+
> {
|
|
9
|
+
return next => async props => {
|
|
10
|
+
const initData = getDatafromDOM(id);
|
|
11
|
+
return { ...(await next({ ...props, initData })), initData };
|
|
15
12
|
};
|
|
16
13
|
}
|
|
17
14
|
function getDatafromDOM(id: string): Record<string, unknown> {
|
|
@@ -2,42 +2,38 @@ import { Controller, Manager, NetworkManager, State } from '@rest-hooks/core';
|
|
|
2
2
|
import type { Store } from 'redux';
|
|
3
3
|
|
|
4
4
|
import { createPersistedStore } from './rhHelp';
|
|
5
|
-
import type {
|
|
6
|
-
|
|
7
|
-
type NeededNext = { initData?: Record<string, () => unknown> } & ResolveProps;
|
|
5
|
+
import type { ServerSpout } from './types';
|
|
8
6
|
|
|
9
7
|
export default function restHooksSpout(
|
|
10
8
|
options: {
|
|
11
9
|
getManagers: () => Manager[];
|
|
12
10
|
} = { getManagers: () => [new NetworkManager()] },
|
|
13
|
-
)
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
);
|
|
11
|
+
): ServerSpout<
|
|
12
|
+
Record<string, unknown>,
|
|
13
|
+
{ controller: Controller; store: Store<State<unknown>> },
|
|
14
|
+
{ initData?: Record<string, () => unknown> }
|
|
15
|
+
> {
|
|
16
|
+
return next => async props => {
|
|
17
|
+
const [ServerCacheProvider, controller, store] = createPersistedStore(
|
|
18
|
+
options.getManagers(),
|
|
19
|
+
);
|
|
23
20
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
21
|
+
const nextProps = await next({
|
|
22
|
+
...props,
|
|
23
|
+
controller,
|
|
24
|
+
store,
|
|
25
|
+
});
|
|
29
26
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
};
|
|
27
|
+
return {
|
|
28
|
+
...nextProps,
|
|
29
|
+
initData: {
|
|
30
|
+
...nextProps.initData,
|
|
31
|
+
resthooks: () => store.getState(),
|
|
32
|
+
},
|
|
33
|
+
app: <ServerCacheProvider>{nextProps.app}</ServerCacheProvider>,
|
|
34
|
+
// TODO: figure out how to only inject in next and not have to also put here
|
|
35
|
+
controller,
|
|
36
|
+
store,
|
|
41
37
|
};
|
|
42
38
|
};
|
|
43
39
|
}
|
package/src/spouts/restHooks.tsx
CHANGED
|
@@ -5,31 +5,25 @@ import {
|
|
|
5
5
|
State,
|
|
6
6
|
} from '@rest-hooks/core';
|
|
7
7
|
|
|
8
|
-
import type {
|
|
9
|
-
|
|
10
|
-
type NeededNext = ResolveProps;
|
|
8
|
+
import type { ClientSpout } from './types';
|
|
11
9
|
|
|
12
10
|
export default function restHooksSpout(
|
|
13
11
|
options: {
|
|
14
12
|
getManagers: () => Manager[];
|
|
15
13
|
} = { getManagers: () => [new NetworkManager()] },
|
|
16
|
-
) {
|
|
17
|
-
return
|
|
18
|
-
|
|
19
|
-
) {
|
|
20
|
-
return async (props: I & { initData: Record<string, unknown> }) => {
|
|
21
|
-
const data = props.initData.resthooks as State<unknown>;
|
|
14
|
+
): ClientSpout<{ initData: Record<string, unknown> }> {
|
|
15
|
+
return next => async props => {
|
|
16
|
+
const data = props.initData.resthooks as State<unknown>;
|
|
22
17
|
|
|
23
|
-
|
|
18
|
+
const nextProps = await next(props);
|
|
24
19
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
};
|
|
20
|
+
return {
|
|
21
|
+
...nextProps,
|
|
22
|
+
app: (
|
|
23
|
+
<CacheProvider initialState={data} managers={options.getManagers()}>
|
|
24
|
+
{nextProps.app}
|
|
25
|
+
</CacheProvider>
|
|
26
|
+
),
|
|
33
27
|
};
|
|
34
28
|
};
|
|
35
29
|
}
|
|
@@ -2,15 +2,19 @@ import { Route, RouteProvider, RouteController } from '@anansi/router';
|
|
|
2
2
|
import React from 'react';
|
|
3
3
|
import { createMemoryHistory } from 'history';
|
|
4
4
|
|
|
5
|
-
import type {
|
|
6
|
-
|
|
7
|
-
type NeededNext = ResolveProps;
|
|
5
|
+
import type { CreateRouter, ServerSpout } from './types';
|
|
8
6
|
|
|
9
7
|
export default function routerSpout<ResolveWith>(options: {
|
|
10
8
|
resolveWith?: any;
|
|
11
9
|
useResolveWith: () => ResolveWith;
|
|
12
10
|
createRouter: CreateRouter<ResolveWith>;
|
|
13
|
-
})
|
|
11
|
+
}): ServerSpout<
|
|
12
|
+
Record<string, unknown>,
|
|
13
|
+
{
|
|
14
|
+
matchedRoutes: Route<ResolveWith>[];
|
|
15
|
+
router: RouteController<Route<ResolveWith, any>>;
|
|
16
|
+
}
|
|
17
|
+
> {
|
|
14
18
|
const createRouteComponent = (
|
|
15
19
|
router: RouteController<Route<ResolveWith, any>>,
|
|
16
20
|
) =>
|
|
@@ -24,36 +28,27 @@ export default function routerSpout<ResolveWith>(options: {
|
|
|
24
28
|
);
|
|
25
29
|
};
|
|
26
30
|
|
|
27
|
-
return
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const Router = createRouteComponent(router);
|
|
49
|
-
|
|
50
|
-
return {
|
|
51
|
-
...nextProps,
|
|
52
|
-
app: <Router>{nextProps.app}</Router>,
|
|
53
|
-
// TODO: figure out how to only inject in next and not have to also put here
|
|
54
|
-
matchedRoutes,
|
|
55
|
-
router,
|
|
56
|
-
};
|
|
31
|
+
return next => async props => {
|
|
32
|
+
const url = props.req.url || '';
|
|
33
|
+
const router = options.createRouter(
|
|
34
|
+
createMemoryHistory({ initialEntries: [url] }),
|
|
35
|
+
);
|
|
36
|
+
const matchedRoutes: Route<ResolveWith>[] = router.getMatchedRoutes(url);
|
|
37
|
+
|
|
38
|
+
const nextProps = await next({
|
|
39
|
+
...props,
|
|
40
|
+
matchedRoutes,
|
|
41
|
+
router,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const Router = createRouteComponent(router);
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
...nextProps,
|
|
48
|
+
app: <Router>{nextProps.app}</Router>,
|
|
49
|
+
// TODO: figure out how to only inject in next and not have to also put here
|
|
50
|
+
matchedRoutes,
|
|
51
|
+
router,
|
|
57
52
|
};
|
|
58
53
|
};
|
|
59
54
|
}
|
package/src/spouts/router.tsx
CHANGED
|
@@ -3,16 +3,20 @@ import React from 'react';
|
|
|
3
3
|
import { createBrowserHistory } from 'history';
|
|
4
4
|
import type { Update } from 'history';
|
|
5
5
|
|
|
6
|
-
import type {
|
|
7
|
-
|
|
8
|
-
type NeededNext = ResolveProps;
|
|
6
|
+
import type { CreateRouter, ClientSpout } from './types';
|
|
9
7
|
|
|
10
8
|
export default function routerSpout<ResolveWith>(options: {
|
|
11
9
|
resolveWith?: any;
|
|
12
10
|
useResolveWith: () => ResolveWith;
|
|
13
11
|
createRouter: CreateRouter<ResolveWith>;
|
|
14
12
|
onChange?: (update: Update, callback: () => void | undefined) => void;
|
|
15
|
-
})
|
|
13
|
+
}): ClientSpout<
|
|
14
|
+
Record<string, unknown>,
|
|
15
|
+
{
|
|
16
|
+
matchedRoutes: Route<ResolveWith, any>[];
|
|
17
|
+
router: RouteController<Route<ResolveWith, any>>;
|
|
18
|
+
}
|
|
19
|
+
> {
|
|
16
20
|
const createRouteComponent = (
|
|
17
21
|
router: RouteController<Route<ResolveWith, any>>,
|
|
18
22
|
) =>
|
|
@@ -30,23 +34,19 @@ export default function routerSpout<ResolveWith>(options: {
|
|
|
30
34
|
);
|
|
31
35
|
};
|
|
32
36
|
|
|
33
|
-
return
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const history = createBrowserHistory();
|
|
38
|
-
const router = options.createRouter(history);
|
|
39
|
-
const matchedRoutes = router.getMatchedRoutes(history.location.pathname);
|
|
37
|
+
return next => async props => {
|
|
38
|
+
const history = createBrowserHistory();
|
|
39
|
+
const router = options.createRouter(history);
|
|
40
|
+
const matchedRoutes = router.getMatchedRoutes(history.location.pathname);
|
|
40
41
|
|
|
41
|
-
|
|
42
|
+
const nextProps = await next({ ...props, matchedRoutes, router });
|
|
42
43
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
};
|
|
44
|
+
const Router = createRouteComponent(router);
|
|
45
|
+
return {
|
|
46
|
+
...nextProps,
|
|
47
|
+
matchedRoutes,
|
|
48
|
+
router,
|
|
49
|
+
app: <Router>{nextProps.app}</Router>,
|
|
50
50
|
};
|
|
51
51
|
};
|
|
52
52
|
}
|
package/src/spouts/types.ts
CHANGED
|
@@ -20,3 +20,21 @@ export type ResolveProps = {
|
|
|
20
20
|
export type CreateRouter<T> = (
|
|
21
21
|
history: History,
|
|
22
22
|
) => RouteController<Route<T, any>>;
|
|
23
|
+
|
|
24
|
+
/* Spouts are middleware for Anansi */
|
|
25
|
+
export type ServerSpout<
|
|
26
|
+
NeededProps extends Record<string, unknown> = Record<string, unknown>,
|
|
27
|
+
ProvidedProps extends Record<string, unknown> = Record<string, unknown>,
|
|
28
|
+
NeededNext extends Record<string, unknown> = NeededProps,
|
|
29
|
+
> = <N extends NeededNext & ResolveProps, I extends NeededProps & ServerProps>(
|
|
30
|
+
next: (props: I & ProvidedProps) => Promise<N>,
|
|
31
|
+
) => (props: I) => Promise<N & ProvidedProps>;
|
|
32
|
+
|
|
33
|
+
/* Spouts are middleware for Anansi */
|
|
34
|
+
export type ClientSpout<
|
|
35
|
+
NeededProps extends Record<string, unknown> = Record<string, unknown>,
|
|
36
|
+
ProvidedProps extends Record<string, unknown> = Record<string, unknown>,
|
|
37
|
+
NeededNext extends Record<string, unknown> = NeededProps,
|
|
38
|
+
> = <N extends NeededNext & ResolveProps, I extends NeededProps>(
|
|
39
|
+
next: (props: I & ProvidedProps) => Promise<N>,
|
|
40
|
+
) => (props: I) => Promise<N & ProvidedProps>;
|