Skip to content

Commit a80cc0f

Browse files
committed
feat: Setup basic prisma integration in Next
1 parent 264cddf commit a80cc0f

4 files changed

Lines changed: 39 additions & 26 deletions

File tree

.changeset/twelve-cows-turn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ts-rest/next': minor
3+
---
4+
5+
Add initial Next implementation, with a single endpoint based implementation

apps/example-next/pages/api/[...ts-rest].tsx

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { apiNested } from '@ts-rest/example-contracts';
22
import { createNextRoute, createNextRouter } from '@ts-rest/next';
3+
import { posts } from '../../server/posts';
34

45
const postsRouter = createNextRoute(apiNested.posts, {
56
createPost: async (args) => {
7+
const newPost = posts.createPost(args.body);
8+
69
return {
710
status: 200,
8-
body: {
9-
id: '1',
10-
},
11+
body: newPost,
1112
};
1213
},
1314
updatePost: async (args) => {
@@ -30,9 +31,9 @@ const postsRouter = createNextRoute(apiNested.posts, {
3031
};
3132
},
3233
getPost: async ({ params }) => {
33-
console.log(params);
34+
const post = await posts.getPost(params.id);
3435

35-
if (params.id === '2') {
36+
if (!post) {
3637
return {
3738
status: 404,
3839
body: null,
@@ -41,38 +42,24 @@ const postsRouter = createNextRoute(apiNested.posts, {
4142

4243
return {
4344
status: 200,
44-
body: {
45-
id: '1',
46-
title: 'title',
47-
tags: [],
48-
description: '',
49-
content: '',
50-
published: false,
51-
},
45+
body: post,
5246
};
5347
},
5448
getPosts: async (args) => {
49+
const allPosts = await posts.getPosts();
50+
5551
return {
5652
status: 200,
5753
body: {
58-
posts: [
59-
{
60-
id: '1',
61-
title: 'title',
62-
tags: [],
63-
description: '',
64-
content: '',
65-
published: false,
66-
},
67-
],
68-
total: 1,
54+
posts: allPosts,
55+
total: allPosts.length,
6956
},
7057
};
7158
},
7259
});
7360

7461
const healthRouter = createNextRoute(apiNested.health, {
75-
check: async () => {
62+
check: async (args) => {
7663
return {
7764
status: 200,
7865
body: { message: 'OK' },

apps/example-next/server/posts.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { PrismaClient } from '@prisma/client';
2+
3+
const prisma = new PrismaClient();
4+
5+
export const posts = {
6+
createPost: async (data: { title: string; content: string }) => {
7+
const newPost = await prisma.post.create({ data });
8+
9+
return newPost;
10+
},
11+
getPost: async (id: string) => {
12+
const post = await prisma.post.findUnique({ where: { id } });
13+
14+
return post || null;
15+
},
16+
getPosts: async () => {
17+
const posts = await prisma.post.findMany({});
18+
19+
return posts;
20+
},
21+
};

apps/example-next/test.http

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ DELETE {{host}}/posts/1 HTTP/1.1
2525
GET {{host}}/posts HTTP/1.1
2626

2727
### Get Post
28-
GET {{host}}/posts/2 HTTP/1.1
28+
GET {{host}}/posts/cl6lbxj360003pikjbw65z6ec HTTP/1.1
2929

3030
### Search Posts
3131
GET {{host}}/posts?search=REST HTTP/1.1

0 commit comments

Comments
 (0)