Skip to content

Commit 33d6a57

Browse files
authored
feat: add single route implementation helper (#537)
1 parent 15d4926 commit 33d6a57

11 files changed

Lines changed: 76 additions & 44 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@ts-rest/express': minor
3+
'@ts-rest/fastify': minor
4+
'@ts-rest/next': minor
5+
---
6+
7+
Add single route implementation helper
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { initServer } from '@ts-rest/express';
2+
import { mockPostFixtureFactory } from './fixtures';
3+
import { apiBlog } from '@ts-rest/example-contracts';
4+
5+
const s = initServer();
6+
export const getPost = s.route(apiBlog.getPost, async ({ params: { id } }) => {
7+
const post = mockPostFixtureFactory({ id });
8+
9+
if (!post) {
10+
return {
11+
status: 404,
12+
body: null,
13+
};
14+
}
15+
16+
return {
17+
status: 200,
18+
body: post,
19+
};
20+
});

apps/example-express/src/main.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { serve, setup } from 'swagger-ui-express';
99
import { mockPostFixtureFactory } from './fixtures';
1010
import cors = require('cors');
1111
import { tsRouter } from './ts-router';
12+
import { getPost } from './get-post';
1213

1314
const app = express();
1415

@@ -19,21 +20,7 @@ app.use(bodyParser.json());
1920
const s = initServer();
2021

2122
const completedRouter = s.router(apiBlog, {
22-
getPost: async ({ params: { id } }) => {
23-
const post = mockPostFixtureFactory({ id });
24-
25-
if (!post) {
26-
return {
27-
status: 404,
28-
body: null,
29-
};
30-
}
31-
32-
return {
33-
status: 200,
34-
body: post,
35-
};
36-
},
23+
getPost,
3724
getPosts: async ({ query }) => {
3825
const posts = [
3926
mockPostFixtureFactory({ id: '1' }),
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { initServer } from '@ts-rest/fastify';
2+
import { apiBlog } from '@ts-rest/example-contracts';
3+
import { mockPostFixtureFactory } from './main';
4+
5+
const s = initServer();
6+
export const getPost = s.route(apiBlog.getPost, async ({ params: { id } }) => {
7+
const post = mockPostFixtureFactory({ id });
8+
9+
if (!post) {
10+
return {
11+
status: 404,
12+
body: null,
13+
};
14+
}
15+
16+
return {
17+
status: 200,
18+
body: post,
19+
};
20+
});

apps/example-fastify/src/main.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as fastify from 'fastify';
22
import { initServer } from '@ts-rest/fastify';
33
import { apiBlog, Post } from '@ts-rest/example-contracts';
4+
import { getPost } from './get-post';
45

56
export const mockPostFixtureFactory = (partial: Partial<Post>): Post => ({
67
id: 'mock-id',
@@ -21,21 +22,7 @@ app.get('/', async (request, reply) => {
2122
const s = initServer();
2223

2324
const router = s.router(apiBlog, {
24-
getPost: async ({ params: { id } }) => {
25-
const post = mockPostFixtureFactory({ id });
26-
27-
if (!post) {
28-
return {
29-
status: 404,
30-
body: null,
31-
};
32-
}
33-
34-
return {
35-
status: 200,
36-
body: post,
37-
};
38-
},
25+
getPost,
3926
getPosts: async ({ query }) => {
4027
const posts = [
4128
mockPostFixtureFactory({ id: '1' }),

libs/ts-rest/express/src/lib/ts-rest-express.spec.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,18 @@ describe('ts-rest-express', () => {
9797

9898
const server = initServer();
9999

100-
const router = server.router(contract, {
101-
postIndex: async ({ body: { echoHtml } }) => {
100+
const postIndex = server.route(
101+
contract.postIndex,
102+
async ({ body: { echoHtml } }) => {
102103
return {
103104
status: 200,
104105
body: echoHtml,
105106
};
106107
},
108+
);
109+
110+
const router = server.router(contract, {
111+
postIndex,
107112
getRobots: async () => {
108113
return {
109114
status: 200,

libs/ts-rest/express/src/lib/ts-rest-express.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export const initServer = () => {
3030
return {
3131
router: <T extends AppRouter>(router: T, args: RecursiveRouterObj<T>) =>
3232
args,
33+
route: <T extends AppRoute>(
34+
route: T,
35+
args: AppRouteImplementationOrOptions<T>,
36+
) => args,
3337
};
3438
};
3539

libs/ts-rest/fastify/src/lib/ts-rest-fastify.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ describe('ts-rest-fastify', () => {
6565
},
6666
};
6767
},
68-
ping: async ({ body }) => {
68+
ping: s.route(contract.ping, async ({ body }) => {
6969
return {
7070
status: 200,
7171
body: {
7272
pong: body.ping,
7373
},
7474
};
75-
},
75+
}),
7676
testPathParams: async ({ params }) => {
7777
return {
7878
status: 200,

libs/ts-rest/fastify/src/lib/ts-rest-fastify.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ export const initServer = () => ({
113113
contract,
114114
routes,
115115
}),
116+
route: <TAppRoute extends AppRoute>(
117+
route: TAppRoute,
118+
implementation: AppRouteImplementation<TAppRoute>,
119+
) => implementation,
116120
registerRouter: <
117121
T extends InitialisedRouter<TContract>,
118122
TContract extends AppRouter,

libs/ts-rest/next/src/lib/ts-rest-next.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ const contract = c.router({
6666
});
6767

6868
const nextEndpoint = createNextRoute(contract, {
69-
get: async ({ query: { test } }) => {
69+
get: createNextRoute(contract.get, async ({ query: { test } }) => {
7070
return {
7171
status: 200,
7272
body: {
7373
message: test,
7474
},
7575
};
76-
},
76+
}),
7777
getWithParams: async ({ params: { id } }) => {
7878
return {
7979
status: 200,

0 commit comments

Comments
 (0)