Skip to content

Commit

Permalink
refactor: remove Result.data
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `ctx.json` no longer has a `.data` property
  • Loading branch information
neurosnap committed Mar 5, 2024
1 parent 7b09e81 commit 6313833
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 43 deletions.
5 changes: 0 additions & 5 deletions mdw/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ export function* json<CurCtx extends FetchJsonCtx = FetchJsonCtx>(
if (ctx.response.status === 204) {
ctx.json = {
ok: true,
data: {},
value: {},
};
yield* next();
Expand All @@ -120,21 +119,18 @@ export function* json<CurCtx extends FetchJsonCtx = FetchJsonCtx>(
if (ctx.response.ok) {
ctx.json = {
ok: true,
data: data.value,
value: data.value,
};
} else {
ctx.json = {
ok: false,
data: data.value,
error: data.value,
};
}
} else {
const dta = { message: data.error.message };
ctx.json = {
ok: false,
data: dta,
error: dta,
};
}
Expand Down Expand Up @@ -190,7 +186,6 @@ export function* payload<CurCtx extends FetchJsonCtx = FetchJsonCtx>(
`found :${key} in endpoint name (${ctx.name}) but payload has falsy value (${val})`;
ctx.json = {
ok: false,
data,
error: data,
};
return;
Expand Down
2 changes: 1 addition & 1 deletion mdw/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export function* queryCtx<Ctx extends ApiCtx = ApiCtx>(ctx: Ctx, next: Next) {
}
if (!ctx.request) ctx.request = ctx.req();
if (!ctx.response) ctx.response = null;
if (!ctx.json) ctx.json = { ok: false, data: {}, error: {} };
if (!ctx.json) ctx.json = { ok: false, error: {} };
if (!ctx.actions) ctx.actions = [];
if (!ctx.bodyType) ctx.bodyType = "json";
yield* next();
Expand Down
8 changes: 0 additions & 8 deletions query/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,10 @@ export type ApiFetchResult<ApiSuccess = any, ApiError = any> =
| {
ok: true;
value: ApiSuccess;
/**
* @deprecated Use {@link ApiFetchResult.value} instead.
*/
data: ApiSuccess;
}
| {
ok: false;
error: ApiError;
/**
* @deprecated Use {@link ApiFetchResult.error} instead.
*/
data: ApiError;
};

export type ApiRequest = Partial<{ url: string } & RequestInit>;
Expand Down
9 changes: 4 additions & 5 deletions test/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,6 @@ it(tests, "with hash key on a large post", async () => {
ctx.response = new Response();
ctx.json = {
ok: true,
data: curUsers,
value: curUsers,
};
},
Expand Down Expand Up @@ -353,7 +352,7 @@ it(tests, "ensure types for get() endpoint", () => {
api.use(function* (ctx, next) {
yield* next();
const data = { result: "wow" };
ctx.json = { ok: true, data, value: data };
ctx.json = { ok: true, value: data };
});

const acc: string[] = [];
Expand Down Expand Up @@ -391,7 +390,7 @@ it(tests, "ensure ability to cast `ctx` in function definition", () => {
api.use(function* (ctx, next) {
yield* next();
const data = { result: "wow" };
ctx.json = { ok: true, data, value: data };
ctx.json = { ok: true, value: data };
});

const acc: string[] = [];
Expand Down Expand Up @@ -428,7 +427,7 @@ it(
api.use(function* (ctx, next) {
yield* next();
const data = { result: "wow" };
ctx.json = { ok: true, data, value: data };
ctx.json = { ok: true, value: data };
});

const acc: string[] = [];
Expand Down Expand Up @@ -493,7 +492,7 @@ it(
api.use(function* (ctx, next) {
yield* next();
const data = { result: "wow" };
ctx.json = { ok: true, data, value: data };
ctx.json = { ok: true, value: data };
});

const acc: string[] = [];
Expand Down
22 changes: 9 additions & 13 deletions test/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ it(
headers: {
"Content-Type": "application/json",
},
}, { ok: true, data: mockUser, value: mockUser }]);
}, { ok: true, value: mockUser }]);
},
);

Expand Down Expand Up @@ -109,7 +109,7 @@ it(
await store.run(waitForLoader(schema.loaders, action));

const data = "this is some text";
expect(actual).toEqual({ ok: true, data, value: data });
expect(actual).toEqual({ ok: true, value: data });
},
);

Expand Down Expand Up @@ -146,7 +146,7 @@ it(tests, "error handling", async () => {

const state = store.getState();
expect(state.cache[action.payload.key]).toEqual(errMsg);
expect(actual).toEqual({ ok: false, data: errMsg, error: errMsg });
expect(actual).toEqual({ ok: false, error: errMsg });
});

it(tests, "status 204", async () => {
Expand Down Expand Up @@ -185,7 +185,7 @@ it(tests, "status 204", async () => {

const state = store.getState();
expect(state.cache[action.payload.key]).toEqual({});
expect(actual).toEqual({ ok: true, data: {}, value: {} });
expect(actual).toEqual({ ok: true, value: {} });
});

it(tests, "malformed json", async () => {
Expand Down Expand Up @@ -227,7 +227,6 @@ it(tests, "malformed json", async () => {
};
expect(actual).toEqual({
ok: false,
data,
error: data,
});
});
Expand Down Expand Up @@ -278,7 +277,6 @@ it(tests, "POST", async () => {

expect(loader.value.meta.json).toEqual({
ok: true,
data: mockUser,
value: mockUser,
});
});
Expand Down Expand Up @@ -351,7 +349,6 @@ it(tests, "POST multiple endpoints with same uri", async () => {

expect(result1.value.meta.json).toEqual({
ok: true,
data: mockUser,
value: mockUser,
});

Expand All @@ -366,7 +363,6 @@ it(tests, "POST multiple endpoints with same uri", async () => {

expect(result2.value.meta.json).toEqual({
ok: true,
data: mockUser,
value: mockUser,
});
});
Expand Down Expand Up @@ -454,7 +450,7 @@ it(

const state = store.getState();
expect(state.cache[action.payload.key]).toEqual(mockUser);
expect(actual).toEqual({ ok: true, data: mockUser, value: mockUser });
expect(actual).toEqual({ ok: true, value: mockUser });
},
);

Expand Down Expand Up @@ -493,7 +489,7 @@ it(
throw loader.error;
}
const data = { message: "error" };
expect(actual).toEqual({ ok: false, data, error: data });
expect(actual).toEqual({ ok: false, error: data });
},
);

Expand Down Expand Up @@ -523,7 +519,7 @@ it(
if (!loader.ok) {
throw loader.error;
}
expect(actual).toEqual({ ok: true, data: mockUser, value: mockUser });
expect(actual).toEqual({ ok: true, value: mockUser });
},
);

Expand Down Expand Up @@ -553,7 +549,7 @@ it(tests, "should use dynamic mdw to mock response", async () => {
if (!loader.ok) {
throw loader.error;
}
expect(actual).toEqual({ ok: true, data: dynamicUser, value: dynamicUser });
expect(actual).toEqual({ ok: true, value: dynamicUser });

// reset dynamic mdw and try again
api.reset();
Expand All @@ -562,5 +558,5 @@ it(tests, "should use dynamic mdw to mock response", async () => {
if (!loader.ok) {
throw loader.error;
}
expect(actual).toEqual({ ok: true, data: mockUser, value: mockUser });
expect(actual).toEqual({ ok: true, value: mockUser });
});
19 changes: 8 additions & 11 deletions test/mdw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ it(tests, "basic", () => {
query.use(query.routes());
query.use(function* fetchApi(ctx, next) {
if (`${ctx.req().url}`.startsWith("/users/")) {
ctx.json = { ok: true, data: mockUser2, value: mockUser2 };
ctx.json = { ok: true, value: mockUser2 };
yield* next();
return;
}
const data = {
users: [mockUser],
};
ctx.json = { ok: true, data, value: data };
ctx.json = { ok: true, value: data };
yield* next();
});

Expand Down Expand Up @@ -116,7 +116,7 @@ it(tests, "with loader", () => {
api.use(function* fetchApi(ctx, next) {
ctx.response = new Response(jsonBlob(mockUser), { status: 200 });
const data = { users: [mockUser] };
ctx.json = { ok: true, data, value: data };
ctx.json = { ok: true, value: data };
yield* next();
});

Expand Down Expand Up @@ -158,7 +158,7 @@ it(tests, "with item loader", () => {
api.use(function* fetchApi(ctx, next) {
ctx.response = new Response(jsonBlob(mockUser), { status: 200 });
const data = { users: [mockUser] };
ctx.json = { ok: true, data, value: data };
ctx.json = { ok: true, value: data };
yield* next();
});

Expand Down Expand Up @@ -254,7 +254,7 @@ it(tests, "simpleCache", () => {
api.use(function* fetchApi(ctx, next) {
const data = { users: [mockUser] };
ctx.response = new Response(jsonBlob(data));
ctx.json = { ok: true, data, value: data };
ctx.json = { ok: true, value: data };
yield* next();
});

Expand Down Expand Up @@ -283,7 +283,7 @@ it(tests, "overriding default loader behavior", () => {
api.use(function* fetchApi(ctx, next) {
const data = { users: [mockUser] };
ctx.response = new Response(jsonBlob(data));
ctx.json = { ok: true, data, value: data };
ctx.json = { ok: true, value: data };
yield* next();
});

Expand Down Expand Up @@ -390,7 +390,6 @@ it(tests, "createApi with own key", async () => {
ctx.response = new Response();
ctx.json = {
ok: true,
data: curUsers,
value: curUsers,
};
},
Expand Down Expand Up @@ -462,7 +461,6 @@ it(tests, "createApi with custom key but no payload", async () => {
ctx.response = new Response();
ctx.json = {
ok: true,
data: curUsers,
value: curUsers,
};
},
Expand Down Expand Up @@ -510,14 +508,14 @@ it(tests, "errorHandler", () => {
query.use(query.routes());
query.use(function* fetchApi(ctx, next) {
if (`${ctx.req().url}`.startsWith("/users/")) {
ctx.json = { ok: true, data: mockUser2, value: mockUser2 };
ctx.json = { ok: true, value: mockUser2 };
yield* next();
return;
}
const data = {
users: [mockUser],
};
ctx.json = { ok: true, data, value: data };
ctx.json = { ok: true, value: data };
yield* next();
});

Expand Down Expand Up @@ -578,6 +576,5 @@ it(tests, "stub predicate", async () => {
expect(actual).toEqual({
ok: true,
value: { frodo: "shire" },
data: { frodo: "shire" },
});
});

0 comments on commit 6313833

Please sign in to comment.