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 {
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<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 {
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<any> {
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<any> {
@ -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;
}

View file

@ -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<INodeExecutionData[][]> {
const items = this.getInputData();
async execute(context: IExecuteFunctions): Promise<INodeExecutionData[][]> {
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;
}