1
1
mirror of https://github.com/n8n-io/n8n.git synced 2024-11-11 12:26:30 +03:00

🔀 Merge branch 'RicardoE105-feature/freshdesk'

This commit is contained in:
Jan Oberhauser 2020-02-21 17:50:39 +01:00
commit 5130895a8a
3 changed files with 902 additions and 305 deletions

View File

@ -18,7 +18,8 @@ export class FreshdeskApi implements ICredentialType {
displayName: 'Domain',
name: 'domain',
type: 'string' as NodePropertyTypes,
placeholder: 'https://domain.freshdesk.com',
placeholder: 'company',
description: 'If the URL you get displayed on Freshdesk is "https://company.freshdesk.com" enter "company"',
default: ''
}
];

File diff suppressed because it is too large Load Diff

View File

@ -2,15 +2,13 @@ import { OptionsWithUri } from 'request';
import {
IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions,
IExecuteSingleFunctions,
BINARY_ENCODING
} from 'n8n-core';
import * as _ from 'lodash';
import { IDataObject } from 'n8n-workflow';
export async function freshdeskApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, resource: string, method: string, body: any = {}, headers?: object): Promise<any> { // tslint:disable-line:no-any
export async function freshdeskApiRequest(this: IExecuteFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, query: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('freshdeskApi');
@ -20,36 +18,61 @@ export async function freshdeskApiRequest(this: IHookFunctions | IExecuteFunctio
const apiKey = `${credentials.apiKey}:X`;
const headerWithAuthentication = Object.assign({}, headers, { Authorization: `${Buffer.from(apiKey).toString(BINARY_ENCODING)}` });
const endpoint = 'freshdesk.com/api/v2';
const endpoint = 'freshdesk.com/api/v2/';
const options: OptionsWithUri = {
headers: headerWithAuthentication,
let options: OptionsWithUri = {
headers: {
'Content-Type': 'application/json',
Authorization: `${Buffer.from(apiKey).toString(BINARY_ENCODING)}`,
},
method,
body,
uri: `https://${credentials.domain}.${endpoint}${resource}`,
qs: query,
uri: uri || `https://${credentials.domain}.${endpoint}${resource}`,
json: true
};
if (_.isEmpty(options.body)) {
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) {
console.error(error);
const errorMessage = error.response.body.message || error.response.body.Message;
if (errorMessage !== undefined) {
throw errorMessage;
if (error.response) {
let errorMessage = error.response.body.message || error.response.body.description || error.message;
if (error.response.body && error.response.body.errors) {
errorMessage = error.response.body.errors.map((err: IDataObject) => `"${err.field}" => ${err.message}`).join(', ');
}
throw new Error(`Freshdesk error response [${error.statusCode}]: ${errorMessage}`);
}
throw error.response.body;
throw error;
}
}
export async function freshdeskApiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
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'] !== undefined &&
responseData.headers['link'].includes('rel="next"')
);
return returnData;
}
export function validateJSON(json: string | undefined): any { // tslint:disable-line:no-any
let result;
try {