From 711563d080dcf9975855af65def6d564f86934f2 Mon Sep 17 00:00:00 2001 From: aiirvizionz Date: Mon, 13 Jul 2026 12:50:47 -0600 Subject: [PATCH] fix(social-spotify): trim custom API base URL --- packages/social/spotify/src/index.test.ts | 25 +++++++++++++++++++++++ packages/social/spotify/src/index.ts | 3 ++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/social/spotify/src/index.test.ts b/packages/social/spotify/src/index.test.ts index bef6fc45..94347122 100644 --- a/packages/social/spotify/src/index.test.ts +++ b/packages/social/spotify/src/index.test.ts @@ -88,6 +88,31 @@ describe('social-spotify playlist publishing', () => { }); }); + it('normalizes custom API base URLs before composing Spotify paths', async () => { + const fetchMock = vi.spyOn(globalThis, 'fetch').mockResolvedValueOnce({ + ok: true, + status: 201, + text: async () => JSON.stringify({ + id: 'playlist_base_url', + external_urls: { spotify: 'https://open.spotify.com/playlist/playlist_base_url' }, + }), + } as Response); + + const ctx = { + ...fakeConnectContext({ SPOTIFY_ACCESS_TOKEN: 'spotify-token' }), + dryRun: false, + }; + + await adapter.post(ctx as any, { + title: 'Base URL', + body: 'Check custom base URL handling', + }, { + baseUrl: 'https://api.spotify.example/v1/', + }); + + expect(fetchMock.mock.calls[0]?.[0]).toBe('https://api.spotify.example/v1/me/playlists'); + }); + it('updates an existing playlist and replaces its items', async () => { const fetchMock = vi.spyOn(globalThis, 'fetch') .mockResolvedValueOnce({ diff --git a/packages/social/spotify/src/index.ts b/packages/social/spotify/src/index.ts index 3e25e6a1..a9ec29b1 100644 --- a/packages/social/spotify/src/index.ts +++ b/packages/social/spotify/src/index.ts @@ -179,7 +179,8 @@ async function spotifyFetch( options: SpotifyFetchOptions = {}, ): Promise { const { allowEmpty, ...init } = options; - const res = await fetch(`${config.baseUrl ?? 'https://api.spotify.com/v1'}${path}`, { + const baseUrl = (config.baseUrl ?? 'https://api.spotify.com/v1').replace(/\/+$/, ''); + const res = await fetch(`${baseUrl}${path}`, { ...init, headers: { authorization: `Bearer ${token}`,