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(