1 | |
2 |
|
3 | |
4 | * List Mailings |
5 | * Lists mailings (sent/scheduled emails), filterable by prospect, state or updatedAt range. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Outreach, |
9 | filter_prospect_id: number | undefined, |
10 | filter_state: |
11 | | ( |
12 | | "bounced" |
13 | | "delivered" |
14 | | "delivering" |
15 | | "drafted" |
16 | | "failed" |
17 | | "opened" |
18 | | "placeholder" |
19 | | "queued" |
20 | | "replied" |
21 | | "scheduled" |
22 | ) |
23 | | undefined, |
24 | filter_updated_at: string | undefined, |
25 | sort: string | undefined, |
26 | page_size: number | undefined, |
27 | page_after: string | undefined |
28 | ) { |
29 | const url = new URL("https://api.outreach.io/api/v2/mailings") |
30 | if (filter_prospect_id !== undefined) { |
31 | url.searchParams.append("filter[prospect][id]", String(filter_prospect_id)) |
32 | } |
33 | if (filter_state !== undefined) { |
34 | url.searchParams.append("filter[state]", filter_state) |
35 | } |
36 | if (filter_updated_at !== undefined && filter_updated_at !== "") { |
37 | url.searchParams.append("filter[updatedAt]", filter_updated_at) |
38 | } |
39 | if (sort !== undefined && sort !== "") { |
40 | url.searchParams.append("sort", sort) |
41 | } |
42 | if (page_size !== undefined) { |
43 | url.searchParams.append("page[size]", String(page_size)) |
44 | } |
45 | if (page_after !== undefined && page_after !== "") { |
46 | url.searchParams.append("page[after]", page_after) |
47 | } |
48 |
|
49 | const response = await fetch(url, { |
50 | headers: { |
51 | Authorization: `Bearer ${auth.token}`, |
52 | Accept: "application/vnd.api+json", |
53 | }, |
54 | }) |
55 |
|
56 | if (!response.ok) { |
57 | throw new Error(`${response.status} ${await response.text()}`) |
58 | } |
59 |
|
60 | return await response.json() |
61 | } |
62 |
|