Skip to content

Commit

Permalink
API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
WebReflection committed Sep 24, 2019
1 parent 19c2150 commit b47616f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 22 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -19,8 +19,8 @@ Either `import StaticEmail from 'static.email'` or put the script on top of your
<script src="https://unpkg.com/static.email"></script>
<script>
StaticEmail({
// your unique identifier for the page
token: 'URL-AUTH-TOKEN',
// your end point allowed to send emails
path: '/api/paperboy',
// optional fields
from: 'Some Body <some@body.me>',
Expand Down
11 changes: 5 additions & 6 deletions cjs/index.js
Expand Up @@ -35,23 +35,22 @@ const {get: status} = getOwnPropertyDescriptor(prototype, 'status');
Object.defineProperty(exports, '__esModule', {value: true}).default = details => new Promise((resolve, reject) => {

const info = keys(details || {});
if (call(indexOf, info, 'token') < 0 || (
if (call(indexOf, info, 'path') < 0 || (
call(indexOf, info, 'html') < 0 &&
call(indexOf, info, 'md') < 0 &&
call(indexOf, info, 'text') < 0
))
return reject(new Error('invalid request'));
return reject(new Error('Bad Request'));

const xhr = new XMLHttpRequest;
call(open, xhr, 'POST', 'https://static.email/paperboy', true);
call(open, xhr, 'POST', details.path, true);
call(setRequestHeader, xhr, 'Content-Type', 'application/x-www-form-urlencoded');
call(setRequestHeader, xhr, 'X-Static-Email-Token', details.token);
call(addEventListener, xhr, 'readystatechange', () => {
if (call(readyState, xhr) == 4) {
if (call(status, xhr) == 200)
resolve();
resolve(call(responseText, xhr));
else
reject(new Error(call(responseText, xhr) || 'network error'));
reject(new Error(call(responseText, xhr) || 'Not Found'));
}
});
call(send, xhr, call(
Expand Down
11 changes: 5 additions & 6 deletions esm/index.js
Expand Up @@ -34,23 +34,22 @@ const {get: status} = getOwnPropertyDescriptor(prototype, 'status');
export default details => new Promise((resolve, reject) => {

const info = keys(details || {});
if (call(indexOf, info, 'token') < 0 || (
if (call(indexOf, info, 'path') < 0 || (
call(indexOf, info, 'html') < 0 &&
call(indexOf, info, 'md') < 0 &&
call(indexOf, info, 'text') < 0
))
return reject(new Error('invalid request'));
return reject(new Error('Bad Request'));

const xhr = new XMLHttpRequest;
call(open, xhr, 'POST', 'https://static.email/paperboy', true);
call(open, xhr, 'POST', details.path, true);
call(setRequestHeader, xhr, 'Content-Type', 'application/x-www-form-urlencoded');
call(setRequestHeader, xhr, 'X-Static-Email-Token', details.token);
call(addEventListener, xhr, 'readystatechange', () => {
if (call(readyState, xhr) == 4) {
if (call(status, xhr) == 200)
resolve();
resolve(call(responseText, xhr));
else
reject(new Error(call(responseText, xhr) || 'network error'));
reject(new Error(call(responseText, xhr) || 'Not Found'));
}
});
call(send, xhr, call(
Expand Down
2 changes: 1 addition & 1 deletion min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions test/index.js
Expand Up @@ -23,7 +23,7 @@ global.XMLHttpRequest = class XMLHttpRequest {
}
open(method, target, asynchronous) {
assert(method === 'POST', 'unexpected method');
assert(target === 'https://static.email/paperboy', 'unexpected target');
assert(target === '/api/paperboy', 'unexpected target');
assert(asynchronous, 'unexpected synchronous request');
this._readyState = 1;
}
Expand All @@ -47,22 +47,22 @@ StaticEmail({})
.then(() => assert(false, 'empty details should fail'))
.catch(Object);

StaticEmail({token: 'SECRET'})
StaticEmail({path: '/api/paperboy'})
.then(() => assert(false, 'only token should fail'))
.catch(Object);

_status = 200;
StaticEmail({token: 'SECRET', text: 'Hello World!'})
StaticEmail({path: '/api/paperboy', text: 'Hello World!'})
.then(() => {
_status = 0;
assert(_params === 'text=Hello%20World!', 'unexpected params');
assert(JSON.stringify(_headers) === '{"Content-Type":"application/x-www-form-urlencoded","X-Static-Email-Token":"SECRET"}', 'unexpected headers');
StaticEmail({token: 'SECRET', text: 'Hello World!'})
assert(JSON.stringify(_headers) === '{"Content-Type":"application/x-www-form-urlencoded"}', 'unexpected headers');
StaticEmail({path: '/api/paperboy', text: 'Hello World!'})
.then(() => exit(new Error('unexpected execution')))
.catch(err => {
assert(err.message === 'network error', 'unexpected error message');
assert(err.message === 'Not Found', 'unexpected error message');
_responseText = 'shenanigans';
StaticEmail({token: 'SECRET', text: 'Hello World!'})
StaticEmail({path: '/api/paperboy', text: 'Hello World!'})
.then(() => exit(new Error('unexpected execution')))
.catch(err => {
assert(err.message === 'shenanigans', 'unexpected error message');
Expand Down

0 comments on commit b47616f

Please sign in to comment.