mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-14 08:34:07 -08:00
61e26804ba
* ⚡ enabled array-type * ⚡ await-thenable on * ⚡ ban-types on * ⚡ default-param-last on * ⚡ dot-notation on * ⚡ member-delimiter-style on * ⚡ no-duplicate-imports on * ⚡ no-empty-interface on * ⚡ no-floating-promises on * ⚡ no-for-in-array on * ⚡ no-invalid-void-type on * ⚡ no-loop-func on * ⚡ no-shadow on * ⚡ ban-ts-comment re enabled * ⚡ @typescript-eslint/lines-between-class-members on * address my own comment * @typescript-eslint/return-await on * @typescript-eslint/promise-function-async on * @typescript-eslint/no-unnecessary-boolean-literal-compare on * @typescript-eslint/no-unnecessary-type-assertion on * prefer-const on * @typescript-eslint/prefer-optional-chain on Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
75 lines
1.9 KiB
TypeScript
75 lines
1.9 KiB
TypeScript
import { OptionsWithUri } from 'request';
|
|
|
|
import { IExecuteFunctions, IHookFunctions } from 'n8n-core';
|
|
|
|
import { IDataObject, NodeApiError } from 'n8n-workflow';
|
|
|
|
import { get } from 'lodash';
|
|
|
|
/**
|
|
* Make an API request to Spotify
|
|
*
|
|
*/
|
|
export async function spotifyApiRequest(
|
|
this: IHookFunctions | IExecuteFunctions,
|
|
method: string,
|
|
endpoint: string,
|
|
body: object,
|
|
query?: object,
|
|
uri?: string,
|
|
): Promise<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> {
|
|
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') && endpoint === '/search') {
|
|
// The search endpoint has a limit of 1000 so step before it returns a 404
|
|
return returnData;
|
|
}
|
|
} while (
|
|
(responseData.next !== null && responseData.next !== undefined) ||
|
|
(responseData[propertyName.split('.')[0]].next !== null &&
|
|
responseData[propertyName.split('.')[0]].next !== undefined)
|
|
);
|
|
|
|
return returnData;
|
|
}
|