0
Search Campaigns
One script reply has been approved by the moderators Verified
Created by rossumblament 934 days ago Viewed 9751 times
0
Submitted by adam186 Deno
Verified 760 days ago
1
/**
2
 * @param fields *(optional)* A list of fields to return in the response.
3
 * Reference parameters of sub-objects with dot notation.
4
 *
5
 * @param exclude_fields *(optional)* A list of fields to exclude from the response.
6
 * Reference parameters of sub-objects with dot notation. If both `fields` and `exclude_fields`
7
 * are present, then only `exclude_fields` will be used.
8
 */
9
type Mailchimp = {
10
  api_key: string;
11
  server: string;
12
};
13
export async function main(
14
  auth: Mailchimp,
15
  fields?: string[],
16
  exclude_fields?: string[],
17
  count?: number,
18
  offset?: number,
19
  sort_dir?: "" | "ASC" | "DESC",
20
  type?: "" | "regular" | "plaintext" | "absplit" | "rss" | "variate",
21
  status?: "" | "save" | "paused" | "schedule" | "sending" | "sent",
22
  before_send_time?: string,
23
  since_send_time?: string,
24
  before_create_time?: string,
25
  since_create_time?: string,
26
  list_id?: string,
27
  folder_id?: string,
28
  member_id?: string,
29
  sort_field?: "" | "create_time" | "send_time",
30
) {
31
  const url = new URL(`https://${auth.server}.api.mailchimp.com/3.0/campaigns`);
32
  const params = {
33
    fields,
34
    exclude_fields,
35
    count,
36
    offset,
37
    sort_dir,
38
    type,
39
    status,
40
    before_send_time,
41
    since_send_time,
42
    before_create_time,
43
    since_create_time,
44
    list_id,
45
    folder_id,
46
    member_id,
47
    sort_field,
48
  };
49
  for (const key in params) {
50
    const value = params[<keyof typeof params>key];
51
    if (value) {
52
      url.searchParams.append(
53
        key,
54
        Array.isArray(value) ? value.join(",") : "" + value,
55
      );
56
    }
57
  }
58

59
  const response = await fetch(url, {
60
    method: "GET",
61
    headers: {
62
      Authorization: `Bearer ${auth.api_key}`,
63
    },
64
  });
65

66
  if (!response.ok) {
67
    throw Error(await response.text());
68
  }
69
  return await response.json();
70
}
71