(request: Request)
| 3 | const AuthorizationHeaderSchema = z.string().regex(/^Bearer .+$/); |
| 4 | |
| 5 | export function getApiKeyFromRequest(request: Request) { |
| 6 | const rawAuthorization = request.headers.get("Authorization"); |
| 7 | |
| 8 | const authorization = AuthorizationHeaderSchema.safeParse(rawAuthorization); |
| 9 | if (!authorization.success) { |
| 10 | return; |
| 11 | } |
| 12 | |
| 13 | const apiKey = authorization.data.replace(/^Bearer /, ""); |
| 14 | const type = isPrivateApiKey(apiKey) ? ("PRIVATE" as const) : ("PUBLIC" as const); |
| 15 | return { apiKey, type }; |
| 16 | } |
| 17 | |
| 18 | function isPrivateApiKey(key: string) { |
| 19 | return key.startsWith("tr_"); |
no test coverage detected
searching dependent graphs…