Last active
June 23, 2020 12:20
-
-
Save flourigh/fd75c93099a34543ab6f586eaaadfb92 to your computer and use it in GitHub Desktop.
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
API que verifica DNS / MX | |
- https://duckstack.herokuapp.com/validator/email | |
-> ``` | |
{ | |
"email":"[email protected]" | |
} | |
``` | |
API que verifica Pattern | |
- https://verify-email.flourigh.workers.dev/validator/email | |
-> ``` | |
{ | |
"email": "[email protected]" | |
} | |
``` |
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
https://git.heroku.com/duckstack.git | |
https://github.com/egulias/EmailValidator |
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
addEventListener('fetch', event => { | |
const { request } = event | |
const { url } = request | |
if (request.method === 'OPTIONS') { | |
return event.respondWith(handleOptionsRequest(request)) | |
} | |
if (url.includes('form')) { | |
return event.respondWith(handleHTMLRequest(buildHTMLRequest)) | |
} else if (url.includes('validator/email')) { | |
if (request.method === 'POST') { | |
return event.respondWith(handlePostRequest(request)) | |
} else { | |
return event.respondWith(handlRequest(request)) | |
} | |
} else { | |
return event.respondWith(handlRequest(request)) | |
} | |
}) | |
async function handleOptionsRequest(request) { | |
// Default response to OPTIONS | |
const init = { | |
headers: { | |
'content-type': 'application/json', | |
'Access-Control-Allow-Origin': '*', | |
'Access-Control-Allow-Headers': 'Content-Type', | |
'Access-Control-Allow-Methods': 'POST, OPTIONS' | |
} | |
} | |
return new Response('{"method":"OPTIONS"}', init) | |
} | |
async function handleHTMLRequest(request) { | |
// Response with HTML Form | |
const init = { | |
headers: { | |
'content-type': 'text/html;charset=UTF-8', | |
'Access-Control-Allow-Origin': '*', | |
'Access-Control-Allow-Headers': 'Content-Type' | |
}, | |
} | |
return new Response(request, init) | |
} | |
async function handlePostRequest(request) { | |
// Response to POST/SAVE | |
const init = { | |
headers: { | |
'content-type': 'application/json;charset=UTF-8', | |
'Access-Control-Allow-Origin': '*', | |
'Access-Control-Allow-Headers': 'Content-Type' | |
}, | |
} | |
return new Response(await buildPostRequest(request), init) | |
} | |
async function handlRequest(request) { | |
// Default response to OPTIONS | |
const init = { | |
headers: { | |
'content-type': 'application/json;charset=UTF-8', | |
'Access-Control-Allow-Origin': '*', | |
'Access-Control-Allow-Headers': 'Content-Type' | |
} | |
} | |
return new Response('{"sucess":"200"}', init) | |
} | |
async function buildPostRequest(request) { | |
// Build a part form to response | |
const { headers } = request | |
const contentType = headers.get('content-type') | |
if (contentType.includes('application/json')) { | |
const body = await request.json() | |
let data = body | |
data.status = await verifyPattern({value: data.email}) | |
return JSON.stringify(data) | |
} else if (contentType.includes('application/text')) { | |
const body = await request.text() | |
let data = body | |
data.status = await verifyPattern({value: data.email}) | |
return data | |
} else if (contentType.includes('text/html')) { | |
const body = await request.text() | |
let data = body | |
data.status = await verifyPattern({value: data.email}) | |
return data | |
} else if (contentType.includes('form')) { | |
const formData = await request.formData() | |
let body = {} | |
for (let entry of formData.entries()) { | |
body[entry[0]] = entry[1] | |
} | |
let data = body | |
data.status = await verifyPattern({value: data.email}) | |
return JSON.stringify(data) | |
} else { | |
let myBlob = await request.blob() | |
var objectURL = URL.createObjectURL(myBlob) | |
let data = objectURL | |
data.status = await verifyPattern({value: data.email}) | |
return data | |
} | |
} | |
async function verifyPattern(param) { | |
return /^(\b[a-zA-Z0-9-]{1,63}\b)(\.\b[a-zA-Z0-9-]{1,63}\b)*@(\b[a-zA-Z0-9-]{1,63}\b)(\.\b[a-zA-Z0-9-]{2,63}\b)+$/i.test(param.value) || (param.text || 'E-mail inválido') | |
} | |
const buildHTMLRequest = ` | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<title>Email verify</title> | |
</head> | |
<body> | |
<form action="validator/email" method="post"> | |
<input type="email" name="email" /> | |
<button>OK</button> | |
</form> | |
</body> | |
</html> | |
` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment