(request: Request)
| 484 | }) |
| 485 | |
| 486 | const handleWebhookRequest = async (request: Request): Promise<Response> => { |
| 487 | if (request.method !== `POST`) { |
| 488 | return json({ error: `Method not allowed` }, 405) |
| 489 | } |
| 490 | |
| 491 | const body = new Uint8Array(await request.arrayBuffer()) |
| 492 | if (webhookSignature) { |
| 493 | const verification = await verifyWebhookSignature( |
| 494 | body, |
| 495 | request.headers.get(`webhook-signature`), |
| 496 | webhookSignature |
| 497 | ) |
| 498 | if (!verification.ok) { |
| 499 | return json({ error: verification.error }, verification.status) |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | let notification: WebhookNotification |
| 504 | try { |
| 505 | notification = JSON.parse( |
| 506 | new TextDecoder().decode(body) |
| 507 | ) as WebhookNotification |
| 508 | } catch (err) { |
| 509 | return json( |
| 510 | { |
| 511 | error: `Invalid JSON`, |
| 512 | details: err instanceof Error ? err.message : String(err), |
| 513 | }, |
| 514 | 400 |
| 515 | ) |
| 516 | } |
| 517 | |
| 518 | const typeName = notification.entity?.type |
| 519 | const entityType = typeName ? getRegisteredType(typeName) : undefined |
| 520 | if (!typeName || !entityType) { |
| 521 | return json( |
| 522 | { error: `Unknown entity type: ${typeName ?? `(none)`}` }, |
| 523 | 503 |
| 524 | ) |
| 525 | } |
| 526 | |
| 527 | dispatchWebhookWake(notification) |
| 528 | return json({ ok: true }, 200) |
| 529 | } |
| 530 | |
| 531 | const handleRequest = async (request: Request): Promise<Response | null> => { |
| 532 | const pathname = new URL(request.url).pathname |
no test coverage detected