Added missing crud resources

This commit is contained in:
Ricardo Espinoza 2020-02-18 14:03:48 -05:00
parent 284a6099f2
commit 4c7da89a69
2 changed files with 840 additions and 214 deletions

File diff suppressed because it is too large Load diff

View file

@ -2,15 +2,13 @@ import { OptionsWithUri } from 'request';
import { import {
IExecuteFunctions, IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions, ILoadOptionsFunctions,
IExecuteSingleFunctions,
BINARY_ENCODING BINARY_ENCODING
} from 'n8n-core'; } 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'); const credentials = this.getCredentials('freshdeskApi');
@ -20,36 +18,58 @@ export async function freshdeskApiRequest(this: IHookFunctions | IExecuteFunctio
const apiKey = `${credentials.apiKey}:X`; 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/'; let options: OptionsWithUri = {
headers: {
const options: OptionsWithUri = { 'Content-Type': 'application/json',
headers: headerWithAuthentication, Authorization: `${Buffer.from(apiKey).toString(BINARY_ENCODING)}`,
},
method, method,
body, body,
uri: `https://${credentials.domain}.${endpoint}${resource}`, qs: query,
uri: uri || `https://${credentials.domain}.${endpoint}${resource}`,
json: true json: true
}; };
if (!Object.keys(body).length) {
if (_.isEmpty(options.body)) {
delete options.body; delete options.body;
} }
if (!Object.keys(query).length) {
delete options.qs;
}
options = Object.assign({}, options, option);
try { try {
return await this.helpers.request!(options); return await this.helpers.request!(options);
} catch (error) { } catch (error) {
console.error(error); console.log(error.response.body)
const errorMessage = error.response.body.message || error.response.body.Message || error.response.body.description;
const errorMessage = error.response.body.message || error.response.body.Message;
if (errorMessage !== undefined) { if (errorMessage !== undefined) {
throw errorMessage; throw new Error(errorMessage);
} };
throw error.response.body; return new Error(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 export function validateJSON(json: string | undefined): any { // tslint:disable-line:no-any
let result; let result;
try { try {