mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
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:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user