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

50 lines
1.2 KiB
TypeScript
Raw Normal View History

import type { OptionsWithUri } from 'request';
import type {
2020-01-20 13:52:34 -08:00
IExecuteFunctions,
IExecuteSingleFunctions,
IHookFunctions,
ILoadOptionsFunctions,
IWebhookFunctions,
} from 'n8n-core';
import type { IDataObject, 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
}
}