0

Create 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
 * Find more information about the parameters at
5
 * https://mailchimp.com/developer/marketing/api/campaigns/add-campaign/
6
 */
7
type Mailchimp = {
8
  api_key: string;
9
  server: string;
10
};
11
export async function main(
12
  auth: Mailchimp,
13
  type: "regular" | "plaintext" | "rss" | "variate",
14
  content_type: "template" | "multichannel" = "template",
15
  rss_opts?: Record<string, any>,
16
  recipients?: Record<string, any>,
17
  variate_settings?: Record<string, any>,
18
  settings?: Record<string, any>,
19
  tracking?: Record<string, any>,
20
  social_card?: Record<string, any>,
21
) {
22
  const url = `https://${auth.server}.api.mailchimp.com/3.0/campaigns`;
23
  const body = {
24
    type,
25
    content_type,
26
    rss_opts,
27
    recipients,
28
    variate_settings,
29
    settings,
30
    tracking,
31
    social_card,
32
  };
33

34
  const response = await fetch(url, {
35
    method: "POST",
36
    headers: {
37
      Authorization: `Bearer ${auth.api_key}`,
38
    },
39
    body: JSON.stringify(removeObjectEmptyFields(body)),
40
  });
41

42
  if (!response.ok) {
43
    throw Error(await response.text());
44
  }
45
  return await response.json();
46
}
47

48
function removeObjectEmptyFields(
49
  object?: Record<string, any>,
50
  removeEmptyArraysAndObjects = true,
51
  createNewObject = true,
52
) {
53
  if (!object || typeof object !== "object") return {}
54
  const obj = createNewObject ? { ...object } : object
55
  const emptyValues = [undefined, null, ""]
56
  for (const key in obj) {
57
    const value = obj[key]
58
    if (emptyValues.includes(value)) {
59
      delete obj[key]
60
    } else if (typeof value === "object") {
61
      if (Object.keys(value).length) {
62
        obj[key] = removeObjectEmptyFields(value, removeEmptyArraysAndObjects, false)
63
      }
64
      if (!Object.keys(value).length && removeEmptyArraysAndObjects) {
65
        delete obj[key]
66
      }
67
    }
68
  }
69
  return obj
70
}
71

Other submissions
  • Submitted by adam186 Deno
    Created 398 days ago
    1
    import { removeObjectEmptyFields } from "https://deno.land/x/[email protected]/mod.ts";
    2
    
    
    3
    /**
    4
     * Find more information about the parameters at
    5
     * https://mailchimp.com/developer/marketing/api/campaigns/add-campaign/
    6
     */
    7
    type Mailchimp = {
    8
      api_key: string;
    9
      server: string;
    10
    };
    11
    export async function main(
    12
      auth: Mailchimp,
    13
      type: "regular" | "plaintext" | "rss" | "variate",
    14
      content_type: "template" | "multichannel" = "template",
    15
      rss_opts?: Record<string, any>,
    16
      recipients?: Record<string, any>,
    17
      variate_settings?: Record<string, any>,
    18
      settings?: Record<string, any>,
    19
      tracking?: Record<string, any>,
    20
      social_card?: Record<string, any>,
    21
    ) {
    22
      const url = `https://${auth.server}.api.mailchimp.com/3.0/campaigns`;
    23
      const body = {
    24
        type,
    25
        content_type,
    26
        rss_opts,
    27
        recipients,
    28
        variate_settings,
    29
        settings,
    30
        tracking,
    31
        social_card,
    32
      };
    33
    
    
    34
      const response = await fetch(url, {
    35
        method: "POST",
    36
        headers: {
    37
          Authorization: `Bearer ${auth.api_key}`,
    38
        },
    39
        body: JSON.stringify(removeObjectEmptyFields(body)),
    40
      });
    41
    
    
    42
      if (!response.ok) {
    43
        throw Error(await response.text());
    44
      }
    45
      return await response.json();
    46
    }
    47