Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed Jun 13, 2019
0 parents commit 31ab9ad
Show file tree
Hide file tree
Showing 15 changed files with 3,998 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
coverage
/lib
8 changes: 8 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
build
dist
lib
.next
node_modules
yarn.lock
.prettierignore
.gitignore
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"trailingComma": "all",
"singleQuote": true,
"semi": false
}
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# email-validator-net

A small library providing a wrapper for [api.email-validator.net](http://www.email-validator.net/email-address-online-verification-api.html)

## Installation

```shell
npm install email-validator-net
# or
yarn add email-validator-net
```

## Usage

ES6 or TypeScript usage:

```js
import EmailValidator from 'email-validator-net'

// YOUR_API_KEY is a string
const validatorInstance = EmailValidator(YOUR_API_KEY)

// validatorInstance is a function and takes one argument
// argument must be a string
// validatorInstance returns a promise
const responseObject = await validatorInstance(EMAIL_TO_VALIDATE) // EMAIL_TO_VALIDATE is a string

// responseObject looks like this:
// {
// apiKey, // used apikey
// email, // validated email address
// statusCode, // statusCode of the service
// // => http://www.email-validator.net/email-verification-results.html
// statusString, // status as string
// fullResponse, // object with the full response from the service
// }
```

## Tests

```shell
yarn test
```

## Release History

- 2.0.0 Rewrite in Typescript and using Promises
- 0.1.0 Initial release
7 changes: 7 additions & 0 deletions jestconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"transform": {
"^.+\\.(t|j)sx?$": "ts-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"]
}
46 changes: 46 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "email-validator-net",
"version": "2.0.0",
"description": "Wrapper for api.email-validator.net",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"author": "Janek Rahrt <[email protected]>",
"homepage": "http://www.email-validator.net/email-address-online-verification-api.html",
"keywords": [
"validator",
"eMail",
"mail",
"byteplant"
],
"license": "MIT",
"scripts": {
"build": "tsc",
"prepublishOnly": "npm test && npm run lint && npm run build",
"preversion": "npm run lint",
"version": "npm run format && git add -A src",
"postversion": "git push && git push --tags",
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
"lint": "tslint -p tsconfig.json",
"test": "jest --config jestconfig.json --coverage"
},
"repository": {
"type": "git",
"url": "git+https://github.com/janek26/email-validator-net.git"
},
"files": [
"lib/**/*"
],
"devDependencies": {
"@types/axios": "^0.14.0",
"@types/jest": "^24.0.14",
"jest": "^24.8.0",
"prettier": "^1.18.2",
"ts-jest": "^24.0.2",
"tslint": "^5.17.0",
"tslint-config-prettier": "^1.18.0",
"typescript": "^3.5.1"
},
"dependencies": {
"axios": "^0.19.0"
}
}
24 changes: 24 additions & 0 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import EmailValidator from '..'
import { statusToString } from '../statusToString'

const WORKING_APIKEY = 'ev-e7160cb71e8db0e574de5e8a4d914d61'
const EMAIL119 = '[email protected]'

test('Create instance of EmailValidator', () => {
expect(() => EmailValidator('')).toThrow()

expect(EmailValidator(WORKING_APIKEY)).toBeInstanceOf(Function)
})

test('Use instance to check eMails', async () => {
const instance = EmailValidator(WORKING_APIKEY)
const call = instance(EMAIL119)

expect(call).toBeInstanceOf(Promise)
expect(call).resolves.toMatchObject({
apiKey: WORKING_APIKEY,
email: EMAIL119,
statusCode: 119,
statusString: statusToString(119),
})
})
11 changes: 11 additions & 0 deletions src/__tests__/isValidAPIKey.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { isValidAPIKey } from '../isValidAPIKey'

test('isValidAPIKey', () => {
expect(isValidAPIKey('')).toBe(false)
expect(isValidAPIKey('ev-')).toBe(false)
expect(isValidAPIKey('c79c5dc37e20e45325f830dee95c15c8')).toBe(false)
expect(isValidAPIKey('ev-e7160cb71e8db0e574de5e8a4d914d61')).toBe(true)
expect(isValidAPIKey('ev-df799342fa9405da27916bcc96e46a5e')).toBe(true)
expect(isValidAPIKey('ev-cd180873104fe2eb1edb20297b8d024e')).toBe(true)
expect(isValidAPIKey('ev-416b1aa14fd1d8bed9b53979bbb64293')).toBe(true)
})
28 changes: 28 additions & 0 deletions src/__tests__/statusToString.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { statusToString } from '../statusToString'

test('statusToString', () => {
expect(statusToString(114)).toBe('Validation Delayed')
expect(statusToString(118)).toBe('Rate Limit Exceeded')
expect(statusToString(119)).toBe('API Key Invalid or Depleted')
expect(statusToString(121)).toBe('Task Accepted')
expect(statusToString(200)).toBe('OK - Valid Address')
expect(statusToString(207)).toBe('OK - Catch-All Active')
expect(statusToString(215)).toBe('OK - Catch-All Test Delayed')
expect(statusToString(302)).toBe('Local Address')
expect(statusToString(303)).toBe('IP Address Literal')
expect(statusToString(305)).toBe('Disposable Address')
expect(statusToString(308)).toBe('Role Address')
expect(statusToString(313)).toBe('Server Unavailable')
expect(statusToString(314)).toBe('Address Unavailable')
expect(statusToString(316)).toBe('Duplicate Address')
expect(statusToString(317)).toBe('Server Reject')
expect(statusToString(401)).toBe('Bad Address')
expect(statusToString(404)).toBe('Domain Not Fully Qualified')
expect(statusToString(406)).toBe('MX Lookup Error')
expect(statusToString(409)).toBe('No-Reply Address')
expect(statusToString(410)).toBe('Address Rejected')
expect(statusToString(413)).toBe('Server Unavailable')
expect(statusToString(414)).toBe('Address Unavailable')
expect(statusToString(420)).toBe('Domain Name Misspelled')
expect(statusToString(0)).toBe('Unknown Status Code')
})
29 changes: 29 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import axios from 'axios'

import { isValidAPIKey } from './isValidAPIKey'
import { statusToString } from './statusToString'

const API_URL = 'https://api.email-validator.net/api/verify'

const emailValidatorInstance = async (apiKey: string, email: string) => {
const serializedMail = encodeURIComponent(email)
const url = `${API_URL}?EmailAddress=${serializedMail}&APIKey=${apiKey}`

const { data } = await axios.get(url)
const statusCode = data.status

return {
apiKey,
email,
fullResponse: data,
statusCode,
statusString: statusToString(statusCode),
}
}

export default (apiKey: string) => {
if (!isValidAPIKey(apiKey)) {
throw new Error('No valid API Key!')
}
return (email: string) => emailValidatorInstance(apiKey, email)
}
3 changes: 3 additions & 0 deletions src/isValidAPIKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const isValidAPIKey = (apikey: string): boolean =>
apikey.substring(0, 3) === 'ev-' &&
/^[a-f0-9]{32}$/g.test(apikey.substring(3))
52 changes: 52 additions & 0 deletions src/statusToString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
export const statusToString = (status: number) => {
switch (status) {
case 114:
return 'Validation Delayed'
case 118:
return 'Rate Limit Exceeded'
case 119:
return 'API Key Invalid or Depleted'
case 121:
return 'Task Accepted'
case 200:
return 'OK - Valid Address'
case 207:
return 'OK - Catch-All Active'
case 215:
return 'OK - Catch-All Test Delayed'
case 302:
return 'Local Address'
case 303:
return 'IP Address Literal'
case 305:
return 'Disposable Address'
case 308:
return 'Role Address'
case 313:
return 'Server Unavailable'
case 314:
return 'Address Unavailable'
case 316:
return 'Duplicate Address'
case 317:
return 'Server Reject'
case 401:
return 'Bad Address'
case 404:
return 'Domain Not Fully Qualified'
case 406:
return 'MX Lookup Error'
case 409:
return 'No-Reply Address'
case 410:
return 'Address Rejected'
case 413:
return 'Server Unavailable'
case 414:
return 'Address Unavailable'
case 420:
return 'Domain Name Misspelled'
default:
return 'Unknown Status Code'
}
}
12 changes: 12 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["es6"],
"module": "commonjs",
"declaration": true,
"outDir": "./lib",
"strict": true
},
"include": ["src"],
"exclude": ["node_modules", "**/__tests__/*"]
}
3 changes: 3 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["tslint:recommended", "tslint-config-prettier"]
}
Loading

0 comments on commit 31ab9ad

Please sign in to comment.