diff --git a/.changeset/bright-ducks-turn.md b/.changeset/bright-ducks-turn.md new file mode 100644 index 000000000..90a8386eb --- /dev/null +++ b/.changeset/bright-ducks-turn.md @@ -0,0 +1,5 @@ +--- +'@flatfile/plugin-webhook-event-forwarder': patch +--- + +Add docs to the webhook-event-forwarder plugin diff --git a/plugins/webhook-event-forwarder/README.md b/plugins/webhook-event-forwarder/README.md index 7db9576c0..88fff8dd9 100644 --- a/plugins/webhook-event-forwarder/README.md +++ b/plugins/webhook-event-forwarder/README.md @@ -1,9 +1,49 @@ -# @flatfile/plugin-webhook-event-forwarder + -This package forwards events via webhook for clients to manipulate at their endpoints in a language-agnostic way. +The `@flatfile/plugin-webhook-event-forwarder` plugin forwards events via webhook for clients to manipulate at their +endpoints in a language-agnostic way. -`npm i @flatfile/plugin-webhook-event-forwarder` +**Event Type:** +`listener.on('**')` -## Get Started + -Follow [this guide](https://flatfile.com/docs/plugins/automations/webhook-event-forwarder) to learn how to use the plugin. +## Parameters + +#### `url` - `string` + +The `url` parameter takes the webhook url where the events will be forwarded. + +#### `callback` - `function` + +The `callback` parameter takes a function that will be called with the webhook response and the event object. + +#### `options` - `object` + +The `options` parameter takes an object with the following properties: + +- `debug` - `boolean` - (optional) - Whether to log debug messages. + + +## Usage + +#### Install + +```bash install +npm i @flatfile/plugin-webhook-event-forwarder +``` + + +#### Import + +```ts import +import { webhookEventForwarder } from "@flatfile/plugin-webhook-event-forwarder"; +``` + +#### `listener.js` + +```ts listener.js +listener.use(webhookEventForwarder("https://webhook.site/...", (data, event) => { + console.log(data, event); +})); +``` diff --git a/plugins/webhook-event-forwarder/package.json b/plugins/webhook-event-forwarder/package.json index 62011147f..d40a995f0 100644 --- a/plugins/webhook-event-forwarder/package.json +++ b/plugins/webhook-event-forwarder/package.json @@ -1,6 +1,7 @@ { "name": "@flatfile/plugin-webhook-event-forwarder", "version": "0.6.0", + "url": "https://github.com/FlatFilers/flatfile-plugins/tree/main/plugins/webhook-event-forwarder", "description": "A plugin to forward events via webhook for clients to manipulate at their endpoints in a language-agnostic way.", "registryMetadata": { "category": "automation" @@ -48,7 +49,10 @@ "test:unit": "vitest run --mode defaults src/*.spec.ts --passWithNoTests --exclude src/*.e2e.spec.ts", "test:e2e": "vitest run --mode defaults src/*.e2e.spec.ts --passWithNoTests --no-file-parallelism" }, - "keywords": [], + "keywords": [ + "flatfile-plugins", + "category-automation" + ], "author": "Morgan Daley", "repository": { "type": "git", @@ -56,10 +60,6 @@ "directory": "plugins/webhook-event-forwarder" }, "license": "ISC", - "bugs": { - "url": "https://github.com/FlatFilers/flatfile-plugins/issues" - }, - "homepage": "https://github.com/FlatFilers/flatfile-plugins#readme", "dependencies": { "cross-fetch": "^4.0.0" }, diff --git a/plugins/webhook-event-forwarder/src/forward-webhook.e2e.spec.ts b/plugins/webhook-event-forwarder/src/forward-webhook.e2e.spec.ts index 43f2c8d1d..1c3a17f9f 100644 --- a/plugins/webhook-event-forwarder/src/forward-webhook.e2e.spec.ts +++ b/plugins/webhook-event-forwarder/src/forward-webhook.e2e.spec.ts @@ -70,6 +70,7 @@ describe('forward-webhook() e2e', () => { } global.fetch = vi.fn().mockResolvedValue({ status: 200, + ok: true, headers: { get: vi.fn().mockReturnValue('application/json'), }, diff --git a/plugins/webhook-event-forwarder/src/forward.webhook.ts b/plugins/webhook-event-forwarder/src/forward.webhook.ts index a345c724c..091e30737 100644 --- a/plugins/webhook-event-forwarder/src/forward.webhook.ts +++ b/plugins/webhook-event-forwarder/src/forward.webhook.ts @@ -1,7 +1,7 @@ import type { FlatfileEvent, FlatfileListener } from '@flatfile/listener' export function webhookEventForward( - url?: string, + url: string, callback?: (data: any, event: FlatfileEvent) => Promise | any, options?: { debug?: boolean @@ -10,13 +10,7 @@ export function webhookEventForward( return async (listener: FlatfileListener) => { return listener.on('**', async (event) => { try { - const apiUrl = url || process.env.WEBHOOK_SITE_URL - - if (typeof apiUrl === 'undefined') { - throw new Error('No url provided') - } - - const response = await fetch(apiUrl, { + const response = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -24,7 +18,7 @@ export function webhookEventForward( body: JSON.stringify(event), }) - if (response.status !== 200) throw new Error('Error forwarding webhook') + if (!response.ok) throw new Error('Error forwarding webhook') const contentType = response.headers.get('content-type') const isJson = contentType && contentType.includes('application/json')