@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.
Files changed (50) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/dist/client.js +45 -46
  3. package/dist/client.js.map +1 -1
  4. package/dist/server.js +139 -145
  5. package/dist/server.js.map +1 -1
  6. package/lib/index.d.ts +1 -0
  7. package/lib/index.d.ts.map +1 -1
  8. package/lib/index.js +3 -2
  9. package/lib/index.server.d.ts +1 -0
  10. package/lib/index.server.d.ts.map +1 -1
  11. package/lib/index.server.js +3 -2
  12. package/lib/spouts/document.d.ts +2 -8
  13. package/lib/spouts/document.d.ts.map +1 -1
  14. package/lib/spouts/document.js +4 -6
  15. package/lib/spouts/document.server.d.ts +3 -5
  16. package/lib/spouts/document.server.d.ts.map +1 -1
  17. package/lib/spouts/document.server.js +40 -42
  18. package/lib/spouts/json.d.ts +3 -3
  19. package/lib/spouts/json.d.ts.map +1 -1
  20. package/lib/spouts/json.js +6 -6
  21. package/lib/spouts/json.server.d.ts +3 -5
  22. package/lib/spouts/json.server.d.ts.map +1 -1
  23. package/lib/spouts/json.server.js +32 -34
  24. package/lib/spouts/restHooks.d.ts +2 -4
  25. package/lib/spouts/restHooks.d.ts.map +1 -1
  26. package/lib/spouts/restHooks.js +9 -11
  27. package/lib/spouts/restHooks.server.d.ts +4 -81
  28. package/lib/spouts/restHooks.server.d.ts.map +1 -1
  29. package/lib/spouts/restHooks.server.js +15 -17
  30. package/lib/spouts/router.d.ts +3 -4
  31. package/lib/spouts/router.d.ts.map +1 -1
  32. package/lib/spouts/router.js +14 -13
  33. package/lib/spouts/router.server.d.ts +2 -6
  34. package/lib/spouts/router.server.d.ts.map +1 -1
  35. package/lib/spouts/router.server.js +17 -19
  36. package/lib/spouts/types.d.ts +2 -0
  37. package/lib/spouts/types.d.ts.map +1 -1
  38. package/lib/spouts/types.js +1 -1
  39. package/package.json +1 -1
  40. package/src/index.server.ts +1 -0
  41. package/src/index.ts +1 -0
  42. package/src/spouts/document.server.tsx +63 -67
  43. package/src/spouts/document.tsx +5 -14
  44. package/src/spouts/json.server.tsx +40 -40
  45. package/src/spouts/json.tsx +8 -11
  46. package/src/spouts/restHooks.server.tsx +25 -29
  47. package/src/spouts/restHooks.tsx +12 -18
  48. package/src/spouts/router.server.tsx +29 -34
  49. package/src/spouts/router.tsx +19 -19
  50. package/src/spouts/types.ts +18 -0
@@ -1,23 +1,24 @@
1
1
  import React from 'react';
2
2
 
3
- import type { ServerProps, ResolveProps } from './types';
3
+ import type { ServerSpout } from './types';
4
4
 
5
5
  type NeededNext = {
6
6
  initData?: Record<string, () => unknown>;
7
7
  scripts?: React.ReactNode[];
8
- } & ResolveProps;
8
+ };
9
9
 
10
10
  export default function JSONSpout({
11
11
  id = 'anansi-json',
12
- }: { id?: string } = {}) {
13
- return function <N extends NeededNext, I extends ServerProps>(
14
- next: (props: I) => Promise<N>,
15
- ) {
16
- return async (props: I) => {
17
- const nextProps = await next(props);
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
- const scripts: React.ReactNode[] = nextProps.scripts ?? [];
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
- const Script = () => {
41
- try {
42
- const data: any = {};
43
- Object.entries(nextProps.initData ?? {}).forEach(([key, getData]) => {
44
- data[key] = getData();
45
- });
46
- const encoded = JSON.stringify(data);
47
- return (
48
- <script
49
- key={id}
50
- id={id}
51
- type="application/json"
52
- dangerouslySetInnerHTML={{
53
- __html: encoded,
54
- }}
55
- nonce={props.nonce}
56
- />
57
- );
58
- } catch (e) {
59
- // TODO: Use unified logging
60
- console.error('Error serializing json');
61
- console.error(e);
62
- return null;
63
- }
64
- };
65
- scripts.push(<Script />);
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
- return {
68
- ...nextProps,
69
- scripts,
70
- };
68
+ return {
69
+ ...nextProps,
70
+ scripts,
71
71
  };
72
72
  };
73
73
  }
@@ -1,17 +1,14 @@
1
- import type { ResolveProps } from './types';
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
- return function <N extends NeededNext, I extends Record<string, unknown>>(
9
- next: (props: I & { initData: Record<string, unknown> }) => Promise<N>,
10
- ) {
11
- return async (props: I) => {
12
- const initData = getDatafromDOM(id);
13
- return await next({ ...props, initData });
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 { ResolveProps, ServerProps } from './types';
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
- return function <N extends NeededNext, I extends ServerProps>(
15
- next: (
16
- props: I & { controller: Controller; store: Store<State<unknown>> },
17
- ) => Promise<N>,
18
- ) {
19
- return async (props: I) => {
20
- const [ServerCacheProvider, controller, store] = createPersistedStore(
21
- options.getManagers(),
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
- const nextProps = await next({
25
- ...props,
26
- controller,
27
- store,
28
- });
21
+ const nextProps = await next({
22
+ ...props,
23
+ controller,
24
+ store,
25
+ });
29
26
 
30
- return {
31
- ...nextProps,
32
- initData: {
33
- ...nextProps.initData,
34
- resthooks: () => store.getState(),
35
- },
36
- app: <ServerCacheProvider>{nextProps.app}</ServerCacheProvider>,
37
- // TODO: figure out how to only inject in next and not have to also put here
38
- controller,
39
- store,
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
  }
@@ -5,31 +5,25 @@ import {
5
5
  State,
6
6
  } from '@rest-hooks/core';
7
7
 
8
- import type { ResolveProps } from './types';
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 function <N extends NeededNext, I extends Record<string, unknown>>(
18
- next: (props: I) => Promise<N>,
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
- const nextProps = await next(props);
18
+ const nextProps = await next(props);
24
19
 
25
- return {
26
- ...nextProps,
27
- app: (
28
- <CacheProvider initialState={data} managers={options.getManagers()}>
29
- {nextProps.app}
30
- </CacheProvider>
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 { ResolveProps, ServerProps, CreateRouter } from './types';
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 function <N extends NeededNext, I extends ServerProps>(
28
- next: (
29
- props: I & {
30
- matchedRoutes: Route<ResolveWith>[];
31
- router: RouteController<Route<ResolveWith, any>>;
32
- },
33
- ) => Promise<N>,
34
- ) {
35
- return async (props: I) => {
36
- const url = props.req.url || '';
37
- const router = options.createRouter(
38
- createMemoryHistory({ initialEntries: [url] }),
39
- );
40
- const matchedRoutes: Route<ResolveWith>[] = router.getMatchedRoutes(url);
41
-
42
- const nextProps = await next({
43
- ...props,
44
- matchedRoutes,
45
- router,
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
  }
@@ -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 { ResolveProps, CreateRouter } from './types';
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 function <N extends NeededNext, I extends Record<string, unknown>>(
34
- next: (initData: Record<string, unknown>) => Promise<N>,
35
- ) {
36
- return async (props: I) => {
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
- const nextProps = await next(props);
42
+ const nextProps = await next({ ...props, matchedRoutes, router });
42
43
 
43
- const Router = createRouteComponent(router);
44
- return {
45
- ...nextProps,
46
- matchedRoutes,
47
- router,
48
- app: <Router>{nextProps.app}</Router>,
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
  }
@@ -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>;