🚧 Setup

Added OAuth2 credentials, UI, generic functions changes
This commit is contained in:
Rupenieks
2020-07-03 11:22:39 +02:00
parent 13f71d3af0
commit de7a6d6dfb
4 changed files with 135 additions and 10 deletions

View File

@@ -6,14 +6,12 @@ import {
ILoadOptionsFunctions,
} from 'n8n-core';
import { IDataObject } from 'n8n-workflow';
import { attachmentFields } from '../Salesforce/AttachmentDescription';
export async function bitlyApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('bitlyApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
const authenticationMethod = this.getNodeParameter('authentication', 0) as string;
let options: OptionsWithUri = {
headers: { Authorization: `Bearer ${credentials.accessToken}`},
headers: {},
method,
qs,
body,
@@ -24,10 +22,21 @@ export async function bitlyApiRequest(this: IHookFunctions | IExecuteFunctions |
if (Object.keys(options.body).length === 0) {
delete options.body;
}
try {
return await this.helpers.request!(options);
} catch (err) {
throw new Error(err);
try{
if (authenticationMethod === 'accessToken') {
const credentials = this.getCredentials('bitlyApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
options.headers = { Authorization: `Bearer ${credentials.accessToken}`};
return await this.helpers.request!(options);
} else {
return await this.helpers.requestOAuth2!.call(this, 'bitlyOAuth2Api', options);
}
} catch(error) {
throw new Error(error);
}
}