0

Get Campaign

by
Published Jun 6, 2022
Script mailchimp Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 6 days ago
1
//native
2

3
/**
4
 * @param fields *(optional)* A list of fields to return in the response.
5
 * Reference parameters of sub-objects with dot notation.
6
 *
7
 * @param exclude_fields *(optional)* A list of fields to exclude from the response.
8
 * Reference parameters of sub-objects with dot notation. If both `fields` and `exclude_fields`
9
 * are present, then only `exclude_fields` will be used.
10
 */
11
type Mailchimp = {
12
  api_key: string;
13
  server: string;
14
};
15
export async function main(
16
  auth: Mailchimp,
17
  campaign_id: string,
18
  fields?: string[],
19
  exclude_fields?: string[],
20
) {
21
  let url = `https://${auth.server}.api.mailchimp.com/3.0/campaigns/${campaign_id}?`;
22
  if (fields?.length) {
23
    url += `fields=${fields.join(",")}`;
24
  }
25
  if (exclude_fields?.length) {
26
    url += `${fields?.length ? "&" : ""}exclude_fields=${exclude_fields.join(
27
      ",",
28
    )}`;
29
  }
30

31
  const response = await fetch(url, {
32
    method: "GET",
33
    headers: {
34
      Authorization: `Bearer ${auth.api_key}`,
35
    },
36
  });
37

38
  if (!response.ok) {
39
    throw Error(await response.text());
40
  }
41
  return await response.json();
42
}
43

Other submissions
  • Submitted by adam186 Deno
    Created 398 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
      campaign_id: string,
    16
      fields?: string[],
    17
      exclude_fields?: string[],
    18
    ) {
    19
      let url = `https://${auth.server}.api.mailchimp.com/3.0/campaigns/${campaign_id}?`;
    20
      if (fields?.length) {
    21
        url += `fields=${fields.join(",")}`;
    22
      }
    23
      if (exclude_fields?.length) {
    24
        url += `${fields?.length ? "&" : ""}exclude_fields=${exclude_fields.join(
    25
          ",",
    26
        )}`;
    27
      }
    28
    
    
    29
      const response = await fetch(url, {
    30
        method: "GET",
    31
        headers: {
    32
          Authorization: `Bearer ${auth.api_key}`,
    33
        },
    34
      });
    35
    
    
    36
      if (!response.ok) {
    37
        throw Error(await response.text());
    38
      }
    39
      return await response.json();
    40
    }
    41