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

43 lines
1 KiB
TypeScript
Raw Normal View History

import type { OptionsWithUri } from 'request';
import type {
IDataObject,
2020-01-26 20:27:14 -08:00
IExecuteFunctions,
IExecuteSingleFunctions,
IHookFunctions,
ILoadOptionsFunctions,
JsonObject,
} from 'n8n-workflow';
import { NodeApiError } from 'n8n-workflow';
2020-01-26 20:27:14 -08:00
export async function clearbitApiRequest(
this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
method: string,
api: string,
resource: string,
body: any = {},
qs: IDataObject = {},
uri?: string,
option: IDataObject = {},
): Promise<any> {
const credentials = await this.getCredentials('clearbitApi');
2020-01-26 20:27:14 -08:00
let options: OptionsWithUri = {
headers: { Authorization: `Bearer ${credentials.apiKey}` },
2020-01-26 20:27:14 -08:00
method,
qs,
body,
uri: uri || `https://${api}.clearbit.com${resource}`,
2020-10-22 06:46:03 -07:00
json: true,
2020-01-26 20:27:14 -08:00
};
options = Object.assign({}, options, option);
if (Object.keys(options.body as IDataObject).length === 0) {
2020-01-26 20:27:14 -08:00
delete options.body;
}
try {
return await this.helpers.request(options);
} catch (error) {
throw new NodeApiError(this.getNode(), error as JsonObject);
2020-01-26 20:27:14 -08:00
}
}