Skip to content

Commit

Permalink
feat(NOJIRA-123): Add Webhook toggle funcion (#74)
Browse files Browse the repository at this point in the history
* Add a way to toggle webhook

Just a simple function to toggle enable on a webhook

* Update webhooks.test.ts

* Update README.md
  • Loading branch information
picsoung authored Oct 5, 2023
1 parent c48541f commit 5bf4e0f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,13 @@ Each one of them encapsulates the operations related to it (like listing, updati
- `secret`: If specified, will be used to sign the webhook payload with HMAC SHA256, so that you can verify that it came from Typeform.
- `verifySSL`: `true` if you want Typeform to verify SSL certificates when delivering payloads.

#### `webhooks.toggle({ uid, tag, enabled })`

- Turn on or off a webhook.
- `uid`: Unique ID for the form.
- `tag`: tag of the webhook created.
- `enabled`: `true` or `false`.

## Examples

### Update specific typeform property, as [referenced here](https://developer.typeform.com/create/reference/update-form-patch/)
Expand Down
12 changes: 12 additions & 0 deletions src/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ export class Webhooks {
url: `/forms/${uid}/webhooks`,
})
}

public toggle (args: { uid: string, tag: string, enabled: boolean }): Promise<Typeform.API.Webhooks.List> {
const { uid, tag, enabled } = args

return this._http.request({
method: 'put',
url: `/forms/${uid}/webhooks/${tag}`,
data: {
enabled
}
})
}

update(args: {
uid: string
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/webhooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,19 @@ test('Delete a webhook has the correct path and method', async () => {
)
expect(axios.history.delete[0].method).toBe('delete')
})

test('toggle(false) Disable a webhook', async () => {
await webhooksRequest.toggle({ uid: '2', tag: 'test', enabled: false })
const bodyParsed = JSON.parse(axios.history.put[0].data)
expect(axios.history.put[0].method).toBe('put')
expect(axios.history.put[0].url).toBe(`${API_BASE_URL}/forms/2/webhooks/test`)
expect(bodyParsed.enabled).toBe(false)
})

test('toggle(true) Enable a webhook', async () => {
await webhooksRequest.toggle({ uid: '2', tag: 'test', enabled: true })
const bodyParsed = JSON.parse(axios.history.put[0].data)
expect(axios.history.put[0].method).toBe('put')
expect(axios.history.put[0].url).toBe(`${API_BASE_URL}/forms/2/webhooks/test`)
expect(bodyParsed.enabled).toBe(true)
})

0 comments on commit 5bf4e0f

Please sign in to comment.