2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IDataObject,
|
2020-01-23 09:27:28 -08:00
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IWebhookFunctions,
|
2023-03-09 09:13:15 -08:00
|
|
|
} from 'n8n-workflow';
|
2020-01-23 09:27:28 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function segmentApiRequest(
|
2023-08-16 06:52:41 -07:00
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions,
|
2022-08-17 08:50:24 -07:00
|
|
|
method: string,
|
|
|
|
resource: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
body: any = {},
|
2022-08-17 08:50:24 -07:00
|
|
|
qs: IDataObject = {},
|
|
|
|
uri?: string,
|
2022-11-08 06:28:21 -08:00
|
|
|
_option: IDataObject = {},
|
2022-08-17 08:50:24 -07:00
|
|
|
): Promise<any> {
|
2020-01-23 09:27:28 -08:00
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
qs,
|
|
|
|
body,
|
2023-01-19 04:37:19 -08: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
|
|
|
};
|
2023-02-27 19:39:43 -08:00
|
|
|
if (!Object.keys(body as IDataObject).length) {
|
2020-01-23 09:27:28 -08:00
|
|
|
delete options.body;
|
|
|
|
}
|
2024-01-17 07:08:50 -08:00
|
|
|
return await this.helpers.requestWithAuthentication.call(this, 'segmentApi', options);
|
2020-01-23 09:27:28 -08:00
|
|
|
}
|