GithubHelp home page GithubHelp logo

suin / email-data Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 3.51 MB

A simple email data representation for TypeScript. This component provides a very simple reusable interface that represents an email. The interface was designed for the use case that treats received emails from POP3, SendGrid Inbound Parse, Gmail API and so on.

License: MIT License

TypeScript 100.00%
email

email-data's Introduction

@suin/email-data

A simple email data representation for TypeScript.

This component provides a very simple reusable interface that represents an email.

The interface was designed for the use case that treats received emails from POP3, SendGrid Inbound Parse, Gmail API and so on.

Features

  • Abstract email data type EmailData
  • Type guard function
  • Validation function
  • Assertion function

Missing Features

The following features hasn't been implemented yet:

  • Some models:
    • BCC header
    • Attachments

Installation

yarn add @suin/email-data
# or
npm install @suin/email-data

Usages

Declaring the EmailData value

import { EmailData } from '@suin/email-data'

const email: EmailData = {
  from: [{ name: 'Alice', address: '[email protected]' }],
  to: [{ name: 'Bob', address: '[email protected]' }],
  cc: [],
  replyTo: [],
  subject: 'Hi',
  bodyText: 'Hello',
  bodyHtml: '<p>Hello</p>',
}

Using the type guard function

import { isEmailData } from '@suin/email-data'

const email: unknown = {
  /* ... */
}

if (isEmailData(email)) {
  console.log(email.subject)
} else {
  console.log('The value is incompatible with EmailData')
}

Using the validation function

import { parseEmailData } from '@suin/email-data'

try {
  const email = parseEmailData(JSON.parse('{}'))
  console.log('The JSON value is compatible with EmailData', email)
} catch (error) {
  console.log(error)
}

Using the assertion function

import { assertEmailData } from '@suin/email-data'

try {
  const email: unknown = JSON.parse('{}')
  assertEmailData(email)
  console.log('The JSON value is compatible with EmailData', email)
} catch (error) {
  console.log(error)
}

API Reference

https://suin.github.io/email-data/

email-data's People

Contributors

dependabot[bot] avatar semantic-release-bot avatar suin avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.