Skip to content

Commit

Permalink
Add removeAddressesFromWebhook method (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
KoenRijpstra authored Feb 20, 2024
1 parent 0fdf3f9 commit 07216ab
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Provides methods for setting up, editing, and managing webhooks, crucial for lis
- [`createWebhook()`](#createWebhook): Creates a new webhook with the provided request.
- [`editWebhook()`](#editWebhook): Edits an existing webhook by its ID with the provided request.
- [`appendAddressesToWebhook()`](#appendAddressesToWebhook): Append new addresses to an existing webhook.
- [`removeAddressesFromWebhook()`](#removeAddressesFromWebhook): Remove addresses from an existing webhook.
- [`deleteWebhook()`](#deleteWebhook): Deletes a webhook by its ID.
- [`getWebhookByID()`](#getWebhookByID): Retrieves a single webhook by its ID.
- [`getAllWebhooks()`](#getAllWebhooks): Retrieves a list of all webhooks.
Expand Down Expand Up @@ -497,6 +498,21 @@ helius.appendAddressesToWebhook("your-webhook-id-here", [
]);
```

### removeAddressesFromWebhook()

For convenience, we've added a method to let you simply remove addresses from an existing webhook:

```ts
import { Helius, Address } from "helius-sdk";

const helius = new Helius("YOUR_API_KEY");

helius.removeAddressesFromWebhook("your-webhook-id-here", [
Address.SQUADS,
Address.JUPITER_V3,
]);
```

### getAllWebhooks()

```ts
Expand Down
32 changes: 32 additions & 0 deletions src/Helius.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,38 @@ export class Helius {
}
}

/**
* Removes an array of addresses from an existing webhook by its ID
* @param {string} webhookID - the ID of the webhook to edit
* @param {string[]} addressesToRemove - the array of addresses to be removed from the webhook
* @returns {Promise<Webhook>} a promise that resolves to the edited webhook object
* @throws {Error} if there is an error calling the webhooks endpoint, if the response contains an error
*/
async removeAddressesFromWebhook(
webhookID: string,
addressesToRemove: string[]
): Promise<Webhook> {
try {
const webhook = await this.getWebhookByID(webhookID);
// Filter out the addresses to be removed
const accountAddresses = webhook.accountAddresses.filter(
address => !addressesToRemove.includes(address)
);
const editRequest: EditWebhookRequest = { accountAddresses };
return this._editWebhook(webhookID, webhook, editRequest);
} catch (err: any | AxiosError) {
if (axios.isAxiosError(err)) {
throw new Error(
`error during removeAddressesFromWebhook: ${
err.response?.data.error || err
}`
);
} else {
throw new Error(`error during removeAddressesFromWebhook: ${err}`);
}
}
}

async createCollectionWebhook(
request: CreateCollectionWebhookRequest
): Promise<Webhook> {
Expand Down

0 comments on commit 07216ab

Please sign in to comment.