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

132 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-01-15 13:06:17 -08:00
import {
IExecuteFunctions,
IHookFunctions,
} from 'n8n-core';
import {
IDataObject,
} from 'n8n-workflow';
import {
OptionsWithUri,
} from 'request';
/**
2021-01-17 16:01:53 -08:00
* Make an authenticated or unauthenticated API request to Reddit.
2021-01-15 13:06:17 -08:00
*/
export async function redditApiRequest(
this: IHookFunctions | IExecuteFunctions,
method: string,
endpoint: string,
2021-01-17 11:17:17 -08:00
qs: IDataObject,
2021-01-15 13:06:17 -08:00
): Promise<any> { // tslint:disable-line:no-any
2021-01-17 10:00:21 -08:00
2021-01-17 15:37:43 -08:00
const resource = this.getNodeParameter('resource', 0) as string;
const authRequired = ['profile', 'post', 'postComment'].includes(resource);
2021-01-17 15:37:43 -08:00
2021-01-15 13:06:17 -08:00
const options: OptionsWithUri = {
headers: {
'user-agent': 'n8n',
},
2021-01-15 13:06:17 -08:00
method,
2021-01-22 14:35:12 -08:00
uri: authRequired ? `https://oauth.reddit.com/${endpoint}` : `https://www.reddit.com/${endpoint}`,
2021-01-17 11:17:17 -08:00
qs,
2021-01-15 13:06:17 -08:00
json: true,
};
2021-01-17 11:17:17 -08:00
if (!Object.keys(qs).length) {
delete options.qs;
2021-01-15 13:06:17 -08:00
}
try {
console.log(options);
2021-01-22 14:35:12 -08:00
return authRequired
2021-01-17 15:37:43 -08:00
? await this.helpers.requestOAuth2.call(this, 'redditOAuth2Api', options)
: await this.helpers.request.call(this, options);
2021-01-15 13:06:17 -08:00
} catch (error) {
if (error.message) {
const errorObject = JSON.parse(error.message.match(/{.*}/)[0]);
throw new Error(`Reddit error response [${errorObject.error}]: ${errorObject.message}`);
}
2021-01-15 13:06:17 -08:00
throw error;
}
}
/**
2021-01-17 16:01:53 -08:00
* Make an unauthenticated API request to Reddit and return all results.
2021-01-15 13:06:17 -08:00
*/
export async function redditApiRequestAllItems(
this: IHookFunctions | IExecuteFunctions,
method: string,
endpoint: string,
qs: IDataObject,
): Promise<any> { // tslint:disable-line:no-any
2021-01-17 11:17:17 -08:00
let responseData;
const returnData: IDataObject[] = [];
do {
2021-01-22 14:35:12 -08:00
console.log(method);
console.log(endpoint);
console.log(qs);
responseData = await redditApiRequest.call(this, method, endpoint, qs);
console.log(responseData);
2021-01-17 11:17:17 -08:00
qs.after = responseData.after;
2021-01-22 14:35:12 -08:00
if (endpoint === 'api/search_subreddits.json') {
responseData.subreddits.forEach((child: any) => returnData.push(child)); // tslint:disable-line:no-any
2021-01-22 14:35:12 -08:00
} else {
responseData.data.children.forEach((child: any) => returnData.push(child.data)); // tslint:disable-line:no-any
}
2021-01-17 11:17:17 -08:00
if (qs.limit && returnData.length >= qs.limit) {
return returnData;
}
} while (responseData.after);
return returnData;
2021-01-15 13:06:17 -08:00
}
2021-01-17 14:36:30 -08:00
/**
* Handles a large Reddit listing by returning all items or up to a limit.
*/
export async function handleListing(
this: IExecuteFunctions,
i: number,
endpoint: string,
): Promise<any> { // tslint:disable-line:no-any
2021-01-22 14:35:12 -08:00
2021-01-17 14:36:30 -08:00
let responseData;
2021-01-22 14:35:12 -08:00
const resource = this.getNodeParameter('resource', 0);
const operation = this.getNodeParameter('operation', 0);
const qs: IDataObject = {};
let requestMethod = 'GET';
2021-01-22 14:35:12 -08:00
if (resource === 'subreddit' && operation === 'getAll') {
const filters = this.getNodeParameter('filters', i) as IDataObject;
if (filters.keyword) {
qs.query = filters.keyword;
2021-01-22 14:35:12 -08:00
}
requestMethod = 'POST';
2021-01-22 14:35:12 -08:00
}
2021-01-17 14:36:30 -08:00
const returnAll = this.getNodeParameter('returnAll', i);
2021-01-22 14:35:12 -08:00
2021-01-17 14:36:30 -08:00
if (returnAll) {
responseData = await redditApiRequestAllItems.call(this, requestMethod, endpoint, qs);
2021-01-22 14:35:12 -08:00
} else {
qs.limit = this.getNodeParameter('limit', i);
responseData = await redditApiRequestAllItems.call(this, requestMethod, endpoint, qs);
2021-01-22 14:35:12 -08:00
responseData = responseData.splice(0, qs.limit);
2021-01-17 14:36:30 -08:00
}
return responseData;
}