* Extracts the value for a RouteHas condition from the request context
( condition: RouteHas, url: URL, headers: Headers )
| 41 | * Extracts the value for a RouteHas condition from the request context |
| 42 | */ |
| 43 | function getConditionValue( |
| 44 | condition: RouteHas, |
| 45 | url: URL, |
| 46 | headers: Headers |
| 47 | ): string | undefined { |
| 48 | switch (condition.type) { |
| 49 | case 'header': |
| 50 | return headers.get(condition.key) || undefined |
| 51 | case 'cookie': { |
| 52 | const cookieHeader = headers.get('cookie') |
| 53 | if (!cookieHeader) return undefined |
| 54 | |
| 55 | // Parse cookies |
| 56 | const cookies = cookieHeader.split(';').reduce( |
| 57 | (acc, cookie) => { |
| 58 | const [key, ...valueParts] = cookie.trim().split('=') |
| 59 | if (key) { |
| 60 | acc[key] = valueParts.join('=') |
| 61 | } |
| 62 | return acc |
| 63 | }, |
| 64 | {} as Record<string, string> |
| 65 | ) |
| 66 | |
| 67 | return cookies[condition.key] |
| 68 | } |
| 69 | case 'query': |
| 70 | return url.searchParams.get(condition.key) || undefined |
| 71 | case 'host': |
| 72 | return url.hostname |
| 73 | default: |
| 74 | return '' |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Normalizes a capture key to only contain a-zA-Z characters |
no test coverage detected
searching dependent graphs…