diff --git a/README.md b/README.md index a7ee2dc..142727f 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/examples/sending/suppressions.ts b/examples/sending/suppressions.ts index ebae105..1dab094 100644 --- a/examples/sending/suppressions.ts +++ b/examples/sending/suppressions.ts @@ -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. diff --git a/src/__tests__/lib/api/resources/Suppressions.test.ts b/src/__tests__/lib/api/resources/Suppressions.test.ts index 2600bf9..4cac37b 100644 --- a/src/__tests__/lib/api/resources/Suppressions.test.ts +++ b/src/__tests__/lib/api/resources/Suppressions.test.ts @@ -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`; diff --git a/src/lib/api/resources/Suppressions.ts b/src/lib/api/resources/Suppressions.ts index 923d1f4..d0ec48e 100644 --- a/src/lib/api/resources/Suppressions.ts +++ b/src/lib/api/resources/Suppressions.ts @@ -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, }), diff --git a/src/types/api/suppressions.ts b/src/types/api/suppressions.ts index 5cd0676..f40d93b 100644 --- a/src/types/api/suppressions.ts +++ b/src/types/api/suppressions.ts @@ -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. @@ -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; @@ -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. @@ -42,7 +66,7 @@ export type ListOptions = { export type CreateSuppressionParams = { email: string; domain_id: number; - sending_stream: "transactional" | "bulk"; + sending_stream: SendingStream; type?: Exclude; };