Skip to content

Instantly share code, notes, and snippets.

@ndunks
Created June 27, 2019 15:13
Show Gist options
  • Save ndunks/699118651dbde3553233dc92830b0249 to your computer and use it in GitHub Desktop.
Save ndunks/699118651dbde3553233dc92830b0249 to your computer and use it in GitHub Desktop.
Getnada email client API for browser with typescript implementation
// Jasmine Standalone testing suite
import { GetnadaClient, ListResponse } from "./email-getnada";
(jasmine.getEnv() as any) .configure({random: false, oneFailurePerSpec: true, failFast: true});
let listResponse: ListResponse;
describe('Plugins testing getnada api client', () => {
const email = '[email protected]';
const api = new GetnadaClient(email);
it('Should read inbox', async done => {
listResponse = await api.lists();
console.log(listResponse);
expect(listResponse).toBeTruthy()
expect( listResponse.last instanceof Date).toEqual(true)
done()
})
it('Should read message', async done => {
if (!listResponse || !listResponse.msgs.length) {
pending('Inbox empty, no email to read test')
return;
}
expect(listResponse).toBeTruthy();
const maxRead = listResponse.msgs.length > 5 ? 5 : listResponse.msgs.length;
for( let i = 0; i < maxRead; i++){
const item = listResponse.msgs[i];
console.log(item);
expect(item).toBeTruthy();
const email = await api.read( item.uid );
console.log(email);
expect(email).toBeTruthy();
expect(email.text).toBeTruthy();
expect(email.html).toBeTruthy();
expect(email.read).toEqual(true);
}
done();
})
})
const EmailFieldTransform = {
// transform known fields
ib: 'inbox',
f: 'from',
s: 'subject',
fe: 'fromEmail',
at: 'attachment',
ru: 'read', // read (by) user ?
r: 'date', // received ?
rf: 'dateText', // received full ?
ii: 'initialIcon',
av: 'avatarColor'
}
export interface EmailItem {
uid: string,
d: boolean,
inbox: string,
from: string,
subject: string,
fromEmail: string,
attachment: any[],
read: boolean,
date: Date,
dateText: string,
initialIcon: string,
avatarColor: string,
// Message detail
text?: string,
html?: string
}
export interface ListResponse {
last: Date,
total: String // numeric
msgs: EmailItem[]
}
function transformEmailItem(item: any): EmailItem {
Object.keys(EmailFieldTransform)
.filter(k => item[k] !== undefined)
.forEach(k => {
item[EmailFieldTransform[k]] = item[k]
delete item[k];
})
// Clone untransformend
if (undefined === item.read) {
item.read = false
}
if (typeof (item.date) === 'number') {
item.date = new Date(item.date * 1000)
}
return item;
}
export class GetnadaClient {
constructor(public email: string) { }
lists(): Promise<ListResponse> {
return this.fetch<ListResponse>('inboxes', this.email)
.then((response: ListResponse) => {
response.msgs.forEach(transformEmailItem)
response.last = new Date(response.last);
return response
})
}
read(uid: string): Promise<EmailItem> {
if (!uid) return Promise.reject('No UID');
return this.fetch<EmailItem>('messages', uid).then(transformEmailItem)
}
private fetch<T extends any = any>(...paths): Promise<T> {
// https://getnada.com/src/mid/config.js
return fetch(['https://getnada.com/api/v1', ...paths].join('/'))
.then(response => response.json())
.catch(err => console.error(err))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment