Create List

Script mailchimp Verified

by armancedinari ยท 6/6/2022

The script

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

3
/**
4
 * Find more information about the parameters at
5
 * https://mailchimp.com/developer/marketing/api/lists/add-list/
6
 */
7
type Mailchimp = {
8
  api_key: string;
9
  server: string;
10
};
11
export async function main(
12
  auth: Mailchimp,
13
  name: string,
14
  permission_reminder: string,
15
  campaign_from_name: string,
16
  campaign_from_email: string,
17
  campaign_subject: string,
18
  campaign_language: string,
19
  email_type_option: boolean,
20
  contact_company: string,
21
  contact_country: string,
22
  contact_city: string,
23
  contact_address1: string,
24
  contact_address2?: string,
25
  contact_state?: string,
26
  contact_zip?: string,
27
  contact_phone?: string,
28
  notify_on_subscribe?: string,
29
  notify_on_unsubscribe?: string,
30
  use_archive_bar?: boolean,
31
  double_optin?: boolean,
32
  marketing_permissions?: boolean,
33
) {
34
  const url = new URL(`https://${auth.server}.api.mailchimp.com/3.0/lists`);
35
  const body = {
36
    name,
37
    contact: {
38
      company: contact_company,
39
      country: contact_country,
40
      city: contact_city,
41
      address1: contact_address1,
42
      address2: contact_address2,
43
      state: contact_state,
44
      zip: contact_zip,
45
      phone: contact_phone,
46
    },
47
    permission_reminder,
48
    campaign_defaults: {
49
      from_name: campaign_from_name,
50
      from_email: campaign_from_email,
51
      subject: campaign_subject,
52
      language: campaign_language,
53
    },
54
    email_type_option,
55
    use_archive_bar,
56
    notify_on_subscribe,
57
    notify_on_unsubscribe,
58
    double_optin,
59
    marketing_permissions,
60
  };
61

62
  const response = await fetch(url, {
63
    method: "POST",
64
    headers: {
65
      Authorization: `Bearer ${auth.api_key}`,
66
    },
67
    body: JSON.stringify(removeObjectEmptyFields(body)),
68
  });
69

70
  if (!response.ok) {
71
    throw Error(await response.text());
72
  }
73
  return await response.json();
74
}
75

76
function removeObjectEmptyFields(
77
  object?: Record<string, any>,
78
  removeEmptyArraysAndObjects = true,
79
  createNewObject = true,
80
) {
81
  if (!object || typeof object !== "object") return {}
82
  const obj = createNewObject ? { ...object } : object
83
  const emptyValues = [undefined, null, ""]
84
  for (const key in obj) {
85
    const value = obj[key]
86
    if (emptyValues.includes(value)) {
87
      delete obj[key]
88
    } else if (typeof value === "object") {
89
      if (Object.keys(value).length) {
90
        obj[key] = removeObjectEmptyFields(value, removeEmptyArraysAndObjects, false)
91
      }
92
      if (!Object.keys(value).length && removeEmptyArraysAndObjects) {
93
        delete obj[key]
94
      }
95
    }
96
  }
97
  return obj
98
}
99

Other submissions
  • Submitted by adam186 Deno
    Created 397 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/lists/add-list/
    6
     */
    7
    type Mailchimp = {
    8
      api_key: string;
    9
      server: string;
    10
    };
    11
    export async function main(
    12
      auth: Mailchimp,
    13
      name: string,
    14
      permission_reminder: string,
    15
      campaign_from_name: string,
    16
      campaign_from_email: string,
    17
      campaign_subject: string,
    18
      campaign_language: string,
    19
      email_type_option: boolean,
    20
      contact_company: string,
    21
      contact_country: string,
    22
      contact_city: string,
    23
      contact_address1: string,
    24
      contact_address2?: string,
    25
      contact_state?: string,
    26
      contact_zip?: string,
    27
      contact_phone?: string,
    28
      notify_on_subscribe?: string,
    29
      notify_on_unsubscribe?: string,
    30
      use_archive_bar?: boolean,
    31
      double_optin?: boolean,
    32
      marketing_permissions?: boolean,
    33
    ) {
    34
      const url = new URL(`https://${auth.server}.api.mailchimp.com/3.0/lists`);
    35
      const body = {
    36
        name,
    37
        contact: {
    38
          company: contact_company,
    39
          country: contact_country,
    40
          city: contact_city,
    41
          address1: contact_address1,
    42
          address2: contact_address2,
    43
          state: contact_state,
    44
          zip: contact_zip,
    45
          phone: contact_phone,
    46
        },
    47
        permission_reminder,
    48
        campaign_defaults: {
    49
          from_name: campaign_from_name,
    50
          from_email: campaign_from_email,
    51
          subject: campaign_subject,
    52
          language: campaign_language,
    53
        },
    54
        email_type_option,
    55
        use_archive_bar,
    56
        notify_on_subscribe,
    57
        notify_on_unsubscribe,
    58
        double_optin,
    59
        marketing_permissions,
    60
      };
    61
    
    
    62
      const response = await fetch(url, {
    63
        method: "POST",
    64
        headers: {
    65
          Authorization: `Bearer ${auth.api_key}`,
    66
        },
    67
        body: JSON.stringify(removeObjectEmptyFields(body)),
    68
      });
    69
    
    
    70
      if (!response.ok) {
    71
        throw Error(await response.text());
    72
      }
    73
      return await response.json();
    74
    }
    75