From 20424436f10f050a21ec05a61287b11f717cc132 Mon Sep 17 00:00:00 2001 From: Narek Hovhannisyan Date: Fri, 10 Nov 2023 14:34:48 +0400 Subject: [PATCH 01/12] types: init attachments. --- src/types/api/attachments.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 src/types/api/attachments.ts diff --git a/src/types/api/attachments.ts b/src/types/api/attachments.ts new file mode 100644 index 0000000..65a1b1c --- /dev/null +++ b/src/types/api/attachments.ts @@ -0,0 +1,14 @@ +export type Attachment = { + id: number; + message_id: number; + filename: string; + attachment_type: string; + content_type: string; + content_id: string; + transfer_encoding: string; + attachment_size: number; + created_at: string; + updated_at: string; + attachment_human_size: string; + download_path: string; +}; From 5df28558a3cf0c08a3cd7c19b87c953b567a23d6 Mon Sep 17 00:00:00 2001 From: Narek Hovhannisyan Date: Fri, 10 Nov 2023 14:53:04 +0400 Subject: [PATCH 02/12] examples: init testing attachments. --- examples/testing/attachments.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 examples/testing/attachments.ts diff --git a/examples/testing/attachments.ts b/examples/testing/attachments.ts new file mode 100644 index 0000000..4eab35b --- /dev/null +++ b/examples/testing/attachments.ts @@ -0,0 +1,29 @@ +import { MailtrapClient } from "../../src" + +const TOKEN = ""; +const TEST_INBOX_ID = "" +const ACCOUNT_ID = "" + +const client = new MailtrapClient({ token: TOKEN, testInboxId: TEST_INBOX_ID, accountId: ACCOUNT_ID }); + +const inboxesClient = client.testing.inboxes +const messagesClient = client.testing.messages +const attachmentsClient = client.testing.attachments + +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 attachmentsClient.getList(firstMessageId, firstInboxId) + const attachment = await attachmentsClient.get(559735926, firstMessageId, firstInboxId) + + console.log(attachment) + } + } + }) From 6d4c4ccc2280bd63cb869a1cc3f057b48196c7cf Mon Sep 17 00:00:00 2001 From: Narek Hovhannisyan Date: Fri, 10 Nov 2023 14:53:17 +0400 Subject: [PATCH 03/12] api: in testing add attachments. --- src/lib/api/Testing.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/lib/api/Testing.ts b/src/lib/api/Testing.ts index 6759c3b..7553625 100644 --- a/src/lib/api/Testing.ts +++ b/src/lib/api/Testing.ts @@ -3,6 +3,8 @@ import axios, { AxiosInstance } from "axios"; import ProjectsApi from "./resources/Projects"; import InboxesApi from "./resources/Inboxes"; import MessagesApi from "./resources/Messages"; +import AttachmentsApi from "./resources/Attachments"; + import encodeMailBuffers from "../mail-buffer-encoder"; import handleSendingError from "../axios-logger"; @@ -26,6 +28,8 @@ export default class TestingAPI { public messages: MessagesApi; + public attachments: AttachmentsApi; + constructor(client: AxiosInstance, testInboxId?: number, accountId?: number) { this.client = client; this.accountId = accountId; @@ -33,6 +37,7 @@ export default class TestingAPI { this.projects = new ProjectsApi(this.client, this.accountId); this.inboxes = new InboxesApi(this.client, this.accountId); this.messages = new MessagesApi(this.client, this.accountId); + this.attachments = new AttachmentsApi(this.client, this.accountId); } public async send(mail: Mail): Promise { From bf9868c48e06dbce5638c8d449aca64e3cccb8ec Mon Sep 17 00:00:00 2001 From: Narek Hovhannisyan Date: Fri, 10 Nov 2023 14:53:33 +0400 Subject: [PATCH 04/12] resources: implement attachments. --- src/lib/api/resources/Attachments.ts | 40 ++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/lib/api/resources/Attachments.ts diff --git a/src/lib/api/resources/Attachments.ts b/src/lib/api/resources/Attachments.ts new file mode 100644 index 0000000..bab3b1e --- /dev/null +++ b/src/lib/api/resources/Attachments.ts @@ -0,0 +1,40 @@ +import { AxiosInstance } from "axios"; + +import CONFIG from "../../../config"; + +import { Attachment } from "../../../types/api/attachments"; + +const { CLIENT_SETTINGS } = CONFIG; +const { GENERAL_ENDPOINT } = CLIENT_SETTINGS; + +export default class AttachmentsApi { + 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`; + } + + /** + * Get message attachments by `inboxId` and `messageId`. + */ + public async getList(messageId: number, inboxId: number) { + const url = `${this.inboxesURL}/${inboxId}/messages/${messageId}/attachments`; + + return this.client.get(url); + } + + /** + * Get message attachments by `inboxId`.`messageId` and `attachmentId`. + */ + public async get(attachmentId: number, messageId: number, inboxId: number) { + const url = `${this.inboxesURL}/${inboxId}/messages/${messageId}/attachments/${attachmentId}`; + + return this.client.get(url); + } +} From a93d0553883286a8cbfa21fa4c057c9fdeea6e02 Mon Sep 17 00:00:00 2001 From: Narek Hovhannisyan Date: Fri, 10 Nov 2023 14:56:37 +0400 Subject: [PATCH 05/12] examples: upgrade attachments. --- examples/testing/attachments.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/examples/testing/attachments.ts b/examples/testing/attachments.ts index 4eab35b..b580ee9 100644 --- a/examples/testing/attachments.ts +++ b/examples/testing/attachments.ts @@ -20,10 +20,14 @@ inboxesClient.getList() if (messages && messages.length > 0) { const firstMessageId = messages[0].id - await attachmentsClient.getList(firstMessageId, firstInboxId) - const attachment = await attachmentsClient.get(559735926, firstMessageId, firstInboxId) + const attachments = await attachmentsClient.getList(firstMessageId, firstInboxId) - console.log(attachment) + if (attachments && attachments.length > 0) { + const firstAttachment = attachments[0].id + const attachmentData = await attachmentsClient.get(firstInboxId, firstMessageId, firstAttachment) + + console.log(attachmentData) + } } } }) From 84994eb76008e27aa7194c2ce026b0c211ba725d Mon Sep 17 00:00:00 2001 From: Narek Hovhannisyan Date: Fri, 10 Nov 2023 14:57:06 +0400 Subject: [PATCH 06/12] lib: add response data interceptor to axios. --- src/lib/mailtrap-client.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/lib/mailtrap-client.ts b/src/lib/mailtrap-client.ts index 03b78c9..75975c3 100644 --- a/src/lib/mailtrap-client.ts +++ b/src/lib/mailtrap-client.ts @@ -43,13 +43,20 @@ export default class MailtrapClient { maxRedirects: MAX_REDIRECTS, timeout: TIMEOUT, }); + + /** + * Init Axios interceptors for handling response.data, errors. + */ this.axios.interceptors.response.use( - (response) => response, + (response) => response.data, handleSendingError ); this.testInboxId = testInboxId; this.accountId = accountId; + /** + * Initialize testing API. + */ this.testingAPI = new TestingAPI( this.axios, this.testInboxId, From b4951c99c6ff298d3a59eb21dd9faf5a5eda2021 Mon Sep 17 00:00:00 2001 From: Narek Hovhannisyan Date: Fri, 10 Nov 2023 14:57:32 +0400 Subject: [PATCH 07/12] resources: tune function arguments order in attachments. --- src/lib/api/resources/Attachments.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/api/resources/Attachments.ts b/src/lib/api/resources/Attachments.ts index bab3b1e..2edf17e 100644 --- a/src/lib/api/resources/Attachments.ts +++ b/src/lib/api/resources/Attachments.ts @@ -23,7 +23,7 @@ export default class AttachmentsApi { /** * Get message attachments by `inboxId` and `messageId`. */ - public async getList(messageId: number, inboxId: number) { + public async getList(inboxId: number, messageId: number) { const url = `${this.inboxesURL}/${inboxId}/messages/${messageId}/attachments`; return this.client.get(url); @@ -32,7 +32,7 @@ export default class AttachmentsApi { /** * Get message attachments by `inboxId`.`messageId` and `attachmentId`. */ - public async get(attachmentId: number, messageId: number, inboxId: number) { + public async get(inboxId: number, messageId: number, attachmentId: number) { const url = `${this.inboxesURL}/${inboxId}/messages/${messageId}/attachments/${attachmentId}`; return this.client.get(url); From 02de6fb6a4bb932a9a413117442247332cad1310 Mon Sep 17 00:00:00 2001 From: Narek Hovhannisyan Date: Fri, 10 Nov 2023 14:57:51 +0400 Subject: [PATCH 08/12] resources: tune inboxes return directly data. --- src/lib/api/resources/Inboxes.ts | 32 ++++++++++---------------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/src/lib/api/resources/Inboxes.ts b/src/lib/api/resources/Inboxes.ts index 813b19e..fb60264 100644 --- a/src/lib/api/resources/Inboxes.ts +++ b/src/lib/api/resources/Inboxes.ts @@ -26,9 +26,8 @@ export default class InboxesApi { 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(url, data); - return apiResponse.data; + return this.client.post(url, data); } /** @@ -36,9 +35,8 @@ export default class InboxesApi { */ public async getInboxAttributes(inboxId: number) { const url = `${this.inboxesURL}/${inboxId}`; - const apiResponse = await this.client.get(url); - return apiResponse.data; + return this.client.get(url); } /** @@ -46,9 +44,7 @@ export default class InboxesApi { */ public async delete(inboxId: number) { const url = `${this.inboxesURL}/${inboxId}`; - const apiResponse = await this.client.delete(url); - - return apiResponse.data; + return this.client.delete(url); } /** @@ -62,9 +58,8 @@ export default class InboxesApi { email_username: params.emailUsername, }, }; - const apiRespone = await this.client.patch(url, data); - return apiRespone.data; + return this.client.patch(url, data); } /** @@ -72,9 +67,8 @@ export default class InboxesApi { */ public async cleanInbox(inboxId: number) { const url = `${this.inboxesURL}/${inboxId}/clean`; - const apiRespone = await this.client.patch(url); - return apiRespone.data; + return this.client.patch(url); } /** @@ -82,9 +76,8 @@ export default class InboxesApi { */ public async markAsRead(inboxId: number) { const url = `${this.inboxesURL}/${inboxId}/all_read`; - const apiRespone = await this.client.patch(url); - return apiRespone.data; + return this.client.patch(url); } /** @@ -92,9 +85,8 @@ export default class InboxesApi { */ public async resetCredentials(inboxId: number) { const url = `${this.inboxesURL}/${inboxId}/reset_credentials`; - const apiRespone = await this.client.patch(url); - return apiRespone.data; + return this.client.patch(url); } /** @@ -102,9 +94,8 @@ export default class InboxesApi { */ public async enableEmailAddress(inboxId: number) { const url = `${this.inboxesURL}/${inboxId}/toggle_email_username`; - const apiRespone = await this.client.patch(url); - return apiRespone.data; + return this.client.patch(url); } /** @@ -112,17 +103,14 @@ export default class InboxesApi { */ public async resetEmailAddress(inboxId: number) { const url = `${this.inboxesURL}/${inboxId}/reset_email_username`; - const apiRespone = await this.client.patch(url); - return apiRespone.data; + return this.client.patch(url); } /** * Gets a list of inboxes. */ public async getList() { - const apiRespone = await this.client.get(this.inboxesURL); - - return apiRespone.data; + return this.client.get(this.inboxesURL); } } From b5b0d949469b4116964c3155b452193a3409e968 Mon Sep 17 00:00:00 2001 From: Narek Hovhannisyan Date: Fri, 10 Nov 2023 14:58:09 +0400 Subject: [PATCH 09/12] resources: tune messages to return data directly. --- src/lib/api/resources/Messages.ts | 39 +++++++++++-------------------- 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/src/lib/api/resources/Messages.ts b/src/lib/api/resources/Messages.ts index a8f80a8..341e0aa 100644 --- a/src/lib/api/resources/Messages.ts +++ b/src/lib/api/resources/Messages.ts @@ -28,9 +28,8 @@ export default class MessagesApi { */ public async showEmailMessage(inboxId: number, messageId: number) { const url = `${this.messagesURL}/${inboxId}/messages/${messageId}`; - const apiResponse = await this.client.get(url); - return apiResponse.data; + return this.client.get(url); } /** @@ -43,9 +42,8 @@ export default class MessagesApi { ) { const url = `${this.messagesURL}/${inboxId}/messages/${messageId}`; const data = { message: { is_read: params.isRead } }; - const apiResponse = await this.client.patch(url, data); - return apiResponse.data; + return this.client.patch(url, data); } /** @@ -53,9 +51,8 @@ export default class MessagesApi { */ public async deleteMessage(inboxId: number, messageId: number) { const url = `${this.messagesURL}/${inboxId}/messages/${messageId}`; - const apiResponse = await this.client.delete(url); - return apiResponse.data; + return this.client.delete(url); } /** @@ -63,9 +60,8 @@ export default class MessagesApi { */ public async get(inboxId: number) { const url = `${this.messagesURL}/${inboxId}/messages`; - const apiResponse = await this.client.get(url); - return apiResponse.data; + return this.client.get(url); } /** @@ -78,9 +74,8 @@ export default class MessagesApi { ) { const url = `${this.messagesURL}/${inboxId}/messages/${messageId}/forward`; const data = { email: emailToForward }; - const apiResponse = await this.client.post(url, data); - return apiResponse.data; + return this.client.post(url, data); } /** @@ -88,9 +83,8 @@ export default class MessagesApi { */ public async getSpamScore(inboxId: number, messageId: number) { const url = `${this.messagesURL}/${inboxId}/messages/${messageId}/spam_report`; - const apiResponse = await this.client.get(url); - return apiResponse.data; + return this.client.get(url); } /** @@ -98,9 +92,8 @@ export default class MessagesApi { */ public async getHtmlAnalysis(inboxId: number, messageId: number) { const url = `${this.messagesURL}/${inboxId}/messages/${messageId}/analyze`; - const apiResponse = await this.client.get(url); - return apiResponse.data; + return this.client.get(url); } /** @@ -108,9 +101,8 @@ export default class MessagesApi { */ public async getTextMessage(inboxId: number, messageId: number) { const url = `${this.messagesURL}/${inboxId}/messages/${messageId}/body.txt`; - const apiResponse = await this.client.get(url); - return apiResponse.data; + return this.client.get(url); } /** @@ -118,9 +110,8 @@ export default class MessagesApi { */ public async getRawMessage(inboxId: number, messageId: number) { const url = `${this.messagesURL}/${inboxId}/messages/${messageId}/body.raw`; - const apiResponse = await this.client.get(url); - return apiResponse.data; + return this.client.get(url); } /** @@ -128,9 +119,8 @@ export default class MessagesApi { */ public async getMessageHtmlSource(inboxId: number, messageId: number) { const url = `${this.messagesURL}/${inboxId}/messages/${messageId}/body.htmlsource`; - const apiResponse = await this.client.get(url); - return apiResponse.data; + return this.client.get(url); } /** @@ -138,9 +128,8 @@ export default class MessagesApi { */ public async getHtmlMessage(inboxId: number, messageId: number) { const url = `${this.messagesURL}/${inboxId}/messages/${messageId}/body.html`; - const apiResponse = await this.client.get(url); - return apiResponse.data; + return this.client.get(url); } /** @@ -148,9 +137,8 @@ export default class MessagesApi { */ public async getMessageAsEml(inboxId: number, messageId: number) { const url = `${this.messagesURL}/${inboxId}/messages/${messageId}/body.eml`; - const apiResponse = await this.client.get(url); - return apiResponse.data; + return this.client.get(url); } /** @@ -158,8 +146,7 @@ export default class MessagesApi { */ public async getMailHeaders(inboxId: number, messageId: number) { const url = `${this.messagesURL}/${inboxId}/messages/${messageId}/mail_headers`; - const apiResponse = await this.client.get(url); - return apiResponse.data; + return this.client.get(url); } } From 78e5191c3eb4549de4af2d6111e023f82cb764d7 Mon Sep 17 00:00:00 2001 From: Narek Hovhannisyan Date: Fri, 10 Nov 2023 14:58:21 +0400 Subject: [PATCH 10/12] resources: tune projects to return data directly. --- src/lib/api/resources/Projects.ts | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/lib/api/resources/Projects.ts b/src/lib/api/resources/Projects.ts index cc4c204..e3194c7 100644 --- a/src/lib/api/resources/Projects.ts +++ b/src/lib/api/resources/Projects.ts @@ -25,18 +25,15 @@ export default class ProjectsApi { */ public async create(projectName: string) { const data = { project: { name: projectName } }; - const apiResponse = await this.client.post(this.projectsURL, data); - return apiResponse.data; + return this.client.post(this.projectsURL, data); } /** * Lists projects and their inboxes to which the API token has access. */ public async getList() { - const apiResponse = await this.client.get(this.projectsURL); - - return apiResponse.data; + return this.client.get(this.projectsURL); } /** @@ -44,9 +41,8 @@ export default class ProjectsApi { */ public async getById(projectId: number) { const url = `${this.projectsURL}/${projectId}`; - const apiResponse = await this.client.get(url); - return apiResponse.data; + return this.client.get(url); } /** @@ -55,9 +51,8 @@ export default class ProjectsApi { public async update(projectId: number, updatedName: string) { const url = `${this.projectsURL}/${projectId}`; const data = { project: { name: updatedName } }; - const apiResponse = await this.client.patch(url, data); - return apiResponse.data; + return this.client.patch(url, data); } /** @@ -65,8 +60,7 @@ export default class ProjectsApi { */ public async delete(projectId: number) { const url = `${this.projectsURL}/${projectId}`; - const apiResponse = await this.client.delete(url); - return apiResponse.data; + return this.client.delete(url); } } From 59b8fd0561805317fd5a598e0ea7185bf83ca9f3 Mon Sep 17 00:00:00 2001 From: Narek Hovhannisyan Date: Fri, 10 Nov 2023 15:06:16 +0400 Subject: [PATCH 11/12] api: tune Testing to return data directly. --- src/lib/api/Testing.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/lib/api/Testing.ts b/src/lib/api/Testing.ts index 7553625..b9f9dfb 100644 --- a/src/lib/api/Testing.ts +++ b/src/lib/api/Testing.ts @@ -45,12 +45,10 @@ export default class TestingAPI { const preparedMail = encodeMailBuffers(mail); try { - const axiosResponse = await this.client.post( + return await this.client.post( url, preparedMail ); - - return axiosResponse.data; } catch (error) { if (axios.isAxiosError(error)) { handleSendingError(error); From d044b562d4b33024a2e20bb24ec8755d839131ad Mon Sep 17 00:00:00 2001 From: Narek Hovhannisyan Date: Fri, 10 Nov 2023 15:06:26 +0400 Subject: [PATCH 12/12] lib: tune mailtrap-client to return data directly. --- src/lib/mailtrap-client.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/lib/mailtrap-client.ts b/src/lib/mailtrap-client.ts index 75975c3..210b4b5 100644 --- a/src/lib/mailtrap-client.ts +++ b/src/lib/mailtrap-client.ts @@ -89,12 +89,10 @@ export default class MailtrapClient { const preparedMail = encodeMailBuffers(mail); try { - const axiosResponse = await this.axios.post( + return await this.axios.post( url, preparedMail ); - - return axiosResponse.data; } catch (error) { if (axios.isAxiosError(error)) { handleSendingError(error);