@@ -12,11 +12,15 @@ export interface ElysiaOptions extends AdapterBaseOptions {
12
12
* Callback method for getting a Prisma instance for the given request context.
13
13
*/
14
14
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 ;
15
19
}
16
20
17
21
/**
18
22
* 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.
20
24
*/
21
25
export function createElysiaHandler ( options : ElysiaOptions ) {
22
26
const { modelMeta, zodSchemas } = loadAssets ( options ) ;
@@ -28,18 +32,25 @@ export function createElysiaHandler(options: ElysiaOptions) {
28
32
if ( ! prisma ) {
29
33
set . status = 500 ;
30
34
return {
31
- message : 'unable to get prisma from request context'
35
+ message : 'unable to get prisma from request context' ,
32
36
} ;
33
37
}
34
38
35
39
const url = new URL ( request . url ) ;
36
40
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
+ }
38
49
39
50
if ( ! path ) {
40
51
set . status = 400 ;
41
52
return {
42
- message : 'missing path parameter'
53
+ message : 'missing path parameter' ,
43
54
} ;
44
55
}
45
56
@@ -60,11 +71,11 @@ export function createElysiaHandler(options: ElysiaOptions) {
60
71
} catch ( err ) {
61
72
set . status = 500 ;
62
73
return {
63
- message : ` An unhandled error occurred: ${ err } `
74
+ message : ' An internal server error occurred' ,
64
75
} ;
65
76
}
66
77
} ) ;
67
78
68
79
return app ;
69
80
} ;
70
- }
81
+ }
0 commit comments