@clxmedia/emailhub-client 2.0.2 → 2.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client.d.ts +8 -4
- package/dist/client.js +49 -16
- package/package.json +1 -1
package/dist/client.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EmailHubConfig, EmailHubEnvironment, EmailHubMessage, EmailHubPriorityLevel, EmailHubSenderProfile } from
|
|
1
|
+
import { EmailHubConfig, EmailHubEnvironment, EmailHubMessage, EmailHubPriorityLevel, EmailHubSenderProfile } from '@clxmedia/types/emailhub';
|
|
2
2
|
export declare class MockEmailHubClient {
|
|
3
3
|
readonly environment: EmailHubEnvironment;
|
|
4
4
|
private extendedLogging;
|
|
@@ -47,8 +47,9 @@ export declare class EmailHubClient {
|
|
|
47
47
|
private sender;
|
|
48
48
|
private server;
|
|
49
49
|
private pubsubClient;
|
|
50
|
-
|
|
51
|
-
|
|
50
|
+
private directApiOnly;
|
|
51
|
+
constructor(cfg: EmailHubConfig, directApiOnly?: boolean);
|
|
52
|
+
static new(cfg: EmailHubConfig, extendedLogging?: boolean, directApiOnly?: boolean): EmailHubClient | MockEmailHubClient;
|
|
52
53
|
private loadPubSubClient;
|
|
53
54
|
static validateEmailAddress(email?: string, regex?: RegExp): boolean;
|
|
54
55
|
createSenderProfile(domain: string): Promise<EmailHubSenderProfile>;
|
|
@@ -58,5 +59,8 @@ export declare class EmailHubClient {
|
|
|
58
59
|
sendEmail(msg: EmailHubMessage, options?: {
|
|
59
60
|
priority?: EmailHubPriorityLevel;
|
|
60
61
|
writeToLog?: boolean;
|
|
61
|
-
}): Promise<
|
|
62
|
+
}): Promise<{
|
|
63
|
+
guid: string;
|
|
64
|
+
messageId?: string;
|
|
65
|
+
}>;
|
|
62
66
|
}
|
package/dist/client.js
CHANGED
|
@@ -50,7 +50,7 @@ const mockEmailHubSenderProfile = {
|
|
|
50
50
|
spf_status: 'Error',
|
|
51
51
|
created_at: luxon_1.DateTime.local().toISO(),
|
|
52
52
|
updated_at: luxon_1.DateTime.local().toISO(),
|
|
53
|
-
verified_at: undefined
|
|
53
|
+
verified_at: undefined,
|
|
54
54
|
};
|
|
55
55
|
class MockEmailHubClient {
|
|
56
56
|
constructor(extendedLogging = false) {
|
|
@@ -78,16 +78,20 @@ class MockEmailHubClient {
|
|
|
78
78
|
}
|
|
79
79
|
exports.MockEmailHubClient = MockEmailHubClient;
|
|
80
80
|
class EmailHubClient {
|
|
81
|
-
constructor(cfg) {
|
|
81
|
+
constructor(cfg, directApiOnly = false) {
|
|
82
82
|
this.environment = emailhub_1.EmailHubEnvironment.emailhub;
|
|
83
|
+
this.directApiOnly = false;
|
|
83
84
|
this.sender = cfg.sender;
|
|
84
85
|
this.server = cfg.server ? cfg.server : DEFAULT_EMAIL_HUB_URL;
|
|
86
|
+
this.directApiOnly = directApiOnly || false;
|
|
85
87
|
}
|
|
86
|
-
static new(cfg, extendedLogging = false) {
|
|
87
|
-
return
|
|
88
|
+
static new(cfg, extendedLogging = false, directApiOnly = false) {
|
|
89
|
+
return cfg.sender.guid && cfg.sender.secret
|
|
90
|
+
? new EmailHubClient(cfg, directApiOnly)
|
|
91
|
+
: new MockEmailHubClient(extendedLogging);
|
|
88
92
|
}
|
|
89
93
|
async loadPubSubClient() {
|
|
90
|
-
if (this.pubsubClient) {
|
|
94
|
+
if (this.pubsubClient || this.directApiOnly) {
|
|
91
95
|
return;
|
|
92
96
|
}
|
|
93
97
|
try {
|
|
@@ -109,7 +113,11 @@ class EmailHubClient {
|
|
|
109
113
|
}
|
|
110
114
|
async createSenderProfile(domain) {
|
|
111
115
|
const result = await axios_1.default.post(`${this.server}/sender-profiles`, { domain }, {
|
|
112
|
-
headers: {
|
|
116
|
+
headers: {
|
|
117
|
+
'Content-Type': 'application/json',
|
|
118
|
+
'x-emailhub-sender-guid': this.sender.guid,
|
|
119
|
+
'x-emailhub-sender-secret': this.sender.secret,
|
|
120
|
+
},
|
|
113
121
|
});
|
|
114
122
|
if (result.status === 400) {
|
|
115
123
|
throw Error('Domain already taken or configured.');
|
|
@@ -118,7 +126,11 @@ class EmailHubClient {
|
|
|
118
126
|
}
|
|
119
127
|
async getSenderProfile(domainGuid) {
|
|
120
128
|
const result = await axios_1.default.get(`${this.server}/sender-profiles/${domainGuid}`, {
|
|
121
|
-
headers: {
|
|
129
|
+
headers: {
|
|
130
|
+
'Content-Type': 'application/json',
|
|
131
|
+
'x-emailhub-sender-guid': this.sender.guid,
|
|
132
|
+
'x-emailhub-sender-secret': this.sender.secret,
|
|
133
|
+
},
|
|
122
134
|
});
|
|
123
135
|
if (result.status >= 400) {
|
|
124
136
|
throw Error('Domain locator invalid or record not found.');
|
|
@@ -127,7 +139,11 @@ class EmailHubClient {
|
|
|
127
139
|
}
|
|
128
140
|
async checkSenderProfile(domainGuid) {
|
|
129
141
|
const result = await axios_1.default.post(`${this.server}/sender-profiles/${domainGuid}`, undefined, {
|
|
130
|
-
headers: {
|
|
142
|
+
headers: {
|
|
143
|
+
'Content-Type': 'application/json',
|
|
144
|
+
'x-emailhub-sender-guid': this.sender.guid,
|
|
145
|
+
'x-emailhub-sender-secret': this.sender.secret,
|
|
146
|
+
},
|
|
131
147
|
});
|
|
132
148
|
if (result.status >= 400) {
|
|
133
149
|
throw Error('Domain locator invalid or record not found.');
|
|
@@ -136,7 +152,11 @@ class EmailHubClient {
|
|
|
136
152
|
}
|
|
137
153
|
async deleteSenderProfile(domainGuid) {
|
|
138
154
|
const result = await axios_1.default.delete(`${this.server}/sender-profiles/${domainGuid}`, {
|
|
139
|
-
headers: {
|
|
155
|
+
headers: {
|
|
156
|
+
'Content-Type': 'application/json',
|
|
157
|
+
'x-emailhub-sender-guid': this.sender.guid,
|
|
158
|
+
'x-emailhub-sender-secret': this.sender.secret,
|
|
159
|
+
},
|
|
140
160
|
});
|
|
141
161
|
return result.status;
|
|
142
162
|
}
|
|
@@ -144,13 +164,22 @@ class EmailHubClient {
|
|
|
144
164
|
await this.loadPubSubClient();
|
|
145
165
|
const xMailGuid = msg && msg.CustomID ? msg.CustomID : uuid.v4();
|
|
146
166
|
msg.CustomID = xMailGuid;
|
|
147
|
-
const topic = options &&
|
|
167
|
+
const topic = options &&
|
|
168
|
+
options.priority &&
|
|
169
|
+
options.priority === emailhub_1.EmailHubPriorityLevel.BULK
|
|
170
|
+
? 'send-new-email-bulk'
|
|
171
|
+
: 'send-new-email';
|
|
148
172
|
if (this.pubsubClient !== null) {
|
|
149
173
|
try {
|
|
150
|
-
await this.pubsubClient
|
|
174
|
+
await this.pubsubClient
|
|
175
|
+
.topic(`projects/xperience-prod/topics/${topic}`)
|
|
151
176
|
.publishMessage({
|
|
152
|
-
json: {
|
|
153
|
-
|
|
177
|
+
json: {
|
|
178
|
+
sender_guid: this.sender.guid,
|
|
179
|
+
sender_secret: this.sender.secret,
|
|
180
|
+
payload: Buffer.from(Pako.deflate(Buffer.from(JSON.stringify(msg)).toString('base64'))).toString('base64'),
|
|
181
|
+
},
|
|
182
|
+
attributes: { sender_guid: this.sender.guid },
|
|
154
183
|
});
|
|
155
184
|
return xMailGuid;
|
|
156
185
|
}
|
|
@@ -158,10 +187,14 @@ class EmailHubClient {
|
|
|
158
187
|
console.log('Failed to send email via PubSub queue. Falling back to Direct API connection.');
|
|
159
188
|
}
|
|
160
189
|
}
|
|
161
|
-
await axios_1.default.post(`${this.server}/email/send`, msg, {
|
|
162
|
-
headers: {
|
|
190
|
+
const result = await axios_1.default.post(`${this.server}/email/send`, msg, {
|
|
191
|
+
headers: {
|
|
192
|
+
'Content-Type': 'application/json',
|
|
193
|
+
'x-emailhub-sender-guid': this.sender.guid,
|
|
194
|
+
'x-emailhub-sender-secret': this.sender.secret,
|
|
195
|
+
},
|
|
163
196
|
});
|
|
164
|
-
return xMailGuid;
|
|
197
|
+
return { guid: xMailGuid, messageId: result.data };
|
|
165
198
|
}
|
|
166
199
|
}
|
|
167
200
|
exports.EmailHubClient = EmailHubClient;
|