Skip to content

Commit 69d9132

Browse files
committed
feat(server): migrate tanstack-start adapter
1 parent fe5f61d commit 69d9132

File tree

5 files changed

+346
-0
lines changed

5 files changed

+346
-0
lines changed

packages/server/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@
108108
"types": "./dist/sveltekit.d.cts",
109109
"default": "./dist/sveltekit.cjs"
110110
}
111+
},
112+
"./tanstack-start": {
113+
"import": {
114+
"types": "./dist/tanstack-start.d.ts",
115+
"default": "./dist/tanstack-start.js"
116+
},
117+
"require": {
118+
"types": "./dist/tanstack-start.d.cts",
119+
"default": "./dist/tanstack-start.cjs"
120+
}
111121
}
112122
},
113123
"dependencies": {
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import type { SchemaDef } from '@zenstackhq/orm/schema';
2+
import type { TanStackStartOptions } from '.';
3+
import { logInternalError } from '../common';
4+
5+
/**
6+
* Creates a TanStack Start server route handler which encapsulates ZenStack CRUD operations.
7+
*
8+
* @param options Options for initialization
9+
* @returns A TanStack Start server route handler
10+
*/
11+
export default function factory<Schema extends SchemaDef>(
12+
options: TanStackStartOptions<Schema>
13+
): ({ request, params }: { request: Request; params: Record<string, string> }) => Promise<Response> {
14+
return async ({ request, params }: { request: Request; params: Record<string, string> }) => {
15+
const client = await options.getClient(request, params);
16+
if (!client) {
17+
return new Response(JSON.stringify({ message: 'unable to get ZenStackClient from request context' }), {
18+
status: 500,
19+
headers: {
20+
'Content-Type': 'application/json',
21+
},
22+
});
23+
}
24+
25+
const url = new URL(request.url);
26+
const query = Object.fromEntries(url.searchParams);
27+
28+
// Extract path from params._splat for catch-all routes
29+
const path = params['_splat'];
30+
31+
if (!path) {
32+
return new Response(JSON.stringify({ message: 'missing path parameter' }), {
33+
status: 400,
34+
headers: {
35+
'Content-Type': 'application/json',
36+
},
37+
});
38+
}
39+
40+
let requestBody: unknown;
41+
if (request.body) {
42+
try {
43+
requestBody = await request.json();
44+
} catch {
45+
// noop
46+
}
47+
}
48+
49+
try {
50+
const r = await options.apiHandler.handleRequest({
51+
method: request.method!,
52+
path,
53+
query,
54+
requestBody,
55+
client,
56+
});
57+
return new Response(JSON.stringify(r.body), {
58+
status: r.status,
59+
headers: {
60+
'Content-Type': 'application/json',
61+
},
62+
});
63+
} catch (err) {
64+
logInternalError(options.apiHandler.log, err);
65+
return new Response(JSON.stringify({ message: 'An internal server error occurred' }), {
66+
status: 500,
67+
headers: {
68+
'Content-Type': 'application/json',
69+
},
70+
});
71+
}
72+
};
73+
}
74+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { ClientContract } from '@zenstackhq/orm';
2+
import type { SchemaDef } from '@zenstackhq/orm/schema';
3+
import type { CommonAdapterOptions } from '../common';
4+
import { default as Handler } from './handler';
5+
6+
/**
7+
* Options for initializing a TanStack Start server route handler.
8+
*/
9+
export interface TanStackStartOptions<Schema extends SchemaDef> extends CommonAdapterOptions<Schema> {
10+
/**
11+
* Callback method for getting a ZenStackClient instance for the given request and params.
12+
*/
13+
getClient: (request: Request, params: Record<string, string>) => ClientContract<Schema> | Promise<ClientContract<Schema>> ;
14+
}
15+
16+
/**
17+
* Creates a TanStack Start server route handler.
18+
* @see https://zenstack.dev/docs/reference/server-adapters/tanstack-start
19+
*/
20+
export function TanStackStartHandler<Schema extends SchemaDef>(options: TanStackStartOptions<Schema>): ReturnType<typeof Handler> {
21+
return Handler(options);
22+
}
23+
24+
export default TanStackStartHandler;
25+
Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
import { SchemaDef } from '@zenstackhq/orm/schema';
2+
import { createPolicyTestClient, createTestClient } from '@zenstackhq/testtools';
3+
import { describe, expect, it } from 'vitest';
4+
import { TanStackStartHandler, TanStackStartOptions } from '../../src/adapter/tanstack-start';
5+
import { RestApiHandler, RPCApiHandler } from '../../src/api';
6+
7+
function makeRequest(method: string, url: string, body?: any): Request {
8+
const payload = body ? JSON.stringify(body) : undefined;
9+
return new Request(url, { method, body: payload });
10+
}
11+
12+
async function unmarshal(response: Response): Promise<any> {
13+
const text = await response.text();
14+
return JSON.parse(text);
15+
}
16+
17+
interface TestClient {
18+
get: () => Promise<{ status: number; body: any }>;
19+
post: () => { send: (data: any) => Promise<{ status: number; body: any }> };
20+
put: () => { send: (data: any) => Promise<{ status: number; body: any }> };
21+
del: () => Promise<{ status: number; body: any }>;
22+
}
23+
24+
function makeTestClient(apiPath: string, options: TanStackStartOptions<SchemaDef>, qArg?: unknown, otherArgs?: any): TestClient {
25+
const pathParts = apiPath.split('/').filter((p) => p);
26+
const path = pathParts.join('/');
27+
28+
const handler = TanStackStartHandler(options);
29+
30+
const params = {
31+
_splat: path,
32+
...otherArgs,
33+
};
34+
35+
const buildUrl = (method: string) => {
36+
const baseUrl = `http://localhost${apiPath}`;
37+
if (method === 'GET' || method === 'DELETE') {
38+
const url = new URL(baseUrl);
39+
if (qArg) {
40+
url.searchParams.set('q', JSON.stringify(qArg));
41+
}
42+
if (otherArgs) {
43+
Object.entries(otherArgs).forEach(([key, value]) => {
44+
url.searchParams.set(key, String(value));
45+
});
46+
}
47+
return url.toString();
48+
}
49+
return baseUrl;
50+
};
51+
52+
const executeRequest = async (method: string, body?: any) => {
53+
const url = buildUrl(method);
54+
const request = makeRequest(method, url, body);
55+
const response = await handler({ request, params });
56+
const responseBody = await unmarshal(response);
57+
return {
58+
status: response.status,
59+
body: responseBody,
60+
};
61+
};
62+
63+
return {
64+
get: async () => executeRequest('GET'),
65+
post: () => ({
66+
send: async (data: any) => executeRequest('POST', data),
67+
}),
68+
put: () => ({
69+
send: async (data: any) => executeRequest('PUT', data),
70+
}),
71+
del: async () => executeRequest('DELETE'),
72+
};
73+
}
74+
75+
describe('TanStack Start adapter tests - rpc handler', () => {
76+
it('simple crud', async () => {
77+
const model = `
78+
model M {
79+
id String @id @default(cuid())
80+
value Int
81+
}
82+
`;
83+
84+
const db = await createTestClient(model);
85+
const options: TanStackStartOptions<SchemaDef> = {
86+
getClient: () => db,
87+
apiHandler: new RPCApiHandler({ schema: db.schema }),
88+
};
89+
90+
const client = await makeTestClient('/m/create', options).post().send({ data: { id: '1', value: 1 } });
91+
expect(client.status).toBe(201);
92+
expect(client.body.data.value).toBe(1);
93+
94+
const findUnique = await makeTestClient('/m/findUnique', options, { where: { id: '1' } }).get();
95+
expect(findUnique.status).toBe(200);
96+
expect(findUnique.body.data.value).toBe(1);
97+
98+
const findFirst = await makeTestClient('/m/findFirst', options, { where: { id: '1' } }).get();
99+
expect(findFirst.status).toBe(200);
100+
expect(findFirst.body.data.value).toBe(1);
101+
102+
const findMany = await makeTestClient('/m/findMany', options, {}).get();
103+
expect(findMany.status).toBe(200);
104+
expect(findMany.body.data).toHaveLength(1);
105+
106+
const update = await makeTestClient('/m/update', options).put().send({ where: { id: '1' }, data: { value: 2 } });
107+
expect(update.status).toBe(200);
108+
expect(update.body.data.value).toBe(2);
109+
110+
const updateMany = await makeTestClient('/m/updateMany', options).put().send({ data: { value: 4 } });
111+
expect(updateMany.status).toBe(200);
112+
expect(updateMany.body.data.count).toBe(1);
113+
114+
const upsert1 = await makeTestClient('/m/upsert', options).post().send({ where: { id: '2' }, create: { id: '2', value: 2 }, update: { value: 3 } });
115+
expect(upsert1.status).toBe(201);
116+
expect(upsert1.body.data.value).toBe(2);
117+
118+
const upsert2 = await makeTestClient('/m/upsert', options).post().send({ where: { id: '2' }, create: { id: '2', value: 2 }, update: { value: 3 } });
119+
expect(upsert2.status).toBe(201);
120+
expect(upsert2.body.data.value).toBe(3);
121+
122+
const count1 = await makeTestClient('/m/count', options, { where: { id: '1' } }).get();
123+
expect(count1.status).toBe(200);
124+
expect(count1.body.data).toBe(1);
125+
126+
const count2 = await makeTestClient('/m/count', options, {}).get();
127+
expect(count2.status).toBe(200);
128+
expect(count2.body.data).toBe(2);
129+
130+
const aggregate = await makeTestClient('/m/aggregate', options, { _sum: { value: true } }).get();
131+
expect(aggregate.status).toBe(200);
132+
expect(aggregate.body.data._sum.value).toBe(7);
133+
134+
const groupBy = await makeTestClient('/m/groupBy', options, { by: ['id'], _sum: { value: true } }).get();
135+
expect(groupBy.status).toBe(200);
136+
const data = groupBy.body.data;
137+
expect(data).toHaveLength(2);
138+
expect(data.find((item: any) => item.id === '1')._sum.value).toBe(4);
139+
expect(data.find((item: any) => item.id === '2')._sum.value).toBe(3);
140+
141+
const deleteOne = await makeTestClient('/m/delete', options, { where: { id: '1' } }).del();
142+
expect(deleteOne.status).toBe(200);
143+
expect(await db.m.count()).toBe(1);
144+
145+
const deleteMany = await makeTestClient('/m/deleteMany', options, {}).del();
146+
expect(deleteMany.status).toBe(200);
147+
expect(deleteMany.body.data.count).toBe(1);
148+
expect(await db.m.count()).toBe(0);
149+
});
150+
151+
it('access policy crud', async () => {
152+
const model = `
153+
model M {
154+
id String @id @default(cuid())
155+
value Int
156+
157+
@@allow('create,update', true)
158+
@@allow('read', value > 0)
159+
@@allow('post-update', value > 1)
160+
@@allow('delete', value > 2)
161+
}
162+
`;
163+
164+
const db = await createPolicyTestClient(model);
165+
const options: TanStackStartOptions<SchemaDef> = {
166+
getClient: () => db,
167+
apiHandler: new RPCApiHandler({ schema: db.schema }),
168+
};
169+
170+
const createForbidden = await makeTestClient('/m/create', options).post().send({ data: { value: 0 } });
171+
expect(createForbidden.status).toBe(403);
172+
expect(createForbidden.body.error.rejectReason).toBe('cannot-read-back');
173+
174+
const create = await makeTestClient('/m/create', options).post().send({ data: { id: '1', value: 1 } });
175+
expect(create.status).toBe(201);
176+
177+
const findMany = await makeTestClient('/m/findMany', options).get();
178+
expect(findMany.status).toBe(200);
179+
expect(findMany.body.data).toHaveLength(1);
180+
181+
const updateForbidden1 = await makeTestClient('/m/update', options).put().send({ where: { id: '1' }, data: { value: 0 } });
182+
expect(updateForbidden1.status).toBe(403);
183+
184+
const update1 = await makeTestClient('/m/update', options).put().send({ where: { id: '1' }, data: { value: 2 } });
185+
expect(update1.status).toBe(200);
186+
187+
const deleteForbidden = await makeTestClient('/m/delete', options, { where: { id: '1' } }).del();
188+
expect(deleteForbidden.status).toBe(404);
189+
190+
const update2 = await makeTestClient('/m/update', options).put().send({ where: { id: '1' }, data: { value: 3 } });
191+
expect(update2.status).toBe(200);
192+
193+
const deleteOne = await makeTestClient('/m/delete', options, { where: { id: '1' } }).del();
194+
expect(deleteOne.status).toBe(200);
195+
});
196+
});
197+
198+
describe('TanStack Start adapter tests - rest handler', () => {
199+
it('properly handles requests', async () => {
200+
const model = `
201+
model M {
202+
id String @id @default(cuid())
203+
value Int
204+
}
205+
`;
206+
207+
const db = await createTestClient(model);
208+
209+
const options: TanStackStartOptions<SchemaDef> = { getClient: () => db, apiHandler: new RestApiHandler({ endpoint: 'http://localhost/api', schema: db.schema }) };
210+
211+
const create = await makeTestClient('/m', options).post().send({ data: { type: 'm', attributes: { id: '1', value: 1 } } });
212+
expect(create.status).toBe(201);
213+
expect(create.body.data.attributes.value).toBe(1);
214+
215+
const getOne = await makeTestClient('/m/1', options).get();
216+
expect(getOne.status).toBe(200);
217+
expect(getOne.body.data.id).toBe('1');
218+
219+
const findWithFilter1 = await makeTestClient('/m', options, undefined, { 'filter[value]': '1' }).get();
220+
expect(findWithFilter1.status).toBe(200);
221+
expect(findWithFilter1.body.data).toHaveLength(1);
222+
223+
const findWithFilter2 = await makeTestClient('/m', options, undefined, { 'filter[value]': '2' }).get();
224+
expect(findWithFilter2.status).toBe(200);
225+
expect(findWithFilter2.body.data).toHaveLength(0);
226+
227+
const update = await makeTestClient('/m/1', options).put().send({ data: { type: 'm', attributes: { value: 2 } } });
228+
expect(update.status).toBe(200);
229+
expect(update.body.data.attributes.value).toBe(2);
230+
231+
const deleteOne = await makeTestClient('/m/1', options).del();
232+
expect(deleteOne.status).toBe(200);
233+
expect(await db.m.count()).toBe(0);
234+
});
235+
});
236+

packages/server/tsup.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export default defineConfig({
1010
nuxt: 'src/adapter/nuxt/index.ts',
1111
hono: 'src/adapter/hono/index.ts',
1212
sveltekit: 'src/adapter/sveltekit/index.ts',
13+
'tanstack-start': 'src/adapter/tanstack-start/index.ts',
1314
},
1415
outDir: 'dist',
1516
splitting: false,

0 commit comments

Comments
 (0)