From c2dd5a2934d19d9d063d2f0b0ef5e9d757a96321 Mon Sep 17 00:00:00 2001 From: Martin Haug Date: Mon, 9 Jan 2023 11:31:38 +0100 Subject: [PATCH] Upgrade to current marketing API --- packages/contact-importer/src/importer.d.ts | 8 +++++--- packages/contact-importer/src/importer.js | 9 ++++++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/contact-importer/src/importer.d.ts b/packages/contact-importer/src/importer.d.ts index 3530389d5..accf3110f 100644 --- a/packages/contact-importer/src/importer.d.ts +++ b/packages/contact-importer/src/importer.d.ts @@ -1,20 +1,22 @@ +import * as sendgrid from "@sendgrid/client"; + declare interface ContactImporterOptions { batchSize?: number; rateLimitLimit?: number; rateLimitPeriod?: number; + listIds?: number[]; } declare interface Contact { email: string; first_name?: string; last_name?: string; - age?: number; } declare class ContactImporter { - constructor(sg, options?: ContactImporterOptions); + constructor(sg: typeof sendgrid, options?: ContactImporterOptions); - push(data: Contact|Contact[]) + push(data: Contact|Contact[]): void; on(event: "success", cb: (result: any, batch: Contact[]) => void): void; on(event: "error", cb: (err: Error, batch?: Contact[]) => void): void; diff --git a/packages/contact-importer/src/importer.js b/packages/contact-importer/src/importer.js index a34248787..26e2e5723 100644 --- a/packages/contact-importer/src/importer.js +++ b/packages/contact-importer/src/importer.js @@ -25,6 +25,9 @@ class ContactImporter extends EventEmitter { // Length of rate limit period (miliseconds). this.rateLimitPeriod = options.rateLimitPeriod || 2000; + // Identifier of the new contact list to create or to add contacts to. + this.listIds = options.listIds || []; + // Create a throttler that will process no more than `rateLimitLimit` requests every `rateLimitPeriod` ms. this.throttle = new Bottleneck(0, 0); this.throttle.changeReservoir(this.rateLimitLimit); @@ -78,9 +81,9 @@ class ContactImporter extends EventEmitter { debug('sending batch (%s items)', data.length); const request = { - method: 'POST', - uri: '/v3/contactdb/recipients', - body: data, + method: 'PUT', + uri: '/v3/marketing/contacts', + body: { contacts: data, list_ids: this.listIds }, }; context.sg.request(request)