fix(mailgun): Normalize package scope and modernize Mailgun client: rename package to @apiclient.xyz/mailgun, update dependencies, refactor HTTP handling, fix types, update TS config and CI, refresh docs and tests
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
/**
|
||||
* autocreated commitinfo by @pushrocks/commitinfo
|
||||
* autocreated commitinfo by @push.rocks/commitinfo
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@mojoio/mailgun',
|
||||
version: '2.0.1',
|
||||
name: '@apiclient.xyz/mailgun',
|
||||
version: '2.0.2',
|
||||
description: 'an api abstraction package for mailgun'
|
||||
}
|
||||
|
||||
@@ -45,38 +45,33 @@ export class MailgunAccount {
|
||||
requestUrl = `${this.apiBaseUrl}${routeArg}`;
|
||||
}
|
||||
console.log(requestUrl);
|
||||
const requestOptions: plugins.smartrequest.ISmartRequestOptions = {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
const response = await plugins.smartrequest.SmartRequest.create()
|
||||
.url(requestUrl)
|
||||
.headers({
|
||||
Authorization: `Basic ${plugins.smartstring.base64.encode(`api:${this.options.apiToken}`)}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
keepAlive: false,
|
||||
};
|
||||
let response: plugins.smartrequest.IExtendedIncomingMessage;
|
||||
if (!binaryArg) {
|
||||
response = await plugins.smartrequest.request(requestUrl, requestOptions);
|
||||
} else {
|
||||
response = await plugins.smartrequest.getBinary(requestUrl, requestOptions);
|
||||
}
|
||||
})
|
||||
.options({ keepAlive: false })
|
||||
.get();
|
||||
return response;
|
||||
}
|
||||
|
||||
public async postFormData(routeArg: string, formFields: plugins.smartrequest.IFormField[]) {
|
||||
public async postFormData(
|
||||
routeArg: string,
|
||||
formFields: plugins.smartrequest.FormField[],
|
||||
) {
|
||||
const requestUrl = `${this.apiBaseUrl}${routeArg}`;
|
||||
console.log(requestUrl);
|
||||
const response = await plugins.smartrequest.postFormData(
|
||||
requestUrl,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Basic ${plugins.smartstring.base64.encode(
|
||||
`api:${this.options.apiToken}`
|
||||
)}`,
|
||||
},
|
||||
keepAlive: false,
|
||||
},
|
||||
formFields
|
||||
);
|
||||
const response = await plugins.smartrequest.SmartRequest.create()
|
||||
.url(requestUrl)
|
||||
.headers({
|
||||
Authorization: `Basic ${plugins.smartstring.base64.encode(
|
||||
`api:${this.options.apiToken}`,
|
||||
)}`,
|
||||
})
|
||||
.options({ keepAlive: false })
|
||||
.formData(formFields)
|
||||
.post();
|
||||
return response;
|
||||
}
|
||||
|
||||
@@ -86,29 +81,25 @@ export class MailgunAccount {
|
||||
public async sendSmartMail(
|
||||
smartmailArg: plugins.smartmail.Smartmail<interfaces.IMailgunMessage>,
|
||||
toArg: string,
|
||||
dataArg = {}
|
||||
dataArg = {},
|
||||
) {
|
||||
const domain = smartmailArg.options.from.split('@')[1].replace('>', '');
|
||||
const formFields: plugins.smartrequest.IFormField[] = [
|
||||
const formFields: plugins.smartrequest.FormField[] = [
|
||||
{
|
||||
name: 'from',
|
||||
type: 'string',
|
||||
payload: smartmailArg.options.from,
|
||||
value: smartmailArg.options.from,
|
||||
},
|
||||
{
|
||||
name: 'to',
|
||||
type: 'string',
|
||||
payload: toArg,
|
||||
value: toArg,
|
||||
},
|
||||
{
|
||||
name: 'subject',
|
||||
type: 'string',
|
||||
payload: smartmailArg.getSubject(dataArg),
|
||||
value: smartmailArg.getSubject(dataArg),
|
||||
},
|
||||
{
|
||||
name: 'html',
|
||||
type: 'string',
|
||||
payload: smartmailArg.getBody(dataArg),
|
||||
value: smartmailArg.getBody(dataArg),
|
||||
},
|
||||
];
|
||||
|
||||
@@ -117,29 +108,31 @@ export class MailgunAccount {
|
||||
for (const attachment of smartmailArg.attachments) {
|
||||
formFields.push({
|
||||
name: 'attachment',
|
||||
type: 'Buffer',
|
||||
payload: attachment.contentBuffer,
|
||||
fileName: attachment.parsedPath.base,
|
||||
value: attachment.contentBuffer,
|
||||
filename: attachment.parsedPath.base,
|
||||
});
|
||||
}
|
||||
|
||||
if (smartmailArg.getBody(dataArg)) {
|
||||
console.log('All requirements for API met');
|
||||
const response = await this.postFormData(`/${domain}/messages`, formFields);
|
||||
if (response.statusCode === 200) {
|
||||
const response = await this.postFormData(
|
||||
`/${domain}/messages`,
|
||||
formFields,
|
||||
);
|
||||
if (response.status === 200) {
|
||||
console.log(
|
||||
`Sent mail with subject ${smartmailArg.getSubject(
|
||||
dataArg
|
||||
)} to ${toArg} using the mailgun API`
|
||||
dataArg,
|
||||
)} to ${toArg} using the mailgun API`,
|
||||
);
|
||||
return response.body;
|
||||
return await response.json();
|
||||
} else {
|
||||
console.log(response.body);
|
||||
console.log(await response.json());
|
||||
throw new Error('could not send email');
|
||||
}
|
||||
} else {
|
||||
console.log(
|
||||
'An empty body was provided. This does not work via the API, but using SMTP instead.'
|
||||
'An empty body was provided. This does not work via the API, but using SMTP instead.',
|
||||
);
|
||||
const wantedSmartsmtp = this.smartSmtps[domain];
|
||||
if (!wantedSmartsmtp) {
|
||||
@@ -149,8 +142,8 @@ export class MailgunAccount {
|
||||
await wantedSmartsmtp.sendSmartMail(smartmailArg, toArg);
|
||||
console.log(
|
||||
`Sent mail with subject "${smartmailArg.getSubject(
|
||||
dataArg
|
||||
)}" to "${toArg}" using an smtp transport over Mailgun.`
|
||||
dataArg,
|
||||
)}" to "${toArg}" using an smtp transport over Mailgun.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -158,29 +151,35 @@ export class MailgunAccount {
|
||||
public async retrieveSmartMailFromMessageUrl(messageUrlArg: string) {
|
||||
console.log(`retrieving message for ${messageUrlArg}`);
|
||||
const response = await this.getRequest(messageUrlArg);
|
||||
if (response.statusCode === 404) {
|
||||
console.log(response.body.message);
|
||||
if (response.status === 404) {
|
||||
const body: any = await response.json();
|
||||
console.log(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,
|
||||
});
|
||||
const responseBody: interfaces.IMailgunMessage = await response.json();
|
||||
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);
|
||||
const attachmentContents = await this.getRequest(
|
||||
attachmentInfo.url,
|
||||
true,
|
||||
);
|
||||
const arrayBuffer = await attachmentContents.arrayBuffer();
|
||||
smartmail.addAttachment(
|
||||
new plugins.smartfile.Smartfile({
|
||||
new plugins.smartfile.SmartFile({
|
||||
path: `./${attachmentName}`,
|
||||
base: `./${attachmentName}`,
|
||||
contentBuffer: attachmentContents.body,
|
||||
})
|
||||
contentBuffer: Buffer.from(arrayBuffer),
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -189,6 +188,8 @@ export class MailgunAccount {
|
||||
}
|
||||
|
||||
public async retrieveSmartMailFromNotifyPayload(notifyPayloadArg: any) {
|
||||
return await this.retrieveSmartMailFromMessageUrl(notifyPayloadArg['message-url']);
|
||||
return await this.retrieveSmartMailFromMessageUrl(
|
||||
notifyPayloadArg['message-url'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
// @pushrocks scope
|
||||
import * as smartfile from '@pushrocks/smartfile';
|
||||
import * as smartmail from '@pushrocks/smartmail';
|
||||
import * as smartrequest from '@pushrocks/smartrequest';
|
||||
import * as smartsmtp from '@pushrocks/smartsmtp';
|
||||
import * as smartstring from '@pushrocks/smartstring';
|
||||
import * as smartfile from '@push.rocks/smartfile';
|
||||
import * as smartmail from '@push.rocks/smartmail';
|
||||
import * as smartrequest from '@push.rocks/smartrequest';
|
||||
import * as smartsmtp from '@push.rocks/smartsmtp';
|
||||
import * as smartstring from '@push.rocks/smartstring';
|
||||
|
||||
export { smartfile, smartmail, smartrequest, smartsmtp, smartstring };
|
||||
|
||||
Reference in New Issue
Block a user