Skip to content

Commit

Permalink
fix: remove trailing slash from slug passed to getAll()
Browse files Browse the repository at this point in the history
  • Loading branch information
edodusi committed Oct 11, 2024
1 parent e5d0481 commit 3839449
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
19 changes: 19 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,8 @@ class Storyblok {
fetchOptions?: ISbCustomFetch
): Promise<any[]> {
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(
Expand Down

0 comments on commit 3839449

Please sign in to comment.