From 3839449f113101db2a98c5aacb3964ccee2e2db4 Mon Sep 17 00:00:00 2001 From: Edoardo Dusi Date: Fri, 11 Oct 2024 13:12:14 +0200 Subject: [PATCH] fix: remove trailing slash from slug passed to getAll() --- src/index.test.ts | 19 +++++++++++++++++++ src/index.ts | 5 ++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/index.test.ts b/src/index.test.ts index a57919c8..46775571 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -289,6 +289,25 @@ describe('StoryblokClient', () => { await client.getAll('links', { per_page: 1 }) expect(mockMakeRequest).toBeCalledTimes(2) }) + + it('should get all stories if the slug is passed with the trailing slash', async () => { + const mockMakeRequest = vi.fn().mockResolvedValue({ + data: { + stories: [ + { id: 1, name: 'Test Story 1' }, + { id: 2, name: 'Test Story 2' }, + ], + }, + total: 2, + status: 200, + }) + client.makeRequest = mockMakeRequest + const result = await client.getAll('cdn/stories/', { version: 'draft' }) + expect(result).toEqual([ + { id: 1, name: 'Test Story 1' }, + { id: 2, name: 'Test Story 2' }, + ]) + }) }) describe('post', () => { diff --git a/src/index.ts b/src/index.ts index 4422a313..07225309 100755 --- a/src/index.ts +++ b/src/index.ts @@ -243,9 +243,8 @@ class Storyblok { fetchOptions?: ISbCustomFetch ): Promise { const perPage = params?.per_page || 25 - const url = `/${slug}` - const urlParts = url.split('/') - const e = entity || urlParts[urlParts.length - 1] + const url = `/${slug}`.replace(/\/$/, '') + const e = entity ?? url.substring(url.lastIndexOf('/') + 1) const firstPage = 1 const firstRes = await this.makeRequest(