Finally fix the build pagination issue
This commit is contained in:
11
src/lib/pagination/dropTakeParams.test.ts
Normal file
11
src/lib/pagination/dropTakeParams.test.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { describe, test, expect } from 'vitest'
|
||||
import { getDropTakeFromPageParams } from './dropTakeParams'
|
||||
|
||||
describe('convert search params', () => {
|
||||
test('should convert from page size and page to offset and limit', () => {
|
||||
expect(getDropTakeFromPageParams(7, 2)).toEqual({
|
||||
offset: 7,
|
||||
limit: 7,
|
||||
})
|
||||
})
|
||||
})
|
42
src/lib/pagination/dropTakeParams.ts
Normal file
42
src/lib/pagination/dropTakeParams.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import { splitEvery } from 'ramda'
|
||||
|
||||
export function parseParams(params: string) {
|
||||
const splittedParams = params.split('/')
|
||||
if (splittedParams.length % 2 !== 0) {
|
||||
return []
|
||||
}
|
||||
const splits = splitEvery(2, splittedParams)
|
||||
return Object.fromEntries(splits)
|
||||
}
|
||||
|
||||
export function toParams(records: Record<string, string>) {
|
||||
return Object.entries(records)
|
||||
.map(([key, value]) => `${key}/${value}`)
|
||||
.join('/')
|
||||
}
|
||||
|
||||
export interface PaginationParams {
|
||||
pageSize: number
|
||||
page: number
|
||||
filters?: Record<string, string>
|
||||
}
|
||||
|
||||
export interface DropTakeParams {
|
||||
offset: number
|
||||
limit: number
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert svelte `load` params into a `offset` and `limit` so they can be used to fetch endpoints with pagination queries
|
||||
*/
|
||||
export function getDropTakeFromPageParams(
|
||||
pageSize: number,
|
||||
page: number
|
||||
): DropTakeParams {
|
||||
const offset = pageSize * (page - 1)
|
||||
const limit = pageSize
|
||||
return {
|
||||
offset,
|
||||
limit,
|
||||
}
|
||||
}
|
@ -1,79 +0,0 @@
|
||||
import { describe, test, expect } from 'vitest'
|
||||
import {
|
||||
getPaginationQueryFromSearchParams,
|
||||
getPaginationSearchParams,
|
||||
parseParams,
|
||||
} from './searchParams'
|
||||
|
||||
describe('convert search params', () => {
|
||||
test('drop take params are not taken as filters', () => {
|
||||
expect(
|
||||
getPaginationQueryFromSearchParams(
|
||||
new URLSearchParams('offset=2&limit=5')
|
||||
)
|
||||
).toEqual({ offset: 2, limit: 5 })
|
||||
})
|
||||
|
||||
test('return empty paginationQuery if ', () => {
|
||||
expect(getPaginationQueryFromSearchParams(new URLSearchParams(''))).toEqual(
|
||||
{}
|
||||
)
|
||||
})
|
||||
|
||||
test('other than drop take params are moved to filters ', () => {
|
||||
expect(
|
||||
getPaginationQueryFromSearchParams(new URLSearchParams('tag=news'))
|
||||
).toEqual({ filters: { tag: 'news' } })
|
||||
})
|
||||
|
||||
test('offset and filter combined', () => {
|
||||
expect(
|
||||
getPaginationQueryFromSearchParams(
|
||||
new URLSearchParams('offset=3&tag=news')
|
||||
)
|
||||
).toEqual({ offset: 3, filters: { tag: 'news' } })
|
||||
})
|
||||
})
|
||||
|
||||
describe('get search params', () => {
|
||||
test('parse params', () => {
|
||||
const params = 'tags/News/page/1'
|
||||
expect(parseParams(params)).toEqual({ tags: 'News', page: '1' })
|
||||
})
|
||||
|
||||
test('should parse values into searchParams for first page', () => {
|
||||
const params = {
|
||||
pageSize: 7,
|
||||
page: 1,
|
||||
filters: {
|
||||
tags: 'News',
|
||||
},
|
||||
}
|
||||
expect(getPaginationSearchParams(params).toString()).toEqual(
|
||||
'limit=7&offset=0&tags=News'
|
||||
)
|
||||
})
|
||||
|
||||
test('should parse values into searchParams for third page', () => {
|
||||
const params = {
|
||||
pageSize: 7,
|
||||
page: 3,
|
||||
filters: {
|
||||
tags: 'News',
|
||||
},
|
||||
}
|
||||
expect(getPaginationSearchParams(params).toString()).toEqual(
|
||||
'limit=7&offset=14&tags=News'
|
||||
)
|
||||
})
|
||||
|
||||
test('should return first page without any params specified', () => {
|
||||
const params = {
|
||||
pageSize: 7,
|
||||
page: 1,
|
||||
}
|
||||
expect(getPaginationSearchParams(params).toString()).toEqual(
|
||||
'limit=7&offset=0'
|
||||
)
|
||||
})
|
||||
})
|
@ -1,60 +0,0 @@
|
||||
import { splitEvery } from 'ramda'
|
||||
import type { PaginationQuery } from './pagination'
|
||||
|
||||
export function getPaginationQueryFromSearchParams(
|
||||
searchParams: URLSearchParams
|
||||
) {
|
||||
return Array.from(searchParams).reduce<PaginationQuery>(
|
||||
(acc, [key, value]) => {
|
||||
const isDropTake = ['offset', 'limit'].includes(key)
|
||||
if (isDropTake) {
|
||||
return {
|
||||
...acc,
|
||||
[key]: Number(value),
|
||||
}
|
||||
}
|
||||
return {
|
||||
...acc,
|
||||
filters: {
|
||||
...acc.filters,
|
||||
[key]: value,
|
||||
},
|
||||
}
|
||||
},
|
||||
{}
|
||||
)
|
||||
}
|
||||
|
||||
export function parseParams(params: string) {
|
||||
const splittedParams = params.split('/')
|
||||
if (splittedParams.length % 2 !== 0) {
|
||||
return []
|
||||
}
|
||||
const splits = splitEvery(2, splittedParams)
|
||||
return Object.fromEntries(splits)
|
||||
}
|
||||
|
||||
export function toParams(records: Record<string, string>) {
|
||||
return Object.entries(records)
|
||||
.map(([key, value]) => `${key}/${value}`)
|
||||
.join('/')
|
||||
}
|
||||
|
||||
export interface PaginationSearchParams {
|
||||
pageSize: number
|
||||
page: number
|
||||
filters?: Record<string, string>
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert svelte `load` params into a `URLSearchParams` so they can be used to fetch endpoints with pagination queries
|
||||
*/
|
||||
export function getPaginationSearchParams({
|
||||
pageSize,
|
||||
page,
|
||||
filters,
|
||||
}: PaginationSearchParams) {
|
||||
const offset = pageSize * (page - 1)
|
||||
const limit = pageSize
|
||||
return new URLSearchParams({ limit, offset, ...filters })
|
||||
}
|
Reference in New Issue
Block a user