Edits history of script submission #210 for ' Create a Segment (mailchimp)'

  • deno
    One script reply has been approved by the moderators
    Ap­pro­ved
    import { removeObjectEmptyFields } from "https://deno.land/x/[email protected]/mod.ts";
    
    /**
     * @param static_segment *(optional but either `static_segment` or `options` is required)*
     * An array of emails to be used for a static segment.
     * Any emails provided that are not present on the list will be ignored.
     * Passing an empty array will create a static segment without any subscribers.
     * This field cannot be provided with the options field.
     *
     * @param options *(optional but either `static_segment` or `options` is required)*
     * The conditions of the segment. Static and fuzzy segments don't have conditions.
     * Find more information about conditions at
     * https://mailchimp.com/developer/marketing/docs/alternative-schemas/#segment-condition-schemas
     */
    type Mailchimp = {
      api_key: string;
      server: string;
    };
    export async function main(
      auth: Mailchimp,
      list_id: string,
      name: string,
      static_segment?: string[],
      options?: {
        match: string;
        conditions: any[];
      },
    ) {
      const url = new URL(
        `https://${auth.server}.api.mailchimp.com/3.0/lists/${list_id}/segments`,
      );
      const body = {
        name,
        static_segment,
        options,
      };
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Authorization: `Bearer ${auth.api_key}`,
        },
        body: JSON.stringify(removeObjectEmptyFields(body)),
      });
    
      if (!response.ok) {
        throw Error(await response.text());
      }
      return await response.json();
    }
    

    Submitted by hugo697 377 days ago

  • deno
    import { removeObjectEmptyFields } from "https://deno.land/x/[email protected]/mod.ts";
    
    /**
     * @param static_segment *(optional but either `static_segment` or `options` is required)*
     * An array of emails to be used for a static segment.
     * Any emails provided that are not present on the list will be ignored.
     * Passing an empty array will create a static segment without any subscribers.
     * This field cannot be provided with the options field.
     *
     * @param options *(optional but either `static_segment` or `options` is required)*
     * The conditions of the segment. Static and fuzzy segments don't have conditions.
     * Find more information about conditions at
     * https://mailchimp.com/developer/marketing/docs/alternative-schemas/#segment-condition-schemas
     */
    type Mailchimp = {
      api_key: string;
      server: string;
    };
    export async function main(
      auth: Mailchimp,
      list_id: string,
      name: string,
      static_segment?: string[],
      options?: {
        match: string;
        conditions: any[];
      },
    ) {
      const url = new URL(
        `https://${auth.server}.api.mailchimp.com/3.0/lists/${list_id}/segments`,
      );
      const body = {
        name,
        static_segment,
        options,
      };
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Authorization: `Bearer ${auth.api_key}`,
        },
        body: JSON.stringify(removeObjectEmptyFields(body)),
      });
    
      if (!response.ok) {
        throw Error(await response.text());
      }
      return await response.json();
    }
    

    Submitted by admin 1013 days ago

  • deno
    import { Resource } from "https://deno.land/x/[email protected]/mod.ts";
    import { removeObjectEmptyFields } from "https://deno.land/x/[email protected]/mod.ts";
    
    /**
     * @param static_segment *(optional but either `static_segment` or `options` is required)*
     * An array of emails to be used for a static segment.
     * Any emails provided that are not present on the list will be ignored.
     * Passing an empty array will create a static segment without any subscribers.
     * This field cannot be provided with the options field.
     *
     * @param options *(optional but either `static_segment` or `options` is required)*
     * The conditions of the segment. Static and fuzzy segments don't have conditions.
     * Find more information about conditions at
     * https://mailchimp.com/developer/marketing/docs/alternative-schemas/#segment-condition-schemas
     */
    export async function main(
      auth: Resource<"mailchimp">,
      list_id: string,
      name: string,
      static_segment?: string[],
      options?: {
        match: string;
        conditions: any[];
      },
    ) {
      const url = new URL(
        `https://${auth.server}.api.mailchimp.com/3.0/lists/${list_id}/segments`,
      );
      const body = {
        name,
        static_segment,
        options,
      };
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Authorization: `Bearer ${auth.api_key}`,
        },
        body: JSON.stringify(removeObjectEmptyFields(body)),
      });
    
      if (!response.ok) {
        throw Error(await response.text());
      }
      return await response.json();
    }
    

    Submitted by adam186 1141 days ago

  • deno
    import { Resource } from "https://deno.land/x/[email protected]/mod.ts";
    import { removeObjectEmptyFields } from "https://deno.land/x/[email protected]/mod.ts";
    
    /**
     * @param static_segment *(optional but either `static_segment` or `options` is required)*
     * An array of emails to be used for a static segment.
     * Any emails provided that are not present on the list will be ignored.
     * Passing an empty array will create a static segment without any subscribers.
     * This field cannot be provided with the options field.
     *
     * @param options *(optional but either `static_segment` or `options` is required)*
     * The conditions of the segment. Static and fuzzy segments don't have conditions.
     * Find more information about conditions at
     * https://mailchimp.com/developer/marketing/docs/alternative-schemas/#segment-condition-schemas
     */
    export async function main(
      auth: Resource<"mailchimp">,
      list_id: string,
      name: string,
      static_segment?: string[],
      options?: {
        match: string;
        conditions: any[];
      },
    ) {
      const url = new URL(
        `https://${auth.server}.api.mailchimp.com/3.0/lists/${list_id}/segments`,
      );
      const body = {
        name,
        static_segment,
        options,
      };
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Authorization: `Bearer ${auth.api_key}`,
        },
        body: JSON.stringify(removeObjectEmptyFields(body)),
      });
    
      if (!response.ok) {
        throw Error(await response.text());
      }
      return await response.json();
    }
    

    Submitted by adam186 1176 days ago

  • deno
    import { Resource } from "https://deno.land/x/[email protected]/mod.ts";
    import { removeObjectEmptyFields } from "https://deno.land/x/[email protected]/mod.ts";
    
    /**
     * @param static_segment *(optional but either `static_segment` or `options` is required)*
     * An array of emails to be used for a static segment.
     * Any emails provided that are not present on the list will be ignored.
     * Passing an empty array will create a static segment without any subscribers.
     * This field cannot be provided with the options field.
     *
     * @param options *(optional but either `static_segment` or `options` is required)*
     * The conditions of the segment. Static and fuzzy segments don't have conditions.
     * Find more information about conditions at
     * https://mailchimp.com/developer/marketing/docs/alternative-schemas/#segment-condition-schemas
     */
    export async function main(
      auth: Resource<"mailchimp">,
      list_id: string,
      name: string,
      static_segment?: string[],
      options?: {
        match: string;
        conditions: any[];
      },
    ) {
      const url = new URL(
        `https://${auth.server}.api.mailchimp.com/3.0/lists/${list_id}/segments`,
      );
      const body = {
        name,
        static_segment,
        options,
      };
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Authorization: `Bearer ${auth.api_key}`,
        },
        body: JSON.stringify(removeObjectEmptyFields(body)),
      });
    
      if (!response.ok) {
        throw Error(await response.text());
      }
      return await response.json();
    }
    

    Submitted by adam186 1247 days ago

  • deno
    /**
     * @param server The data center for your Mailchimp account.
     * 
     * @param static_segment *(optional but either `static_segment` or `options` is required)* 
     * An array of emails to be used for a static segment. 
     * Any emails provided that are not present on the list will be ignored. 
     * Passing an empty array will create a static segment without any subscribers. 
     * This field cannot be provided with the options field.
     * 
     * @param options *(optional but either `static_segment` or `options` is required)* 
     * The conditions of the segment. Static and fuzzy segments don't have conditions.
     * Find more information about conditions at
     * https://mailchimp.com/developer/marketing/docs/alternative-schemas/#segment-condition-schemas
     */
    export async function main(
      api_key: string,
      server: string,
      list_id: string,
      name: string,
      static_segment?: string[],
      options?: {
        match: string
        conditions: any[]
      },
    ) {
      const url = new URL(`https://${server}.api.mailchimp.com/3.0/lists/${list_id}/segments`)
      removeEmptyFields(options)
      const body = {
        name,
        static_segment,
        options
      }
      removeEmptyFields(body)
    
      const response = await fetch(url, {
        method: 'POST',
        headers: {
          Authorization: `Bearer ${api_key}`
        },
        body: JSON.stringify(body)
      })
      
      if(!response.ok) {
        throw Error(await response.text())
      }
      return await response.json()
    }
    
    function removeEmptyFields(obj: Record<string, any> | undefined) {
      if(!obj || typeof obj !== 'object') {
        return
      }
      for(const key in obj) {
        const value = obj[<keyof typeof obj>key]
        console.log(key, value, typeof value === 'object' && !Object.keys(value).length)
        if(
          value === undefined ||
          value === null ||
          value === '' ||
          (typeof value === 'object' && !Object.keys(value).length)
        ) {
          delete obj[<keyof typeof obj>key]
        }
      }
    }
    

    Submitted by adam186 1267 days ago