Skip to content

Commit 153047b

Browse files
authored
feat: add tanstack start adapter (#2266)
1 parent 07ab028 commit 153047b

File tree

4 files changed

+378
-1
lines changed

4 files changed

+378
-1
lines changed

packages/server/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"nextjs",
2323
"sveltekit",
2424
"nuxtjs",
25-
"elysia"
25+
"elysia",
26+
"tanstack-start"
2627
],
2728
"author": "ZenStack Team",
2829
"license": "MIT",
@@ -74,6 +75,7 @@
7475
"./nestjs": "./nestjs/index.js",
7576
"./hono": "./hono/index.js",
7677
"./elysia": "./elysia/index.js",
78+
"./tanstack-start": "./tanstack-start/index.js",
7779
"./types": "./types.js"
7880
}
7981
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/* eslint-disable @typescript-eslint/no-non-null-assertion */
2+
3+
import { DbClientContract } from '@zenstackhq/runtime';
4+
import { TanStackStartOptions } from '.';
5+
import { RPCApiHandler } from '../api';
6+
import { loadAssets } from '../shared';
7+
8+
/**
9+
* Creates a TanStack Start server route handler which encapsulates Prisma CRUD operations.
10+
*
11+
* @param options Options for initialization
12+
* @returns A TanStack Start server route handler
13+
*/
14+
export default function factory(
15+
options: TanStackStartOptions
16+
): ({ request, params }: { request: Request; params: Record<string, string> }) => Promise<Response> {
17+
const { modelMeta, zodSchemas } = loadAssets(options);
18+
19+
const requestHandler = options.handler || RPCApiHandler();
20+
21+
return async ({ request, params }: { request: Request; params: Record<string, string> }) => {
22+
const prisma = (await options.getPrisma(request, params)) as DbClientContract;
23+
if (!prisma) {
24+
return new Response(JSON.stringify({ message: 'unable to get prisma from request context' }), {
25+
status: 500,
26+
headers: {
27+
'Content-Type': 'application/json',
28+
},
29+
});
30+
}
31+
32+
const url = new URL(request.url);
33+
const query = Object.fromEntries(url.searchParams);
34+
35+
// Extract path from params._splat for catch-all routes
36+
const path = params._splat;
37+
38+
if (!path) {
39+
return new Response(JSON.stringify({ message: 'missing path parameter' }), {
40+
status: 400,
41+
headers: {
42+
'Content-Type': 'application/json',
43+
},
44+
});
45+
}
46+
47+
let requestBody: unknown;
48+
if (request.body) {
49+
try {
50+
requestBody = await request.json();
51+
} catch {
52+
// noop
53+
}
54+
}
55+
56+
try {
57+
const r = await requestHandler({
58+
method: request.method!,
59+
path,
60+
query,
61+
requestBody,
62+
prisma,
63+
modelMeta,
64+
zodSchemas,
65+
logger: options.logger,
66+
});
67+
return new Response(JSON.stringify(r.body), {
68+
status: r.status,
69+
headers: {
70+
'Content-Type': 'application/json',
71+
},
72+
});
73+
} catch (err) {
74+
return new Response(JSON.stringify({ message: `An unhandled error occurred: ${err}` }), {
75+
status: 500,
76+
headers: {
77+
'Content-Type': 'application/json',
78+
},
79+
});
80+
}
81+
};
82+
}
83+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { AdapterBaseOptions } from '../types';
2+
import { default as Handler } from './handler';
3+
4+
/**
5+
* Options for initializing a TanStack Start server route handler.
6+
*/
7+
export interface TanStackStartOptions extends AdapterBaseOptions {
8+
/**
9+
* Callback method for getting a Prisma instance for the given request and params.
10+
*/
11+
getPrisma: (request: Request, params: Record<string, string>) => Promise<unknown> | unknown;
12+
}
13+
14+
/**
15+
* Creates a TanStack Start server route handler.
16+
* @see https://zenstack.dev/docs/reference/server-adapters/tanstack-start
17+
*/
18+
export function TanStackStartHandler(options: TanStackStartOptions): ReturnType<typeof Handler> {
19+
return Handler(options);
20+
}
21+
22+
export default TanStackStartHandler;
23+
Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
/* eslint-disable @typescript-eslint/no-var-requires */
2+
/* eslint-disable @typescript-eslint/no-explicit-any */
3+
import { loadSchema } from '@zenstackhq/testtools';
4+
import path from 'path';
5+
import Rest from '../../src/api/rest';
6+
import { TanStackStartHandler, TanStackStartOptions } from '../../src/tanstack-start';
7+
8+
function makeRequest(method: string, url: string, body?: any): Request {
9+
const payload = body ? JSON.stringify(body) : undefined;
10+
return new Request(url, { method, body: payload });
11+
}
12+
13+
async function unmarshal(response: Response): Promise<any> {
14+
const text = await response.text();
15+
return JSON.parse(text);
16+
}
17+
18+
interface TestClient {
19+
get: () => Promise<{ status: number; body: any }>;
20+
post: () => { send: (data: any) => Promise<{ status: number; body: any }> };
21+
put: () => { send: (data: any) => Promise<{ status: number; body: any }> };
22+
del: () => Promise<{ status: number; body: any }>;
23+
}
24+
25+
function makeTestClient(apiPath: string, options: TanStackStartOptions, qArg?: unknown, otherArgs?: any): TestClient {
26+
const pathParts = apiPath.split('/').filter((p) => p);
27+
const path = pathParts.join('/');
28+
29+
const handler = TanStackStartHandler(options);
30+
31+
const params = {
32+
_splat: path,
33+
...otherArgs,
34+
};
35+
36+
const buildUrl = (method: string) => {
37+
const baseUrl = `http://localhost${apiPath}`;
38+
if (method === 'GET' || method === 'DELETE') {
39+
const url = new URL(baseUrl);
40+
if (qArg) {
41+
url.searchParams.set('q', JSON.stringify(qArg));
42+
}
43+
if (otherArgs) {
44+
Object.entries(otherArgs).forEach(([key, value]) => {
45+
url.searchParams.set(key, String(value));
46+
});
47+
}
48+
return url.toString();
49+
}
50+
return baseUrl;
51+
};
52+
53+
const executeRequest = async (method: string, body?: any) => {
54+
const url = buildUrl(method);
55+
const request = makeRequest(method, url, body);
56+
const response = await handler({ request, params });
57+
const responseBody = await unmarshal(response);
58+
return {
59+
status: response.status,
60+
body: responseBody,
61+
};
62+
};
63+
64+
return {
65+
get: async () => executeRequest('GET'),
66+
post: () => ({
67+
send: async (data: any) => executeRequest('POST', data),
68+
}),
69+
put: () => ({
70+
send: async (data: any) => executeRequest('PUT', data),
71+
}),
72+
del: async () => executeRequest('DELETE'),
73+
};
74+
}
75+
76+
describe('TanStack Start adapter tests - rpc handler', () => {
77+
let origDir: string;
78+
79+
beforeEach(() => {
80+
origDir = process.cwd();
81+
});
82+
83+
afterEach(() => {
84+
process.chdir(origDir);
85+
});
86+
87+
it('simple crud', async () => {
88+
const model = `
89+
model M {
90+
id String @id @default(cuid())
91+
value Int
92+
}
93+
`;
94+
95+
const { prisma } = await loadSchema(model);
96+
97+
const client = await makeTestClient('/m/create', { getPrisma: () => prisma }).post().send({ data: { id: '1', value: 1 } });
98+
expect(client.status).toBe(201);
99+
expect(client.body.data.value).toBe(1);
100+
101+
const findUnique = await makeTestClient('/m/findUnique', { getPrisma: () => prisma }, { where: { id: '1' } }).get();
102+
expect(findUnique.status).toBe(200);
103+
expect(findUnique.body.data.value).toBe(1);
104+
105+
const findFirst = await makeTestClient('/m/findFirst', { getPrisma: () => prisma }, { where: { id: '1' } }).get();
106+
expect(findFirst.status).toBe(200);
107+
expect(findFirst.body.data.value).toBe(1);
108+
109+
const findMany = await makeTestClient('/m/findMany', { getPrisma: () => prisma }, {}).get();
110+
expect(findMany.status).toBe(200);
111+
expect(findMany.body.data).toHaveLength(1);
112+
113+
const update = await makeTestClient('/m/update', { getPrisma: () => prisma }).put().send({ where: { id: '1' }, data: { value: 2 } });
114+
expect(update.status).toBe(200);
115+
expect(update.body.data.value).toBe(2);
116+
117+
const updateMany = await makeTestClient('/m/updateMany', { getPrisma: () => prisma }).put().send({ data: { value: 4 } });
118+
expect(updateMany.status).toBe(200);
119+
expect(updateMany.body.data.count).toBe(1);
120+
121+
const upsert1 = await makeTestClient('/m/upsert', { getPrisma: () => prisma }).post().send({ where: { id: '2' }, create: { id: '2', value: 2 }, update: { value: 3 } });
122+
expect(upsert1.status).toBe(201);
123+
expect(upsert1.body.data.value).toBe(2);
124+
125+
const upsert2 = await makeTestClient('/m/upsert', { getPrisma: () => prisma }).post().send({ where: { id: '2' }, create: { id: '2', value: 2 }, update: { value: 3 } });
126+
expect(upsert2.status).toBe(201);
127+
expect(upsert2.body.data.value).toBe(3);
128+
129+
const count1 = await makeTestClient('/m/count', { getPrisma: () => prisma }, { where: { id: '1' } }).get();
130+
expect(count1.status).toBe(200);
131+
expect(count1.body.data).toBe(1);
132+
133+
const count2 = await makeTestClient('/m/count', { getPrisma: () => prisma }, {}).get();
134+
expect(count2.status).toBe(200);
135+
expect(count2.body.data).toBe(2);
136+
137+
const aggregate = await makeTestClient('/m/aggregate', { getPrisma: () => prisma }, { _sum: { value: true } }).get();
138+
expect(aggregate.status).toBe(200);
139+
expect(aggregate.body.data._sum.value).toBe(7);
140+
141+
const groupBy = await makeTestClient('/m/groupBy', { getPrisma: () => prisma }, { by: ['id'], _sum: { value: true } }).get();
142+
expect(groupBy.status).toBe(200);
143+
const data = groupBy.body.data;
144+
expect(data).toHaveLength(2);
145+
expect(data.find((item: any) => item.id === '1')._sum.value).toBe(4);
146+
expect(data.find((item: any) => item.id === '2')._sum.value).toBe(3);
147+
148+
const deleteOne = await makeTestClient('/m/delete', { getPrisma: () => prisma }, { where: { id: '1' } }).del();
149+
expect(deleteOne.status).toBe(200);
150+
expect(await prisma.m.count()).toBe(1);
151+
152+
const deleteMany = await makeTestClient('/m/deleteMany', { getPrisma: () => prisma }, {}).del();
153+
expect(deleteMany.status).toBe(200);
154+
expect(deleteMany.body.data.count).toBe(1);
155+
expect(await prisma.m.count()).toBe(0);
156+
});
157+
158+
it('custom load path', async () => {
159+
const model = `
160+
model M {
161+
id String @id @default(cuid())
162+
value Int
163+
}
164+
`;
165+
166+
const { prisma, projectDir } = await loadSchema(model, { output: './zen' });
167+
168+
const client = await makeTestClient('/m/create', {
169+
getPrisma: () => prisma,
170+
modelMeta: require(path.join(projectDir, './zen/model-meta')).default,
171+
zodSchemas: require(path.join(projectDir, './zen/zod')),
172+
}).post().send({ data: { id: '1', value: 1 } });
173+
174+
expect(client.status).toBe(201);
175+
expect(client.body.data.value).toBe(1);
176+
});
177+
178+
it('access policy crud', async () => {
179+
const model = `
180+
model M {
181+
id String @id @default(cuid())
182+
value Int
183+
184+
@@allow('create', true)
185+
@@allow('read', value > 0)
186+
@@allow('update', future().value > 1)
187+
@@allow('delete', value > 2)
188+
}
189+
`;
190+
191+
const { enhance } = await loadSchema(model);
192+
193+
const createForbidden = await makeTestClient('/m/create', { getPrisma: () => enhance() }).post().send({ data: { value: 0 } });
194+
expect(createForbidden.status).toBe(403);
195+
expect(createForbidden.body.error.reason).toBe('RESULT_NOT_READABLE');
196+
197+
const create = await makeTestClient('/m/create', { getPrisma: () => enhance() }).post().send({ data: { id: '1', value: 1 } });
198+
expect(create.status).toBe(201);
199+
200+
const findMany = await makeTestClient('/m/findMany', { getPrisma: () => enhance() }).get();
201+
expect(findMany.status).toBe(200);
202+
expect(findMany.body.data).toHaveLength(1);
203+
204+
const updateForbidden1 = await makeTestClient('/m/update', { getPrisma: () => enhance() }).put().send({ where: { id: '1' }, data: { value: 0 } });
205+
expect(updateForbidden1.status).toBe(403);
206+
207+
const update1 = await makeTestClient('/m/update', { getPrisma: () => enhance() }).put().send({ where: { id: '1' }, data: { value: 2 } });
208+
expect(update1.status).toBe(200);
209+
210+
const deleteForbidden = await makeTestClient('/m/delete', { getPrisma: () => enhance() }, { where: { id: '1' } }).del();
211+
expect(deleteForbidden.status).toBe(403);
212+
213+
const update2 = await makeTestClient('/m/update', { getPrisma: () => enhance() }).put().send({ where: { id: '1' }, data: { value: 3 } });
214+
expect(update2.status).toBe(200);
215+
216+
const deleteOne = await makeTestClient('/m/delete', { getPrisma: () => enhance() }, { where: { id: '1' } }).del();
217+
expect(deleteOne.status).toBe(200);
218+
});
219+
});
220+
221+
describe('TanStack Start adapter tests - rest handler', () => {
222+
let origDir: string;
223+
224+
beforeEach(() => {
225+
origDir = process.cwd();
226+
});
227+
228+
afterEach(() => {
229+
process.chdir(origDir);
230+
});
231+
232+
it('adapter test - rest', async () => {
233+
const model = `
234+
model M {
235+
id String @id @default(cuid())
236+
value Int
237+
}
238+
`;
239+
240+
const { prisma, modelMeta } = await loadSchema(model);
241+
242+
const options = { getPrisma: () => prisma, handler: Rest({ endpoint: 'http://localhost/api' }), modelMeta };
243+
244+
const create = await makeTestClient('/m', options).post().send({ data: { type: 'm', attributes: { id: '1', value: 1 } } });
245+
expect(create.status).toBe(201);
246+
expect(create.body.data.attributes.value).toBe(1);
247+
248+
const getOne = await makeTestClient('/m/1', options).get();
249+
expect(getOne.status).toBe(200);
250+
expect(getOne.body.data.id).toBe('1');
251+
252+
const findWithFilter1 = await makeTestClient('/m', options, undefined, { 'filter[value]': '1' }).get();
253+
expect(findWithFilter1.status).toBe(200);
254+
expect(findWithFilter1.body.data).toHaveLength(1);
255+
256+
const findWithFilter2 = await makeTestClient('/m', options, undefined, { 'filter[value]': '2' }).get();
257+
expect(findWithFilter2.status).toBe(200);
258+
expect(findWithFilter2.body.data).toHaveLength(0);
259+
260+
const update = await makeTestClient('/m/1', options).put().send({ data: { type: 'm', attributes: { value: 2 } } });
261+
expect(update.status).toBe(200);
262+
expect(update.body.data.attributes.value).toBe(2);
263+
264+
const deleteOne = await makeTestClient('/m/1', options).del();
265+
expect(deleteOne.status).toBe(200);
266+
expect(await prisma.m.count()).toBe(0);
267+
});
268+
});
269+

0 commit comments

Comments
 (0)