Smaller changes and add wildcard-event

This commit is contained in:
Jan Oberhauser
2019-12-01 20:19:38 +01:00
parent 246429b09d
commit ee8d63f34a
4 changed files with 40 additions and 24 deletions

View File

@@ -13,8 +13,8 @@ import {
IDataObject,
} from 'n8n-workflow';
export async function paypalApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions | IWebhookFunctions, endpoint: string, method: string, body: any = {}, query?: IDataObject, uri?: string): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('paypalApi');
export async function payPalApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions | IWebhookFunctions, endpoint: string, method: string, body: any = {}, query?: IDataObject, uri?: string): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('payPalApi');
const env = getEnviroment(credentials!.env as string);
const tokenInfo = await getAccessToken.call(this);
const headerWithAuthentication = Object.assign({ },
@@ -30,7 +30,16 @@ export async function paypalApiRequest(this: IHookFunctions | IExecuteFunctions
try {
return await this.helpers.request!(options);
} catch (error) {
throw error.response.body;
if (error.response.body) {
let errorMessage = error.response.body.message;
if (error.response.body.details) {
errorMessage += ` - Details: ${JSON.stringify(error.response.body.details)}`;
}
throw new Error(errorMessage);
}
throw error;
}
}
@@ -38,12 +47,12 @@ function getEnviroment(env: string): string {
// @ts-ignore
return {
'sanbox': 'https://api.sandbox.paypal.com',
'live': 'https://api.paypal.com'
'live': 'https://api.paypal.com',
}[env];
}
async function getAccessToken(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions | IWebhookFunctions): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('paypalApi');
const credentials = this.getCredentials('payPalApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
@@ -66,9 +75,9 @@ async function getAccessToken(this: IHookFunctions | IExecuteFunctions | IExecut
const errorMessage = error.response.body.message || error.response.body.Message;
if (errorMessage !== undefined) {
throw errorMessage;
throw new Error(errorMessage);
}
throw error.response.body;
throw new Error(error.response.body);
}
}
@@ -76,7 +85,7 @@ async function getAccessToken(this: IHookFunctions | IExecuteFunctions | IExecut
* Make an API request to paginated paypal endpoint
* and return all results
*/
export async function paypalApiRequestAllItems(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, propertyName: string, endpoint: string, method: string, body: any = {}, query?: IDataObject, uri?: string): Promise<any> { // tslint:disable-line:no-any
export async function payPalApiRequestAllItems(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, propertyName: string, endpoint: string, method: string, body: any = {}, query?: IDataObject, uri?: string): Promise<any> { // tslint:disable-line:no-any
const returnData: IDataObject[] = [];
@@ -85,7 +94,7 @@ export async function paypalApiRequestAllItems(this: IHookFunctions | IExecuteFu
query!.page_size = 1000;
do {
responseData = await paypalApiRequest.call(this, endpoint, method, body, query, uri);
responseData = await payPalApiRequest.call(this, endpoint, method, body, query, uri);
uri = getNext(responseData.links);
returnData.push.apply(returnData, responseData[propertyName]);
} while (