From 05e529b423979cfa0671f32907cce45db9045c9f Mon Sep 17 00:00:00 2001 From: Jonathan Bennetts Date: Thu, 7 Nov 2024 11:42:47 +0000 Subject: [PATCH] feat(Gotify Node): Update credentials to use new style --- .../credentials/GotifyApi.credentials.ts | 31 ++++++++++++- .../nodes/Gotify/GenericFunctions.ts | 29 +++++-------- .../nodes-base/nodes/Gotify/Gotify.node.ts | 43 ++++++++++--------- 3 files changed, 62 insertions(+), 41 deletions(-) diff --git a/packages/nodes-base/credentials/GotifyApi.credentials.ts b/packages/nodes-base/credentials/GotifyApi.credentials.ts index 6d86c0872d..4d4acc189e 100644 --- a/packages/nodes-base/credentials/GotifyApi.credentials.ts +++ b/packages/nodes-base/credentials/GotifyApi.credentials.ts @@ -1,4 +1,10 @@ -import type { ICredentialType, INodeProperties } from 'n8n-workflow'; +import type { + ICredentialDataDecryptedObject, + ICredentialTestRequest, + ICredentialType, + IHttpRequestOptions, + INodeProperties, +} from 'n8n-workflow'; export class GotifyApi implements ICredentialType { name = 'gotifyApi'; @@ -43,4 +49,27 @@ export class GotifyApi implements ICredentialType { description: 'Whether to connect even if SSL certificate validation is not possible', }, ]; + + async authenticate( + credentials: ICredentialDataDecryptedObject, + requestOptions: IHttpRequestOptions, + ): Promise { + const { appApiToken, clientApiToken } = credentials as { + appApiToken: string; + clientApiToken: string; + }; + requestOptions.headers = { + 'X-Gotify-Key': requestOptions.method === 'POST' ? appApiToken : clientApiToken, + accept: 'application/json', + }; + return requestOptions; + } + + test: ICredentialTestRequest = { + request: { + baseURL: '={{$credentials.url.replace(new RegExp("/$"), "") }}', + url: '/current/user', + skipSslCertificateValidation: '={{$credentials.ignoreSSLIssues}}', + }, + }; } diff --git a/packages/nodes-base/nodes/Gotify/GenericFunctions.ts b/packages/nodes-base/nodes/Gotify/GenericFunctions.ts index 3b63797197..4da52806bc 100644 --- a/packages/nodes-base/nodes/Gotify/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Gotify/GenericFunctions.ts @@ -1,55 +1,46 @@ import type { IExecuteFunctions, - ILoadOptionsFunctions, IDataObject, JsonObject, IHttpRequestMethods, - IRequestOptions, + IHttpRequestOptions, } from 'n8n-workflow'; import { NodeApiError } from 'n8n-workflow'; export async function gotifyApiRequest( - this: IExecuteFunctions | ILoadOptionsFunctions, + context: IExecuteFunctions, method: IHttpRequestMethods, path: string, - body: any = {}, qs: IDataObject = {}, uri?: string | undefined, _option = {}, ): Promise { - const credentials = await this.getCredentials('gotifyApi'); + const credentials = await context.getCredentials('gotifyApi'); - const options: IRequestOptions = { + const options: IHttpRequestOptions = { method, - headers: { - 'X-Gotify-Key': method === 'POST' ? credentials.appApiToken : credentials.clientApiToken, - accept: 'application/json', - }, body, qs, - uri: uri || `${credentials.url}${path}`, + url: uri ?? `${credentials.url}${path}`, json: true, - rejectUnauthorized: !credentials.ignoreSSLIssues as boolean, + skipSslCertificateValidation: credentials.ignoreSSLIssues as boolean, }; try { if (Object.keys(body as IDataObject).length === 0) { delete options.body; } - - //@ts-ignore - return await this.helpers.request.call(this, options); + return await context.helpers.httpRequestWithAuthentication.call(context, 'gotifyApi', options); } catch (error) { - throw new NodeApiError(this.getNode(), error as JsonObject); + throw new NodeApiError(context.getNode(), error as JsonObject); } } export async function gotifyApiRequestAllItems( - this: IExecuteFunctions | ILoadOptionsFunctions, + context: IExecuteFunctions, propertyName: string, method: IHttpRequestMethods, endpoint: string, - body: any = {}, query: IDataObject = {}, ): Promise { @@ -59,7 +50,7 @@ export async function gotifyApiRequestAllItems( let uri: string | undefined; query.limit = 100; do { - responseData = await gotifyApiRequest.call(this, method, endpoint, body, query, uri); + responseData = await gotifyApiRequest(context, method, endpoint, body, query, uri); if (responseData.paging.next) { uri = responseData.paging.next; } diff --git a/packages/nodes-base/nodes/Gotify/Gotify.node.ts b/packages/nodes-base/nodes/Gotify/Gotify.node.ts index aba203c5de..0c1059ca9c 100644 --- a/packages/nodes-base/nodes/Gotify/Gotify.node.ts +++ b/packages/nodes-base/nodes/Gotify/Gotify.node.ts @@ -2,14 +2,13 @@ import type { IExecuteFunctions, IDataObject, INodeExecutionData, - INodeType, INodeTypeDescription, } from 'n8n-workflow'; -import { NodeConnectionType } from 'n8n-workflow'; +import { Node, NodeConnectionType } from 'n8n-workflow'; import { gotifyApiRequest, gotifyApiRequestAllItems } from './GenericFunctions'; -export class Gotify implements INodeType { +export class Gotify extends Node { description: INodeTypeDescription = { displayName: 'Gotify', name: 'gotify', @@ -194,22 +193,22 @@ export class Gotify implements INodeType { ], }; - async execute(this: IExecuteFunctions): Promise { - const items = this.getInputData(); + async execute(context: IExecuteFunctions): Promise { + const items = context.getInputData(); const returnData: INodeExecutionData[] = []; const length = items.length; const qs: IDataObject = {}; let responseData; - const resource = this.getNodeParameter('resource', 0); - const operation = this.getNodeParameter('operation', 0); + const resource = context.getNodeParameter('resource', 0); + const operation = context.getNodeParameter('operation', 0); for (let i = 0; i < length; i++) { try { if (resource === 'message') { if (operation === 'create') { - const message = this.getNodeParameter('message', i) as string; + const message = context.getNodeParameter('message', i) as string; - const additionalFields = this.getNodeParameter('additionalFields', i); - const options = this.getNodeParameter('options', i); + const additionalFields = context.getNodeParameter('additionalFields', i); + const options = context.getNodeParameter('options', i); const body: IDataObject = { message, @@ -225,21 +224,23 @@ export class Gotify implements INodeType { Object.assign(body, additionalFields); - responseData = await gotifyApiRequest.call(this, 'POST', '/message', body); + responseData = await gotifyApiRequest(context, 'POST', '/message', body); + + responseData = await gotifyApiRequest(context, 'POST', '/message', body); } if (operation === 'delete') { - const messageId = this.getNodeParameter('messageId', i) as string; + const messageId = context.getNodeParameter('messageId', i) as string; - responseData = await gotifyApiRequest.call(this, 'DELETE', `/message/${messageId}`); + responseData = await gotifyApiRequest(context, 'DELETE', `/message/${messageId}`); responseData = { success: true }; } if (operation === 'getAll') { - const returnAll = this.getNodeParameter('returnAll', i); + const returnAll = context.getNodeParameter('returnAll', i); if (returnAll) { - responseData = await gotifyApiRequestAllItems.call( - this, + responseData = await gotifyApiRequestAllItems( + context, 'messages', 'GET', '/message', @@ -247,20 +248,20 @@ export class Gotify implements INodeType { qs, ); } else { - qs.limit = this.getNodeParameter('limit', i); - responseData = await gotifyApiRequest.call(this, 'GET', '/message', {}, qs); + qs.limit = context.getNodeParameter('limit', i); + responseData = await gotifyApiRequest(context, 'GET', '/message', {}, qs); responseData = responseData.messages; } } } - const executionData = this.helpers.constructExecutionMetaData( - this.helpers.returnJsonArray(responseData as IDataObject[]), + const executionData = context.helpers.constructExecutionMetaData( + context.helpers.returnJsonArray(responseData as IDataObject[]), { itemData: { item: i } }, ); returnData.push(...executionData); } catch (error) { - if (this.continueOnFail()) { + if (context.continueOnFail()) { returnData.push({ json: { error: error.message } }); continue; }