2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IDataObject,
|
2019-11-16 14:16:11 -08:00
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
2023-03-09 09:13:15 -08:00
|
|
|
JsonObject,
|
2024-10-31 09:38:11 -07:00
|
|
|
IHttpRequestOptions,
|
2024-02-14 07:29:09 -08:00
|
|
|
IHttpRequestMethods,
|
2023-03-09 09:13:15 -08:00
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2022-08-17 08:50:24 -07:00
|
|
|
|
|
|
|
export async function intercomApiRequest(
|
2023-08-16 06:52:41 -07:00
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
2022-08-17 08:50:24 -07:00
|
|
|
endpoint: string,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body: any = {},
|
|
|
|
query?: IDataObject,
|
|
|
|
uri?: string,
|
|
|
|
): Promise<any> {
|
2024-10-31 09:38:11 -07:00
|
|
|
const options: IHttpRequestOptions = {
|
2019-11-16 14:16:11 -08:00
|
|
|
method,
|
2019-11-22 14:04:06 -08:00
|
|
|
qs: query,
|
2024-10-31 09:38:11 -07:00
|
|
|
url: uri ?? `https://api.intercom.io${endpoint}`,
|
2019-11-16 14:16:11 -08:00
|
|
|
body,
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2019-11-16 14:16:11 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2024-10-31 09:38:11 -07:00
|
|
|
return await this.helpers.httpRequestWithAuthentication.call(this, 'intercomApi', options);
|
2019-11-16 14:16:11 -08:00
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2019-11-16 14:16:11 -08:00
|
|
|
}
|
|
|
|
}
|
2019-11-16 15:47:28 -08:00
|
|
|
|
2019-11-22 14:04:06 -08:00
|
|
|
/**
|
|
|
|
* Make an API request to paginated intercom endpoint
|
|
|
|
* and return all results
|
|
|
|
*/
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function intercomApiRequestAllItems(
|
|
|
|
this: IHookFunctions | IExecuteFunctions,
|
|
|
|
propertyName: string,
|
|
|
|
endpoint: string,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body: any = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2019-11-22 14:04:06 -08:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
query.per_page = 60;
|
|
|
|
|
|
|
|
let uri: string | undefined;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await intercomApiRequest.call(this, endpoint, method, body, query, uri);
|
|
|
|
uri = responseData.pages.next;
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push.apply(returnData, responseData[propertyName] as IDataObject[]);
|
2022-12-02 12:54:28 -08:00
|
|
|
} while (responseData.pages?.next !== null);
|
2019-11-22 14:04:06 -08:00
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export function validateJSON(json: string | undefined): any {
|
2019-11-16 15:47:28 -08:00
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = JSON.parse(json!);
|
|
|
|
} catch (exception) {
|
|
|
|
result = '';
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|