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

Attachments and response interceptors #25

Merged
merged 12 commits into from
Nov 13, 2023
Merged
33 changes: 33 additions & 0 deletions examples/testing/attachments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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
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

const attachments = await attachmentsClient.getList(firstMessageId, firstInboxId)

if (attachments && attachments.length > 0) {
const firstAttachment = attachments[0].id
const attachmentData = await attachmentsClient.get(firstInboxId, firstMessageId, firstAttachment)

console.log(attachmentData)
}
}
}
})
9 changes: 6 additions & 3 deletions src/lib/api/Testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -26,26 +28,27 @@ export default class TestingAPI {

public messages: MessagesApi;

public attachments: AttachmentsApi;

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);
this.attachments = new AttachmentsApi(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>(
return await this.client.post<SendResponse, SendResponse>(
url,
preparedMail
);

return axiosResponse.data;
} catch (error) {
if (axios.isAxiosError(error)) {
handleSendingError(error);
Expand Down
40 changes: 40 additions & 0 deletions src/lib/api/resources/Attachments.ts
Original file line number Diff line number Diff line change
@@ -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(inboxId: number, messageId: number) {
const url = `${this.inboxesURL}/${inboxId}/messages/${messageId}/attachments`;

return this.client.get<Attachment[], Attachment[]>(url);
}

/**
* Get message attachments by `inboxId`.`messageId` and `attachmentId`.
*/
public async get(inboxId: number, messageId: number, attachmentId: number) {
const url = `${this.inboxesURL}/${inboxId}/messages/${messageId}/attachments/${attachmentId}`;

return this.client.get<Attachment, Attachment>(url);
}
}
32 changes: 10 additions & 22 deletions src/lib/api/resources/Inboxes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,25 @@ 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<Inbox>(url, data);

return apiResponse.data;
return this.client.post<Inbox, Inbox>(url, 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;
return this.client.get<Inbox, Inbox>(url);
}

/**
* 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;
return this.client.delete<Inbox, Inbox>(url);
}

/**
Expand All @@ -62,67 +58,59 @@ export default class InboxesApi {
email_username: params.emailUsername,
},
};
const apiRespone = await this.client.patch<Inbox>(url, data);

return apiRespone.data;
return this.client.patch<Inbox, Inbox>(url, 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;
return this.client.patch<Inbox, Inbox>(url);
}

/**
* 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;
return this.client.patch<Inbox, Inbox>(url);
}

/**
* 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;
return this.client.patch<Inbox, Inbox>(url);
}

/**
* 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;
return this.client.patch<Inbox, Inbox>(url);
}

/**
* 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;
return this.client.patch<Inbox, Inbox>(url);
}

/**
* Gets a list of inboxes.
*/
public async getList() {
const apiRespone = await this.client.get<Inbox[]>(this.inboxesURL);

return apiRespone.data;
return this.client.get<Inbox[], Inbox[]>(this.inboxesURL);
}
}
39 changes: 13 additions & 26 deletions src/lib/api/resources/Messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Message>(url);

return apiResponse.data;
return this.client.get<Message, Message>(url);
}

/**
Expand All @@ -43,29 +42,26 @@ export default class MessagesApi {
) {
const url = `${this.messagesURL}/${inboxId}/messages/${messageId}`;
const data = { message: { is_read: params.isRead } };
const apiResponse = await this.client.patch<Message>(url, data);

return apiResponse.data;
return this.client.patch<Message, Message>(url, data);
}

/**
* Deletes message from inbox.
*/
public async deleteMessage(inboxId: number, messageId: number) {
const url = `${this.messagesURL}/${inboxId}/messages/${messageId}`;
const apiResponse = await this.client.delete<Message>(url);

return apiResponse.data;
return this.client.delete<Message, Message>(url);
}

/**
* Gets all messages in inboxes.
*/
public async get(inboxId: number) {
const url = `${this.messagesURL}/${inboxId}/messages`;
const apiResponse = await this.client.get<Message[]>(url);

return apiResponse.data;
return this.client.get<Message[], Message[]>(url);
}

/**
Expand All @@ -78,88 +74,79 @@ export default class MessagesApi {
) {
const url = `${this.messagesURL}/${inboxId}/messages/${messageId}/forward`;
const data = { email: emailToForward };
const apiResponse = await this.client.post<Message>(url, data);

return apiResponse.data;
return this.client.post<Message, Message>(url, data);
}

/**
* Gets a brief spam report by given `messageId`.
*/
public async getSpamScore(inboxId: number, messageId: number) {
const url = `${this.messagesURL}/${inboxId}/messages/${messageId}/spam_report`;
const apiResponse = await this.client.get<SpamReport>(url);

return apiResponse.data;
return this.client.get<SpamReport, SpamReport>(url);
}

/**
* Gets a brief HTML report by message ID.
*/
public async getHtmlAnalysis(inboxId: number, messageId: number) {
const url = `${this.messagesURL}/${inboxId}/messages/${messageId}/analyze`;
const apiResponse = await this.client.get<Report>(url);

return apiResponse.data;
return this.client.get<Report, Report>(url);
}

/**
* Gets text email body, if it exists.
*/
public async getTextMessage(inboxId: number, messageId: number) {
const url = `${this.messagesURL}/${inboxId}/messages/${messageId}/body.txt`;
const apiResponse = await this.client.get<string>(url);

return apiResponse.data;
return this.client.get<string, string>(url);
}

/**
* Gets raw email body.
*/
public async getRawMessage(inboxId: number, messageId: number) {
const url = `${this.messagesURL}/${inboxId}/messages/${messageId}/body.raw`;
const apiResponse = await this.client.get<string>(url);

return apiResponse.data;
return this.client.get<string, string>(url);
}

/**
* Gets HTML source of email.
*/
public async getMessageHtmlSource(inboxId: number, messageId: number) {
const url = `${this.messagesURL}/${inboxId}/messages/${messageId}/body.htmlsource`;
const apiResponse = await this.client.get<string>(url);

return apiResponse.data;
return this.client.get<string, string>(url);
}

/**
* Gets formatted HTML email body. Not applicable for plain text emails.
*/
public async getHtmlMessage(inboxId: number, messageId: number) {
const url = `${this.messagesURL}/${inboxId}/messages/${messageId}/body.html`;
const apiResponse = await this.client.get<string>(url);

return apiResponse.data;
return this.client.get<string, string>(url);
}

/**
* Gets email message in .eml format.
*/
public async getMessageAsEml(inboxId: number, messageId: number) {
const url = `${this.messagesURL}/${inboxId}/messages/${messageId}/body.eml`;
const apiResponse = await this.client.get<string>(url);

return apiResponse.data;
return this.client.get<string>(url);
}

/**
* Gets mail headers of a message.
*/
public async getMailHeaders(inboxId: number, messageId: number) {
const url = `${this.messagesURL}/${inboxId}/messages/${messageId}/mail_headers`;
const apiResponse = await this.client.get<EmailHeaders>(url);

return apiResponse.data;
return this.client.get<EmailHeaders, EmailHeaders>(url);
}
}
Loading
Loading