n8n/packages/nodes-base/nodes/Spotify/GenericFunctions.ts
Ricardo Espinoza 2e309bef7f
🐛 Fix Spotify pagination bug (#2061)
* Fixed missing value

Fixed infinite amount of interval triggered nodes executions

* Fixed inifite loop on Spotify Node

* Hide Limit parameter

* Logic operator fix

* Spotify getFollowingArtists returnAll fix

*  Small improvements to #2054

* 👕 Fix lint issue

Co-authored-by: Лебедев Иван <11215636+X-pech@users.noreply.github.com>
Co-authored-by: X-pech <lisgml@gmail.com>
2021-08-08 09:58:04 +02:00

79 lines
2 KiB
TypeScript

import {
OptionsWithUri,
} from 'request';
import {
IExecuteFunctions,
IHookFunctions,
} from 'n8n-core';
import {
IDataObject,
NodeApiError,
NodeOperationError,
} from 'n8n-workflow';
import {
get,
} from 'lodash';
/**
* Make an API request to Spotify
*
* @param {IHookFunctions} this
* @param {string} method
* @param {string} url
* @param {object} body
* @returns {Promise<any>}
*/
export async function spotifyApiRequest(this: IHookFunctions | IExecuteFunctions,
method: string, endpoint: string, body: object, query?: object, uri?: string): Promise<any> { // tslint:disable-line:no-any
const options: OptionsWithUri = {
method,
headers: {
'User-Agent': 'n8n',
'Content-Type': 'text/plain',
'Accept': ' application/json',
},
qs: query,
uri: uri || `https://api.spotify.com/v1${endpoint}`,
json: true,
};
if (Object.keys(body).length > 0) {
options.body = body;
}
try {
return await this.helpers.requestOAuth2.call(this, 'spotifyOAuth2Api', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
}
export async function spotifyApiRequestAllItems(this: IHookFunctions | IExecuteFunctions,
propertyName: string, method: string, endpoint: string, body: object, query?: object): Promise<any> { // tslint:disable-line:no-any
const returnData: IDataObject[] = [];
let responseData;
let uri: string | undefined;
do {
responseData = await spotifyApiRequest.call(this, method, endpoint, body, query, uri);
returnData.push.apply(returnData, get(responseData, propertyName));
uri = responseData.next || responseData[propertyName.split('.')[0]].next;
//remove the query as the query parameters are already included in the next, else api throws error.
query = {};
if (uri?.includes('offset=1000')) {
return returnData;
}
} while (
(responseData['next'] !== null && responseData['next'] !== undefined) ||
(responseData[propertyName.split('.')[0]].next !== null && responseData[propertyName.split('.')[0]].next !== undefined)
);
return returnData;
}