From a7b9e62d75481f7e32c0179489e1807d655dbfc5 Mon Sep 17 00:00:00 2001 From: Artem Zakharchenko Date: Tue, 6 Jul 2021 20:34:51 +0200 Subject: [PATCH] generateHandlers: Fixes required both "take" and "skip" ("cursor") query parameters --- src/model/generateRestHandlers.ts | 4 +- test/model/toRestHandlers.test.ts | 123 ++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+), 2 deletions(-) diff --git a/src/model/generateRestHandlers.ts b/src/model/generateRestHandlers.ts index a2980833..2e844531 100644 --- a/src/model/generateRestHandlers.ts +++ b/src/model/generateRestHandlers.ts @@ -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 }) } diff --git a/test/model/toRestHandlers.test.ts b/test/model/toRestHandlers.test.ts index 774c9eae..fa62a3de 100644 --- a/test/model/toRestHandlers.test.ts +++ b/test/model/toRestHandlers.test.ts @@ -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({ @@ -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({