Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

n8n-3563-node-fix-mailjet-trigger #3281

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/nodes-base/credentials/MailjetEmailApi.credentials.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {
IAuthenticateBasicAuth,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
Expand Down Expand Up @@ -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',
},
};
}
16 changes: 16 additions & 0 deletions packages/nodes-base/credentials/MailjetSmsApi.credentials.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {
IAuthenticateHeaderAuth,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
Expand All @@ -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',
},
};
}
21 changes: 11 additions & 10 deletions packages/nodes-base/nodes/Mailjet/EmailDescription.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
import {
INodeProperties,
} from 'n8n-workflow';

Expand All @@ -7,6 +7,7 @@ export const emailOperations: INodeProperties[] = [
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: [
Expand Down Expand Up @@ -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',
Expand All @@ -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: '',
},
],
},
Expand Down
70 changes: 22 additions & 48 deletions packages/nodes-base/nodes/Mailjet/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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);
}
Expand All @@ -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 {
Expand Down
30 changes: 3 additions & 27 deletions packages/nodes-base/nodes/Mailjet/Mailjet.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ import {
} from 'n8n-core';

import {
ICredentialDataDecryptedObject,
ICredentialsDecrypted,
ICredentialTestFunctions,
IDataObject,
ILoadOptionsFunctions,
INodeCredentialTestResult,
INodeExecutionData,
INodePropertyOptions,
INodeType,
Expand All @@ -20,7 +16,6 @@ import {
import {
IMessage,
mailjetApiRequest,
validateCredentials,
validateJSON,
} from './GenericFunctions';

Expand Down Expand Up @@ -51,7 +46,6 @@ export class Mailjet implements INodeType {
{
name: 'mailjetEmailApi',
required: true,
testedBy: 'mailjetEmailApiTest',
displayOptions: {
show: {
resource: [
Expand All @@ -77,6 +71,7 @@ export class Mailjet implements INodeType {
displayName: 'Resource',
name: 'resource',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Email',
Expand All @@ -88,7 +83,7 @@ export class Mailjet implements INodeType {
},
],
default: 'email',
description: 'Resource to consume.',
description: 'Resource to consume',
},
...emailOperations,
...emailFields,
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions packages/nodes-base/nodes/Mailjet/MailjetTrigger.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion packages/nodes-base/nodes/Mailjet/SmsDescription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const smsOperations: INodeProperties[] = [
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: [
Expand All @@ -20,7 +21,6 @@ export const smsOperations: INodeProperties[] = [
},
],
default: 'send',
description: 'The operation to perform.',
},
];

Expand Down