n8n/packages/nodes-base/nodes/Hubspot/GenericFunctions.ts

115 lines
3.2 KiB
TypeScript
Raw Normal View History

2020-04-07 21:35:50 -07:00
import {
OptionsWithUri,
} from 'request';
2019-12-03 13:48:17 -08:00
import {
IExecuteFunctions,
2020-04-17 14:42:41 -07:00
IExecuteSingleFunctions,
2019-12-03 13:48:17 -08:00
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
let authenticationMethod = this.getNodeParameter('authentication', 0);
if (this.getNode().type.includes('Trigger')) {
authenticationMethod = 'developerApi';
}
2019-12-03 13:48:17 -08:00
const options: OptionsWithUri = {
method,
qs: query,
uri: uri || `https://api.hubapi.com${endpoint}`,
body,
2019-12-04 09:21:02 -08:00
json: true,
useQuerystring: true,
2019-12-03 13:48:17 -08:00
};
2020-06-08 05:40:23 -07:00
2019-12-03 13:48:17 -08:00
try {
if (authenticationMethod === 'apiKey') {
2020-06-08 05:40:23 -07:00
const credentials = this.getCredentials('hubspotApi');
options.qs.hapikey = credentials!.apiKey as string;
return await this.helpers.request!(options);
} else if (authenticationMethod === 'developerApi') {
const credentials = this.getCredentials('hubspotDeveloperApi');
options.qs.hapikey = credentials!.apiKey as string;
2020-06-08 05:40:23 -07:00
return await this.helpers.request!(options);
} else {
// @ts-ignore
return await this.helpers.requestOAuth2!.call(this, 'hubspotOAuth2Api', options, { tokenType: 'Bearer' });
2020-06-08 05:40:23 -07:00
}
2019-12-03 13:48:17 -08:00
} catch (error) {
let errorMessages;
if (error.response && error.response.body) {
if (error.response.body.message) {
errorMessages = [error.response.body.message];
} else if (error.response.body.errors) {
// Try to return the error prettier
errorMessages = error.response.body.errors;
if (errorMessages[0].message) {
// @ts-ignore
errorMessages = errorMessages.map(errorItem => errorItem.message);
}
}
2020-04-17 14:42:41 -07:00
throw new Error(`Hubspot error response [${error.statusCode}]: ${errorMessages.join('|')}`);
}
2019-12-03 13:48:17 -08:00
throw error;
}
}
2019-12-03 13:48:17 -08:00
/**
* 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;
2020-04-07 21:35:50 -07:00
query.limit = query.limit || 250;
2019-12-03 13:48:17 -08:00
query.count = 100;
2020-04-17 14:42:41 -07:00
body.limit = body.limit || 100;
2019-12-03 13:48:17 -08:00
do {
responseData = await hubspotApiRequest.call(this, method, endpoint, body, query);
query.offset = responseData.offset;
query.vidOffset = responseData['vid-offset'];
2019-12-03 13:48:17 -08:00
returnData.push.apply(returnData, responseData[propertyName]);
//ticket:getAll endpoint does not support setting a limit, so return once the limit is reached
if (query.limit && query.limit <= returnData.length && endpoint.includes('/tickets/paged')) {
2020-04-07 21:35:50 -07:00
return returnData;
}
2019-12-03 13:48:17 -08:00
} 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;
}