@@ -22,17 +22,6 @@ import { LoggerConfig, Response } from '../../types';
22
22
import { APIHandlerBase , RequestContext } from '../base' ;
23
23
import { logWarning , registerCustomSerializers } from '../utils' ;
24
24
25
- const urlPatterns = {
26
- // collection operations
27
- collection : new UrlPattern ( '/:type' ) ,
28
- // single resource operations
29
- single : new UrlPattern ( '/:type/:id' ) ,
30
- // related entity fetching
31
- fetchRelationship : new UrlPattern ( '/:type/:id/:relationship' ) ,
32
- // relationship operations
33
- relationship : new UrlPattern ( '/:type/:id/relationships/:relationship' ) ,
34
- } ;
35
-
36
25
/**
37
26
* Request handler options
38
27
*/
@@ -215,9 +204,26 @@ class RequestHandler extends APIHandlerBase {
215
204
private typeMap : Record < string , ModelInfo > ;
216
205
public idDivider ;
217
206
207
+ private urlPatterns ;
208
+
218
209
constructor ( private readonly options : Options ) {
219
210
super ( ) ;
220
211
this . idDivider = options . idDivider ?? '_' ;
212
+ this . urlPatterns = this . buildUrlPatterns ( this . idDivider ) ;
213
+ }
214
+
215
+ buildUrlPatterns ( idDivider : string ) {
216
+ const options = { segmentNameCharset : `a-zA-Z0-9-_~ %${ idDivider } ` } ;
217
+ return {
218
+ // collection operations
219
+ collection : new UrlPattern ( '/:type' , options ) ,
220
+ // single resource operations
221
+ single : new UrlPattern ( '/:type/:id' , options ) ,
222
+ // related entity fetching
223
+ fetchRelationship : new UrlPattern ( '/:type/:id/:relationship' , options ) ,
224
+ // relationship operations
225
+ relationship : new UrlPattern ( '/:type/:id/relationships/:relationship' , options ) ,
226
+ } ;
221
227
}
222
228
223
229
async handleRequest ( {
@@ -251,19 +257,19 @@ class RequestHandler extends APIHandlerBase {
251
257
try {
252
258
switch ( method ) {
253
259
case 'GET' : {
254
- let match = urlPatterns . single . match ( path ) ;
260
+ let match = this . urlPatterns . single . match ( path ) ;
255
261
if ( match ) {
256
262
// single resource read
257
263
return await this . processSingleRead ( prisma , match . type , match . id , query ) ;
258
264
}
259
265
260
- match = urlPatterns . fetchRelationship . match ( path ) ;
266
+ match = this . urlPatterns . fetchRelationship . match ( path ) ;
261
267
if ( match ) {
262
268
// fetch related resource(s)
263
269
return await this . processFetchRelated ( prisma , match . type , match . id , match . relationship , query ) ;
264
270
}
265
271
266
- match = urlPatterns . relationship . match ( path ) ;
272
+ match = this . urlPatterns . relationship . match ( path ) ;
267
273
if ( match ) {
268
274
// read relationship
269
275
return await this . processReadRelationship (
@@ -275,7 +281,7 @@ class RequestHandler extends APIHandlerBase {
275
281
) ;
276
282
}
277
283
278
- match = urlPatterns . collection . match ( path ) ;
284
+ match = this . urlPatterns . collection . match ( path ) ;
279
285
if ( match ) {
280
286
// collection read
281
287
return await this . processCollectionRead ( prisma , match . type , query ) ;
@@ -289,13 +295,13 @@ class RequestHandler extends APIHandlerBase {
289
295
return this . makeError ( 'invalidPayload' ) ;
290
296
}
291
297
292
- let match = urlPatterns . collection . match ( path ) ;
298
+ let match = this . urlPatterns . collection . match ( path ) ;
293
299
if ( match ) {
294
300
// resource creation
295
301
return await this . processCreate ( prisma , match . type , query , requestBody , modelMeta , zodSchemas ) ;
296
302
}
297
303
298
- match = urlPatterns . relationship . match ( path ) ;
304
+ match = this . urlPatterns . relationship . match ( path ) ;
299
305
if ( match ) {
300
306
// relationship creation (collection relationship only)
301
307
return await this . processRelationshipCRUD (
@@ -319,7 +325,7 @@ class RequestHandler extends APIHandlerBase {
319
325
return this . makeError ( 'invalidPayload' ) ;
320
326
}
321
327
322
- let match = urlPatterns . single . match ( path ) ;
328
+ let match = this . urlPatterns . single . match ( path ) ;
323
329
if ( match ) {
324
330
// resource update
325
331
return await this . processUpdate (
@@ -333,7 +339,7 @@ class RequestHandler extends APIHandlerBase {
333
339
) ;
334
340
}
335
341
336
- match = urlPatterns . relationship . match ( path ) ;
342
+ match = this . urlPatterns . relationship . match ( path ) ;
337
343
if ( match ) {
338
344
// relationship update
339
345
return await this . processRelationshipCRUD (
@@ -351,13 +357,13 @@ class RequestHandler extends APIHandlerBase {
351
357
}
352
358
353
359
case 'DELETE' : {
354
- let match = urlPatterns . single . match ( path ) ;
360
+ let match = this . urlPatterns . single . match ( path ) ;
355
361
if ( match ) {
356
362
// resource deletion
357
363
return await this . processDelete ( prisma , match . type , match . id ) ;
358
364
}
359
365
360
- match = urlPatterns . relationship . match ( path ) ;
366
+ match = this . urlPatterns . relationship . match ( path ) ;
361
367
if ( match ) {
362
368
// relationship deletion (collection relationship only)
363
369
return await this . processRelationshipCRUD (
0 commit comments