Skip to content

Commit edf0f1c

Browse files
fix: code review comments
1 parent 7f8050b commit edf0f1c

File tree

3 files changed

+147
-11
lines changed

3 files changed

+147
-11
lines changed

packages/server/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"express",
2222
"nextjs",
2323
"sveltekit",
24-
"nuxtjs"
24+
"nuxtjs",
25+
"elysia"
2526
],
2627
"author": "ZenStack Team",
2728
"license": "MIT",

packages/server/src/elysia/handler.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ export interface ElysiaOptions extends AdapterBaseOptions {
1212
* Callback method for getting a Prisma instance for the given request context.
1313
*/
1414
getPrisma: (context: ElysiaContext) => Promise<unknown> | unknown;
15+
/**
16+
* Optional base path to strip from the request path before passing to the API handler.
17+
*/
18+
basePath?: string;
1519
}
1620

1721
/**
1822
* Creates an Elysia middleware handler for ZenStack.
19-
* This handler provides RPC API functionality through Elysia's routing system.
23+
* This handler provides automatic CRUD APIs through Elysia's routing system.
2024
*/
2125
export function createElysiaHandler(options: ElysiaOptions) {
2226
const { modelMeta, zodSchemas } = loadAssets(options);
@@ -28,18 +32,25 @@ export function createElysiaHandler(options: ElysiaOptions) {
2832
if (!prisma) {
2933
set.status = 500;
3034
return {
31-
message: 'unable to get prisma from request context'
35+
message: 'unable to get prisma from request context',
3236
};
3337
}
3438

3539
const url = new URL(request.url);
3640
const query = Object.fromEntries(url.searchParams);
37-
const path = url.pathname;
41+
let path = url.pathname;
42+
43+
if (options.basePath && path.startsWith(options.basePath)) {
44+
path = path.slice(options.basePath.length);
45+
if (!path.startsWith('/')) {
46+
path = '/' + path;
47+
}
48+
}
3849

3950
if (!path) {
4051
set.status = 400;
4152
return {
42-
message: 'missing path parameter'
53+
message: 'missing path parameter',
4354
};
4455
}
4556

@@ -60,11 +71,11 @@ export function createElysiaHandler(options: ElysiaOptions) {
6071
} catch (err) {
6172
set.status = 500;
6273
return {
63-
message: `An unhandled error occurred: ${err}`
74+
message: 'An internal server error occurred',
6475
};
6576
}
6677
});
6778

6879
return app;
6980
};
70-
}
81+
}

0 commit comments

Comments
 (0)