Repository navigation
Add inbound forward rules, forward outcomes, thread delivery and search #163
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,50 @@ | ||
| import { MailtrapClient } from "mailtrap"; | ||
|
|
||
| const TOKEN = "<YOUR-TOKEN-HERE>"; | ||
| const INBOX_ID = Number("<YOUR-INBOX-ID-HERE>"); | ||
|
|
||
| const client = new MailtrapClient({ token: TOKEN }); | ||
| const forwardRulesClient = client.inbound.forwardRules; | ||
|
|
||
| async function forwardRulesFlow() { | ||
| try { | ||
| const created = await forwardRulesClient.create(INBOX_ID, { | ||
| name: "Copy billing mail to finance", | ||
| conditions: [ | ||
| { | ||
| match_type: "sender", | ||
| operator: "ends_with", | ||
| value: "@billing.example.com", | ||
| }, | ||
| ], | ||
| destinations: [{ email: "finance@example.com" }], | ||
| }); | ||
| console.log("Created forward rule:", created.data); | ||
|
|
||
| const ruleId = created.data.id; | ||
|
|
||
| console.log( | ||
| "All forward rules:", | ||
| await forwardRulesClient.getList(INBOX_ID) | ||
| ); | ||
| console.log( | ||
| "One forward rule:", | ||
| await forwardRulesClient.get(INBOX_ID, ruleId) | ||
| ); | ||
|
|
||
| const updated = await forwardRulesClient.update(INBOX_ID, ruleId, { | ||
| destinations: [ | ||
| { email: "finance@example.com" }, | ||
| { email: "accounting@example.com" }, | ||
| ], | ||
| }); | ||
| console.log("Updated forward rule:", updated.data); | ||
|
|
||
| await forwardRulesClient.delete(INBOX_ID, ruleId); | ||
|
mklocek marked this conversation as resolved.
|
||
| console.log("Deleted forward rule", ruleId); | ||
| } catch (error) { | ||
| console.error(error instanceof Error ? error.message : String(error)); | ||
| } | ||
| } | ||
|
|
||
| forwardRulesFlow(); | ||
This file contains hidden or 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 hidden or 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 hidden or 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
205 changes: 205 additions & 0 deletions
205
src/__tests__/lib/api/resources/inbound/ForwardRules.test.ts
This file contains hidden or 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,205 @@ | ||
| import axios, { AxiosInstance } from "axios"; | ||
| import MockAdapter from "axios-mock-adapter"; | ||
|
|
||
| import ForwardRulesApi from "../../../../../lib/api/resources/inbound/ForwardRules"; | ||
| import handleSendingError from "../../../../../lib/axios-logger"; | ||
| import MailtrapError from "../../../../../lib/MailtrapError"; | ||
| import { | ||
| CreateForwardRuleParams, | ||
| ForwardRule, | ||
| ForwardRuleResponse, | ||
| ForwardRulesListResponse, | ||
| UpdateForwardRuleParams, | ||
| } from "../../../../../types/api/inbound/forward-rules"; | ||
|
|
||
| describe("lib/api/resources/inbound/ForwardRules: ", () => { | ||
| const axiosInstance: AxiosInstance = axios.create(); | ||
| const mock = new MockAdapter(axiosInstance); | ||
| axiosInstance.interceptors.response.use( | ||
| (response) => response.data, | ||
| handleSendingError | ||
| ); | ||
|
|
||
| const forwardRulesAPI = new ForwardRulesApi(axiosInstance); | ||
| const forwardRulesURL = | ||
| "https://mailtrap.io/api/inbound/inboxes/42/forward_rules"; | ||
|
|
||
| const rule: ForwardRule = { | ||
| id: 7, | ||
| name: "Copy billing mail to finance", | ||
| created_at: "2026-05-08T10:30:00.000Z", | ||
| updated_at: "2026-05-08T10:30:00.000Z", | ||
| conditions: [ | ||
| { | ||
| match_type: "sender", | ||
| operator: "ends_with", | ||
| value: "@billing.example.com", | ||
| header_key: null, | ||
| }, | ||
| ], | ||
| destinations: [{ email: "finance@example.com" }], | ||
| }; | ||
| const ruleResponse: ForwardRuleResponse = { data: rule }; | ||
|
|
||
| const expectMailtrapError = async (request: () => Promise<unknown>) => { | ||
| expect.assertions(2); | ||
|
|
||
| try { | ||
| await request(); | ||
| } catch (error) { | ||
| expect(error).toBeInstanceOf(MailtrapError); | ||
|
|
||
| if (error instanceof MailtrapError) { | ||
| expect(error.message).toEqual("Not Found"); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| afterEach(() => mock.reset()); | ||
|
|
||
| describe("getList(): ", () => { | ||
| it("lists forward rules in an inbox.", async () => { | ||
| const listResponse: ForwardRulesListResponse = { | ||
| data: [ | ||
| rule, | ||
| { | ||
| id: 8, | ||
| name: "Forward everything to archive", | ||
| created_at: "2026-05-08T11:00:00.000Z", | ||
| updated_at: "2026-05-08T11:00:00.000Z", | ||
| conditions: [], | ||
| destinations: [{ email: "archive@example.com" }], | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| mock.onGet(forwardRulesURL).reply(200, listResponse); | ||
|
|
||
| const result = await forwardRulesAPI.getList(42); | ||
|
|
||
| expect(result).toEqual(listResponse); | ||
| }); | ||
|
|
||
| it("fails with error.", async () => { | ||
| mock.onGet(forwardRulesURL).reply(404, { error: "Not Found" }); | ||
|
|
||
| await expectMailtrapError(() => forwardRulesAPI.getList(42)); | ||
| }); | ||
| }); | ||
|
|
||
| describe("get(): ", () => { | ||
| it("returns a single forward rule.", async () => { | ||
| const headerRule: ForwardRuleResponse = { | ||
| data: { | ||
| ...rule, | ||
| id: 9, | ||
| conditions: [ | ||
| { | ||
| match_type: "header", | ||
| operator: "equal", | ||
| value: "high", | ||
| header_key: "X-Priority-Level", | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
|
|
||
| mock.onGet(`${forwardRulesURL}/9`).reply(200, headerRule); | ||
|
|
||
| const result = await forwardRulesAPI.get(42, 9); | ||
|
|
||
| expect(result).toEqual(headerRule); | ||
| }); | ||
|
|
||
| it("fails with error.", async () => { | ||
| mock.onGet(`${forwardRulesURL}/9`).reply(404, { error: "Not Found" }); | ||
|
|
||
| await expectMailtrapError(() => forwardRulesAPI.get(42, 9)); | ||
| }); | ||
| }); | ||
|
|
||
| describe("create(): ", () => { | ||
| const params: CreateForwardRuleParams = { | ||
| name: "Copy billing mail to finance", | ||
| conditions: [ | ||
| { | ||
| match_type: "sender", | ||
| operator: "ends_with", | ||
| value: "@billing.example.com", | ||
| }, | ||
| ], | ||
| destinations: [{ email: "finance@example.com" }], | ||
| }; | ||
|
|
||
| it("creates a forward rule with an unwrapped body.", async () => { | ||
| mock.onPost(forwardRulesURL).reply(201, ruleResponse); | ||
|
|
||
| const result = await forwardRulesAPI.create(42, params); | ||
|
|
||
| expect(JSON.parse(mock.history.post[0].data)).toEqual(params); | ||
| expect(result).toEqual(ruleResponse); | ||
| }); | ||
|
|
||
| it("fails with error.", async () => { | ||
| mock.onPost(forwardRulesURL).reply(404, { error: "Not Found" }); | ||
|
|
||
| await expectMailtrapError(() => forwardRulesAPI.create(42, params)); | ||
| }); | ||
| }); | ||
|
|
||
| describe("update(): ", () => { | ||
| it("sends only the fields provided.", async () => { | ||
| const params: UpdateForwardRuleParams = { | ||
| destinations: [ | ||
| { email: "finance@example.com" }, | ||
| { email: "accounting@example.com" }, | ||
| ], | ||
| }; | ||
|
|
||
| mock.onPatch(`${forwardRulesURL}/7`).reply(200, ruleResponse); | ||
|
|
||
| const result = await forwardRulesAPI.update(42, 7, params); | ||
|
|
||
| expect(JSON.parse(mock.history.patch[0].data)).toEqual(params); | ||
| expect(result).toEqual(ruleResponse); | ||
| }); | ||
|
|
||
| it("sends an empty array to clear conditions.", async () => { | ||
| const params: UpdateForwardRuleParams = { conditions: [] }; | ||
|
|
||
| mock.onPatch(`${forwardRulesURL}/7`).reply(200, { | ||
| data: { ...rule, conditions: [] }, | ||
| }); | ||
|
|
||
| await forwardRulesAPI.update(42, 7, params); | ||
|
|
||
| expect(JSON.parse(mock.history.patch[0].data)).toEqual({ | ||
| conditions: [], | ||
| }); | ||
| }); | ||
|
|
||
| it("fails with error.", async () => { | ||
| mock.onPatch(`${forwardRulesURL}/7`).reply(404, { error: "Not Found" }); | ||
|
|
||
| await expectMailtrapError(() => | ||
| forwardRulesAPI.update(42, 7, { name: "Renamed" }) | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("delete(): ", () => { | ||
| it("deletes a forward rule.", async () => { | ||
| mock.onDelete(`${forwardRulesURL}/7`).reply(204); | ||
|
|
||
| const result = await forwardRulesAPI.delete(42, 7); | ||
|
|
||
| expect(result).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("fails with error.", async () => { | ||
| mock.onDelete(`${forwardRulesURL}/7`).reply(404, { error: "Not Found" }); | ||
|
|
||
| await expectMailtrapError(() => forwardRulesAPI.delete(42, 7)); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would be better to read from environment... as a general practice across all OSS examples. I raised it elsewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be fixed separately. 43 or 44 examples in this repo use this pattern.