Skip to content

Commit

Permalink
Merge pull request #861 from storyblok/fix/getall-typeerror
Browse files Browse the repository at this point in the history
fix: remove trailing slash from slug passed to getAll()
  • Loading branch information
edodusi authored Oct 16, 2024
2 parents e5d0481 + 3839449 commit d1b77f9
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 d1b77f9

Please sign in to comment.