feat(Mailjet Node): Add credential tests and support for sandbox, JSON parameters & variables (#2987)

* Add Variables JSON to Mailjet Batch send

*  Improvements

*  Add credential verification

*  Small improvement

Co-authored-by: Marcin Koziej <marcin@cahoots.pl>
This commit is contained in:
Ricardo Espinoza
2022-03-20 15:13:18 -04:00
committed by GitHub
parent 26a7c61175
commit d2756de090
4 changed files with 197 additions and 11 deletions

View File

@@ -9,6 +9,8 @@ import {
} from 'n8n-core';
import {
ICredentialDataDecryptedObject,
ICredentialTestFunctions,
IDataObject,
IHookFunctions,
JsonObject,
@@ -16,7 +18,7 @@ import {
} from 'n8n-workflow';
export async function mailjetApiRequest(this: IExecuteFunctions | IExecuteSingleFunctions | IHookFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const emailApiCredentials = await this.getCredentials('mailjetEmailApi');
const emailApiCredentials = await this.getCredentials('mailjetEmailApi') as { apiKey: string, secretKey: string, sandboxMode: boolean };
let options: OptionsWithUri = {
headers: {
Accept: 'application/json',
@@ -33,8 +35,12 @@ export async function mailjetApiRequest(this: IExecuteFunctions | IExecuteSingle
delete options.body;
}
if (emailApiCredentials !== undefined) {
const base64Credentials = Buffer.from(`${emailApiCredentials.apiKey}:${emailApiCredentials.secretKey}`).toString('base64');
options.headers!['Authorization'] = `Basic ${base64Credentials}`;
const { sandboxMode } = emailApiCredentials;
Object.assign(body, { SandboxMode: sandboxMode });
options.auth = {
username: emailApiCredentials.apiKey,
password: emailApiCredentials.secretKey,
};
} else {
const smsApiCredentials = await this.getCredentials('mailjetSmsApi');
options.headers!['Authorization'] = `Bearer ${smsApiCredentials!.token}`;
@@ -65,6 +71,47 @@ export async function mailjetApiRequestAllItems(this: IExecuteFunctions | IHookF
return returnData;
}
export async function validateCredentials(
this: ICredentialTestFunctions,
decryptedCredentials: ICredentialDataDecryptedObject,
): Promise<any> { // tslint:disable-line:no-any
const credentials = decryptedCredentials;
const {
apiKey,
secretKey,
} = credentials as {
apiKey: string,
secretKey: string,
};
const options: OptionsWithUri = {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
auth: {
username: apiKey,
password: secretKey,
},
method: 'GET',
uri: `https://api.mailjet.com/v3/REST/template`,
json: true,
};
return await this.helpers.request(options);
}
export function validateJSON(json: string | undefined): IDataObject | undefined { // tslint:disable-line:no-any
let result;
try {
result = JSON.parse(json!);
} catch (exception) {
result = undefined;
}
return result;
}
export interface IMessage {
From?: { Email?: string, Name?: string };
Subject?: string;