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

48 lines
1.1 KiB
TypeScript
Raw Normal View History

import type { OptionsWithUri } from 'request';
import type {
2020-01-16 15:51:01 -08:00
IExecuteFunctions,
IExecuteSingleFunctions,
IHookFunctions,
ILoadOptionsFunctions,
IWebhookFunctions,
} from 'n8n-core';
import type { IDataObject, JsonObject } from 'n8n-workflow';
import { NodeApiError } from 'n8n-workflow';
2020-01-16 15:51:01 -08:00
export async function gumroadApiRequest(
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('gumroadApi');
2020-01-16 15:52:58 -08:00
body = Object.assign({ access_token: credentials.accessToken }, body);
2020-01-16 15:51:01 -08:00
let options: OptionsWithUri = {
method,
qs,
body,
uri: uri || `https://api.gumroad.com/v2${resource}`,
2020-10-22 06:46:03 -07:00
json: true,
2020-01-16 15:51:01 -08:00
};
options = Object.assign({}, options, option);
if (Object.keys(options.body as IDataObject).length === 0) {
2020-01-16 15:51:01 -08:00
delete options.body;
}
try {
return await this.helpers.request(options);
2020-01-16 15:51:01 -08:00
} catch (error) {
throw new NodeApiError(this.getNode(), error as JsonObject);
2020-01-16 15:51:01 -08:00
}
}