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

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

44 lines
1 KiB
TypeScript
Raw Normal View History

import type {
IDataObject,
2020-01-16 15:51:01 -08:00
IExecuteFunctions,
IHookFunctions,
IHttpRequestMethods,
2020-01-16 15:51:01 -08:00
ILoadOptionsFunctions,
IRequestOptions,
2020-01-16 15:51:01 -08:00
IWebhookFunctions,
JsonObject,
} from 'n8n-workflow';
import { NodeApiError } from 'n8n-workflow';
2020-01-16 15:51:01 -08:00
export async function gumroadApiRequest(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions,
method: IHttpRequestMethods,
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: IRequestOptions = {
2020-01-16 15:51:01 -08:00
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
}
}