feat(Gotify Node): Update credentials to use new style

This commit is contained in:
Jonathan Bennetts 2024-11-07 11:42:47 +00:00
parent 20fd38f351
commit 05e529b423
No known key found for this signature in database
3 changed files with 62 additions and 41 deletions

View file

@ -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 { export class GotifyApi implements ICredentialType {
name = 'gotifyApi'; name = 'gotifyApi';
@ -43,4 +49,27 @@ export class GotifyApi implements ICredentialType {
description: 'Whether to connect even if SSL certificate validation is not possible', description: 'Whether to connect even if SSL certificate validation is not possible',
}, },
]; ];
async authenticate(
credentials: ICredentialDataDecryptedObject,
requestOptions: IHttpRequestOptions,
): Promise<IHttpRequestOptions> {
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}}',
},
};
} }

View file

@ -1,55 +1,46 @@
import type { import type {
IExecuteFunctions, IExecuteFunctions,
ILoadOptionsFunctions,
IDataObject, IDataObject,
JsonObject, JsonObject,
IHttpRequestMethods, IHttpRequestMethods,
IRequestOptions, IHttpRequestOptions,
} from 'n8n-workflow'; } from 'n8n-workflow';
import { NodeApiError } from 'n8n-workflow'; import { NodeApiError } from 'n8n-workflow';
export async function gotifyApiRequest( export async function gotifyApiRequest(
this: IExecuteFunctions | ILoadOptionsFunctions, context: IExecuteFunctions,
method: IHttpRequestMethods, method: IHttpRequestMethods,
path: string, path: string,
body: any = {}, body: any = {},
qs: IDataObject = {}, qs: IDataObject = {},
uri?: string | undefined, uri?: string | undefined,
_option = {}, _option = {},
): Promise<any> { ): Promise<any> {
const credentials = await this.getCredentials('gotifyApi'); const credentials = await context.getCredentials('gotifyApi');
const options: IRequestOptions = { const options: IHttpRequestOptions = {
method, method,
headers: {
'X-Gotify-Key': method === 'POST' ? credentials.appApiToken : credentials.clientApiToken,
accept: 'application/json',
},
body, body,
qs, qs,
uri: uri || `${credentials.url}${path}`, url: uri ?? `${credentials.url}${path}`,
json: true, json: true,
rejectUnauthorized: !credentials.ignoreSSLIssues as boolean, skipSslCertificateValidation: credentials.ignoreSSLIssues as boolean,
}; };
try { try {
if (Object.keys(body as IDataObject).length === 0) { if (Object.keys(body as IDataObject).length === 0) {
delete options.body; delete options.body;
} }
return await context.helpers.httpRequestWithAuthentication.call(context, 'gotifyApi', options);
//@ts-ignore
return await this.helpers.request.call(this, options);
} catch (error) { } catch (error) {
throw new NodeApiError(this.getNode(), error as JsonObject); throw new NodeApiError(context.getNode(), error as JsonObject);
} }
} }
export async function gotifyApiRequestAllItems( export async function gotifyApiRequestAllItems(
this: IExecuteFunctions | ILoadOptionsFunctions, context: IExecuteFunctions,
propertyName: string, propertyName: string,
method: IHttpRequestMethods, method: IHttpRequestMethods,
endpoint: string, endpoint: string,
body: any = {}, body: any = {},
query: IDataObject = {}, query: IDataObject = {},
): Promise<any> { ): Promise<any> {
@ -59,7 +50,7 @@ export async function gotifyApiRequestAllItems(
let uri: string | undefined; let uri: string | undefined;
query.limit = 100; query.limit = 100;
do { do {
responseData = await gotifyApiRequest.call(this, method, endpoint, body, query, uri); responseData = await gotifyApiRequest(context, method, endpoint, body, query, uri);
if (responseData.paging.next) { if (responseData.paging.next) {
uri = responseData.paging.next; uri = responseData.paging.next;
} }

View file

@ -2,14 +2,13 @@ import type {
IExecuteFunctions, IExecuteFunctions,
IDataObject, IDataObject,
INodeExecutionData, INodeExecutionData,
INodeType,
INodeTypeDescription, INodeTypeDescription,
} from 'n8n-workflow'; } from 'n8n-workflow';
import { NodeConnectionType } from 'n8n-workflow'; import { Node, NodeConnectionType } from 'n8n-workflow';
import { gotifyApiRequest, gotifyApiRequestAllItems } from './GenericFunctions'; import { gotifyApiRequest, gotifyApiRequestAllItems } from './GenericFunctions';
export class Gotify implements INodeType { export class Gotify extends Node {
description: INodeTypeDescription = { description: INodeTypeDescription = {
displayName: 'Gotify', displayName: 'Gotify',
name: 'gotify', name: 'gotify',
@ -194,22 +193,22 @@ export class Gotify implements INodeType {
], ],
}; };
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> { async execute(context: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData(); const items = context.getInputData();
const returnData: INodeExecutionData[] = []; const returnData: INodeExecutionData[] = [];
const length = items.length; const length = items.length;
const qs: IDataObject = {}; const qs: IDataObject = {};
let responseData; let responseData;
const resource = this.getNodeParameter('resource', 0); const resource = context.getNodeParameter('resource', 0);
const operation = this.getNodeParameter('operation', 0); const operation = context.getNodeParameter('operation', 0);
for (let i = 0; i < length; i++) { for (let i = 0; i < length; i++) {
try { try {
if (resource === 'message') { if (resource === 'message') {
if (operation === 'create') { 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 additionalFields = context.getNodeParameter('additionalFields', i);
const options = this.getNodeParameter('options', i); const options = context.getNodeParameter('options', i);
const body: IDataObject = { const body: IDataObject = {
message, message,
@ -225,21 +224,23 @@ export class Gotify implements INodeType {
Object.assign(body, additionalFields); 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') { 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 }; responseData = { success: true };
} }
if (operation === 'getAll') { if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i); const returnAll = context.getNodeParameter('returnAll', i);
if (returnAll) { if (returnAll) {
responseData = await gotifyApiRequestAllItems.call( responseData = await gotifyApiRequestAllItems(
this, context,
'messages', 'messages',
'GET', 'GET',
'/message', '/message',
@ -247,20 +248,20 @@ export class Gotify implements INodeType {
qs, qs,
); );
} else { } else {
qs.limit = this.getNodeParameter('limit', i); qs.limit = context.getNodeParameter('limit', i);
responseData = await gotifyApiRequest.call(this, 'GET', '/message', {}, qs); responseData = await gotifyApiRequest(context, 'GET', '/message', {}, qs);
responseData = responseData.messages; responseData = responseData.messages;
} }
} }
} }
const executionData = this.helpers.constructExecutionMetaData( const executionData = context.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(responseData as IDataObject[]), context.helpers.returnJsonArray(responseData as IDataObject[]),
{ itemData: { item: i } }, { itemData: { item: i } },
); );
returnData.push(...executionData); returnData.push(...executionData);
} catch (error) { } catch (error) {
if (this.continueOnFail()) { if (context.continueOnFail()) {
returnData.push({ json: { error: error.message } }); returnData.push({ json: { error: error.message } });
continue; continue;
} }