Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generateHandlers: Fixes required both "take" and "skip" ("cursor") parameters #105

Merged
merged 1 commit into from
Jul 6, 2021
Merged
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 src/model/generateRestHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ export function generateRestHandlers<
)

let options = { where: filters }
if (take && skip) {
if (take || skip) {
options = Object.assign(options, { take, skip })
}
if (take && cursor) {
if (take || cursor) {
options = Object.assign(options, { take, cursor })
}

Expand Down
123 changes: 123 additions & 0 deletions test/model/toRestHandlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,84 @@ describe('GET /users', () => {
])
})

it('returns offset paginated entities without an explicit "skip" parameter', async () => {
server.use(...db.user.toHandlers('rest', 'http://localhost'))
db.user.create({
id: 'abc-123',
firstName: 'John',
lastName: 'White',
})
db.user.create({
id: 'def-456',
firstName: 'Kate',
lastName: 'Moen',
})
db.user.create({
id: 'ghi-789',
firstName: 'Joseph',
lastName: 'Sipes',
})
db.user.create({
id: 'xyz-321',
firstName: 'Eva',
lastName: 'Grant',
})

const res = await fetch('http://localhost/users?take=2')
const users = await res.json()
expect(users).toEqual([
{
id: 'abc-123',
firstName: 'John',
lastName: 'White',
},
{
id: 'def-456',
firstName: 'Kate',
lastName: 'Moen',
},
])
})

it('returns offset paginated entities with the "skip" parameter set to 0', async () => {
server.use(...db.user.toHandlers('rest', 'http://localhost'))
db.user.create({
id: 'abc-123',
firstName: 'John',
lastName: 'White',
})
db.user.create({
id: 'def-456',
firstName: 'Kate',
lastName: 'Moen',
})
db.user.create({
id: 'ghi-789',
firstName: 'Joseph',
lastName: 'Sipes',
})
db.user.create({
id: 'xyz-321',
firstName: 'Eva',
lastName: 'Grant',
})

const res = await fetch('http://localhost/users?skip=0&take=2')
const users = await res.json()
expect(users).toEqual([
{
id: 'abc-123',
firstName: 'John',
lastName: 'White',
},
{
id: 'def-456',
firstName: 'Kate',
lastName: 'Moen',
},
])
})

it('returns cursor paginated entities', async () => {
server.use(...db.user.toHandlers('rest', 'http://localhost'))
db.user.create({
Expand Down Expand Up @@ -151,6 +229,51 @@ describe('GET /users', () => {
])
})

it('returns cursor paginated entities without an explicit "take" parameter', async () => {
server.use(...db.user.toHandlers('rest', 'http://localhost'))
db.user.create({
id: 'abc-123',
firstName: 'John',
lastName: 'White',
})
db.user.create({
id: 'def-456',
firstName: 'Kate',
lastName: 'Moen',
})
db.user.create({
id: 'ghi-789',
firstName: 'Joseph',
lastName: 'Sipes',
})
db.user.create({
id: 'xyz-321',
firstName: 'Eva',
lastName: 'Grant',
})

const res = await fetch('http://localhost/users?cursor=abc-123')
const users = await res.json()

expect(users).toEqual([
{
id: 'def-456',
firstName: 'Kate',
lastName: 'Moen',
},
{
id: 'ghi-789',
firstName: 'Joseph',
lastName: 'Sipes',
},
{
id: 'xyz-321',
firstName: 'Eva',
lastName: 'Grant',
},
])
})

it('return filtered entities', async () => {
server.use(...db.user.toHandlers('rest', 'http://localhost'))
db.user.create({
Expand Down