n8n/packages/nodes-base/nodes/Freshdesk/GenericFunctions.ts
Michael Kret 61e26804ba
refactor(core): Remove linting exceptions in nodes-base (#4794)
*  enabled array-type

*  await-thenable on

*  ban-types on

*  default-param-last on

*  dot-notation on

*  member-delimiter-style on

*  no-duplicate-imports on

*  no-empty-interface on

*  no-floating-promises on

*  no-for-in-array on

*  no-invalid-void-type on

*  no-loop-func on

*  no-shadow on

*  ban-ts-comment re enabled

*  @typescript-eslint/lines-between-class-members on

* address my own comment

* @typescript-eslint/return-await on

* @typescript-eslint/promise-function-async on

* @typescript-eslint/no-unnecessary-boolean-literal-compare on

* @typescript-eslint/no-unnecessary-type-assertion on

* prefer-const on

* @typescript-eslint/prefer-optional-chain on

Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
2022-12-02 21:54:28 +01:00

87 lines
2.1 KiB
TypeScript

import { OptionsWithUri } from 'request';
import { BINARY_ENCODING, IExecuteFunctions, ILoadOptionsFunctions } from 'n8n-core';
import { IDataObject, NodeApiError } from 'n8n-workflow';
export async function freshdeskApiRequest(
this: IExecuteFunctions | ILoadOptionsFunctions,
method: string,
resource: string,
body: any = {},
query: IDataObject = {},
uri?: string,
option: IDataObject = {},
) {
const credentials = await this.getCredentials('freshdeskApi');
const apiKey = `${credentials.apiKey}:X`;
const endpoint = 'freshdesk.com/api/v2';
let options: OptionsWithUri = {
headers: {
'Content-Type': 'application/json',
Authorization: `${Buffer.from(apiKey).toString(BINARY_ENCODING)}`,
},
method,
body,
qs: query,
uri: uri || `https://${credentials.domain}.${endpoint}${resource}`,
json: true,
};
if (!Object.keys(body).length) {
delete options.body;
}
if (!Object.keys(query).length) {
delete options.qs;
}
options = Object.assign({}, options, option);
try {
return await this.helpers.request!(options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
}
export async function freshdeskApiRequestAllItems(
this: IExecuteFunctions | ILoadOptionsFunctions,
method: string,
endpoint: string,
body: any = {},
query: IDataObject = {},
) {
const returnData: IDataObject[] = [];
let responseData;
let uri: string | undefined;
query.per_page = 100;
do {
responseData = await freshdeskApiRequest.call(this, method, endpoint, body, query, uri, {
resolveWithFullResponse: true,
});
if (responseData.headers.link) {
uri = responseData.headers.link.split(';')[0].replace('<', '').replace('>', '');
}
returnData.push.apply(returnData, responseData.body);
} while (responseData.headers.link?.includes('rel="next"'));
return returnData;
}
export function validateJSON(json: string | undefined): any {
let result;
try {
result = JSON.parse(json!);
} catch (exception) {
result = [];
}
return result;
}
export function capitalize(s: string): string {
if (typeof s !== 'string') return '';
return s.charAt(0).toUpperCase() + s.slice(1);
}