-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from railsware/bulk-sending-api
Bulk sending api
- Loading branch information
Showing
12 changed files
with
221 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { MailtrapClient } from "mailtrap" | ||
|
||
/** | ||
* For this example, you need to have ready-to-use sending domain or, | ||
* a Demo domain that allows sending emails to your own account email. | ||
* @see https://help.mailtrap.io/article/69-sending-domain-setup | ||
*/ | ||
|
||
", @see https://help.mailtrap.io/article/69-sending-domain-setup#Demo-Domain--oYOU5" | ||
|
||
const TOKEN = "<YOUR-TOKEN-HERE>"; | ||
const SENDER_EMAIL = "<[email protected]>"; | ||
const RECIPIENT_EMAIL = "<[email protected]>"; | ||
|
||
const client = new MailtrapClient({ token: TOKEN }); | ||
|
||
client.bulk.send({ | ||
from: { name: "Mailtrap Test", email: SENDER_EMAIL }, | ||
to: [{ email: RECIPIENT_EMAIL }], | ||
subject: "Hello from Mailtrap!", | ||
text: "Welcome to Mailtrap Sending!", | ||
}) | ||
.then(console.log) | ||
.catch(console.error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import axios from "axios"; | ||
import AxiosMockAdapter from "axios-mock-adapter"; | ||
|
||
import BulkAPI from "../../../lib/api/Bulk"; | ||
import handleSendingError from "../../../lib/axios-logger"; | ||
import MailtrapError from "../../../lib/MailtrapError"; | ||
|
||
import CONFIG from "../../../config"; | ||
|
||
const { CLIENT_SETTINGS } = CONFIG; | ||
const { BULK_ENDPOINT } = CLIENT_SETTINGS; | ||
|
||
describe("lib/api/Bulk: ", () => { | ||
let mock: AxiosMockAdapter; | ||
const bulkAPI = new BulkAPI(axios); | ||
|
||
describe("class Testing(): ", () => { | ||
describe("init: ", () => { | ||
it("initalizes with all necessary params.", () => { | ||
expect(bulkAPI).toHaveProperty("send"); | ||
}); | ||
}); | ||
|
||
beforeAll(() => { | ||
/** | ||
* Init Axios interceptors for handling response.data, errors. | ||
*/ | ||
axios.interceptors.response.use( | ||
(response) => response.data, | ||
handleSendingError | ||
); | ||
mock = new AxiosMockAdapter(axios); | ||
}); | ||
|
||
afterEach(() => { | ||
mock.reset(); | ||
}); | ||
|
||
describe("send(): ", () => { | ||
it("successfully sends email.", async () => { | ||
const endpoint = `${BULK_ENDPOINT}/api/send`; | ||
const expectedResponseData = { | ||
success: true, | ||
message_ids: ["0c7fd939-02cf-11ed-88c2-0a58a9feac02"], | ||
}; | ||
mock.onPost(endpoint).reply(200, expectedResponseData); | ||
|
||
const emailData = { | ||
from: { | ||
email: "[email protected]", | ||
name: "sender", | ||
}, | ||
to: [ | ||
{ | ||
email: "[email protected]", | ||
name: "recipient", | ||
}, | ||
], | ||
subject: "mock-subject", | ||
text: "Mock text", | ||
html: "<div>Mock text</div>", | ||
}; | ||
|
||
const result = await bulkAPI.send(emailData); | ||
|
||
expect(mock.history.post[0].url).toEqual(endpoint); | ||
expect(mock.history.post[0].data).toEqual(JSON.stringify(emailData)); | ||
expect(result).toEqual(expectedResponseData); | ||
}); | ||
|
||
it("handles an API error.", async () => { | ||
const responseData = { | ||
success: false, | ||
errors: ["mock-error-1", "mock-error-2"], | ||
}; | ||
|
||
const endpoint = `${BULK_ENDPOINT}/api/send`; | ||
|
||
mock.onPost(endpoint).reply(400, responseData); | ||
|
||
const emailData = { | ||
from: { | ||
email: "[email protected]", | ||
name: "sender", | ||
}, | ||
to: [ | ||
{ | ||
email: "[email protected]", | ||
name: "recipient", | ||
}, | ||
], | ||
subject: "mock-subject", | ||
text: "Mock text", | ||
html: "<div>Mock text</div>", | ||
}; | ||
|
||
const expectedErrorMessage = responseData.errors.join(","); | ||
|
||
expect.assertions(3); | ||
|
||
try { | ||
await bulkAPI.send(emailData); | ||
} catch (error) { | ||
expect(mock.history.post[0].url).toEqual(endpoint); | ||
expect(mock.history.post[0].data).toEqual(JSON.stringify(emailData)); | ||
|
||
if (error instanceof Error) { | ||
expect(error.message).toEqual(expectedErrorMessage); | ||
} | ||
} | ||
}); | ||
|
||
it("handles an HTTP transport error.", async () => { | ||
const emailData = { | ||
from: { | ||
email: "[email protected]", | ||
name: "sender", | ||
}, | ||
to: [ | ||
{ | ||
email: "[email protected]", | ||
name: "recipient", | ||
}, | ||
], | ||
subject: "mock-subject", | ||
text: "Mock text", | ||
html: "<div>Mock text</div>", | ||
}; | ||
|
||
const expectedErrorMessage = "Request failed with status code 404"; | ||
|
||
expect.assertions(2); | ||
|
||
try { | ||
await bulkAPI.send(emailData); | ||
} catch (error) { | ||
expect(error).toBeInstanceOf(MailtrapError); | ||
|
||
if (error instanceof MailtrapError) { | ||
expect(error.message).toEqual(expectedErrorMessage); | ||
} | ||
} | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,7 +73,7 @@ describe("lib/api/Testing: ", () => { | |
expect(result).toEqual(expectedResponseData); | ||
}); | ||
|
||
it("rejects with api error.", async () => { | ||
it("handles an API error.", async () => { | ||
const responseData = { | ||
success: false, | ||
errors: ["mock-error-1", "mock-error-2"], | ||
|
@@ -115,7 +115,7 @@ describe("lib/api/Testing: ", () => { | |
} | ||
}); | ||
|
||
it("rejects with axios error.", async () => { | ||
it("handles an HTTP transport error.", async () => { | ||
const emailData = { | ||
from: { | ||
email: "[email protected]", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { AxiosInstance } from "axios"; | ||
|
||
import encodeMailBuffers from "../mail-buffer-encoder"; | ||
|
||
import CONFIG from "../../config"; | ||
|
||
import { Mail, SendResponse } from "../../types/mailtrap"; | ||
|
||
const { CLIENT_SETTINGS } = CONFIG; | ||
const { BULK_ENDPOINT } = CLIENT_SETTINGS; | ||
|
||
export default class BulkAPI { | ||
private client: AxiosInstance; | ||
|
||
constructor(client: AxiosInstance) { | ||
this.client = client; | ||
} | ||
|
||
public async send(mail: Mail): Promise<SendResponse> { | ||
const url = `${BULK_ENDPOINT}/api/send`; | ||
const preparedMail = encodeMailBuffers(mail); | ||
|
||
return this.client.post<SendResponse, SendResponse>(url, preparedMail); | ||
} | ||
} |