@@ -22,17 +22,6 @@ import { LoggerConfig, Response } from '../../types';
2222import { APIHandlerBase , RequestContext } from '../base' ;
2323import { logWarning , registerCustomSerializers } from '../utils' ;
2424
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-
3625/**
3726 * Request handler options
3827 */
@@ -215,9 +204,26 @@ class RequestHandler extends APIHandlerBase {
215204 private typeMap : Record < string , ModelInfo > ;
216205 public idDivider ;
217206
207+ private urlPatterns ;
208+
218209 constructor ( private readonly options : Options ) {
219210 super ( ) ;
220211 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+ } ;
221227 }
222228
223229 async handleRequest ( {
@@ -251,19 +257,19 @@ class RequestHandler extends APIHandlerBase {
251257 try {
252258 switch ( method ) {
253259 case 'GET' : {
254- let match = urlPatterns . single . match ( path ) ;
260+ let match = this . urlPatterns . single . match ( path ) ;
255261 if ( match ) {
256262 // single resource read
257263 return await this . processSingleRead ( prisma , match . type , match . id , query ) ;
258264 }
259265
260- match = urlPatterns . fetchRelationship . match ( path ) ;
266+ match = this . urlPatterns . fetchRelationship . match ( path ) ;
261267 if ( match ) {
262268 // fetch related resource(s)
263269 return await this . processFetchRelated ( prisma , match . type , match . id , match . relationship , query ) ;
264270 }
265271
266- match = urlPatterns . relationship . match ( path ) ;
272+ match = this . urlPatterns . relationship . match ( path ) ;
267273 if ( match ) {
268274 // read relationship
269275 return await this . processReadRelationship (
@@ -275,7 +281,7 @@ class RequestHandler extends APIHandlerBase {
275281 ) ;
276282 }
277283
278- match = urlPatterns . collection . match ( path ) ;
284+ match = this . urlPatterns . collection . match ( path ) ;
279285 if ( match ) {
280286 // collection read
281287 return await this . processCollectionRead ( prisma , match . type , query ) ;
@@ -289,13 +295,13 @@ class RequestHandler extends APIHandlerBase {
289295 return this . makeError ( 'invalidPayload' ) ;
290296 }
291297
292- let match = urlPatterns . collection . match ( path ) ;
298+ let match = this . urlPatterns . collection . match ( path ) ;
293299 if ( match ) {
294300 // resource creation
295301 return await this . processCreate ( prisma , match . type , query , requestBody , modelMeta , zodSchemas ) ;
296302 }
297303
298- match = urlPatterns . relationship . match ( path ) ;
304+ match = this . urlPatterns . relationship . match ( path ) ;
299305 if ( match ) {
300306 // relationship creation (collection relationship only)
301307 return await this . processRelationshipCRUD (
@@ -319,7 +325,7 @@ class RequestHandler extends APIHandlerBase {
319325 return this . makeError ( 'invalidPayload' ) ;
320326 }
321327
322- let match = urlPatterns . single . match ( path ) ;
328+ let match = this . urlPatterns . single . match ( path ) ;
323329 if ( match ) {
324330 // resource update
325331 return await this . processUpdate (
@@ -333,7 +339,7 @@ class RequestHandler extends APIHandlerBase {
333339 ) ;
334340 }
335341
336- match = urlPatterns . relationship . match ( path ) ;
342+ match = this . urlPatterns . relationship . match ( path ) ;
337343 if ( match ) {
338344 // relationship update
339345 return await this . processRelationshipCRUD (
@@ -351,13 +357,13 @@ class RequestHandler extends APIHandlerBase {
351357 }
352358
353359 case 'DELETE' : {
354- let match = urlPatterns . single . match ( path ) ;
360+ let match = this . urlPatterns . single . match ( path ) ;
355361 if ( match ) {
356362 // resource deletion
357363 return await this . processDelete ( prisma , match . type , match . id ) ;
358364 }
359365
360- match = urlPatterns . relationship . match ( path ) ;
366+ match = this . urlPatterns . relationship . match ( path ) ;
361367 if ( match ) {
362368 // relationship deletion (collection relationship only)
363369 return await this . processRelationshipCRUD (
0 commit comments