-
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 #24 from
email-testing
.
- Loading branch information
Showing
16 changed files
with
775 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { MailtrapClient } from "../../src" | ||
|
||
const TOKEN = "<YOUR-TOKEN-HERE>"; | ||
const TEST_INBOX_ID = "<YOUR-TEST-INBOX-ID-HERE>" | ||
const ACCOUNT_ID = "<YOUR-ACCOUNT-ID-HERE>" | ||
|
||
const client = new MailtrapClient({ token: TOKEN, testInboxId: TEST_INBOX_ID, accountId: ACCOUNT_ID }); | ||
|
||
const projectsClient = client.testing.projects | ||
const inboxesClient = client.testing.inboxes | ||
|
||
projectsClient.getList() | ||
.then(async (projects) => { | ||
if (projects && projects.length > 0) { | ||
const firstProjectId = projects[0].id | ||
|
||
await inboxesClient.create(firstProjectId, 'test-inbox') | ||
|
||
const inboxes = await inboxesClient.getList() | ||
|
||
if (inboxes && inboxes.length > 0) { | ||
const firstInboxId = inboxes[0].id | ||
|
||
const inboxAttributes = await inboxesClient.getInboxAttributes(firstInboxId) | ||
await inboxesClient.updateInbox(firstInboxId, {name: 'mock-name', emailUsername: 'mocker'}) | ||
await inboxesClient.cleanInbox(firstInboxId) | ||
await inboxesClient.markAsRead(firstInboxId) | ||
await inboxesClient.resetCredentials(firstInboxId) | ||
await inboxesClient.enableEmailAddress(firstInboxId) | ||
const response = await inboxesClient.resetEmailAddress(firstInboxId) | ||
|
||
console.log(response) | ||
} | ||
} | ||
}) |
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,41 @@ | ||
import { MailtrapClient } from "../../src" | ||
|
||
const TOKEN = "<YOUR-TOKEN-HERE>"; | ||
const TEST_INBOX_ID = "<YOUR-TEST-INBOX-ID-HERE>" | ||
const ACCOUNT_ID = "<YOUR-ACCOUNT-ID-HERE>" | ||
|
||
const client = new MailtrapClient({ token: TOKEN, testInboxId: TEST_INBOX_ID, accountId: ACCOUNT_ID }); | ||
|
||
const inboxesClient = client.testing.inboxes | ||
const messagesClient = client.testing.messages | ||
|
||
inboxesClient.getList() | ||
.then(async (inboxes) => { | ||
if (inboxes && inboxes.length > 0) { | ||
const firstInboxId = inboxes[0].id | ||
|
||
const messages = await messagesClient.get(firstInboxId) | ||
|
||
if (messages && messages.length > 0) { | ||
const firstMessageId = messages[0].id | ||
|
||
await messagesClient.get(firstInboxId) | ||
await messagesClient.getHtmlAnalysis(firstInboxId, firstMessageId) | ||
await messagesClient.getHtmlMessage(firstInboxId, firstMessageId) | ||
await messagesClient.getTextMessage(firstInboxId, firstMessageId) | ||
await messagesClient.getMailHeaders(firstInboxId, firstMessageId) | ||
await messagesClient.getMessageAsEml(firstInboxId, firstMessageId) | ||
await messagesClient.getMessageHtmlSource(firstInboxId, firstMessageId) | ||
await messagesClient.getRawMessage(firstInboxId, firstMessageId) | ||
await messagesClient.getSpamScore(firstInboxId, firstMessageId) | ||
await messagesClient.showEmailMessage(firstInboxId, firstMessageId) | ||
await messagesClient.updateMessage(firstInboxId, firstMessageId, { | ||
isRead: false | ||
}) | ||
await messagesClient.forward(firstInboxId, firstMessageId, '[email protected]') | ||
const response = await messagesClient.deleteMessage(firstInboxId, firstMessageId) | ||
|
||
console.log(response) | ||
} | ||
} | ||
}) |
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,22 @@ | ||
import { MailtrapClient } from "mailtrap" | ||
|
||
const TOKEN = "<YOUR-TOKEN-HERE>"; | ||
const TEST_INBOX_ID = "<YOUR-TEST-INBOX-ID-HERE>" | ||
const ACCOUNT_ID = "<YOUR-ACCOUNT-ID-HERE>" | ||
|
||
const client = new MailtrapClient({ token: TOKEN, testInboxId: TEST_INBOX_ID, accountId: ACCOUNT_ID }); | ||
|
||
const projectsClient = client.testing.projects | ||
|
||
projectsClient.getList() | ||
.then(async (projects) => { | ||
if (projects) { | ||
const firstProject = projects[0].id // Grab the first project. | ||
|
||
const updatedProject = | ||
await projectsClient.update(firstProject, 'test-project') // Update the name of the project. | ||
|
||
await projectsClient.delete() | ||
} | ||
}) | ||
.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import axios, { AxiosInstance } from "axios"; | ||
|
||
import ProjectsApi from "./resources/Projects"; | ||
import InboxesApi from "./resources/Inboxes"; | ||
import MessagesApi from "./resources/Messages"; | ||
import encodeMailBuffers from "../mail-buffer-encoder"; | ||
import handleSendingError from "../axios-logger"; | ||
|
||
import CONFIG from "../../config"; | ||
|
||
import { Mail, SendResponse } from "../../types/mailtrap"; | ||
|
||
const { CLIENT_SETTINGS } = CONFIG; | ||
const { TESTING_ENDPOINT } = CLIENT_SETTINGS; | ||
|
||
export default class TestingAPI { | ||
private client: AxiosInstance; | ||
|
||
private testInboxId?: number; | ||
|
||
private accountId?: number; | ||
|
||
public projects: ProjectsApi; | ||
|
||
public inboxes: InboxesApi; | ||
|
||
public messages: MessagesApi; | ||
|
||
constructor(client: AxiosInstance, testInboxId?: number, accountId?: number) { | ||
this.client = client; | ||
this.accountId = accountId; | ||
this.testInboxId = testInboxId; | ||
this.projects = new ProjectsApi(this.client, this.accountId); | ||
this.inboxes = new InboxesApi(this.client, this.accountId); | ||
this.messages = new MessagesApi(this.client, this.accountId); | ||
} | ||
|
||
public async send(mail: Mail): Promise<SendResponse> { | ||
const url = `${TESTING_ENDPOINT}/api/send/${this.testInboxId}`; | ||
const preparedMail = encodeMailBuffers(mail); | ||
|
||
try { | ||
const axiosResponse = await this.client.post<SendResponse>( | ||
url, | ||
preparedMail | ||
); | ||
|
||
return axiosResponse.data; | ||
} catch (error) { | ||
if (axios.isAxiosError(error)) { | ||
handleSendingError(error); | ||
} | ||
|
||
throw error; // should not happen, but otherwise rethrow error as is | ||
} | ||
} | ||
} |
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,128 @@ | ||
import { AxiosInstance } from "axios"; | ||
|
||
import CONFIG from "../../../config"; | ||
|
||
import { Inbox, UpdateInboxParams } from "../../../types/api/inboxes"; | ||
|
||
const { CLIENT_SETTINGS } = CONFIG; | ||
const { GENERAL_ENDPOINT } = CLIENT_SETTINGS; | ||
|
||
export default class InboxesApi { | ||
private client: AxiosInstance; | ||
|
||
private accountId?: number; | ||
|
||
private inboxesURL: string; | ||
|
||
constructor(client: AxiosInstance, accountId?: number) { | ||
this.client = client; | ||
this.accountId = accountId; | ||
this.inboxesURL = `${GENERAL_ENDPOINT}/api/accounts/${this.accountId}/inboxes`; | ||
} | ||
|
||
/** | ||
* Creates an inbox in a project. | ||
*/ | ||
public async create(projectId: number, inboxName: string) { | ||
const url = `${GENERAL_ENDPOINT}/api/accounts/${this.accountId}/projects/${projectId}/inboxes`; | ||
const data = { inbox: { name: inboxName } }; | ||
const apiResponse = await this.client.post<Inbox>(url, data); | ||
|
||
return apiResponse.data; | ||
} | ||
|
||
/** | ||
* Gets inbox attributes by inbox id. | ||
*/ | ||
public async getInboxAttributes(inboxId: number) { | ||
const url = `${this.inboxesURL}/${inboxId}`; | ||
const apiResponse = await this.client.get<Inbox>(url); | ||
|
||
return apiResponse.data; | ||
} | ||
|
||
/** | ||
* Deletes an inbox with all its emails. | ||
*/ | ||
public async delete(inboxId: number) { | ||
const url = `${this.inboxesURL}/${inboxId}`; | ||
const apiResponse = await this.client.delete<Inbox>(url); | ||
|
||
return apiResponse.data; | ||
} | ||
|
||
/** | ||
* Updates inbox name, inbox email username. | ||
*/ | ||
public async updateInbox(inboxId: number, params: UpdateInboxParams) { | ||
const url = `${this.inboxesURL}/${inboxId}`; | ||
const data = { | ||
inbox: { | ||
name: params.name, | ||
email_username: params.emailUsername, | ||
}, | ||
}; | ||
const apiRespone = await this.client.patch<Inbox>(url, data); | ||
|
||
return apiRespone.data; | ||
} | ||
|
||
/** | ||
* Deletes all messages (emails) from inbox. | ||
*/ | ||
public async cleanInbox(inboxId: number) { | ||
const url = `${this.inboxesURL}/${inboxId}/clean`; | ||
const apiRespone = await this.client.patch<Inbox>(url); | ||
|
||
return apiRespone.data; | ||
} | ||
|
||
/** | ||
* Marks all messages in the inbox as read. | ||
*/ | ||
public async markAsRead(inboxId: number) { | ||
const url = `${this.inboxesURL}/${inboxId}/all_read`; | ||
const apiRespone = await this.client.patch<Inbox>(url); | ||
|
||
return apiRespone.data; | ||
} | ||
|
||
/** | ||
* Resets SMTP credentials of the inbox. | ||
*/ | ||
public async resetCredentials(inboxId: number) { | ||
const url = `${this.inboxesURL}/${inboxId}/reset_credentials`; | ||
const apiRespone = await this.client.patch<Inbox>(url); | ||
|
||
return apiRespone.data; | ||
} | ||
|
||
/** | ||
* Turns the email address of the inbox on/off. | ||
*/ | ||
public async enableEmailAddress(inboxId: number) { | ||
const url = `${this.inboxesURL}/${inboxId}/toggle_email_username`; | ||
const apiRespone = await this.client.patch<Inbox>(url); | ||
|
||
return apiRespone.data; | ||
} | ||
|
||
/** | ||
* Resets username of email address per inbox. | ||
*/ | ||
public async resetEmailAddress(inboxId: number) { | ||
const url = `${this.inboxesURL}/${inboxId}/reset_email_username`; | ||
const apiRespone = await this.client.patch<Inbox>(url); | ||
|
||
return apiRespone.data; | ||
} | ||
|
||
/** | ||
* Gets a list of inboxes. | ||
*/ | ||
public async getList() { | ||
const apiRespone = await this.client.get<Inbox[]>(this.inboxesURL); | ||
|
||
return apiRespone.data; | ||
} | ||
} |
Oops, something went wrong.