Skip to content
This repository was archived by the owner on Oct 26, 2026. It is now read-only.

Commit c88fb99

Browse files
committed
feat: Rename data to body
1 parent 2e662af commit c88fb99

20 files changed

Lines changed: 119 additions & 99 deletions

File tree

‎.changeset/nasty-pumpkins-tap.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@ts-rest/core': major
3+
'@ts-rest/express': major
4+
'@ts-rest/nest': major
5+
'@ts-rest/react-query': major
6+
---
7+
8+
Rename data to body to be more HTTP spec compliant

‎apps/docs/docs/core/errors.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ export const routerBasic = c.router({
2222
The default fetch client has support for this,
2323

2424
```typescript
25-
const { status, data } = await client.user({ params: { id: '1' } });
25+
const { status, body } = await client.user({ params: { id: '1' } });
2626

2727
if (status === 200) {
28-
console.log(data.email);
28+
console.log(body.email);
2929
} else if (status === 400) {
3030
console.log('Not found');
3131
} else {
@@ -40,15 +40,15 @@ The typed response includes the typed status codes along with any other possible
4040
```typescript
4141
const updatedUser: {
4242
status: 200;
43-
data: User
43+
body: User
4444
} | {
4545
status: 400;
46-
data: {
46+
body: {
4747
message: string;
4848
}
4949
} | {
5050
status: 100 | 101 | 102 | 201 | 202 | 203 | ... 47 more ... | 511;
51-
data: unknown;
51+
body: unknown;
5252
}
5353
```
5454

‎apps/docs/docs/core/fetch.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ Return type
4444
```typescript
4545
const data: {
4646
status: 200;
47-
data: User
47+
body: User
4848
} | {
4949
status: 400 | 100 | 101 | 102 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 300 | 301 | 302 | 303 | 304 | 305 | 307 | ... 36 more ... | 511;
50-
data: unknown;
50+
body: unknown;
5151
}
5252
```
5353

‎apps/docs/docs/express.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ const router = s.router(router, {
77
getPost: async ({ params: { id } }) => {
88
const post = prisma.post.findUnique({ where: { id } });
99

10-
return post ?? null;
10+
return {
11+
status: 200,
12+
body: post ?? null,
13+
};
1114
},
1215
});
1316

‎apps/docs/docs/nest.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ export class PostController implements ControllerShape {
1616
const post = await this.postService.getPost(id);
1717

1818
if (!post) {
19-
return { status: 404 as const, data: null };
19+
return { status: 404 as const, body: null };
2020
}
2121

22-
return { status: 200 as const, data: post };
22+
return { status: 200 as const, body: post };
2323
}
2424
}
2525
```

‎apps/docs/docs/quickstart.mdx‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export class PostController implements ControllerShape {
118118
async getPost(@ApiDecorator() { params: { id } }: RouteShape['getPost']) {
119119
const post = await this.postService.getPost(id);
120120

121-
return { status: 200 as const, data: post };
121+
return { status: 200 as const, body: post };
122122
}
123123

124124
@Api(s.route.createPost)
@@ -128,7 +128,7 @@ export class PostController implements ControllerShape {
128128
body: body.body,
129129
});
130130

131-
return { status: 201 as const, data: post };
131+
return { status: 201 as const, body: post };
132132
}
133133
}
134134
```
@@ -154,7 +154,7 @@ const router = s.router(contract, {
154154

155155
return {
156156
status: 200,
157-
data: post,
157+
body: post,
158158
};
159159
},
160160
createPost: async ({ body }) => {
@@ -164,7 +164,7 @@ const router = s.router(contract, {
164164

165165
return {
166166
status: 201,
167-
data: post,
167+
body: post,
168168
};
169169
},
170170
});
@@ -206,19 +206,19 @@ const client = initClient(contract, {
206206
baseHeaders: {},
207207
});
208208

209-
const { data, status } = await client.createPost({
209+
const { body, status } = await client.createPost({
210210
body: {
211211
title: 'Post Title',
212212
body: 'Post Body',
213213
},
214214
});
215215

216216
if (status === 201) {
217-
// data is Post
218-
console.log(data);
217+
// body is Post
218+
console.log(body);
219219
} else {
220-
// data is unknown
221-
console.log(data);
220+
// body is unknown
221+
console.log(body);
222222
}
223223
```
224224

@@ -244,7 +244,7 @@ export const Index = () => {
244244
return <div>Error</div>;
245245
}
246246

247-
return <div>{data.data.title}</div>;
247+
return <div>{data.body.title}</div>;
248248
};
249249
```
250250

@@ -258,7 +258,7 @@ If status is 2XX, it's part of the "data" return. If it's any other status code
258258
const data:
259259
| {
260260
status: 200;
261-
data: Post | null;
261+
body: Post | null;
262262
}
263263
| undefined;
264264
```
@@ -267,11 +267,11 @@ const data:
267267
const error:
268268
| {
269269
status: 404;
270-
data: null
270+
body: null
271271
}
272272
| {
273273
status: 404 | 100 | 101 | 102 | 300 | 301 | 302 | 303 | 304 | 305 | 307 | 308 | 400 | 401 | 402 | 403 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | ... 16 more ... | 511;
274-
data: unknown;
274+
body: unknown;
275275
}
276276
| null
277277
```

‎apps/docs/docs/react-query.md‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,24 @@ export const client = initReactQueryClient(router, {
1010

1111
const App = () => {
1212
// Effectively a useQuery hook
13-
const { data, isLoading } = client.posts.get.useQuery(['posts']);
13+
const { data, isLoading, error } = client.posts.get.useQuery(['posts']);
1414

1515
// Effectively a useMutation hook
1616
const { mutate, isLoading } = client.posts.create.useMutation();
1717

18+
if (isLoading) {
19+
return <div>Loading...</div>;
20+
}
21+
22+
if (data?.status !== 200) {
23+
return <div>Error</div>;
24+
}
25+
1826
return (
1927
<div>
20-
{isLoading ? (
21-
<div>Loading...</div>
22-
) : (
23-
<div>{data.map((post) => post.title)}</div>
24-
)}
28+
{data.body.map((post) => (
29+
<p key={post.id}>post.title</p>
30+
))}
2531
</div>
2632
);
2733
};
@@ -33,7 +39,7 @@ const App = () => {
3339

3440
```typescript
3541
// Normal fetch
36-
const { data } = await client.posts.get.query();
42+
const { body, status } = await client.posts.get.query();
3743

3844
// useQuery hook
3945
const { data, isLoading } = client.posts.get.useQuery();

‎apps/example-express/src/main.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ const completedRouter = s.router(apiBlog, {
2222
if (!post) {
2323
return {
2424
status: 404,
25-
data: null,
25+
body: null,
2626
};
2727
}
2828

2929
return {
3030
status: 200,
31-
data: post,
31+
body: post,
3232
};
3333
},
3434
getPosts: async ({ query }) => {
@@ -40,7 +40,7 @@ const completedRouter = s.router(apiBlog, {
4040

4141
return {
4242
status: 200,
43-
data: {
43+
body: {
4444
posts,
4545
total: 0,
4646
},
@@ -53,7 +53,7 @@ const completedRouter = s.router(apiBlog, {
5353

5454
return {
5555
status: 201,
56-
data: post,
56+
body: post,
5757
};
5858
},
5959
updatePost: async ({ body, params }) => {
@@ -66,7 +66,7 @@ const completedRouter = s.router(apiBlog, {
6666

6767
return {
6868
status: 200,
69-
data: post,
69+
body: post,
7070
};
7171
},
7272
deletePost: async ({ params }) => {
@@ -78,7 +78,7 @@ const completedRouter = s.router(apiBlog, {
7878

7979
return {
8080
status: 200,
81-
data: { message: 'Post deleted' },
81+
body: { message: 'Post deleted' },
8282
};
8383
},
8484
});

‎apps/example-nest/src/app/post.controller.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('PostController', () => {
3030
.get('/posts')
3131
.expect(200)
3232
.expect({
33-
data: postService.getPosts({}),
33+
body: postService.getPosts({}),
3434
});
3535
});
3636

‎apps/example-nest/src/app/post.controller.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class PostController implements ControllerShape {
2323

2424
return {
2525
status: 200 as const,
26-
data: { posts, total: totalPosts },
26+
body: { posts, total: totalPosts },
2727
};
2828
}
2929

@@ -32,10 +32,10 @@ export class PostController implements ControllerShape {
3232
const post = await this.postService.getPost(id);
3333

3434
if (!post) {
35-
return { status: 404 as const, data: null };
35+
return { status: 404 as const, body: null };
3636
}
3737

38-
return { status: 200 as const, data: post };
38+
return { status: 200 as const, body: post };
3939
}
4040

4141
@Api(s.route.createPost)
@@ -47,7 +47,7 @@ export class PostController implements ControllerShape {
4747
description: body.description,
4848
});
4949

50-
return { status: 201 as const, data: post };
50+
return { status: 201 as const, body: post };
5151
}
5252

5353
@Api(s.route.updatePost)
@@ -61,7 +61,7 @@ export class PostController implements ControllerShape {
6161
description: body.description,
6262
});
6363

64-
return { status: 200 as const, data: post };
64+
return { status: 200 as const, body: post };
6565
}
6666

6767
@Api(s.route.deletePost)
@@ -70,6 +70,6 @@ export class PostController implements ControllerShape {
7070
) {
7171
await this.postService.deletePost(id);
7272

73-
return { status: 200 as const, data: { message: 'Post Deleted' } };
73+
return { status: 200 as const, body: { message: 'Post Deleted' } };
7474
}
7575
}

0 commit comments

Comments
 (0)