1
1
mirror of https://github.com/n8n-io/n8n.git synced 2024-10-07 10:07:19 +03:00

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 {
IAuthenticateBasicAuth,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} 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',
},
];
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 {
IAuthenticateHeaderAuth,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
@ -15,4 +17,18 @@ export class MailjetSmsApi implements ICredentialType {
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,
} from 'n8n-workflow';
@ -7,6 +7,7 @@ export const emailOperations: INodeProperties[] = [
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: [
@ -443,11 +444,10 @@ export const emailFields: INodeProperties[] = [
default: '',
},
{
displayName: 'Track Opens',
name: 'trackOpens',
type: 'string',
description: 'Enable or disable open tracking on this message',
default: '',
displayName: 'Template Language',
name: 'templateLanguage',
type: 'boolean',
default: false,
},
{
displayName: 'Track Clicks',
@ -457,10 +457,11 @@ export const emailFields: INodeProperties[] = [
default: '',
},
{
displayName: 'Template Language',
name: 'templateLanguage',
type: 'boolean',
default: false,
displayName: 'Track Opens',
name: 'trackOpens',
type: 'string',
description: 'Enable or disable open tracking on this message',
default: '',
},
],
},

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 {

View File

@ -3,12 +3,8 @@ import {
} from 'n8n-core';
import {
ICredentialDataDecryptedObject,
ICredentialsDecrypted,
ICredentialTestFunctions,
IDataObject,
ILoadOptionsFunctions,
INodeCredentialTestResult,
INodeExecutionData,
INodePropertyOptions,
INodeType,
@ -20,7 +16,6 @@ import {
import {
IMessage,
mailjetApiRequest,
validateCredentials,
validateJSON,
} from './GenericFunctions';
@ -51,7 +46,6 @@ export class Mailjet implements INodeType {
{
name: 'mailjetEmailApi',
required: true,
testedBy: 'mailjetEmailApiTest',
displayOptions: {
show: {
resource: [
@ -77,6 +71,7 @@ export class Mailjet implements INodeType {
displayName: 'Resource',
name: 'resource',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Email',
@ -88,7 +83,7 @@ export class Mailjet implements INodeType {
},
],
default: 'email',
description: 'Resource to consume.',
description: 'Resource to consume',
},
...emailOperations,
...emailFields,
@ -98,25 +93,6 @@ export class Mailjet implements INodeType {
};
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: {
// Get all the available custom fields to display them to user so that he can
// select them easily
@ -273,7 +249,7 @@ export class Mailjet implements INodeType {
body.Variables![variable.name as string] = variable.value;
}
}
if (additionalFields.bccEmail) {
const bccEmail = (additionalFields.bccEmail as string).split(',') as string[];
for (const email of bccEmail) {

View File

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

View File

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