| 87 | * client's `fetch('/blog/og-image/…')` can find it. Null for any other file. |
| 88 | */ |
| 89 | export function apiRoutePathForFile(filePath: string): string | null { |
| 90 | const dir = APP_DIR.exec(filePath); |
| 91 | if (!dir) return null; |
| 92 | const rel = filePath.slice(dir.index + dir[0].length); |
| 93 | const ext = /\.(tsx|ts|jsx|js|mjs|cjs)$/.exec(rel); |
| 94 | if (!ext) return null; |
| 95 | const bare = rel.slice(0, ext.index); |
| 96 | if (!bare.endsWith('+api')) return null; |
| 97 | const segs = bare.slice(0, -'+api'.length).split('/').filter((seg) => seg.length > 0 && !(seg.startsWith('(') && seg.endsWith(')'))); |
| 98 | if (segs[segs.length - 1] === 'index') segs.pop(); |
| 99 | return '/' + segs.map((seg) => seg.replace(/^\[\.\.\.(.+)\]$/, ':$1*').replace(/^\[(.+)\]$/, ':$1')).join('/'); |
| 100 | } |
| 101 | |
| 102 | const API_METHODS = 'GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS'; |
| 103 | |