Skip to content
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ General API:

- Templates CRUD, paginated (experimental) – [`templates/everything.ts`](examples/templates/everything.ts)
- Email templates CRUD (`/api/email_templates`) – [`templates/email-templates.ts`](examples/templates/email-templates.ts)
- Suppressions (create, find & delete) – [`sending/suppressions.ts`](examples/sending/suppressions.ts)
- Suppressions (create, find by email or date & delete) – [`sending/suppressions.ts`](examples/sending/suppressions.ts)
- Tracking Opt-outs (list, create & delete) – [`sending/tracking-opt-outs.ts`](examples/sending/tracking-opt-outs.ts)
- Billing info – [`general/billing.ts`](examples/general/billing.ts)
- Accounts info – [`general/accounts.ts`](examples/general/accounts.ts)
Expand Down
5 changes: 5 additions & 0 deletions examples/sending/suppressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ async function suppressionsFlow() {
const filteredSuppressions = await client.suppressions.getList({email: "test@example.com"});
console.log("Filtered suppressions:", filteredSuppressions);

// Get suppressions created in the last 30 days (up to 1000 per request)
const startTime = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000).toISOString();
const recentSuppressions = await client.suppressions.getList({start_time: startTime});
console.log("Suppressions from the last 30 days (up to 1000):", recentSuppressions);

// Include contacts' unsubscriptions from single subscription topics, as
// "topic unsubscription" rows with their `subscription_topic_id`. Needs a
// token that can read contacts; without subscription topics there are none.
Expand Down
34 changes: 34 additions & 0 deletions src/__tests__/lib/api/resources/Suppressions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,40 @@ describe("lib/api/resources/Suppressions: ", () => {
expect(result[2].subscription_topic_id).toEqual(12);
});

it("passes start_time, end_time and last_id when set.", async () => {
const endpoint = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/suppressions`;

const params = {
start_time: "2023-01-01T00:00:00Z",
end_time: "2023-01-31T23:59:59Z",
last_id: "2",
};

expect.assertions(1);

mock.onGet(endpoint).reply(200, mockSuppressions);
await suppressionsAPI.getList(params);

expect(mock.history.get[0].params).toEqual(params);
});

it("returns rows stored without a sending stream as any.", async () => {
const endpoint = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/suppressions`;
const anyStream: Suppression = {
...mockSuppression,
id: "4",
type: "unsubscription",
sending_stream: "any",
};

expect.assertions(1);

mock.onGet(endpoint).reply(200, [anyStream]);
const result = await suppressionsAPI.getList();

expect(result[0].sending_stream).toEqual("any");
});

it("sends nothing for include_topic_unsubscriptions: false.", async () => {
const endpoint = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/suppressions`;

Expand Down
6 changes: 5 additions & 1 deletion src/lib/api/resources/Suppressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ export default class SuppressionsApi {
}

/**
* List and search suppressions by email. The endpoint returns up to 1000 suppressions per request.
* List suppressions, optionally filtered by email or creation time.
* The endpoint returns up to 1000 suppressions per request.
*/
public async getList(options?: ListOptions) {
const params = {
...(options?.email && { email: options.email }),
...(options?.start_time && { start_time: options.start_time }),
...(options?.end_time && { end_time: options.end_time }),
...(options?.last_id && { last_id: options.last_id }),
...(options?.include_topic_unsubscriptions && {
include_topic_unsubscriptions: true,
}),
Expand Down
28 changes: 26 additions & 2 deletions src/types/api/suppressions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { SendingStream } from "./webhooks";

/**
* `topic unsubscription` rows are listed only with
* `include_topic_unsubscriptions`, and can't be created or deleted.
Expand All @@ -9,12 +11,18 @@ export type SuppressionType =
| "manual import"
| "topic unsubscription";

/**
* `any` marks an opt-out stored without a sending stream; it can't be passed
* to `create`.
*/
export type SuppressionSendingStream = SendingStream | "any";

export type Suppression = {
id: string;
type: SuppressionType;
created_at: string;
email: string;
sending_stream: "transactional" | "bulk";
sending_stream: SuppressionSendingStream;
domain_name: string | null;
message_bounce_category: string | null;
message_category: string | null;
Expand All @@ -31,7 +39,23 @@ export type Suppression = {
};

export type ListOptions = {
/** Filters by exact address (case-insensitive). */
email?: string;
/**
* Start of the `created_at` window (inclusive), ISO 8601, e.g.
* `2024-01-01T00:00:00Z`. Not applied when `email` is set.
*/
start_time?: string;
/**
* End of the `created_at` window (inclusive), ISO 8601, e.g.
* `2024-01-31T23:59:59Z`. Not applied when `email` is set.
*/
end_time?: string;
/**
* Returns the suppressions after the one with this `id`. Suppressions created
* in the same second as that one can be returned again.
*/
last_id?: string;
/**
* Adds the contacts' unsubscriptions from single subscription topics, as
* `topic unsubscription` rows. Needs a token that can read contacts.
Expand All @@ -42,7 +66,7 @@ export type ListOptions = {
export type CreateSuppressionParams = {
email: string;
domain_id: number;
sending_stream: "transactional" | "bulk";
sending_stream: SendingStream;
type?: Exclude<SuppressionType, "topic unsubscription">;
};

Expand Down