Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/core/auth-js/src/GoTrueAdminApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ export default class GoTrueAdminApi {
const links = response.headers.get('link')?.split(',') ?? []
if (links.length > 0) {
links.forEach((link: string) => {
const page = parseInt(link.split(';')[0].split('=')[1].substring(0, 1))
const page = parseInt(link.split(';')[0].match(/[?&]page=(\d+)/)?.[1] ?? '', 10)
const rel = JSON.parse(link.split(';')[1].split('=')[1])
pagination[`${rel}Page`] = page
})
Expand Down Expand Up @@ -934,7 +934,7 @@ export default class GoTrueAdminApi {
const links = response.headers.get('link')?.split(',') ?? []
if (links.length > 0) {
links.forEach((link: string) => {
const page = parseInt(link.split(';')[0].split('=')[1].substring(0, 1))
const page = parseInt(link.split(';')[0].match(/[?&]page=(\d+)/)?.[1] ?? '', 10)
const rel = JSON.parse(link.split(';')[1].split('=')[1])
pagination[`${rel}Page`] = page
})
Expand Down
32 changes: 32 additions & 0 deletions packages/core/auth-js/test/GoTrueAdminApi.pagination.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { MockServer } from 'jest-mock-server'
import GoTrueAdminApi from '../src/GoTrueAdminApi'

describe('GoTrueAdminApi listUsers() pagination', () => {
const server = new MockServer()

beforeAll(async () => await server.start())
afterAll(async () => await server.stop())
beforeEach(() => server.reset())

test('parses multi-digit page numbers from the Link header', async () => {
const base = server.getURL().toString().replace(/\/$/, '')

server.get('/admin/users').mockImplementation((ctx) => {
ctx.set(
'Link',
`<${base}/admin/users?page=10&per_page=50>; rel="next", ` +
`<${base}/admin/users?page=23&per_page=50>; rel="last"`
)
ctx.set('x-total-count', '1150')
ctx.status = 200
ctx.body = { users: [], aud: 'authenticated' }
})

const admin = new GoTrueAdminApi({ url: base, headers: {} })
const { data, error } = await admin.listUsers({ page: 9, perPage: 50 })

expect(error).toBeNull()
// Regression: the old `.substring(0, 1)` parse truncated these to 1 and 2.
expect(data).toMatchObject({ nextPage: 10, lastPage: 23, total: 1150 })
})
})