repos / starfx

supercharged async flow control library.
git clone https://github.com/neurosnap/starfx.git

commit
6313833
parent
7b09e81
author
Eric Bower
date
2024-03-05 19:55:10 +0000 UTC
refactor: remove Result.data

BREAKING CHANGE: `ctx.json` no longer has a `.data` property
6 files changed,  +22, -43
M mdw/fetch.ts
+0, -5
 1@@ -103,7 +103,6 @@ export function* json<CurCtx extends FetchJsonCtx = FetchJsonCtx>(
 2   if (ctx.response.status === 204) {
 3     ctx.json = {
 4       ok: true,
 5-      data: {},
 6       value: {},
 7     };
 8     yield* next();
 9@@ -120,13 +119,11 @@ export function* json<CurCtx extends FetchJsonCtx = FetchJsonCtx>(
10     if (ctx.response.ok) {
11       ctx.json = {
12         ok: true,
13-        data: data.value,
14         value: data.value,
15       };
16     } else {
17       ctx.json = {
18         ok: false,
19-        data: data.value,
20         error: data.value,
21       };
22     }
23@@ -134,7 +131,6 @@ export function* json<CurCtx extends FetchJsonCtx = FetchJsonCtx>(
24     const dta = { message: data.error.message };
25     ctx.json = {
26       ok: false,
27-      data: dta,
28       error: dta,
29     };
30   }
31@@ -190,7 +186,6 @@ export function* payload<CurCtx extends FetchJsonCtx = FetchJsonCtx>(
32         `found :${key} in endpoint name (${ctx.name}) but payload has falsy value (${val})`;
33       ctx.json = {
34         ok: false,
35-        data,
36         error: data,
37       };
38       return;
M mdw/query.ts
+1, -1
1@@ -89,7 +89,7 @@ export function* queryCtx<Ctx extends ApiCtx = ApiCtx>(ctx: Ctx, next: Next) {
2   }
3   if (!ctx.request) ctx.request = ctx.req();
4   if (!ctx.response) ctx.response = null;
5-  if (!ctx.json) ctx.json = { ok: false, data: {}, error: {} };
6+  if (!ctx.json) ctx.json = { ok: false, error: {} };
7   if (!ctx.actions) ctx.actions = [];
8   if (!ctx.bodyType) ctx.bodyType = "json";
9   yield* next();
M query/types.ts
+0, -8
 1@@ -30,18 +30,10 @@ export type ApiFetchResult<ApiSuccess = any, ApiError = any> =
 2   | {
 3     ok: true;
 4     value: ApiSuccess;
 5-    /**
 6-     * @deprecated Use {@link ApiFetchResult.value} instead.
 7-     */
 8-    data: ApiSuccess;
 9   }
10   | {
11     ok: false;
12     error: ApiError;
13-    /**
14-     * @deprecated Use {@link ApiFetchResult.error} instead.
15-     */
16-    data: ApiError;
17   };
18 
19 export type ApiRequest = Partial<{ url: string } & RequestInit>;
M test/api.test.ts
+4, -5
 1@@ -285,7 +285,6 @@ it(tests, "with hash key on a large post", async () => {
 2       ctx.response = new Response();
 3       ctx.json = {
 4         ok: true,
 5-        data: curUsers,
 6         value: curUsers,
 7       };
 8     },
 9@@ -353,7 +352,7 @@ it(tests, "ensure types for get() endpoint", () => {
10   api.use(function* (ctx, next) {
11     yield* next();
12     const data = { result: "wow" };
13-    ctx.json = { ok: true, data, value: data };
14+    ctx.json = { ok: true, value: data };
15   });
16 
17   const acc: string[] = [];
18@@ -391,7 +390,7 @@ it(tests, "ensure ability to cast `ctx` in function definition", () => {
19   api.use(function* (ctx, next) {
20     yield* next();
21     const data = { result: "wow" };
22-    ctx.json = { ok: true, data, value: data };
23+    ctx.json = { ok: true, value: data };
24   });
25 
26   const acc: string[] = [];
27@@ -428,7 +427,7 @@ it(
28     api.use(function* (ctx, next) {
29       yield* next();
30       const data = { result: "wow" };
31-      ctx.json = { ok: true, data, value: data };
32+      ctx.json = { ok: true, value: data };
33     });
34 
35     const acc: string[] = [];
36@@ -493,7 +492,7 @@ it(
37     api.use(function* (ctx, next) {
38       yield* next();
39       const data = { result: "wow" };
40-      ctx.json = { ok: true, data, value: data };
41+      ctx.json = { ok: true, value: data };
42     });
43 
44     const acc: string[] = [];
M test/fetch.test.ts
+9, -13
  1@@ -71,7 +71,7 @@ it(
  2       headers: {
  3         "Content-Type": "application/json",
  4       },
  5-    }, { ok: true, data: mockUser, value: mockUser }]);
  6+    }, { ok: true, value: mockUser }]);
  7   },
  8 );
  9 
 10@@ -109,7 +109,7 @@ it(
 11     await store.run(waitForLoader(schema.loaders, action));
 12 
 13     const data = "this is some text";
 14-    expect(actual).toEqual({ ok: true, data, value: data });
 15+    expect(actual).toEqual({ ok: true, value: data });
 16   },
 17 );
 18 
 19@@ -146,7 +146,7 @@ it(tests, "error handling", async () => {
 20 
 21   const state = store.getState();
 22   expect(state.cache[action.payload.key]).toEqual(errMsg);
 23-  expect(actual).toEqual({ ok: false, data: errMsg, error: errMsg });
 24+  expect(actual).toEqual({ ok: false, error: errMsg });
 25 });
 26 
 27 it(tests, "status 204", async () => {
 28@@ -185,7 +185,7 @@ it(tests, "status 204", async () => {
 29 
 30   const state = store.getState();
 31   expect(state.cache[action.payload.key]).toEqual({});
 32-  expect(actual).toEqual({ ok: true, data: {}, value: {} });
 33+  expect(actual).toEqual({ ok: true, value: {} });
 34 });
 35 
 36 it(tests, "malformed json", async () => {
 37@@ -227,7 +227,6 @@ it(tests, "malformed json", async () => {
 38   };
 39   expect(actual).toEqual({
 40     ok: false,
 41-    data,
 42     error: data,
 43   });
 44 });
 45@@ -278,7 +277,6 @@ it(tests, "POST", async () => {
 46 
 47   expect(loader.value.meta.json).toEqual({
 48     ok: true,
 49-    data: mockUser,
 50     value: mockUser,
 51   });
 52 });
 53@@ -351,7 +349,6 @@ it(tests, "POST multiple endpoints with same uri", async () => {
 54 
 55   expect(result1.value.meta.json).toEqual({
 56     ok: true,
 57-    data: mockUser,
 58     value: mockUser,
 59   });
 60 
 61@@ -366,7 +363,6 @@ it(tests, "POST multiple endpoints with same uri", async () => {
 62 
 63   expect(result2.value.meta.json).toEqual({
 64     ok: true,
 65-    data: mockUser,
 66     value: mockUser,
 67   });
 68 });
 69@@ -454,7 +450,7 @@ it(
 70 
 71     const state = store.getState();
 72     expect(state.cache[action.payload.key]).toEqual(mockUser);
 73-    expect(actual).toEqual({ ok: true, data: mockUser, value: mockUser });
 74+    expect(actual).toEqual({ ok: true, value: mockUser });
 75   },
 76 );
 77 
 78@@ -493,7 +489,7 @@ it(
 79       throw loader.error;
 80     }
 81     const data = { message: "error" };
 82-    expect(actual).toEqual({ ok: false, data, error: data });
 83+    expect(actual).toEqual({ ok: false, error: data });
 84   },
 85 );
 86 
 87@@ -523,7 +519,7 @@ it(
 88     if (!loader.ok) {
 89       throw loader.error;
 90     }
 91-    expect(actual).toEqual({ ok: true, data: mockUser, value: mockUser });
 92+    expect(actual).toEqual({ ok: true, value: mockUser });
 93   },
 94 );
 95 
 96@@ -553,7 +549,7 @@ it(tests, "should use dynamic mdw to mock response", async () => {
 97   if (!loader.ok) {
 98     throw loader.error;
 99   }
100-  expect(actual).toEqual({ ok: true, data: dynamicUser, value: dynamicUser });
101+  expect(actual).toEqual({ ok: true, value: dynamicUser });
102 
103   // reset dynamic mdw and try again
104   api.reset();
105@@ -562,5 +558,5 @@ it(tests, "should use dynamic mdw to mock response", async () => {
106   if (!loader.ok) {
107     throw loader.error;
108   }
109-  expect(actual).toEqual({ ok: true, data: mockUser, value: mockUser });
110+  expect(actual).toEqual({ ok: true, value: mockUser });
111 });
M test/mdw.test.ts
+8, -11
 1@@ -52,14 +52,14 @@ it(tests, "basic", () => {
 2   query.use(query.routes());
 3   query.use(function* fetchApi(ctx, next) {
 4     if (`${ctx.req().url}`.startsWith("/users/")) {
 5-      ctx.json = { ok: true, data: mockUser2, value: mockUser2 };
 6+      ctx.json = { ok: true, value: mockUser2 };
 7       yield* next();
 8       return;
 9     }
10     const data = {
11       users: [mockUser],
12     };
13-    ctx.json = { ok: true, data, value: data };
14+    ctx.json = { ok: true, value: data };
15     yield* next();
16   });
17 
18@@ -116,7 +116,7 @@ it(tests, "with loader", () => {
19   api.use(function* fetchApi(ctx, next) {
20     ctx.response = new Response(jsonBlob(mockUser), { status: 200 });
21     const data = { users: [mockUser] };
22-    ctx.json = { ok: true, data, value: data };
23+    ctx.json = { ok: true, value: data };
24     yield* next();
25   });
26 
27@@ -158,7 +158,7 @@ it(tests, "with item loader", () => {
28   api.use(function* fetchApi(ctx, next) {
29     ctx.response = new Response(jsonBlob(mockUser), { status: 200 });
30     const data = { users: [mockUser] };
31-    ctx.json = { ok: true, data, value: data };
32+    ctx.json = { ok: true, value: data };
33     yield* next();
34   });
35 
36@@ -254,7 +254,7 @@ it(tests, "simpleCache", () => {
37   api.use(function* fetchApi(ctx, next) {
38     const data = { users: [mockUser] };
39     ctx.response = new Response(jsonBlob(data));
40-    ctx.json = { ok: true, data, value: data };
41+    ctx.json = { ok: true, value: data };
42     yield* next();
43   });
44 
45@@ -283,7 +283,7 @@ it(tests, "overriding default loader behavior", () => {
46   api.use(function* fetchApi(ctx, next) {
47     const data = { users: [mockUser] };
48     ctx.response = new Response(jsonBlob(data));
49-    ctx.json = { ok: true, data, value: data };
50+    ctx.json = { ok: true, value: data };
51     yield* next();
52   });
53 
54@@ -390,7 +390,6 @@ it(tests, "createApi with own key", async () => {
55       ctx.response = new Response();
56       ctx.json = {
57         ok: true,
58-        data: curUsers,
59         value: curUsers,
60       };
61     },
62@@ -462,7 +461,6 @@ it(tests, "createApi with custom key but no payload", async () => {
63       ctx.response = new Response();
64       ctx.json = {
65         ok: true,
66-        data: curUsers,
67         value: curUsers,
68       };
69     },
70@@ -510,14 +508,14 @@ it(tests, "errorHandler", () => {
71   query.use(query.routes());
72   query.use(function* fetchApi(ctx, next) {
73     if (`${ctx.req().url}`.startsWith("/users/")) {
74-      ctx.json = { ok: true, data: mockUser2, value: mockUser2 };
75+      ctx.json = { ok: true, value: mockUser2 };
76       yield* next();
77       return;
78     }
79     const data = {
80       users: [mockUser],
81     };
82-    ctx.json = { ok: true, data, value: data };
83+    ctx.json = { ok: true, value: data };
84     yield* next();
85   });
86 
87@@ -578,6 +576,5 @@ it(tests, "stub predicate", async () => {
88   expect(actual).toEqual({
89     ok: true,
90     value: { frodo: "shire" },
91-    data: { frodo: "shire" },
92   });
93 });