Skip to content

Commit 7268b16

Browse files
committed
feat: use body instead of data for response payload name
1 parent ef6e3a8 commit 7268b16

File tree

5 files changed

+22
-22
lines changed

5 files changed

+22
-22
lines changed

lib/build/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export function generateClientType(server: Server) {
120120
ok: { name: (+code >= 200 && +code < 300) ? "true" : "false", required: true },
121121
headers: { name: "Headers", required: true },
122122
url: { name: "string", required: true },
123-
data: { node: generateType(schema as ExtendedObjectSchema), required: isRequired(schema as ExtendedSchema) },
123+
body: { node: generateType(schema as ExtendedObjectSchema), required: isRequired(schema as ExtendedSchema) },
124124
});
125125
statements.push(typeAliasDeclaration(responseCodeTypeName, responseNode, true));
126126
responseTypeList.push(responseCodeTypeName);
@@ -139,7 +139,7 @@ export function generateClientType(server: Server) {
139139
ok: { name: "boolean", required: true },
140140
headers: { name: "Headers", required: true },
141141
url: { name: "string", required: true },
142-
data: { name: "unknown", required: false },
142+
body: { name: "unknown", required: false },
143143
});
144144
statements.push(typeAliasDeclaration(unknownResponseName, unknownResponseNode, true));
145145
responseTypeList.push(unknownResponseName);

lib/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
MatchingRoute,
77
RequestMethod,
88
ResponseType,
9-
ResultType,
9+
BodyType,
1010
Route,
1111
Simplify,
1212
StatusCode,
@@ -84,7 +84,7 @@ export class Client<T extends Route> {
8484
status: injectResponse.statusCode as StatusCode,
8585
ok: injectResponse.statusCode >= 200 && injectResponse.statusCode < 300,
8686
headers: new Headers(injectResponse.headers as Record<string, string>),
87-
data: isJsonResponse ? JSON.parse(injectResponse.payload) as ResultType<R> : injectResponse.payload,
87+
body: isJsonResponse ? JSON.parse(injectResponse.payload) as BodyType<R> : injectResponse.payload,
8888
};
8989

9090
return response;
@@ -111,7 +111,7 @@ export class Client<T extends Route> {
111111
status: fetchResponse.status as StatusCode,
112112
ok: fetchResponse.ok,
113113
headers: fetchResponse.headers,
114-
data: isJsonResponse ? (await fetchResponse.json()) as ResultType<R> : (await fetchResponse.text()),
114+
body: isJsonResponse ? (await fetchResponse.json()) as BodyType<R> : (await fetchResponse.text()),
115115
};
116116

117117
return response;

lib/types.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export type RouteSettings = {
7676
ok: boolean;
7777
headers: Headers;
7878
url: string;
79-
data?: unknown;
79+
body?: unknown;
8080
};
8181
};
8282

@@ -116,17 +116,17 @@ export type PayloadType<R extends Route> = R["settings"]["payload"];
116116
*/
117117
export type ResponseType<R extends Route> = R["settings"]["response"];
118118
/**
119-
* Get the result type for a route (this is likely a union)
119+
* Get the response body type for a route (this is likely a union)
120120
*/
121-
export type ResultType<R extends Route> = R["settings"]["response"]["data"];
121+
export type BodyType<R extends Route> = R["settings"]["response"]["body"];
122122
/**
123123
* Get the response type for a route matching a specific status code
124124
*/
125125
export type SpecificResponseType<R extends Route, S extends StatusCode = StatusCode> = Extract<R["settings"]["response"], { status: S }>;
126126
/**
127-
* Get the result type for a route matching a specific status code
127+
* Get the response body type for a route matching a specific status code
128128
*/
129-
export type SpecificResultType<R extends Route, S extends StatusCode = StatusCode> = SpecificResponseType<R, S>["data"];
129+
export type SpecificBodyType<R extends Route, S extends StatusCode = StatusCode> = SpecificResponseType<R, S>["body"];
130130

131131
/**
132132
* Get a list of all available paths for a given method on a route union

test/fetch.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { test, type TestContext } from "node:test";
22
import { server, Client } from "./fixture.ts";
3-
import type { MatchingRoute, QueryType, SpecificResultType } from "@code4rena/typed-client";
3+
import type { MatchingRoute, QueryType, SpecificBodyType } from "@code4rena/typed-client";
44
import type { Routes } from "./generated.ts";
55

66
await test("fetch()", async (t) => {
@@ -44,7 +44,7 @@ await test("fetch()", async (t) => {
4444
const res = await client.get("/simple");
4545
t.assert.equal(res.status, 200);
4646
t.assert.equal(res.url, `${server.info.uri}/simple`);
47-
t.assert.deepStrictEqual<SpecificResultType<routeType, 200>>(res.data, { success: true });
47+
t.assert.deepStrictEqual<SpecificBodyType<routeType, 200>>(res.body, { success: true });
4848
});
4949

5050
await t.test("/query", async (t: TestContext) => {
@@ -53,13 +53,13 @@ await test("fetch()", async (t) => {
5353
const withoutQueryRes = await client.get("/query");
5454
t.assert.equal(withoutQueryRes.status, 200);
5555
t.assert.equal(withoutQueryRes.url, `${server.info.uri}/query`);
56-
t.assert.deepStrictEqual<SpecificResultType<routeType, 200>>(withoutQueryRes.data, { flag: false });
56+
t.assert.deepStrictEqual<SpecificBodyType<routeType, 200>>(withoutQueryRes.body, { flag: false });
5757

5858
const query: QueryType<routeType> = { flag: true };
5959
const withQueryRes = await client.get("/query", { query });
6060
t.assert.equal(withQueryRes.status, 200);
6161
t.assert.equal(withQueryRes.url, `${server.info.uri}/query?flag=true`);
62-
t.assert.deepStrictEqual<SpecificResultType<routeType, 200>>(withQueryRes.data, { flag: true });
62+
t.assert.deepStrictEqual<SpecificBodyType<routeType, 200>>(withQueryRes.body, { flag: true });
6363
});
6464

6565
await t.test("/param/{param}", async (t: TestContext) => {
@@ -68,11 +68,11 @@ await test("fetch()", async (t) => {
6868
const passingRes = await client.get("/param/{param}", { params: { param: "pass" } });
6969
t.assert.equal(passingRes.status, 200);
7070
t.assert.equal(passingRes.url, `${server.info.uri}/param/pass`);
71-
t.assert.deepStrictEqual<SpecificResultType<routeType, 200>>(passingRes.data, { success: true });
71+
t.assert.deepStrictEqual<SpecificBodyType<routeType, 200>>(passingRes.body, { success: true });
7272

7373
const failingRes = await client.get("/param/{param}", { params: { param: "fail" } });
7474
t.assert.equal(failingRes.status, 400);
7575
t.assert.equal(failingRes.url, `${server.info.uri}/param/fail`);
76-
t.assert.deepStrictEqual<SpecificResultType<routeType, 400>>(failingRes.data, { success: false, message: "failed" });
76+
t.assert.deepStrictEqual<SpecificBodyType<routeType, 400>>(failingRes.body, { success: false, message: "failed" });
7777
});
7878
});

test/inject.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { test, type TestContext } from "node:test";
22
import { server, Client } from "./fixture.ts";
3-
import type { MatchingRoute, QueryType, SpecificResultType } from "@code4rena/typed-client";
3+
import type { MatchingRoute, QueryType, SpecificBodyType } from "@code4rena/typed-client";
44
import type { Routes } from "./generated.ts";
55

66
await test("GET - server.inject", async (t) => {
@@ -12,7 +12,7 @@ await test("GET - server.inject", async (t) => {
1212
const res = await client.get("/simple");
1313
t.assert.equal(res.status, 200);
1414
t.assert.equal(res.url, "/simple");
15-
t.assert.deepStrictEqual<SpecificResultType<routeType, 200>>(res.data, { success: true });
15+
t.assert.deepStrictEqual<SpecificBodyType<routeType, 200>>(res.body, { success: true });
1616
});
1717

1818
await t.test("/query", async (t: TestContext) => {
@@ -21,13 +21,13 @@ await test("GET - server.inject", async (t) => {
2121
const withoutQueryRes = await client.get("/query");
2222
t.assert.equal(withoutQueryRes.status, 200);
2323
t.assert.equal(withoutQueryRes.url, "/query");
24-
t.assert.deepStrictEqual<SpecificResultType<routeType, 200>>(withoutQueryRes.data, { flag: false });
24+
t.assert.deepStrictEqual<SpecificBodyType<routeType, 200>>(withoutQueryRes.body, { flag: false });
2525

2626
const query: QueryType<routeType> = { flag: true };
2727
const withQueryRes = await client.get("/query", { query });
2828
t.assert.equal(withQueryRes.status, 200);
2929
t.assert.equal(withQueryRes.url, "/query?flag=true");
30-
t.assert.deepStrictEqual<SpecificResultType<routeType, 200>>(withQueryRes.data, { flag: true });
30+
t.assert.deepStrictEqual<SpecificBodyType<routeType, 200>>(withQueryRes.body, { flag: true });
3131
});
3232

3333
await t.test("/param/{param}", async (t: TestContext) => {
@@ -36,11 +36,11 @@ await test("GET - server.inject", async (t) => {
3636
const passingRes = await client.get("/param/{param}", { params: { param: "pass" } });
3737
t.assert.equal(passingRes.status, 200);
3838
t.assert.equal(passingRes.url, "/param/pass");
39-
t.assert.deepStrictEqual<SpecificResultType<routeType, 200>>(passingRes.data, { success: true });
39+
t.assert.deepStrictEqual<SpecificBodyType<routeType, 200>>(passingRes.body, { success: true });
4040

4141
const failingRes = await client.get("/param/{param}", { params: { param: "fail" } });
4242
t.assert.equal(failingRes.status, 400);
4343
t.assert.equal(failingRes.url, "/param/fail");
44-
t.assert.deepStrictEqual<SpecificResultType<routeType, 400>>(failingRes.data, { success: false, message: "failed" });
44+
t.assert.deepStrictEqual<SpecificBodyType<routeType, 400>>(failingRes.body, { success: false, message: "failed" });
4545
});
4646
});

0 commit comments

Comments
 (0)