fix(core): update

This commit is contained in:
2020-01-13 08:09:37 +00:00
parent 9d5907a7a7
commit 815b455dbb
7 changed files with 109 additions and 42 deletions

1
ts/interfaces/index.ts Normal file
View File

@ -0,0 +1 @@
export * from './message';

30
ts/interfaces/message.ts Normal file
View File

@ -0,0 +1,30 @@
export interface IMailgunMessage {
Received: string;
From: string;
'X-Envelope-From': string;
recipients: string;
'X-Google-Dkim-Signature': string;
To: string;
'message-headers': [[string, string]];
'Dkim-Signature': string;
'content-id-map': any;
subject: 'test2';
'stripped-html': string;
'X-Mailgun-Incoming': 'Yes';
'X-Received': string;
'X-Gm-Message-State': string;
'body-plain': string;
attachments: Array<{
url: string,
'content-type': string,
name: string,
size: number
}>;
'body-html': string;
'Mime-Version': string;
Date: string,
'Message-Id': string;
'Content-Type': string;
'X-Google-Smtp-Source': string;
Subject: string;
}

View File

@ -1,19 +1,20 @@
import * as plugins from './mailgun.plugins';
import * as interfaces from './interfaces';
export class MailgunAccount {
public baseUrl = 'https://api.mailgun.net/v3';
public baseUrlSe = 'https://se.api.mailgun.net/v3';
public apiToken: string;
constructor(apiTokenArg: string) {
this.apiToken = apiTokenArg;
}
public getRequest(routeArg: string) {
public async getRequest(routeArg: string, binaryArg: boolean = false) {
let requestUrl = routeArg;
const needsBaseUrlPrefix = (routeArg.startsWith(this.baseUrl)) || routeArg.startsWith(this.baseUrlSe);
const needsBaseUrlPrefix = !routeArg.startsWith('https://');
needsBaseUrlPrefix ? requestUrl = `${this.baseUrl}${routeArg}` : null;
const response = plugins.smartrequest.request(requestUrl, {
console.log(requestUrl);
const requestOptions: plugins.smartrequest.ISmartRequestOptions = {
method: 'GET',
headers: {
Authorization: `Basic ${plugins.smartstring.base64.encode(
@ -21,7 +22,13 @@ export class MailgunAccount {
)}`,
'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;
}
@ -69,6 +76,8 @@ export class MailgunAccount {
}
];
console.log(smartmailArg.attachments);
for (const attachment of smartmailArg.attachments) {
formFields.push({
name: 'attachment',
@ -89,9 +98,27 @@ export class MailgunAccount {
public async retrieveSmartMailFromMessageUrl(messageUrlArg: string) {
const response = await this.getRequest(messageUrlArg);
return response.body;
}
const responseBody: interfaces.IMailgunMessage = response.body;
const smartmail = new plugins.smartmail.Smartmail({
from: responseBody.From,
body: responseBody["body-html"],
subject: responseBody.Subject,
});
// lets care about attachments
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']);
}

View File

@ -1,6 +1,7 @@
// @pushrocks scope
import * as smartfile from '@pushrocks/smartfile';
import * as smartmail from '@pushrocks/smartmail';
import * as smartrequest from '@pushrocks/smartrequest';
import * as smartstring from '@pushrocks/smartstring';
export { smartmail, smartrequest, smartstring };
export { smartfile, smartmail, smartrequest, smartstring };