fix(core): update
This commit is contained in:
		| @@ -1,41 +1,54 @@ | ||||
| import * as plugins from './mailgun.plugins'; | ||||
| import * as interfaces from './interfaces'; | ||||
| import * as plugins from './mailgun.plugins.js'; | ||||
| import * as interfaces from './interfaces/index.js'; | ||||
|  | ||||
| export interface IMailgunAccountContructorOptions { | ||||
|   apiToken: string; | ||||
|   region: 'eu' | 'us'; | ||||
| } | ||||
|  | ||||
| export class MailgunAccount { | ||||
|   public baseUrl = 'https://api.mailgun.net/v3'; | ||||
|   public apiToken: string; | ||||
|   public apiBaseUrl: string; | ||||
|  | ||||
|   public options: IMailgunAccountContructorOptions; | ||||
|  | ||||
|   public smartSmtps: { [domain: string]: plugins.smartsmtp.Smartsmtp } = {}; | ||||
|  | ||||
|   constructor(apiTokenArg: string) { | ||||
|     this.apiToken = apiTokenArg; | ||||
|   constructor(optionsArg: IMailgunAccountContructorOptions) { | ||||
|     this.options = optionsArg; | ||||
|     this.apiBaseUrl = | ||||
|       this.options.region === 'eu' | ||||
|         ? 'https://api.eu.mailgun.net/v3' | ||||
|         : 'https://api..mailgun.net/v3'; | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * allows adding smtp credentials | ||||
|    * Format: [domain]|[username]|[password] | ||||
|    */ | ||||
|   public addSmtpCredentials(credentials: string) { | ||||
|   public async addSmtpCredentials(credentials: string) { | ||||
|     const credentialArray = credentials.split('|'); | ||||
|     if (credentialArray.length !== 3) { | ||||
|       throw new Error('credentials are in the wrong format'); | ||||
|     } | ||||
|     this.smartSmtps[credentialArray[0]] = new plugins.smartsmtp.Smartsmtp({ | ||||
|       smtpServer: 'smtp.mailgun.org', | ||||
|       smtpUser: credentialArray[1], | ||||
|       smtpPassword: credentialArray[2], | ||||
|     }); | ||||
|     this.smartSmtps[credentialArray[0]] = | ||||
|       await plugins.smartsmtp.Smartsmtp.createSmartsmtpWithRelay({ | ||||
|         smtpServer: 'smtp.eu.mailgun.org', | ||||
|         smtpUser: credentialArray[1], | ||||
|         smtpPassword: credentialArray[2], | ||||
|       }); | ||||
|   } | ||||
|  | ||||
|   public async getRequest(routeArg: string, binaryArg: boolean = false) { | ||||
|     let requestUrl = routeArg; | ||||
|     const needsBaseUrlPrefix = !routeArg.startsWith('https://'); | ||||
|     needsBaseUrlPrefix ? (requestUrl = `${this.baseUrl}${routeArg}`) : null; | ||||
|     if (needsBaseUrlPrefix) { | ||||
|       requestUrl = `${this.apiBaseUrl}${routeArg}`; | ||||
|     } | ||||
|     console.log(requestUrl); | ||||
|     const requestOptions: plugins.smartrequest.ISmartRequestOptions = { | ||||
|       method: 'GET', | ||||
|       headers: { | ||||
|         Authorization: `Basic ${plugins.smartstring.base64.encode(`api:${this.apiToken}`)}`, | ||||
|         Authorization: `Basic ${plugins.smartstring.base64.encode(`api:${this.options.apiToken}`)}`, | ||||
|         'Content-Type': 'application/json', | ||||
|       }, | ||||
|       keepAlive: false, | ||||
| @@ -50,12 +63,15 @@ export class MailgunAccount { | ||||
|   } | ||||
|  | ||||
|   public async postFormData(routeArg: string, formFields: plugins.smartrequest.IFormField[]) { | ||||
|     const requestUrl = `${this.baseUrl}${routeArg}`; // TODO; | ||||
|     const requestUrl = `${this.apiBaseUrl}${routeArg}`; | ||||
|     console.log(requestUrl); | ||||
|     const response = await plugins.smartrequest.postFormData( | ||||
|       requestUrl, | ||||
|       { | ||||
|         headers: { | ||||
|           Authorization: `Basic ${plugins.smartstring.base64.encode(`api:${this.apiToken}`)}`, | ||||
|           Authorization: `Basic ${plugins.smartstring.base64.encode( | ||||
|             `api:${this.options.apiToken}` | ||||
|           )}`, | ||||
|         }, | ||||
|         keepAlive: false, | ||||
|       }, | ||||
| @@ -130,16 +146,17 @@ export class MailgunAccount { | ||||
|         console.log('did not find appropriate smtp credentials'); | ||||
|         return; | ||||
|       } | ||||
|       wantedSmartsmtp.sendSmartMail(smartmailArg, toArg); | ||||
|       await wantedSmartsmtp.sendSmartMail(smartmailArg, toArg); | ||||
|       console.log( | ||||
|         `Sent mail with subject ${smartmailArg.getSubject( | ||||
|         `Sent mail with subject "${smartmailArg.getSubject( | ||||
|           dataArg | ||||
|         )} to ${toArg} using an smtp transport over Mailgun` | ||||
|         )}" to "${toArg}" using an smtp transport over Mailgun.` | ||||
|       ); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   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); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user