From 08db7fe050a59634645b3c3b95863b5b45b15eab Mon Sep 17 00:00:00 2001 From: Kjell-Morten Date: Thu, 5 Apr 2018 15:16:22 +0200 Subject: [PATCH] Set links.next to null when last page is passed --- lib/utils/createPagePath-test.js | 10 +++++++++ lib/utils/createPagePath.js | 4 ++++ package-lock.json | 8 +++---- package.json | 4 ++-- tests/helpers/adapters.js | 8 +++++++ tests/paging-test.js | 38 +++++++++++++++++++++++++++----- 6 files changed, 60 insertions(+), 12 deletions(-) diff --git a/lib/utils/createPagePath-test.js b/lib/utils/createPagePath-test.js index 297e9cf..78dcde5 100644 --- a/lib/utils/createPagePath-test.js +++ b/lib/utils/createPagePath-test.js @@ -22,6 +22,16 @@ test('should return path for next page and replace existing page qs', (t) => { t.is(ret, expected) }) +test('should return null when no next page', (t) => { + const paging = {next: null} + const request = {path: '/pages'} + const expected = null + + const ret = createPagePath(paging, request) + + t.is(ret, expected) +}) + test('should return path with baseUri', (t) => { const paging = {next: {type: 'page', pageAfter: 'page19', pageSize: 20}} const request = {path: '/pages'} diff --git a/lib/utils/createPagePath.js b/lib/utils/createPagePath.js index ebff55f..ee2a642 100644 --- a/lib/utils/createPagePath.js +++ b/lib/utils/createPagePath.js @@ -1,6 +1,10 @@ const querystring = require('querystring') function createPagePath (paging, {path}, baseUri = '') { + if (!paging.next) { + return null + } + if (baseUri.endsWith('/')) { baseUri = baseUri.substr(0, baseUri.length - 1) } diff --git a/package-lock.json b/package-lock.json index 0ea20dc..fd8b911 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "integreat-api-json", - "version": "0.1.1", + "version": "0.1.2", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -4378,9 +4378,9 @@ "dev": true }, "jsonwebtoken": { - "version": "8.2.0", - "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-8.2.0.tgz", - "integrity": "sha512-1Wxh8ADP3cNyPl8tZ95WtraHXCAyXupgc0AhMHjU9er98BV+UcKsO7OJUjfhIu0Uba9A40n1oSx8dbJYrm+EoQ==", + "version": "8.2.1", + "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-8.2.1.tgz", + "integrity": "sha512-l8rUBr0fqYYwPc8/ZGrue7GiW7vWdZtZqelxo4Sd5lMvuEeCK8/wS54sEo6tJhdZ6hqfutsj6COgC0d1XdbHGw==", "requires": { "jws": "3.1.4", "lodash.includes": "4.3.0", diff --git a/package.json b/package.json index 9a9924b..979c579 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "integreat-api-json", - "version": "0.1.1", + "version": "0.1.2", "description": "Integreat json api", "author": "Kjell-Morten Bratsberg Thorsen (http://kjellmorten.no)", "license": "ISC", @@ -53,6 +53,6 @@ "standard": "^11.0.1" }, "dependencies": { - "jsonwebtoken": "^8.2.0" + "jsonwebtoken": "^8.2.1" } } diff --git a/tests/helpers/adapters.js b/tests/helpers/adapters.js index 0fed1c6..7bf8eb8 100644 --- a/tests/helpers/adapters.js +++ b/tests/helpers/adapters.js @@ -46,6 +46,14 @@ const send = async ({params, action, data, relationship}) => { next: {type: 'page', pageSize: 2, pageAfter: 'page4'} } } + } else if (params.pageAfter === 'page4') { + return { + status: 'ok', + data: setDateAttrs([]), + paging: { + next: null + } + } } } else if (params.authCode === '12345') { return { diff --git a/tests/paging-test.js b/tests/paging-test.js index 2f8ea0b..c94c121 100644 --- a/tests/paging-test.js +++ b/tests/paging-test.js @@ -22,8 +22,8 @@ const defs = { const great = integreat(defs, {adapters}) -const firstPage = 'eyJ0eXBlIjoicGFnZSIsInBhZ2VTaXplIjoyLCJwYWdlQWZ0ZXIiOiJwYWdlMiJ9' -const secondPage = 'eyJ0eXBlIjoicGFnZSIsInBhZ2VTaXplIjoyLCJwYWdlQWZ0ZXIiOiJwYWdlNCJ9' +const secondPage = 'eyJ0eXBlIjoicGFnZSIsInBhZ2VTaXplIjoyLCJwYWdlQWZ0ZXIiOiJwYWdlMiJ9' +const thirdPage = 'eyJ0eXBlIjoicGFnZSIsInBhZ2VTaXplIjoyLCJwYWdlQWZ0ZXIiOiJwYWdlNCJ9' // Tests @@ -55,7 +55,7 @@ test('should GET first page', async (t) => { first: null, last: null, prev: null, - next: `https://api.somesite.com/pages?page=${firstPage}` + next: `https://api.somesite.com/pages?page=${secondPage}` } } @@ -72,8 +72,8 @@ test('should GET first page', async (t) => { test('should GET second page', async (t) => { const request = { method: 'GET', - path: `/pages?page=${firstPage}`, - params: {page: firstPage} + path: `/pages?page=${secondPage}`, + params: {page: secondPage} } const expected = { data: [ @@ -94,7 +94,33 @@ test('should GET second page', async (t) => { first: null, last: null, prev: null, - next: `/pages?page=${secondPage}` + next: `/pages?page=${thirdPage}` + } + } + + const routes = jsonapi(great) + const route = findRoute(routes, {path: '/pages', method: 'GET'}) + const response = await route.handler(request) + + t.truthy(response) + t.is(response.statusCode, 200, response.statusMessage) + t.truthy(response.body) + t.deepEqual(response.body, expected) +}) + +test('should GET empty result after last page', async (t) => { + const request = { + method: 'GET', + path: `/pages?page=${thirdPage}`, + params: {page: thirdPage} + } + const expected = { + data: [], + links: { + first: null, + last: null, + prev: null, + next: null } }