-
Notifications
You must be signed in to change notification settings - Fork 16
Contacts api #64
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
Contacts api #64
Changes from 17 commits
7431edd
55aaa1e
0ae5397
ebc3855
497f1c5
6598f3a
8b86d36
2e5af4d
c7a75f3
62032eb
71c477f
81d6ef9
5ca3119
f992507
4a7d388
63f4b79
91b0cde
9f0a69a
e971cc8
7f75f82
47b77d4
25619ca
38ee629
ac9323b
398c486
891fff6
535b25b
72a7bd4
ab67329
c0d6116
608fb4f
df7e902
e972d48
b168aa7
32f5cbd
317e038
d0a2a1e
de5cb1c
fb3d521
54365d2
e58c703
ad40640
4344a16
7479575
1dedd74
a155271
36dc156
fb67805
1bf1854
5c3e4ca
d697894
44aa5e2
18eef15
3d18a03
e2eac12
8d01d01
2c9e1c4
86003d2
5a4c562
ca246cd
970fe76
3532edb
a07ebbf
272964f
26f43e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { MailtrapClient } from "mailtrap"; | ||
|
|
||
| const TOKEN = "<YOUR-TOKEN-HERE>"; | ||
| const ACCOUNT_ID = "<YOUR-ACCOUNT-ID-HERE>" | ||
|
|
||
| const client = new MailtrapClient({ | ||
| token: TOKEN, | ||
| accountId: ACCOUNT_ID | ||
| }); | ||
|
|
||
| // List all contact lists | ||
| client.contactLists | ||
| .list() | ||
| .then(response => { | ||
| console.log("Contact lists:", response.data); | ||
| }) | ||
| .catch(error => { | ||
| console.error("Error fetching contact lists:", error); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { MailtrapClient } from "mailtrap"; | ||
|
|
||
| const TOKEN = "<YOUR-TOKEN-HERE>"; | ||
| const ACCOUNT_ID = "<YOUR-ACCOUNT-ID-HERE>" | ||
|
|
||
| const client = new MailtrapClient({ | ||
| token: TOKEN, | ||
| accountId: ACCOUNT_ID | ||
| }); | ||
|
|
||
| const contactData = { | ||
| email: "[email protected]", | ||
| fields: { | ||
| first_name: "John", | ||
| last_name: "Smith" | ||
| }, | ||
| list_ids: [1, 2, 3] | ||
| }; | ||
|
|
||
| // Create contact | ||
| client.contacts | ||
| .create(contactData) | ||
| .then(async (response) => { | ||
| console.log("Contact created:", response.data); | ||
| const contactId = response.data.id; | ||
|
|
||
| // Update contact | ||
| const updateResponse = await client.contacts | ||
| .update(contactId, { | ||
| fields: { | ||
| first_name: "Johnny", | ||
| last_name: "Smith", | ||
| company: "New Corp" | ||
| } | ||
| }) | ||
| console.log("Contact updated:", updateResponse.data); | ||
| // Delete contact | ||
| await client.general.contacts | ||
| .delete(contactId) | ||
| console.log("Contact deleted"); | ||
narekhovhannisyan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }) | ||
| .catch(error => { | ||
| console.error("Error in contact lifecycle:", error); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,8 @@ import TestingAPI from "../../lib/api/Testing"; | |
| import GeneralAPI from "../../lib/api/General"; | ||
|
|
||
| const { ERRORS, CLIENT_SETTINGS } = CONFIG; | ||
| const { TESTING_ENDPOINT, BULK_ENDPOINT, SENDING_ENDPOINT } = CLIENT_SETTINGS; | ||
| const { TESTING_ENDPOINT, BULK_ENDPOINT, SENDING_ENDPOINT, GENERAL_ENDPOINT } = | ||
| CLIENT_SETTINGS; | ||
| const { TEST_INBOX_ID_MISSING, ACCOUNT_ID_MISSING, BULK_SANDBOX_INCOMPATIBLE } = | ||
| ERRORS; | ||
|
|
||
|
|
@@ -729,4 +730,143 @@ describe("lib/mailtrap-client: ", () => { | |
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("contacts API:", () => { | ||
| it("successfully creates a contact", async () => { | ||
| const client = new MailtrapClient({ | ||
| token: "MY_API_TOKEN", | ||
| accountId: 123, | ||
| }); | ||
| const endpoint = `${GENERAL_ENDPOINT}/api/accounts/123/contacts`; | ||
| const expectedResponseData = { | ||
| id: 1, | ||
| email: "[email protected]", | ||
| fields: { first_name: "John", last_name: "Smith" }, | ||
| }; | ||
| mock.onPost(endpoint).reply(200, expectedResponseData); | ||
|
|
||
| const contactData = { | ||
| email: "[email protected]", | ||
| fields: { first_name: "John", last_name: "Smith" }, | ||
| }; | ||
|
|
||
| const result = await client.contacts.create(contactData); | ||
|
|
||
| expect(mock.history.post[0].url).toEqual(endpoint); | ||
| expect(mock.history.post[0].data).toEqual( | ||
| JSON.stringify({ contact: contactData }) | ||
| ); | ||
| expect(result).toEqual(expectedResponseData); | ||
| }); | ||
|
|
||
| it("successfully updates a contact", async () => { | ||
| const client = new MailtrapClient({ | ||
| token: "MY_API_TOKEN", | ||
| accountId: 123, | ||
| }); | ||
| const contactId = 1; | ||
| const endpoint = `${GENERAL_ENDPOINT}/api/accounts/123/contacts/${contactId}`; | ||
| const expectedResponseData = { | ||
| id: contactId, | ||
| email: "[email protected]", | ||
| fields: { first_name: "Johnny", last_name: "Smith" }, | ||
| }; | ||
| mock.onPatch(endpoint).reply(200, expectedResponseData); | ||
|
|
||
| const updateData = { | ||
| email: "[email protected]", | ||
| fields: { first_name: "Johnny", last_name: "Smith" }, | ||
| }; | ||
|
|
||
| const result = await client.contacts.update(contactId, updateData); | ||
|
|
||
| expect(mock.history.patch[0].url).toEqual(endpoint); | ||
| expect(mock.history.patch[0].data).toEqual( | ||
| JSON.stringify({ contact: updateData }) | ||
| ); | ||
| expect(result).toEqual(expectedResponseData); | ||
| }); | ||
|
|
||
| it("successfully deletes a contact", async () => { | ||
| const client = new MailtrapClient({ | ||
| token: "MY_API_TOKEN", | ||
| accountId: 123, | ||
| }); | ||
| const contactId = 1; | ||
| const endpoint = `${GENERAL_ENDPOINT}/api/accounts/123/contacts/${contactId}`; | ||
| const expectedResponseData = { success: true }; | ||
| mock.onDelete(endpoint).reply(200, expectedResponseData); | ||
|
|
||
| const result = await client.contacts.delete(contactId); | ||
|
|
||
| expect(mock.history.delete[0].url).toEqual(endpoint); | ||
| expect(result).toEqual(expectedResponseData); | ||
| }); | ||
|
|
||
| it("handles API errors for contacts", async () => { | ||
| const client = new MailtrapClient({ | ||
| token: "MY_API_TOKEN", | ||
| accountId: 123, | ||
| }); | ||
| const endpoint = `${GENERAL_ENDPOINT}/api/accounts/123/contacts`; | ||
| const responseData = { | ||
| success: false, | ||
| errors: ["email is required"], | ||
| }; | ||
| mock.onPost(endpoint).reply(400, responseData); | ||
|
|
||
| try { | ||
| await client.contacts.create({} as any); | ||
| } catch (error) { | ||
| expect(error).toBeInstanceOf(MailtrapError); | ||
| if (error instanceof MailtrapError) { | ||
| expect(error.message).toEqual("email is required"); | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe("contact lists API:", () => { | ||
| it("successfully lists contact lists", async () => { | ||
| const client = new MailtrapClient({ | ||
| token: "MY_API_TOKEN", | ||
| accountId: 123, | ||
| }); | ||
| const endpoint = `${GENERAL_ENDPOINT}/api/accounts/123/contacts/lists`; | ||
| const expectedResponseData = { | ||
| lists: [ | ||
| { id: 1, name: "Marketing" }, | ||
| { id: 2, name: "Sales" }, | ||
| ], | ||
| }; | ||
| mock.onGet(endpoint).reply(200, expectedResponseData); | ||
|
|
||
| const result = await client.contactLists.list(); | ||
|
|
||
| expect(mock.history.get[0].url).toEqual(endpoint); | ||
| expect(result).toEqual(expectedResponseData); | ||
| }); | ||
|
|
||
| it("handles API errors for contact lists", async () => { | ||
| const client = new MailtrapClient({ | ||
| token: "MY_API_TOKEN", | ||
| accountId: 123, | ||
| }); | ||
| const endpoint = `${GENERAL_ENDPOINT}/api/accounts/123/contacts/lists`; | ||
| const responseData = { | ||
| success: false, | ||
| errors: ["account not found"], | ||
| }; | ||
| mock.onGet(endpoint).reply(404, responseData); | ||
|
|
||
| try { | ||
| await client.contactLists.list(); | ||
| } catch (error) { | ||
| expect(error).toBeInstanceOf(MailtrapError); | ||
| if (error instanceof MailtrapError) { | ||
| expect(error.message).toEqual("account not found"); | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { AxiosInstance } from "axios"; | ||
|
|
||
| import ContactsApi from "./resources/contacts/Contacts"; | ||
| import ContactListsApi from "./resources/contacts/ContactLists"; | ||
|
|
||
| export default class ContactsAPI { | ||
| private client: AxiosInstance; | ||
|
|
||
| private accountId?: number; | ||
|
|
||
| public contacts: ContactsApi; | ||
|
|
||
| public contactLists: ContactListsApi; | ||
|
|
||
| constructor(client: AxiosInstance, accountId?: number) { | ||
| this.client = client; | ||
| this.accountId = accountId; | ||
| this.contacts = new ContactsApi(this.client, this.accountId); | ||
| this.contactLists = new ContactListsApi(this.client); | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { AxiosInstance } from "axios"; | ||
|
|
||
| import CONFIG from "../../../../config"; | ||
|
|
||
| const { CLIENT_SETTINGS } = CONFIG; | ||
| const { GENERAL_ENDPOINT } = CLIENT_SETTINGS; | ||
|
|
||
| export default class ContactListsApi { | ||
| private client: AxiosInstance; | ||
|
|
||
| private contactListsURL: string; | ||
|
|
||
| constructor(client: AxiosInstance, accountId?: number) { | ||
| this.client = client; | ||
| this.contactListsURL = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/contacts/lists`; | ||
| } | ||
|
|
||
| /** | ||
| * Lists all contact lists for the account. | ||
| */ | ||
| public async list() { | ||
| return this.client.get(this.contactListsURL); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { AxiosInstance } from "axios"; | ||
| import { ContactData } from "../../../../types/mailtrap"; | ||
| import CONFIG from "../../../../config"; | ||
|
|
||
| const { CLIENT_SETTINGS } = CONFIG; | ||
| const { GENERAL_ENDPOINT } = CLIENT_SETTINGS; | ||
|
|
||
| export default class ContactsApi { | ||
| private client: AxiosInstance; | ||
|
|
||
| private contactsURL: string; | ||
|
|
||
| constructor(client: AxiosInstance, accountId?: number) { | ||
| this.client = client; | ||
| this.contactsURL = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/contacts`; | ||
| } | ||
narekhovhannisyan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Creates a new contact. | ||
| */ | ||
| public async create(data: ContactData) { | ||
| return this.client.post(this.contactsURL, { contact: data }); | ||
| } | ||
|
|
||
| /** | ||
| * Updates an existing contact. | ||
| */ | ||
| public async update(id: number, data: ContactData) { | ||
| const url = `${this.contactsURL}/${id}`; | ||
| return this.client.patch(url, { contact: data }); | ||
| } | ||
|
|
||
| /** | ||
| * Deletes a contact. | ||
| */ | ||
| public async delete(id: number) { | ||
| const url = `${this.contactsURL}/${id}`; | ||
| return this.client.delete(url); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -111,3 +111,15 @@ export interface BatchSendRequest { | |
| custom_variables?: Record<string, string>; | ||
| }[]; | ||
| } | ||
|
|
||
| export interface ContactFields { | ||
| first_name?: string; | ||
| last_name?: string; | ||
|
||
| [key: string]: string | undefined; | ||
|
||
| } | ||
|
|
||
| export interface ContactData { | ||
| email: string; | ||
| fields?: ContactFields; | ||
| list_ids?: number[]; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.