diff --git a/example/server.js b/example/server.js index 1872531a..eb3b0276 100644 --- a/example/server.js +++ b/example/server.js @@ -7,7 +7,7 @@ const fs = require('fs') const path = require('path') const debug = require('debug') -jsonApi.setConfig({ +const config = { graphiql: true, swagger: { title: 'Example JSON:API Server', @@ -43,7 +43,9 @@ jsonApi.setConfig({ meta: { description: 'This block shows up in the root node of every payload' } -}) +} + +jsonApi.setConfig(config) jsonApi.authenticate((request, callback) => { // If a "blockMe" header is provided, block access. @@ -79,4 +81,5 @@ if (typeof describe === 'undefined') { } server.start = jsonApi.start server.close = jsonApi.close +server.setupTestServer = opts => (jsonApi.setConfig({...config, ...opts}), jsonApi.start()) server.getExpressServer = jsonApi.getExpressServer diff --git a/lib/pagination.js b/lib/pagination.js index b5d5978d..ffc2595e 100644 --- a/lib/pagination.js +++ b/lib/pagination.js @@ -1,6 +1,7 @@ 'use strict' const pagination = module.exports = { } +const jsonApi = require('./jsonApi.js') const ourJoi = require('./ourJoi.js') const url = require('url') @@ -27,8 +28,9 @@ pagination.validatePaginationParams = request => { } const page = request.params.page - page.offset = parseInt(page.offset, 10) || 0 - page.limit = parseInt(page.limit, 10) || 50 + // Set your own pagination defaults through setConfig() + page.offset = parseInt(page.offset, 10) || (jsonApi._apiConfig.page && jsonApi._apiConfig.page.offset) || 0 + page.limit = parseInt(page.limit, 10) || (jsonApi._apiConfig.page && jsonApi._apiConfig.page.limit) || 50 } pagination.enforcePagination = (request, results) => results.slice(0, request.params.page.size) diff --git a/test/pagination.js b/test/pagination.js index b721169e..fddf5421 100644 --- a/test/pagination.js +++ b/test/pagination.js @@ -169,3 +169,49 @@ describe('Testing jsonapi-server', () => { jsonApiTestServer.close() }) }) + +describe('Testing configurable pagination', () => { + after(() => { + jsonApiTestServer.close() + }) + + it('Setting page defaults will set offset and limit', done => { + jsonApiTestServer.setupTestServer({ page: { offset: 1, limit: 3 }}) + + const data = { + method: 'get', + url: 'http://localhost:16006/rest/tags' + } + helpers.request(data, (err, res, json) => { + assert.strictEqual(err, null) + json = helpers.validateJson(json) + + assert.strictEqual(res.statusCode, 200, 'Expecting 200') + assert.strictEqual(json.meta.page.offset, 1, 'should be at offset 0') + assert.strictEqual(json.meta.page.limit, 3, 'should have a limit of 3 record') + assert.strictEqual(json.meta.page.total, 5, 'should have a total of 5 records') + + done() + }) + }) + + it('Setting no page defaults will use the jagql defaults', done => { + jsonApiTestServer.setupTestServer() + + const data = { + method: 'get', + url: 'http://localhost:16006/rest/tags' + } + helpers.request(data, (err, res, json) => { + assert.strictEqual(err, null) + json = helpers.validateJson(json) + + assert.strictEqual(res.statusCode, 200, 'Expecting 200') + assert.strictEqual(json.meta.page.offset, 0, 'should be at offset 0') + assert.strictEqual(json.meta.page.limit, 50, 'should have a limit of 50 record') + assert.strictEqual(json.meta.page.total, 5, 'should have a total of 5 records') + + done() + }) + }) +})