From 908fa9458903387616c114070c4d1f0f2cda9b18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Phan=20Kh=E1=BA=AFc=20=C4=90=E1=BA=A1o?= Date: Tue, 23 Jun 2020 02:10:52 +0700 Subject: [PATCH] Use requestHandler of next-server as the default handler (#92) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update index.js Use requestHandler of next-server as the default handler * Remove the opts.next check * Updated tests * test: Close the fastify instance in the callback for t.tearDown * test: Removed the unit test for the option next Co-authored-by: Phan Khắc Đạo --- index.d.ts | 1 - index.js | 10 ++------ pages/api/user.js | 5 ++++ test.js | 59 +++++++++++++++++++++++++---------------------- 4 files changed, 38 insertions(+), 37 deletions(-) create mode 100644 pages/api/user.js diff --git a/index.d.ts b/index.d.ts index 5d8a1aa9..ccb5f364 100644 --- a/index.d.ts +++ b/index.d.ts @@ -24,7 +24,6 @@ declare module 'fastify' { | { method: HTTPMethods; schema: FastifySchema; - next: Router; } | FastifyNextCallback, handle?: FastifyNextCallback diff --git a/index.js b/index.js index 713d50d7..44896d31 100644 --- a/index.js +++ b/index.js @@ -17,12 +17,7 @@ function fastifyNext (fastify, options, next) { app.close() }) .after(() => { - fastify.next('/_next/*', - (app, req, reply) => handleNextRequests(req.raw, reply.raw) - .then(() => { - reply.sent = true - }) - ) + fastify.next('/_next/*') }) next() }) @@ -38,7 +33,6 @@ function fastifyNext (fastify, options, next) { assert(typeof path === 'string', 'path must be a string') if (opts.method) { assert(typeof opts.method === 'string', 'options.method must be a string') } if (opts.schema) { assert(typeof opts.schema === 'object', 'options.schema must be an object') } - if (opts.next) { assert(typeof opts.next === 'object', 'options.next must be an object') } if (callback) { assert(typeof callback === 'function', 'callback must be a function') } const method = opts.method || 'get' @@ -49,7 +43,7 @@ function fastifyNext (fastify, options, next) { return callback(app, req, reply) } - app.render(req.raw, reply.res, path, req.query, opts.next || {}) + return handleNextRequests(req.raw, reply.raw) } } } diff --git a/pages/api/user.js b/pages/api/user.js new file mode 100644 index 00000000..30137a3e --- /dev/null +++ b/pages/api/user.js @@ -0,0 +1,5 @@ +export default (req, res) => { + res.statusCode = 200 + res.setHeader('Content-Type', 'application/json') + res.end(JSON.stringify({ name: 'John Doe' })) +} diff --git a/test.js b/test.js index 6c568701..988331f1 100644 --- a/test.js +++ b/test.js @@ -30,6 +30,8 @@ test('should return an html document', t => { t.plan(3) const fastify = Fastify() + t.tearDown(() => fastify.close()) + fastify .register(require('./index')) .after(() => { @@ -44,14 +46,14 @@ test('should return an html document', t => { t.equal(res.statusCode, 200) t.equal(res.headers['content-type'], 'text/html; charset=utf-8') }) - - fastify.close() }) test('should support different methods', t => { t.plan(3) const fastify = Fastify() + t.tearDown(() => fastify.close()) + fastify .register(require('./index')) .after(() => { @@ -66,14 +68,14 @@ test('should support different methods', t => { t.equal(res.statusCode, 200) t.equal(res.headers['content-type'], 'text/html; charset=utf-8') }) - - fastify.close() }) test('should support a custom handler', t => { t.plan(3) const fastify = Fastify() + t.tearDown(() => fastify.close()) + fastify .register(require('./index')) .after(() => { @@ -90,14 +92,14 @@ test('should support a custom handler', t => { t.equal(res.statusCode, 200) t.equal(res.headers['content-type'], 'text/html; charset=utf-8') }) - - fastify.close() }) test('should return 404 on undefined route', t => { t.plan(2) const fastify = Fastify() + t.tearDown(() => fastify.close()) + fastify .register(require('./index')) .after(() => { @@ -111,8 +113,6 @@ test('should return 404 on undefined route', t => { t.error(err) t.equal(res.statusCode, 404) }) - - fastify.close() }) test('should throw if path is not a string', t => { @@ -172,25 +172,6 @@ test('should throw if opts.schema is not an object', t => { fastify.close() }) -test('should throw if opts.next is not an object', t => { - t.plan(2) - - const fastify = Fastify() - fastify - .register(require('./index')) - .after(err => { - t.error(err) - try { - fastify.next('/hello', { next: 1 }) - t.fail() - } catch (e) { - t.equal(e.message, 'options.next must be an object') - } - }) - - fastify.close() -}) - test('should throw if callback is not a function', t => { t.plan(2) @@ -224,6 +205,8 @@ test('should serve /_next/* static assets', t => { fastify.next('/hello') }) + t.tearDown(() => fastify.close()) + const pagePrefix = `/_next/static/${buildId}/pages` testNextAsset(t, fastify, `${pagePrefix}/hello.js`) @@ -232,8 +215,28 @@ test('should serve /_next/* static assets', t => { const commonAssets = manifest.pages['/hello'] commonAssets.map(suffix => testNextAsset(t, fastify, `/_next/${suffix}`)) +}) - fastify.close() +test('should return a json data on api route', t => { + t.plan(3) + + const fastify = Fastify() + fastify + .register(require('./index')) + .after(() => { + fastify.next('/api/*') + }) + + t.tearDown(() => fastify.close()) + + fastify.inject({ + url: '/api/user', + method: 'GET' + }, (err, res) => { + t.error(err) + t.equal(res.statusCode, 200) + t.equal(res.headers['content-type'], 'application/json') + }) }) function testNextAsset (t, fastify, url) {