(id: string, pathname: string)
| 18 | } |
| 19 | |
| 20 | function createPageRouteHandler(id: string, pathname: string) { |
| 21 | return { |
| 22 | pattern: new URLPattern({ pathname }), |
| 23 | handler: async (request: Request, match: URLPatternResult) => { |
| 24 | try { |
| 25 | const page: Page = (await import(`/pages/${id}`)).default; |
| 26 | |
| 27 | const isRunningLocally = isAppRunningLocally(request); |
| 28 | |
| 29 | const { |
| 30 | get, |
| 31 | post, |
| 32 | put, |
| 33 | patch, |
| 34 | delete: deleteAction, |
| 35 | options, |
| 36 | catchAll, |
| 37 | } = page; |
| 38 | |
| 39 | const { user, session, tokenData } = (await getDataFromRequest(request)) || {}; |
| 40 | |
| 41 | switch (request.method) { |
| 42 | case 'GET': |
| 43 | case 'HEAD': |
| 44 | case 'OPTIONS': |
| 45 | if (options) { |
| 46 | return await options({ |
| 47 | request, |
| 48 | match, |
| 49 | user, |
| 50 | session: { userSession: session, tokenData }, |
| 51 | isRunningLocally, |
| 52 | }); |
| 53 | } |
| 54 | |
| 55 | if (get) { |
| 56 | if (request.method === 'OPTIONS') { |
| 57 | const allowedMethods = ['GET', 'HEAD', 'OPTIONS']; |
| 58 | if (post) { |
| 59 | allowedMethods.push('POST'); |
| 60 | } |
| 61 | if (put) { |
| 62 | allowedMethods.push('PUT'); |
| 63 | } |
| 64 | if (patch) { |
| 65 | allowedMethods.push('PATCH'); |
| 66 | } |
| 67 | if (deleteAction) { |
| 68 | allowedMethods.push('DELETE'); |
| 69 | } |
| 70 | |
| 71 | return new Response(null, { |
| 72 | status: 204, |
| 73 | headers: { |
| 74 | Allow: allowedMethods.join(', '), |
| 75 | }, |
| 76 | }); |
| 77 | } |
no test coverage detected