| 20 | |
| 21 | // just an example using raw fetch with error handling |
| 22 | #createWithFetch( |
| 23 | key: IntegrationTaskKey, |
| 24 | params: { |
| 25 | topic: WebhookTopic; |
| 26 | address: string; |
| 27 | fields?: string[]; |
| 28 | } |
| 29 | ): Promise<WebhookSubscription> { |
| 30 | return this.runTask( |
| 31 | key, |
| 32 | async (client, task, io) => { |
| 33 | const resource = { |
| 34 | webhook: { |
| 35 | topic: params.topic, |
| 36 | address: params.address, |
| 37 | fields: params.fields, |
| 38 | }, |
| 39 | }; |
| 40 | |
| 41 | const request = new Request(new URL("webhooks.json", this.#apiUrl(client)), { |
| 42 | method: "POST", |
| 43 | headers: { |
| 44 | "X-Shopify-Access-Token": client.config.adminApiAccessToken, |
| 45 | "Content-Type": "application/json", |
| 46 | }, |
| 47 | body: JSON.stringify(resource), |
| 48 | }); |
| 49 | |
| 50 | const response = await fetch(request.clone()); |
| 51 | |
| 52 | if (!response.ok) { |
| 53 | await handleWebhookError("WEBHOOK_CREATE", request, response); |
| 54 | } |
| 55 | |
| 56 | const webhook = await response.json(); |
| 57 | const parsed = WebhookSubscriptionDataSchema.parse(webhook); |
| 58 | |
| 59 | return parsed.webhook; |
| 60 | }, |
| 61 | { |
| 62 | name: "Create Webhook with Fetch", |
| 63 | params, |
| 64 | properties: [ |
| 65 | { label: "Webhook URL", text: params.address }, |
| 66 | { label: "Topic", text: params.topic }, |
| 67 | ], |
| 68 | } |
| 69 | ); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | export function createWebhookEventSource(integration: Shopify) { |