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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 73 additions and 90 deletions

View file

@ -1,4 +1,6 @@
import { import {
IAuthenticateBasicAuth,
ICredentialTestRequest,
ICredentialType, ICredentialType,
INodeProperties, INodeProperties,
} from 'n8n-workflow'; } from 'n8n-workflow';
@ -28,4 +30,18 @@ export class MailjetEmailApi implements ICredentialType {
description: 'Whether to allow to run the API call in a Sandbox mode, where all validations of the payload will be done without delivering the message', description: 'Whether to allow to run the API call in a Sandbox mode, where all validations of the payload will be done without delivering the message',
}, },
]; ];
authenticate: IAuthenticateBasicAuth = {
type: 'basicAuth',
properties: {
userPropertyName: 'apiKey',
passwordPropertyName: 'secretKey',
},
};
test: ICredentialTestRequest = {
request: {
baseURL: `https://api.mailjet.com`,
url: '/v3/REST/template',
method: 'GET',
},
};
} }

View file

@ -1,4 +1,6 @@
import { import {
IAuthenticateHeaderAuth,
ICredentialTestRequest,
ICredentialType, ICredentialType,
INodeProperties, INodeProperties,
} from 'n8n-workflow'; } from 'n8n-workflow';
@ -15,4 +17,18 @@ export class MailjetSmsApi implements ICredentialType {
default: '', default: '',
}, },
]; ];
authenticate: IAuthenticateHeaderAuth = {
type: 'headerAuth',
properties: {
name: 'Authorization',
value: '=Bearer {{$credentials.token}}',
},
};
test: ICredentialTestRequest = {
request: {
baseURL: `https://api.mailjet.com`,
url: '/v4/sms',
method: 'GET',
},
};
} }

View file

@ -1,4 +1,4 @@
import { import {
INodeProperties, INodeProperties,
} from 'n8n-workflow'; } from 'n8n-workflow';
@ -7,6 +7,7 @@ export const emailOperations: INodeProperties[] = [
displayName: 'Operation', displayName: 'Operation',
name: 'operation', name: 'operation',
type: 'options', type: 'options',
noDataExpression: true,
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
@ -443,11 +444,10 @@ export const emailFields: INodeProperties[] = [
default: '', default: '',
}, },
{ {
displayName: 'Track Opens', displayName: 'Template Language',
name: 'trackOpens', name: 'templateLanguage',
type: 'string', type: 'boolean',
description: 'Enable or disable open tracking on this message', default: false,
default: '',
}, },
{ {
displayName: 'Track Clicks', displayName: 'Track Clicks',
@ -457,10 +457,11 @@ export const emailFields: INodeProperties[] = [
default: '', default: '',
}, },
{ {
displayName: 'Template Language', displayName: 'Track Opens',
name: 'templateLanguage', name: 'trackOpens',
type: 'boolean', type: 'string',
default: false, description: 'Enable or disable open tracking on this message',
default: '',
}, },
], ],
}, },

View file

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

View file

@ -3,12 +3,8 @@ import {
} from 'n8n-core'; } from 'n8n-core';
import { import {
ICredentialDataDecryptedObject,
ICredentialsDecrypted,
ICredentialTestFunctions,
IDataObject, IDataObject,
ILoadOptionsFunctions, ILoadOptionsFunctions,
INodeCredentialTestResult,
INodeExecutionData, INodeExecutionData,
INodePropertyOptions, INodePropertyOptions,
INodeType, INodeType,
@ -20,7 +16,6 @@ import {
import { import {
IMessage, IMessage,
mailjetApiRequest, mailjetApiRequest,
validateCredentials,
validateJSON, validateJSON,
} from './GenericFunctions'; } from './GenericFunctions';
@ -51,7 +46,6 @@ export class Mailjet implements INodeType {
{ {
name: 'mailjetEmailApi', name: 'mailjetEmailApi',
required: true, required: true,
testedBy: 'mailjetEmailApiTest',
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
@ -77,6 +71,7 @@ export class Mailjet implements INodeType {
displayName: 'Resource', displayName: 'Resource',
name: 'resource', name: 'resource',
type: 'options', type: 'options',
noDataExpression: true,
options: [ options: [
{ {
name: 'Email', name: 'Email',
@ -88,7 +83,7 @@ export class Mailjet implements INodeType {
}, },
], ],
default: 'email', default: 'email',
description: 'Resource to consume.', description: 'Resource to consume',
}, },
...emailOperations, ...emailOperations,
...emailFields, ...emailFields,
@ -98,25 +93,6 @@ export class Mailjet implements INodeType {
}; };
methods = { methods = {
credentialTest: {
async mailjetEmailApiTest(this: ICredentialTestFunctions, credential: ICredentialsDecrypted): Promise<INodeCredentialTestResult> {
try {
await validateCredentials.call(this, credential.data as ICredentialDataDecryptedObject);
} catch (error) {
const err = error as JsonObject;
if (err.statusCode === 401) {
return {
status: 'Error',
message: `Invalid credentials`,
};
}
}
return {
status: 'OK',
message: 'Authentication successful',
};
},
},
loadOptions: { loadOptions: {
// Get all the available custom fields to display them to user so that he can // Get all the available custom fields to display them to user so that he can
// select them easily // select them easily
@ -273,7 +249,7 @@ export class Mailjet implements INodeType {
body.Variables![variable.name as string] = variable.value; body.Variables![variable.name as string] = variable.value;
} }
} }
if (additionalFields.bccEmail) { if (additionalFields.bccEmail) {
const bccEmail = (additionalFields.bccEmail as string).split(',') as string[]; const bccEmail = (additionalFields.bccEmail as string).split(',') as string[];
for (const email of bccEmail) { for (const email of bccEmail) {

View file

@ -49,14 +49,14 @@ export class MailjetTrigger implements INodeType {
required: true, required: true,
default: 'open', default: 'open',
options: [ options: [
{
name: 'email.bounce',
value: 'bounce',
},
{ {
name: 'email.blocked', name: 'email.blocked',
value: 'blocked', value: 'blocked',
}, },
{
name: 'email.bounce',
value: 'bounce',
},
{ {
name: 'email.open', name: 'email.open',
value: 'open', value: 'open',

View file

@ -5,6 +5,7 @@ export const smsOperations: INodeProperties[] = [
displayName: 'Operation', displayName: 'Operation',
name: 'operation', name: 'operation',
type: 'options', type: 'options',
noDataExpression: true,
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
@ -20,7 +21,6 @@ export const smsOperations: INodeProperties[] = [
}, },
], ],
default: 'send', default: 'send',
description: 'The operation to perform.',
}, },
]; ];