2020-01-23 09:27:28 -08:00
|
|
|
import { OptionsWithUri } from 'request';
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IExecuteSingleFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IWebhookFunctions,
|
|
|
|
} from 'n8n-core';
|
2022-08-17 08:50:24 -07:00
|
|
|
import { IDataObject, NodeApiError, NodeOperationError } from 'n8n-workflow';
|
2020-01-23 09:27:28 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function segmentApiRequest(
|
|
|
|
this:
|
|
|
|
| IHookFunctions
|
|
|
|
| IExecuteFunctions
|
|
|
|
| IExecuteSingleFunctions
|
|
|
|
| ILoadOptionsFunctions
|
|
|
|
| IWebhookFunctions,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
2022-08-24 01:26:48 -07:00
|
|
|
body: any = {}, // tslint:disable-line:no-any
|
2022-08-17 08:50:24 -07:00
|
|
|
qs: IDataObject = {},
|
|
|
|
uri?: string,
|
|
|
|
option: IDataObject = {},
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
): Promise<any> {
|
2020-01-23 09:27:28 -08:00
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
qs,
|
|
|
|
body,
|
2022-08-17 08:50:24 -07:00
|
|
|
uri: uri || `https://api.segment.io/v1${resource}`,
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2020-01-23 09:27:28 -08:00
|
|
|
};
|
|
|
|
if (!Object.keys(body).length) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
try {
|
2022-08-24 01:26:48 -07:00
|
|
|
return await this.helpers.requestWithAuthentication.call(this, 'segmentApi', options);
|
2020-01-23 09:27:28 -08:00
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2020-01-23 09:27:28 -08:00
|
|
|
}
|
|
|
|
}
|