|
| 1 | +import { |
| 2 | + HttpClient, |
| 3 | + LinkedApiError, |
| 4 | + TDeleteWebhookParams, |
| 5 | + TLinkedApiErrorType, |
| 6 | + TReplayWebhookDeliveryParams, |
| 7 | + TSetWebhookParams, |
| 8 | + TSetWebhookPayloadModeParams, |
| 9 | + TWebhookDelivery, |
| 10 | + TWebhookSubscription, |
| 11 | +} from '../types'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Manage the client's registered outbound webhook and inspect recent deliveries. |
| 15 | + * |
| 16 | + * A client may hold at most one active webhook. It receives every event Linked API emits |
| 17 | + * (workflow lifecycle + account status changes); filter by the event `type` on your receiver. |
| 18 | + * |
| 19 | + * @see {@link https://linkedapi.io/docs/ Linked API Documentation} |
| 20 | + */ |
| 21 | +export class AdminWebhooks { |
| 22 | + constructor(private readonly httpClient: HttpClient) {} |
| 23 | + |
| 24 | + public async set(params: TSetWebhookParams): Promise<TWebhookSubscription> { |
| 25 | + const response = await this.httpClient.post<{ webhook: TWebhookSubscription }>( |
| 26 | + '/admin/webhook.set', |
| 27 | + params, |
| 28 | + ); |
| 29 | + if (response.success && response.result) { |
| 30 | + return response.result.webhook; |
| 31 | + } |
| 32 | + throw new LinkedApiError( |
| 33 | + (response.error?.type ?? 'httpError') as TLinkedApiErrorType, |
| 34 | + response.error?.message ?? 'Failed to set webhook', |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + public async get(): Promise<Array<TWebhookSubscription>> { |
| 39 | + const response = await this.httpClient.post<{ webhooks: Array<TWebhookSubscription> }>( |
| 40 | + '/admin/webhook.get', |
| 41 | + ); |
| 42 | + if (response.success && response.result) { |
| 43 | + return response.result.webhooks; |
| 44 | + } |
| 45 | + throw new LinkedApiError( |
| 46 | + (response.error?.type ?? 'httpError') as TLinkedApiErrorType, |
| 47 | + response.error?.message ?? 'Failed to get webhooks', |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + public async setPayloadMode(params: TSetWebhookPayloadModeParams): Promise<TWebhookSubscription> { |
| 52 | + const response = await this.httpClient.post<{ webhook: TWebhookSubscription }>( |
| 53 | + '/admin/webhook.setPayloadMode', |
| 54 | + params, |
| 55 | + ); |
| 56 | + if (response.success && response.result) { |
| 57 | + return response.result.webhook; |
| 58 | + } |
| 59 | + throw new LinkedApiError( |
| 60 | + (response.error?.type ?? 'httpError') as TLinkedApiErrorType, |
| 61 | + response.error?.message ?? 'Failed to set webhook payload mode', |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + public async delete(params: TDeleteWebhookParams): Promise<void> { |
| 66 | + const response = await this.httpClient.post('/admin/webhook.delete', params); |
| 67 | + if (response.success) { |
| 68 | + return; |
| 69 | + } |
| 70 | + throw new LinkedApiError( |
| 71 | + (response.error?.type ?? 'httpError') as TLinkedApiErrorType, |
| 72 | + response.error?.message ?? 'Failed to delete webhook', |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + public async deliveries(): Promise<Array<TWebhookDelivery>> { |
| 77 | + const response = await this.httpClient.post<{ deliveries: Array<TWebhookDelivery> }>( |
| 78 | + '/admin/webhook.deliveries', |
| 79 | + ); |
| 80 | + if (response.success && response.result) { |
| 81 | + return response.result.deliveries; |
| 82 | + } |
| 83 | + throw new LinkedApiError( |
| 84 | + (response.error?.type ?? 'httpError') as TLinkedApiErrorType, |
| 85 | + response.error?.message ?? 'Failed to get webhook deliveries', |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + public async replayDelivery(params: TReplayWebhookDeliveryParams): Promise<void> { |
| 90 | + const response = await this.httpClient.post('/admin/webhook.replayDelivery', params); |
| 91 | + if (response.success) { |
| 92 | + return; |
| 93 | + } |
| 94 | + throw new LinkedApiError( |
| 95 | + (response.error?.type ?? 'httpError') as TLinkedApiErrorType, |
| 96 | + response.error?.message ?? 'Failed to replay webhook delivery', |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + public async sendTest(): Promise<void> { |
| 101 | + const response = await this.httpClient.post('/admin/webhook.sendTest'); |
| 102 | + if (response.success) { |
| 103 | + return; |
| 104 | + } |
| 105 | + throw new LinkedApiError( |
| 106 | + (response.error?.type ?? 'httpError') as TLinkedApiErrorType, |
| 107 | + response.error?.message ?? 'Failed to send test webhook', |
| 108 | + ); |
| 109 | + } |
| 110 | +} |
0 commit comments