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

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

46 lines
1.1 KiB
TypeScript
Raw Normal View History

import type {
IDataObject,
2020-01-20 13:52:34 -08:00
IExecuteFunctions,
IHookFunctions,
IHttpRequestMethods,
2020-01-20 13:52:34 -08:00
ILoadOptionsFunctions,
IRequestOptions,
2020-01-20 13:52:34 -08:00
IWebhookFunctions,
JsonObject,
} from 'n8n-workflow';
import { NodeApiError } from 'n8n-workflow';
2020-01-20 13:52:34 -08:00
export async function jotformApiRequest(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions,
method: IHttpRequestMethods,
resource: string,
body: any = {},
qs: IDataObject = {},
uri?: string,
option: IDataObject = {},
): Promise<any> {
const credentials = await this.getCredentials('jotFormApi');
let options: IRequestOptions = {
2020-01-20 13:52:34 -08:00
headers: {
APIKEY: credentials.apiKey,
2020-01-20 13:52:34 -08:00
'Content-Type': 'application/x-www-form-urlencoded',
},
method,
qs,
form: body,
uri: uri || `https://${credentials.apiDomain || 'api.jotform.com'}${resource}`,
2020-10-22 06:46:03 -07:00
json: true,
2020-01-20 13:52:34 -08:00
};
if (!Object.keys(body as IDataObject).length) {
2020-01-20 13:52:34 -08:00
delete options.form;
}
options = Object.assign({}, options, option);
try {
return await this.helpers.request(options);
2020-01-20 13:52:34 -08:00
} catch (error) {
throw new NodeApiError(this.getNode(), error as JsonObject);
2020-01-20 13:52:34 -08:00
}
}