Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Spotify Node): Fix issue with null values breaking the response #12080

Merged
8 changes: 4 additions & 4 deletions packages/nodes-base/nodes/Spotify/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
IHookFunctions,
JsonObject,
IHttpRequestMethods,
IRequestOptions,
IHttpRequestOptions,
} from 'n8n-workflow';
import { NodeApiError } from 'n8n-workflow';

Expand All @@ -21,23 +21,23 @@ export async function spotifyApiRequest(
query?: IDataObject,
uri?: string,
): Promise<any> {
const options: IRequestOptions = {
const options: IHttpRequestOptions = {
method,
headers: {
'User-Agent': 'n8n',
'Content-Type': 'text/plain',
Accept: ' application/json',
},
qs: query,
uri: uri || `https://api.spotify.com/v1${endpoint}`,
url: 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);
return await this.helpers.httpRequestWithAuthentication.call(this, 'spotifyOAuth2Api', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error as JsonObject);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/nodes-base/nodes/Spotify/Spotify.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,10 @@ export class Spotify implements INodeType {
);
}

// Remove null values from the response
if (operation === 'getUserPlaylists') {
responseData = responseData.filter((item: IDataObject) => item !== null);
}
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(responseData as IDataObject[]),
{ itemData: { item: i } },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import type { IExecuteFunctions, IHookFunctions } from 'n8n-workflow';
import { NodeApiError } from 'n8n-workflow';

import { spotifyApiRequest } from '../GenericFunctions';

describe('Spotify -> GenericFunctions', () => {
let mockThis: IHookFunctions | IExecuteFunctions;

beforeEach(() => {
mockThis = {
helpers: {
httpRequestWithAuthentication: jest.fn(),
},
getNode: jest.fn().mockReturnValue({}),
} as unknown as IHookFunctions | IExecuteFunctions;
});

it('should make a request with the correct options', async () => {
const method = 'GET';
const endpoint = '/me';
const body = {};
const query = { limit: 10 };
const response = { data: 'test' };

(mockThis.helpers.httpRequestWithAuthentication as jest.Mock).mockResolvedValue(response);

const result = await spotifyApiRequest.call(mockThis, method, endpoint, body, query);

expect(mockThis.helpers.httpRequestWithAuthentication).toHaveBeenCalledWith(
'spotifyOAuth2Api',
{
method,
headers: {
'User-Agent': 'n8n',
'Content-Type': 'text/plain',
Accept: ' application/json',
},
qs: query,
url: `https://api.spotify.com/v1${endpoint}`,
json: true,
},
);

expect(result).toEqual(response);
});

it('should throw a NodeApiError on request failure', async () => {
const method = 'GET';
const endpoint = '/me';
const body = {};
const query = { limit: 10 };
const error = new Error('Request failed');

(mockThis.helpers.httpRequestWithAuthentication as jest.Mock).mockRejectedValue(error);

await expect(spotifyApiRequest.call(mockThis, method, endpoint, body, query)).rejects.toThrow(
NodeApiError,
);

expect(mockThis.getNode).toHaveBeenCalled();
});
});
Loading
Loading