n8n/packages/nodes-base/nodes/Hubspot/GenericFunctions.ts
2020-06-08 14:40:23 +02:00

96 lines
2.6 KiB
TypeScript

import {
OptionsWithUri,
} from 'request';
import {
IExecuteFunctions,
IExecuteSingleFunctions,
IHookFunctions,
ILoadOptionsFunctions,
} from 'n8n-core';
import {
IDataObject,
} from 'n8n-workflow';
export async function hubspotApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: any = {}, query: IDataObject = {}, uri?: string): Promise<any> { // tslint:disable-line:no-any
const authenticationMethod = this.getNodeParameter('authentication', 0);
const options: OptionsWithUri = {
method,
qs: query,
uri: uri || `https://api.hubapi.com${endpoint}`,
body,
json: true,
useQuerystring: true,
};
try {
if (authenticationMethod === 'accessToken') {
const credentials = this.getCredentials('hubspotApi');
options.qs.hapikey = credentials!.apiKey as string;
return await this.helpers.request!(options);
} else {
// @ts-ignore
return await this.helpers.requestOAuth2!.call(this, 'hubspotOAuth2Api', options, 'Bearer');
}
} catch (error) {
if (error.response && error.response.body && error.response.body.errors) {
// Try to return the error prettier
let errorMessages = error.response.body.errors;
if (errorMessages[0].message) {
// @ts-ignore
errorMessages = errorMessages.map(errorItem => errorItem.message);
}
throw new Error(`Hubspot error response [${error.statusCode}]: ${errorMessages.join('|')}`);
}
throw error;
}
}
/**
* Make an API request to paginated hubspot endpoint
* and return all results
*/
export async function hubspotApiRequestAllItems(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, propertyName: string, method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const returnData: IDataObject[] = [];
let responseData;
query.limit = query.limit || 250;
query.count = 100;
body.limit = body.limit || 100;
do {
responseData = await hubspotApiRequest.call(this, method, endpoint, body, query);
query.offset = responseData.offset;
query['vid-offset'] = responseData['vid-offset'];
returnData.push.apply(returnData, responseData[propertyName]);
if (query.limit && query.limit <= returnData.length) {
return returnData;
}
} while (
responseData['has-more'] !== undefined &&
responseData['has-more'] !== null &&
responseData['has-more'] !== false
);
return returnData;
}
export function validateJSON(json: string | undefined): any { // tslint:disable-line:no-any
let result;
try {
result = JSON.parse(json!);
} catch (exception) {
result = '';
}
return result;
}