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

51 lines
1.1 KiB
TypeScript
Raw Normal View History

import type { OptionsWithUri } from 'request';
import type {
IDataObject,
2020-01-20 13:52:34 -08:00
IExecuteFunctions,
IExecuteSingleFunctions,
IHookFunctions,
ILoadOptionsFunctions,
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
| IExecuteSingleFunctions
| ILoadOptionsFunctions
| IWebhookFunctions,
method: string,
resource: string,
body: any = {},
qs: IDataObject = {},
uri?: string,
option: IDataObject = {},
): Promise<any> {
const credentials = await this.getCredentials('jotFormApi');
2020-01-20 13:52:34 -08:00
let options: OptionsWithUri = {
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
}
}