Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/contact-importer/src/importer.d.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
9 changes: 6 additions & 3 deletions packages/contact-importer/src/importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down