mailgun/ts/mailgun.classes.account.ts
2020-04-26 23:24:03 +00:00

133 lines
4.0 KiB
TypeScript

import * as plugins from './mailgun.plugins';
import * as interfaces from './interfaces';
export class MailgunAccount {
public baseUrl = 'https://api.mailgun.net/v3';
public apiToken: string;
constructor(apiTokenArg: string) {
this.apiToken = apiTokenArg;
}
public async getRequest(routeArg: string, binaryArg: boolean = false) {
let requestUrl = routeArg;
const needsBaseUrlPrefix = !routeArg.startsWith('https://');
needsBaseUrlPrefix ? requestUrl = `${this.baseUrl}${routeArg}` : null;
console.log(requestUrl);
const requestOptions: plugins.smartrequest.ISmartRequestOptions = {
method: 'GET',
headers: {
Authorization: `Basic ${plugins.smartstring.base64.encode(
`api:${this.apiToken}`
)}`,
'Content-Type': 'application/json'
}
};
let response: plugins.smartrequest.IExtendedIncomingMessage;
if (!binaryArg) {
response = await plugins.smartrequest.request(requestUrl, requestOptions);
} else {
response = await plugins.smartrequest.getBinary(requestUrl, requestOptions);
}
return response;
}
public async postFormData(routeArg: string, formFields: plugins.smartrequest.IFormField[]) {
const requestUrl = `${this.baseUrl}${routeArg}`; // TODO;
const response = await plugins.smartrequest.postFormData(
requestUrl,
{
headers: {
Authorization: `Basic ${plugins.smartstring.base64.encode(
`api:${this.apiToken}`
)}`
}
},
formFields
);
return response;
}
/**
* sends a SmartMail
*/
public async sendSmartMail(smartmailArg: plugins.smartmail.Smartmail<interfaces.IMailgunMessage>, toArg: string, dataArg = {}) {
const domain = smartmailArg.options.from.split('@')[1].replace('>', '');
const formFields: plugins.smartrequest.IFormField[] = [
{
name: 'from',
type: 'string',
payload: smartmailArg.options.from
},
{
name: 'to',
type: 'string',
payload: toArg
},
{
name: 'subject',
type: 'string',
payload: smartmailArg.getSubject(dataArg)
},
{
name: 'html',
type: 'string',
payload: smartmailArg.getBody(dataArg)
}
];
console.log(smartmailArg.attachments);
for (const attachment of smartmailArg.attachments) {
formFields.push({
name: 'attachment',
type: 'Buffer',
payload: attachment.contentBuffer,
fileName: attachment.parsedPath.base
});
}
const response = await this.postFormData(`/${domain}/messages`, formFields);
if (response.statusCode === 200) {
return response.body;
} else {
console.log(response.body);
throw new Error('could not send email');
}
}
public async retrieveSmartMailFromMessageUrl(messageUrlArg: string) {
const response = await this.getRequest(messageUrlArg);
if (response.statusCode === 404) {
console.log(response.body.message);
return null;
}
const responseBody: interfaces.IMailgunMessage = response.body;
const smartmail = new plugins.smartmail.Smartmail<interfaces.IMailgunMessage>({
from: responseBody.From,
body: responseBody["body-html"],
subject: responseBody.Subject,
creationObjectRef: responseBody
});
// lets care about attachments
if (responseBody.attachments && responseBody.attachments instanceof Array) {
for (const attachmentInfo of responseBody.attachments) {
const attachmentName = attachmentInfo.name;
const attachmentContents = await this.getRequest(attachmentInfo.url, true);
smartmail.addAttachment(new plugins.smartfile.Smartfile({
path: `./${attachmentName}`,
base: `./${attachmentName}`,
contentBuffer: attachmentContents.body
}));
}
}
return smartmail;
}
public async retrieveSmartMailFromNotifyPayload(notifyPayloadArg: any) {
return await this.retrieveSmartMailFromMessageUrl(notifyPayloadArg['message-url']);
}
}