fix(Mailjet Trigger Node): Fix issue that node could not get activated (#3281)

* 🔨 fix and clean up

*  Improvements

Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
This commit is contained in:
Michael Kret
2022-05-15 21:39:54 +03:00
committed by GitHub
parent 50246d174a
commit e09e349fed
7 changed files with 73 additions and 90 deletions

View File

@@ -9,16 +9,31 @@ import {
} from 'n8n-core';
import {
ICredentialDataDecryptedObject,
ICredentialTestFunctions,
IDataObject,
IHookFunctions,
JsonObject,
NodeApiError,
} 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') as { apiKey: string, secretKey: string, sandboxMode: boolean };
export async function mailjetApiRequest(this: IExecuteFunctions | IExecuteSingleFunctions | IHookFunctions | ILoadOptionsFunctions, method: string, path: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const resource = this.getNodeParameter('resource', 0) as string;
let credentialType;
if (resource === 'email' || this.getNode().type.includes('Trigger')) {
credentialType = 'mailjetEmailApi';
const { sandboxMode } = await this.getCredentials('mailjetEmailApi') as
{ sandboxMode: boolean };
if (!this.getNode().type.includes('Trigger')) {
Object.assign(body, { SandboxMode: sandboxMode });
}
} else {
credentialType = 'mailjetSmsApi';
}
let options: OptionsWithUri = {
headers: {
Accept: 'application/json',
@@ -27,26 +42,16 @@ export async function mailjetApiRequest(this: IExecuteFunctions | IExecuteSingle
method,
qs,
body,
uri: uri || `https://api.mailjet.com${resource}`,
uri: uri || `https://api.mailjet.com${path}`,
json: true,
};
options = Object.assign({}, options, option);
if (Object.keys(options.body).length === 0) {
delete options.body;
}
if (emailApiCredentials !== undefined) {
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}`;
}
try {
return await this.helpers.request!(options);
return await this.helpers.requestWithAuthentication.call(this, credentialType, options);
} catch (error) {
throw new NodeApiError(this.getNode(), error as JsonObject);
}
@@ -71,37 +76,6 @@ 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 {