(bodyObject: B)
| 30 | } |
| 31 | |
| 32 | const callWebhookEndpoint = async <B>(bodyObject: B): Promise<void> => { |
| 33 | if (!WEBHOOK_URI) { |
| 34 | return |
| 35 | } |
| 36 | |
| 37 | const headers = { 'Content-Type': 'application/json' } |
| 38 | // if the webhook token is specified as env var, sends a token with the request |
| 39 | if (WEBHOOK_TOKEN) { |
| 40 | headers['CodeRoad-User-Token'] = WEBHOOK_TOKEN |
| 41 | } |
| 42 | |
| 43 | const body = JSON.stringify(bodyObject) |
| 44 | |
| 45 | try { |
| 46 | const sendEvent = await fetch(WEBHOOK_URI, { |
| 47 | method: 'POST', |
| 48 | headers, |
| 49 | body, |
| 50 | }) |
| 51 | if (!sendEvent.ok) { |
| 52 | throw new Error('Error sending event') |
| 53 | } |
| 54 | logger(`Called webhook endpoint ${WEBHOOK_URI} with body ${JSON.stringify(body)}`) |
| 55 | } catch (err: unknown) { |
| 56 | logger(`Failed to call webhook endpoint ${WEBHOOK_URI} with body ${JSON.stringify(body)}`) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | type WebhookEventInit = { |
| 61 | tutorialId: string |
no test coverage detected