- commit
- f43c20f
- parent
- 2bf9e4c
- author
- Eric Bower
- date
- 2024-01-10 17:08:18 +0000 UTC
refactor(store): export storeMdw as collection
5 files changed,
+32,
-32
+3,
-3
1@@ -239,7 +239,7 @@ it(tests, "createApi with hash key on a large post", async () => {
2 const { store, schema } = testStore();
3 const query = createApi();
4 query.use(mdw.api());
5- query.use(storeMdw(schema.db));
6+ query.use(storeMdw.store(schema.db));
7 query.use(query.routes());
8 query.use(function* fetchApi(ctx, next) {
9 const data = {
10@@ -308,7 +308,7 @@ it(tests, "createApi - two identical endpoints", async () => {
11 const { store, schema } = testStore();
12 const api = createApi();
13 api.use(mdw.api());
14- api.use(storeMdw(schema.db));
15+ api.use(storeMdw.store(schema.db));
16 api.use(mdw.nameParser);
17 api.use(api.routes());
18
19@@ -462,7 +462,7 @@ it(tests, "should bubble up error", () => {
20 }
21 });
22 api.use(mdw.queryCtx);
23- api.use(storeMdw(schema.db));
24+ api.use(storeMdw.store(schema.db));
25 api.use(api.routes());
26
27 const fetchUser = api.get(
+12,
-12
1@@ -42,7 +42,7 @@ it(
2 const { store, schema } = testStore();
3 const api = createApi();
4 api.use(mdw.api());
5- api.use(storeMdw(schema.db));
6+ api.use(storeMdw.store(schema.db));
7 api.use(api.routes());
8 api.use(fetchMdw.headers);
9 api.use(mdw.fetch({ baseUrl }));
10@@ -91,7 +91,7 @@ it(
11 const { store, schema } = testStore();
12 const api = createApi();
13 api.use(mdw.api());
14- api.use(storeMdw(schema.db));
15+ api.use(storeMdw.store(schema.db));
16 api.use(api.routes());
17 api.use(mdw.fetch({ baseUrl }));
18
19@@ -127,7 +127,7 @@ it(tests, "error handling", async () => {
20 const { schema, store } = testStore();
21 const api = createApi();
22 api.use(mdw.api());
23- api.use(storeMdw(schema.db));
24+ api.use(storeMdw.store(schema.db));
25 api.use(api.routes());
26 api.use(mdw.fetch({ baseUrl }));
27
28@@ -163,7 +163,7 @@ it(tests, "status 204", async () => {
29 const { schema, store } = testStore();
30 const api = createApi();
31 api.use(mdw.api());
32- api.use(storeMdw(schema.db));
33+ api.use(storeMdw.store(schema.db));
34 api.use(api.routes());
35 api.use(function* (ctx, next) {
36 const url = ctx.req().url;
37@@ -203,7 +203,7 @@ it(tests, "malformed json", async () => {
38 const { schema, store } = testStore();
39 const api = createApi();
40 api.use(mdw.api());
41- api.use(storeMdw(schema.db));
42+ api.use(storeMdw.store(schema.db));
43 api.use(api.routes());
44 api.use(function* (ctx, next) {
45 const url = ctx.req().url;
46@@ -248,7 +248,7 @@ it(tests, "POST", async () => {
47 const { schema, store } = testStore();
48 const api = createApi();
49 api.use(mdw.api());
50- api.use(storeMdw(schema.db));
51+ api.use(storeMdw.store(schema.db));
52 api.use(api.routes());
53 api.use(fetchMdw.headers);
54 api.use(mdw.fetch({ baseUrl }));
55@@ -291,7 +291,7 @@ it(tests, "POST multiple endpoints with same uri", async () => {
56 const { store, schema } = testStore();
57 const api = createApi();
58 api.use(mdw.api());
59- api.use(storeMdw(schema.db));
60+ api.use(storeMdw.store(schema.db));
61 api.use(api.routes());
62 api.use(fetchMdw.headers);
63 api.use(mdw.fetch({ baseUrl }));
64@@ -353,7 +353,7 @@ it(
65 const { store, schema } = testStore();
66 const api = createApi();
67 api.use(mdw.api());
68- api.use(storeMdw(schema.db));
69+ api.use(storeMdw.store(schema.db));
70 api.use(api.routes());
71 api.use(mdw.fetch({ baseUrl }));
72
73@@ -402,7 +402,7 @@ it(
74 const { schema, store } = testStore();
75 const api = createApi();
76 api.use(mdw.api());
77- api.use(storeMdw(schema.db));
78+ api.use(storeMdw.store(schema.db));
79 api.use(api.routes());
80 api.use(mdw.fetch({ baseUrl }));
81
82@@ -448,7 +448,7 @@ it(
83 let actual = null;
84 const api = createApi();
85 api.use(mdw.api());
86- api.use(storeMdw(schema.db));
87+ api.use(storeMdw.store(schema.db));
88 api.use(api.routes());
89 api.use(mdw.fetch({ baseUrl }));
90
91@@ -479,7 +479,7 @@ it(
92 let actual = null;
93 const api = createApi();
94 api.use(mdw.api());
95- api.use(storeMdw(schema.db));
96+ api.use(storeMdw.store(schema.db));
97 api.use(api.routes());
98 api.use(mdw.fetch({ baseUrl }));
99
100@@ -504,7 +504,7 @@ it(tests, "should use dynamic mdw to mock response", async () => {
101 let actual = null;
102 const api = createApi();
103 api.use(mdw.api());
104- api.use(storeMdw(schema.db));
105+ api.use(storeMdw.store(schema.db));
106 api.use(api.routes());
107 api.use(mdw.fetch({ baseUrl }));
108
+7,
-8
1@@ -3,7 +3,6 @@ import { createApi, createKey, mdw } from "../query/mod.ts";
2 import type { ApiCtx, Next, ThunkCtx } from "../query/mod.ts";
3 import { createQueryState } from "../action.ts";
4 import { sleep } from "../test.ts";
5-
6 import {
7 configureStore,
8 createSchema,
9@@ -110,7 +109,7 @@ it(tests, "with loader", () => {
10 const { schema, store } = testStore();
11 const api = createApi<ApiCtx>();
12 api.use(mdw.api());
13- api.use(storeMdw(schema.db));
14+ api.use(storeMdw.store(schema.db));
15 api.use(api.routes());
16 api.use(function* fetchApi(ctx, next) {
17 ctx.response = new Response(jsonBlob(mockUser), { status: 200 });
18@@ -153,7 +152,7 @@ it(tests, "with item loader", () => {
19 const { store, schema } = testStore();
20 const api = createApi<ApiCtx>();
21 api.use(mdw.api());
22- api.use(storeMdw(schema.db));
23+ api.use(storeMdw.store(schema.db));
24 api.use(api.routes());
25 api.use(function* fetchApi(ctx, next) {
26 ctx.response = new Response(jsonBlob(mockUser), { status: 200 });
27@@ -250,7 +249,7 @@ it(tests, "simpleCache", () => {
28 const { store, schema } = testStore();
29 const api = createApi<ApiCtx>();
30 api.use(mdw.api());
31- api.use(storeMdw(schema.db));
32+ api.use(storeMdw.store(schema.db));
33 api.use(api.routes());
34 api.use(function* fetchApi(ctx, next) {
35 const data = { users: [mockUser] };
36@@ -280,7 +279,7 @@ it(tests, "overriding default loader behavior", () => {
37 const { store, schema } = testStore();
38 const api = createApi<ApiCtx>();
39 api.use(mdw.api());
40- api.use(storeMdw(schema.db));
41+ api.use(storeMdw.store(schema.db));
42 api.use(api.routes());
43 api.use(function* fetchApi(ctx, next) {
44 const data = { users: [mockUser] };
45@@ -337,7 +336,7 @@ it(tests, "mdw.api() - error handler", () => {
46 const { schema, store } = testStore();
47 const query = createApi<ApiCtx>();
48 query.use(mdw.api());
49- query.use(storeMdw(schema.db));
50+ query.use(storeMdw.store(schema.db));
51 query.use(query.routes());
52 query.use(function* () {
53 throw new Error("something happened");
54@@ -353,7 +352,7 @@ it(tests, "createApi with own key", async () => {
55 const { schema, store } = testStore();
56 const query = createApi();
57 query.use(mdw.api());
58- query.use(storeMdw(schema.db));
59+ query.use(storeMdw.store(schema.db));
60 query.use(query.routes());
61 query.use(mdw.customKey);
62 query.use(function* fetchApi(ctx, next) {
63@@ -424,7 +423,7 @@ it(tests, "createApi with custom key but no payload", async () => {
64 const { store, schema } = testStore();
65 const query = createApi();
66 query.use(mdw.api());
67- query.use(storeMdw(schema.db));
68+ query.use(storeMdw.store(schema.db));
69 query.use(query.routes());
70 query.use(mdw.customKey);
71 query.use(function* fetchApi(ctx, next) {
+2,
-1
1@@ -2,10 +2,11 @@ export * from "./context.ts";
2 export * from "./fx.ts";
3 export * from "./store.ts";
4 export * from "./types.ts";
5-export * from "./query.ts";
6 export * from "./supervisor.ts";
7 export { createSelector } from "../deps.ts";
8 export * from "./slice/mod.ts";
9 export * from "./schema.ts";
10 export * from "./batch.ts";
11 export * from "./persist.ts";
12+import * as storeMdw from "./query.ts";
13+export { storeMdw };
+8,
-8
1@@ -6,18 +6,18 @@ import { put, select, updateStore } from "./fx.ts";
2 import { LoaderOutput } from "./slice/loader.ts";
3 import { TableOutput } from "./slice/table.ts";
4
5-export function storeMdw<
6+export function store<
7 Ctx extends ApiCtx = ApiCtx,
8 M extends AnyState = AnyState,
9->({ cache, loaders, errorFn }: {
10+>(props: {
11 loaders: LoaderOutput<M, AnyState>;
12 cache: TableOutput<any, AnyState>;
13 errorFn?: (ctx: Ctx) => string;
14 }) {
15 return compose<Ctx>([
16- dispatchActions,
17- loadingMonitor(loaders, errorFn),
18- simpleCache(cache),
19+ actions,
20+ loaderApi(props.loaders, props.errorFn),
21+ cache(props.cache),
22 ]);
23 }
24
25@@ -25,7 +25,7 @@ export function storeMdw<
26 * This middleware will automatically cache any data found inside `ctx.json`
27 * which is where we store JSON data from the {@link mdw.fetch} middleware.
28 */
29-export function simpleCache<Ctx extends ApiCtx = ApiCtx>(
30+export function cache<Ctx extends ApiCtx = ApiCtx>(
31 dataSchema: TableOutput<any, AnyState>,
32 ) {
33 return function* (
34@@ -54,7 +54,7 @@ export function simpleCache<Ctx extends ApiCtx = ApiCtx>(
35 * within the pipeline of the middleware and instead of dispatching them serially this
36 * improves performance by only hitting the reducers once.
37 */
38-export function* dispatchActions(ctx: { actions: AnyAction[] }, next: Next) {
39+export function* actions(ctx: { actions: AnyAction[] }, next: Next) {
40 if (!ctx.actions) ctx.actions = [];
41 yield* next();
42 if (ctx.actions.length === 0) return;
43@@ -100,7 +100,7 @@ export function loader<
44 /**
45 * This middleware will track the status of a fetch request.
46 */
47-export function loadingMonitor<
48+export function loaderApi<
49 Ctx extends ApiCtx = ApiCtx,
50 M extends AnyState = AnyState,
51 >(