Edits history of script submission #8943 for ' Create or Update Subscriber (mailerlite)'

  • nativets
    One script reply has been approved by the moderators
    Ap­pro­ved
    type MailerLite = {
    	apiToken: string
    }
    
    export async function main(
    	resource: MailerLite,
    	body: {
    		email: string //
    		fields?: {
    			name?: string
    			last_name?: string
    			company?: string
    			country?: string
    			city?: string
    			phone?: string
    			state?: string
    			z_i_p?: string
    		}
    		groups?: string[]
    		status?: 'active' | 'unsubscribed' | 'unconfirmed' | 'bounced' | 'junk'
    		subscribed_at?: string // yyyy-MM-dd HH:mm:ss
    		ip_address?: string
    		opted_in_at?: string // yyyy-MM-dd HH:mm:ss
    		optin_ip?: string
    		unsubscribed_at?: string // yyyy-MM-dd HH:mm:ss
    	}
    ) {
    	const endpoint = `https://connect.mailerlite.com/api/subscribers`
    
    	const response = await fetch(endpoint, {
    		method: 'POST',
    		headers: {
    			'Content-Type': 'application/json',
    			Accept: 'application/json',
    			Authorization: `Bearer ${resource.apiToken}`
    		},
    		body: JSON.stringify(body)
    	})
    
    	if (!response.ok) {
    		const error = await response.json()
    		console.log(error.message)
    		throw new Error(`HTTP error! status: ${response.status}`)
    	}
    
    	const { data } = await response.json()
    	return data
    }
    

    Submitted by hugo697 652 days ago