Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/bright-ducks-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@flatfile/plugin-webhook-event-forwarder': patch
---

Add docs to the webhook-event-forwarder plugin
50 changes: 45 additions & 5 deletions plugins/webhook-event-forwarder/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,49 @@
# @flatfile/plugin-webhook-event-forwarder
<!-- START_INFOCARD -->

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
<!-- END_INFOCARD -->

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);
}));
```
10 changes: 5 additions & 5 deletions plugins/webhook-event-forwarder/package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -48,18 +49,17 @@
"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",
"url": "git+https://github.com/FlatFilers/flatfile-plugins.git",
"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"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ describe('forward-webhook() e2e', () => {
}
global.fetch = vi.fn().mockResolvedValue({
status: 200,
ok: true,
headers: {
get: vi.fn().mockReturnValue('application/json'),
},
Expand Down
12 changes: 3 additions & 9 deletions plugins/webhook-event-forwarder/src/forward.webhook.ts
Original file line number Diff line number Diff line change
@@ -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> | any,
options?: {
debug?: boolean
Expand All @@ -10,21 +10,15 @@ 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',
},
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')
Expand Down
Loading