Skip to content

Template api integration #67

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open

Conversation

narekhovhannisyan
Copy link
Collaborator

@narekhovhannisyan narekhovhannisyan commented Jun 21, 2025

Motivation

The Mailtrap API supports email templates functionality, but the Node.js client was missing this capability. Users need to be able to create, read, update, and delete email templates programmatically through the client.

Changes

  • Added Templates API implementation with full CRUD operations
  • Created TemplatesApi class in src/lib/api/resources/Templates.ts with methods: getList(), get(), create(), update(), delete()
  • Added TemplatesBaseAPI class in src/lib/api/Templates.ts to integrate with the main client
  • Added TypeScript types in src/types/api/templates.ts for Template, TemplateCreateParams, and TemplateUpdateParams
  • Integrated templates API into MailtrapClient class with proper error handling for missing accountId
  • Added comprehensive test coverage including unit tests for the API class and integration tests for the client
  • Created example usage in examples/templates/everything.ts demonstrating all template operations
  • Updated README.md to include Templates API documentation and example reference

How to test

  • Run npm test to ensure all new tests pass
  • Test template creation: node examples/templates/everything.ts (with valid token and account ID)
  • Verify template CRUD operations work correctly (create, read, update, delete)
  • Test error handling when accountId is missing from client configuration
  • Verify TypeScript compilation succeeds with new types
  • Check that README.md renders correctly with the new Templates API section

Summary by CodeRabbit

  • New Features

    • Introduced support for managing email templates, including creating, retrieving, updating, and deleting templates via the API.
    • Added new API methods and interfaces for template operations.
    • Provided example scripts demonstrating template management capabilities.
  • Documentation

    • Updated and reorganized the README for improved clarity, including renaming example links and adding a new section for Templates API.
  • Tests

    • Added comprehensive tests for template-related API functionality, ensuring correct behavior and error handling.

Copy link

coderabbitai bot commented Jun 21, 2025

Walkthrough

A new Templates API feature is introduced, enabling CRUD operations for email templates. This includes new API classes, TypeScript types, documentation updates, example scripts, and comprehensive test suites. The MailtrapClient now exposes a templates getter, providing access to template management functionality.

Changes

File(s) Change Summary
README.md Updated documentation: clarified example descriptions, renamed links for CRUD emphasis, added Templates API section and example.
examples/templates/everything.ts Added example script demonstrating template CRUD operations using the new API.
src/types/api/templates.ts Introduced Template-related TypeScript interfaces and types for API contracts.
src/lib/api/resources/Templates.ts Added TemplatesApi class implementing CRUD methods for templates via HTTP requests.
src/lib/api/Templates.ts Added TemplatesBaseAPI class, delegating template API calls to TemplatesApi.
src/lib/MailtrapClient.ts Added templates getter to expose template management through TemplatesBaseAPI.
src/tests/lib/api/resources/Templates.test.ts Added tests for TemplatesApi covering all CRUD operations and error handling.
src/tests/lib/api/Templates.test.ts Added test verifying TemplatesBaseAPI initialization and presence of key methods.
src/tests/lib/mailtrap-client.test.ts Extended tests for MailtrapClient to cover the new templates getter and error conditions.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant MailtrapClient
    participant TemplatesBaseAPI
    participant TemplatesApi
    participant MailtrapAPI

    User->>MailtrapClient: Access templates getter
    MailtrapClient->>TemplatesBaseAPI: Instantiate (with axios, accountId)
    User->>TemplatesBaseAPI: Call CRUD method (create/getList/get/update/delete)
    TemplatesBaseAPI->>TemplatesApi: Delegate method call
    TemplatesApi->>MailtrapAPI: Perform HTTP request
    MailtrapAPI-->>TemplatesApi: Return API response
    TemplatesApi-->>TemplatesBaseAPI: Return result
    TemplatesBaseAPI-->>User: Return result
Loading

Suggested reviewers

  • vittorius
  • VladimirTaytor
  • mklocek

Poem

A bunny hopped into the code,
With templates new, a CRUD-y load!
Now emails dress in style and flair,
Create, update, delete with care.
Docs are spruced, the tests are bright—
Template magic, day and night! 🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8b7e2c2 and 3ebee7e.

📒 Files selected for processing (1)
  • examples/templates/everything.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • examples/templates/everything.ts
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
README.md (1)

64-99: Fix markdown list indentation.

The static analysis tool correctly identifies indentation issues in the unordered lists that should be addressed for proper formatting.

Apply this diff to fix the markdown indentation:

-- [Contacts CRUD](examples/contacts/everything.ts)
+- [Contacts CRUD](examples/contacts/everything.ts)

-- [Contact Lists CRUD](examples/contact-lists/everything.ts)
+- [Contact Lists CRUD](examples/contact-lists/everything.ts)

-- [Templates CRUD](examples/templates/everything.ts)
+- [Templates CRUD](examples/templates/everything.ts)

-- [Transactional emails](examples/batch/transactional.ts)
-- [Bulk emails](examples/batch/bulk.ts)
-- [Sandbox emails](examples/batch/sandbox.ts)
-- [Emails from template](examples/batch/template.ts)
+- [Transactional emails](examples/batch/transactional.ts)
+- [Bulk emails](examples/batch/bulk.ts)
+- [Sandbox emails](examples/batch/sandbox.ts)
+- [Emails from template](examples/batch/template.ts)

-- [Transport](examples/bulk/transport.ts)
+- [Transport](examples/bulk/transport.ts)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between abaea8a and 8b7e2c2.

📒 Files selected for processing (9)
  • README.md (3 hunks)
  • examples/templates/everything.ts (1 hunks)
  • src/__tests__/lib/api/Templates.test.ts (1 hunks)
  • src/__tests__/lib/api/resources/Templates.test.ts (1 hunks)
  • src/__tests__/lib/mailtrap-client.test.ts (2 hunks)
  • src/lib/MailtrapClient.ts (2 hunks)
  • src/lib/api/Templates.ts (1 hunks)
  • src/lib/api/resources/Templates.ts (1 hunks)
  • src/types/api/templates.ts (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (5)
src/lib/MailtrapClient.ts (2)
src/lib/MailtrapError.ts (1)
  • MailtrapError (1-1)
src/lib/api/Templates.ts (1)
  • TemplatesBaseAPI (5-30)
src/__tests__/lib/mailtrap-client.test.ts (2)
src/lib/MailtrapError.ts (1)
  • MailtrapError (1-1)
src/lib/api/Templates.ts (1)
  • TemplatesBaseAPI (5-30)
src/lib/api/Templates.ts (2)
src/lib/api/resources/Templates.ts (1)
  • TemplatesApi (13-72)
src/lib/MailtrapClient.ts (1)
  • templates (134-140)
src/__tests__/lib/api/resources/Templates.test.ts (4)
src/lib/api/resources/Templates.ts (1)
  • TemplatesApi (13-72)
src/types/api/templates.ts (3)
  • TemplateCreateParams (13-19)
  • Template (1-11)
  • TemplateUpdateParams (21-27)
src/lib/axios-logger.ts (1)
  • handleSendingError (20-69)
src/lib/MailtrapError.ts (1)
  • MailtrapError (1-1)
src/lib/api/resources/Templates.ts (1)
src/types/api/templates.ts (3)
  • Template (1-11)
  • TemplateCreateParams (13-19)
  • TemplateUpdateParams (21-27)
🪛 markdownlint-cli2 (0.17.2)
README.md

64-64: Unordered list indentation
Expected: 0; Actual: 1

(MD007, ul-indent)


68-68: Unordered list indentation
Expected: 0; Actual: 1

(MD007, ul-indent)


72-72: Unordered list indentation
Expected: 0; Actual: 1

(MD007, ul-indent)


83-83: Unordered list indentation
Expected: 0; Actual: 1

(MD007, ul-indent)


84-84: Unordered list indentation
Expected: 0; Actual: 1

(MD007, ul-indent)


85-85: Unordered list indentation
Expected: 0; Actual: 1

(MD007, ul-indent)


86-86: Unordered list indentation
Expected: 0; Actual: 1

(MD007, ul-indent)


99-99: Unordered list indentation
Expected: 0; Actual: 1

(MD007, ul-indent)

🪛 LanguageTool
README.md

[duplication] ~85-~85: Possible typo: you repeated a word.
Context: ...ls](examples/batch/bulk.ts) - Sandbox emails - [Emails from template](examples/batch/template....

(ENGLISH_WORD_REPEAT_RULE)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (20)
src/lib/MailtrapClient.ts (2)

14-14: LGTM! Import follows existing pattern.

The import is correctly placed with other API imports and follows the established naming convention.


134-140: LGTM! Templates getter implementation is consistent.

The implementation follows the exact same pattern as other API getters with proper accountId validation and error handling.

src/__tests__/lib/mailtrap-client.test.ts (2)

14-14: LGTM! Import correctly added for testing.

The TemplatesBaseAPI import is properly placed with other API imports.


787-811: LGTM! Test coverage follows established patterns.

The test cases appropriately cover both error handling (missing accountId) and successful instantiation, maintaining consistency with other API getter tests.

src/__tests__/lib/api/Templates.test.ts (1)

1-21: LGTM! Basic initialization test is appropriate.

The test adequately verifies that the TemplatesBaseAPI wrapper class initializes with all required CRUD methods. This level of testing is appropriate for a delegation wrapper.

examples/templates/everything.ts (1)

1-44: LGTM! Comprehensive example demonstrates all CRUD operations.

The example provides excellent coverage of the Templates API functionality with clear console logging for each operation.

README.md (1)

70-72: LGTM! Templates API documentation properly added.

The new Templates API section follows the established pattern and correctly references the example file.

src/lib/api/Templates.ts (2)

5-19: Clean delegation pattern with proper type safety.

The use of TypeScript's mapped types for property declarations ensures type safety while maintaining a clean interface. The delegation pattern is well-implemented.


20-29: Method binding approach is correct and necessary.

The explicit binding of methods in the constructor ensures proper this context when the methods are called from the delegating instance. This is the correct approach for this delegation pattern.

src/types/api/templates.ts (2)

1-11: Well-structured Template interface.

The interface comprehensively covers all template fields with appropriate types. The optional body_text field is correctly marked as such.


13-27: Proper separation of create and update parameter interfaces.

The design correctly separates creation parameters (required fields) from update parameters (optional fields for partial updates), following REST API best practices.

src/__tests__/lib/api/resources/Templates.test.ts (3)

18-73: Comprehensive test setup with realistic mock data.

The test suite provides excellent coverage with realistic mock data that properly follows the TypeScript interfaces. The setup includes both create and update scenarios with appropriate test data.


75-81: Proper axios interceptor setup for error handling.

The response interceptor setup correctly handles errors through the handleSendingError function, ensuring consistent error handling across all test scenarios.


87-297: Thorough test coverage for all CRUD operations.

Each method is tested for both success and failure scenarios with proper assertions. The tests verify correct URL construction, request payloads, and response handling. Error cases properly validate MailtrapError instances.

src/lib/api/resources/Templates.ts (6)

26-33: Clean implementation of getList method.

The method correctly implements the GET operation with proper TypeScript typing for both request and response.


35-42: Proper implementation of get method with ID parameter.

The method correctly constructs the URL with the template ID and maintains type safety.


44-52: Correct parameter wrapping for create operation.

The wrapping of parameters in { email_template: params } follows the expected API format for creation requests.


54-62: Appropriate PATCH method for update operation.

Using PATCH with optional parameters allows for partial updates, which is the correct approach for template modifications.


64-72: Standard delete implementation.

The delete method correctly implements the DELETE operation with proper URL construction.


20-24: Verify accountId handling in URL construction.

The URL construction assumes accountId is defined, but it's marked as optional in the constructor. If accountId is undefined, the URL will contain "undefined".

#!/bin/bash
# Description: Verify that accountId validation exists upstream in MailtrapClient
# Expected: MailtrapClient should validate accountId before creating TemplatesBaseAPI

rg -A 10 -B 5 "new TemplatesBaseAPI|templates.*getter|accountId.*missing" src/lib/MailtrapClient.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant