Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructure SDK #51

Open
wants to merge 29 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
b96f604
types: add bulk and sandbox flags to mailtrap
narekhovhannisyan Aug 19, 2024
36a9df3
api: move testing send method to client
narekhovhannisyan Aug 19, 2024
0b49830
api: drop bulk
narekhovhannisyan Aug 19, 2024
ef8e1b9
lib: refactor MailtrapClient to incapsulate bulk, sandbox send fnality
narekhovhannisyan Aug 19, 2024
dbd5fc6
config: add bulk sandbox incompatible message
narekhovhannisyan Aug 19, 2024
e8d764b
test: drop moved functionality from Testing
narekhovhannisyan Aug 19, 2024
6f3dcd5
test: drop Bulk
narekhovhannisyan Aug 19, 2024
ce67277
test: cover new mailtrap client functionality with units
narekhovhannisyan Aug 19, 2024
e53b1c0
examples: fix mailtrap imports in testing
narekhovhannisyan Aug 19, 2024
13fc15d
examples: fix mailtrap imports in general
narekhovhannisyan Aug 19, 2024
10046b8
examples: update bulk sending example
narekhovhannisyan Aug 19, 2024
a5d4d20
docs: update Readme file, remove missing testing api notice
narekhovhannisyan Aug 19, 2024
50c8c56
types: drop sending from additional fields
narekhovhannisyan Aug 19, 2024
10f73e5
lib: drop switchable client
narekhovhannisyan Aug 19, 2024
a8b56bc
test: drop transport sandbox switch
narekhovhannisyan Aug 19, 2024
f24a304
examples: update transport sample
narekhovhannisyan Aug 19, 2024
b57bcfe
.github: configure codeql
narekhovhannisyan Aug 26, 2024
9d00310
.github: configure codeql tests path
narekhovhannisyan Aug 26, 2024
88e2acf
.github: add newline at the end of codeql config
narekhovhannisyan Aug 26, 2024
c2f1f89
.github: update codeql config to ignore all files in tests
narekhovhannisyan Aug 26, 2024
16abea3
.github: drop codeql from test workflow
narekhovhannisyan Aug 26, 2024
96d79a9
.github: tune codeql config
narekhovhannisyan Aug 26, 2024
f764542
WIP
vittorius Aug 27, 2024
7584350
.github: update codeql config path
narekhovhannisyan Aug 29, 2024
51c959c
test: drop commented code from mailtrap client
narekhovhannisyan Aug 29, 2024
7968a07
docs: move info on previous releases to the end of README
narekhovhannisyan Sep 9, 2024
49e099d
lib: separate host detection logic from send method
narekhovhannisyan Sep 9, 2024
eed53c6
examples: init transport samples, send testing mail sample
narekhovhannisyan Sep 9, 2024
b03961e
examples: fix missing comma in transport
narekhovhannisyan Sep 18, 2024
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
7 changes: 7 additions & 0 deletions .github/codeql/codeql-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
paths:
- 'src'
paths-ignore:
- 'src/__tests__/**/*.js'
- 'src/__tests__/**/*.ts'
- 'src/__tests__/**/*.jsx'
- 'src/__tests__/**/*.tsx'
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ on: push
jobs:
lint:
runs-on: ubuntu-latest

steps:
- uses: github/codeql-action/init@v3
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess since codeql is not triggered from the workflow, we don't have to mention it here.

with:
config-file: .github/codeql/codeql-config.yml
- uses: actions/checkout@v2
- uses: actions/setup-node@v3
with:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Quickly add email sending functionality to your Node.js application with Mailtra

## Compatibility with previous releases

Versions of this package up to 2.0.2 were an [unofficial client](https://github.com/vchin/mailtrap-client) developed by [@vchin](https://github.com/vchin). Package version 3 is a completely new package. It is still under development and does not support the testing API yet. Please continue using version 2 if you need access to the testing API.
Versions of this package up to 2.0.2 were an [unofficial client](https://github.com/vchin/mailtrap-client) developed by [@vchin](https://github.com/vchin). Package version 3 is a completely new package.
narekhovhannisyan marked this conversation as resolved.
Show resolved Hide resolved

## Installation

Expand Down
4 changes: 2 additions & 2 deletions examples/bulk/send-mail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ const TOKEN = "<YOUR-TOKEN-HERE>";
const SENDER_EMAIL = "<SENDER@YOURDOMAIN.COM>";
const RECIPIENT_EMAIL = "<RECIPIENT@EMAIL.COM>";

const client = new MailtrapClient({ token: TOKEN });
const client = new MailtrapClient({ token: TOKEN, bulk: true });
narekhovhannisyan marked this conversation as resolved.
Show resolved Hide resolved

client.bulk.send({
client.send({
from: { name: "Mailtrap Test", email: SENDER_EMAIL },
to: [{ email: RECIPIENT_EMAIL }],
subject: "Hello from Mailtrap!",
Expand Down
2 changes: 1 addition & 1 deletion examples/general/account-accesses.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MailtrapClient } from "../../src"
import { MailtrapClient } from "mailtrap"

const TOKEN = "<YOUR-TOKEN-HERE>";
const TEST_INBOX_ID = "<YOUR-TEST-INBOX-ID-HERE>"
Expand Down
2 changes: 1 addition & 1 deletion examples/general/accounts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MailtrapClient } from "../../src"
import { MailtrapClient } from "mailtrap"

const TOKEN = "<YOUR-TOKEN-HERE>";
const TEST_INBOX_ID = "<YOUR-TEST-INBOX-ID-HERE>"
Expand Down
2 changes: 1 addition & 1 deletion examples/general/permissions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MailtrapClient } from "../../src"
import { MailtrapClient } from "mailtrap"

const TOKEN = "<YOUR-TOKEN-HERE>";
const TEST_INBOX_ID = "<YOUR-TEST-INBOX-ID-HERE>"
Expand Down
3 changes: 1 addition & 2 deletions examples/sending/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ const SENDER_EMAIL = "<SENDER@YOURDOMAIN.COM>";
const RECIPIENT_EMAIL = "<RECIPIENT@EMAIL.COM>";

const transport = Nodemailer.createTransport(MailtrapTransport({
token: TOKEN
token: TOKEN,
}))

// Note: 'sandbox: true' can be passed for making requests to Testing API
transport.sendMail({
text: "Welcome to Mailtrap Sending!",
to: {
Expand Down
2 changes: 1 addition & 1 deletion examples/testing/attachments.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MailtrapClient } from "../../src"
import { MailtrapClient } from "mailtrap"

const TOKEN = "<YOUR-TOKEN-HERE>";
const TEST_INBOX_ID = "<YOUR-TEST-INBOX-ID-HERE>"
Expand Down
2 changes: 1 addition & 1 deletion examples/testing/inboxes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MailtrapClient } from "../../src"
import { MailtrapClient } from "mailtrap"

const TOKEN = "<YOUR-TOKEN-HERE>";
const TEST_INBOX_ID = "<YOUR-TEST-INBOX-ID-HERE>"
Expand Down
2 changes: 1 addition & 1 deletion examples/testing/messages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MailtrapClient } from "../../src"
import { MailtrapClient } from "mailtrap"

const TOKEN = "<YOUR-TOKEN-HERE>";
const TEST_INBOX_ID = "<YOUR-TEST-INBOX-ID-HERE>"
Expand Down
2 changes: 1 addition & 1 deletion examples/testing/template.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MailtrapClient } from "../../src"
import { MailtrapClient } from "mailtrap"

/**
* For this example to work, you need to set up a sending domain,
Expand Down
146 changes: 0 additions & 146 deletions src/__tests__/lib/api/Bulk.test.ts

This file was deleted.

132 changes: 0 additions & 132 deletions src/__tests__/lib/api/Testing.test.ts
Original file line number Diff line number Diff line change
@@ -1,151 +1,19 @@
import axios from "axios";
import AxiosMockAdapter from "axios-mock-adapter";

import Testing from "../../../lib/api/Testing";
import handleSendingError from "../../../lib/axios-logger";

import CONFIG from "../../../config";
import MailtrapError from "../../../lib/MailtrapError";

const { CLIENT_SETTINGS } = CONFIG;
const { TESTING_ENDPOINT } = CLIENT_SETTINGS;

describe("lib/api/Testing: ", () => {
let mock: AxiosMockAdapter;
const testInboxId = 100;
const testingAPI = new Testing(axios, testInboxId);

describe("class Testing(): ", () => {
describe("init: ", () => {
it("initalizes with all necessary params.", () => {
expect(testingAPI).toHaveProperty("send");
expect(testingAPI).toHaveProperty("projects");
expect(testingAPI).toHaveProperty("inboxes");
expect(testingAPI).toHaveProperty("messages");
expect(testingAPI).toHaveProperty("attachments");
});
});

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 = `${TESTING_ENDPOINT}/api/send/${testInboxId}`;
const expectedResponseData = {
success: true,
message_ids: ["0c7fd939-02cf-11ed-88c2-0a58a9feac02"],
};
mock.onPost(endpoint).reply(200, expectedResponseData);

const emailData = {
from: {
email: "sender.mock@email.com",
name: "sender",
},
to: [
{
email: "recipient.mock@email.com",
name: "recipient",
},
],
subject: "mock-subject",
text: "Mock text",
html: "<div>Mock text</div>",
};

const result = await testingAPI.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 = `${TESTING_ENDPOINT}/api/send/${testInboxId}`;

mock.onPost(endpoint).reply(400, responseData);

const emailData = {
from: {
email: "sender.mock@email.com",
name: "sender",
},
to: [
{
email: "recipient.mock@email.com",
name: "recipient",
},
],
subject: "mock-subject",
text: "Mock text",
html: "<div>Mock text</div>",
};

const expectedErrorMessage = responseData.errors.join(",");

expect.assertions(3);

try {
await testingAPI.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: "sender.mock@email.com",
name: "sender",
},
to: [
{
email: "recipient.mock@email.com",
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 testingAPI.send(emailData);
} catch (error) {
expect(error).toBeInstanceOf(MailtrapError);

if (error instanceof MailtrapError) {
expect(error.message).toEqual(expectedErrorMessage);
}
}
});
});
});
});
Loading
Loading