diff --git a/packages/nodes-base/credentials/MailjetEmailApi.credentials.ts b/packages/nodes-base/credentials/MailjetEmailApi.credentials.ts index a6b5888d86..0c29f0bd18 100644 --- a/packages/nodes-base/credentials/MailjetEmailApi.credentials.ts +++ b/packages/nodes-base/credentials/MailjetEmailApi.credentials.ts @@ -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', + }, + }; } diff --git a/packages/nodes-base/credentials/MailjetSmsApi.credentials.ts b/packages/nodes-base/credentials/MailjetSmsApi.credentials.ts index 051173e783..9ad4cf94ae 100644 --- a/packages/nodes-base/credentials/MailjetSmsApi.credentials.ts +++ b/packages/nodes-base/credentials/MailjetSmsApi.credentials.ts @@ -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', + }, + }; } diff --git a/packages/nodes-base/nodes/Mailjet/EmailDescription.ts b/packages/nodes-base/nodes/Mailjet/EmailDescription.ts index cc887b162e..a14e8ce27c 100644 --- a/packages/nodes-base/nodes/Mailjet/EmailDescription.ts +++ b/packages/nodes-base/nodes/Mailjet/EmailDescription.ts @@ -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: '', }, ], }, diff --git a/packages/nodes-base/nodes/Mailjet/GenericFunctions.ts b/packages/nodes-base/nodes/Mailjet/GenericFunctions.ts index 7807eda359..c8f861c141 100644 --- a/packages/nodes-base/nodes/Mailjet/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Mailjet/GenericFunctions.ts @@ -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 { // 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 { // 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 { // 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 { diff --git a/packages/nodes-base/nodes/Mailjet/Mailjet.node.ts b/packages/nodes-base/nodes/Mailjet/Mailjet.node.ts index 37eec5828b..bb9a7bb5f4 100644 --- a/packages/nodes-base/nodes/Mailjet/Mailjet.node.ts +++ b/packages/nodes-base/nodes/Mailjet/Mailjet.node.ts @@ -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 { - 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) { diff --git a/packages/nodes-base/nodes/Mailjet/MailjetTrigger.node.ts b/packages/nodes-base/nodes/Mailjet/MailjetTrigger.node.ts index c352ca731f..c25bca6889 100644 --- a/packages/nodes-base/nodes/Mailjet/MailjetTrigger.node.ts +++ b/packages/nodes-base/nodes/Mailjet/MailjetTrigger.node.ts @@ -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', diff --git a/packages/nodes-base/nodes/Mailjet/SmsDescription.ts b/packages/nodes-base/nodes/Mailjet/SmsDescription.ts index 930b65ee41..545c71248c 100644 --- a/packages/nodes-base/nodes/Mailjet/SmsDescription.ts +++ b/packages/nodes-base/nodes/Mailjet/SmsDescription.ts @@ -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.', }, ];