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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

42 lines
1,016 B
TypeScript
Raw Normal View History

import type {
IDataObject,
2020-01-26 20:27:14 -08:00
IExecuteFunctions,
IHookFunctions,
ILoadOptionsFunctions,
JsonObject,
IRequestOptions,
IHttpRequestMethods,
} from 'n8n-workflow';
import { NodeApiError } from 'n8n-workflow';
2020-01-26 20:27:14 -08:00
export async function clearbitApiRequest(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
method: IHttpRequestMethods,
api: string,
resource: string,
body: any = {},
qs: IDataObject = {},
uri?: string,
option: IDataObject = {},
): Promise<any> {
const credentials = await this.getCredentials('clearbitApi');
let options: IRequestOptions = {
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
}
}