Skip to content

Commit

Permalink
Use requestHandler of next-server as the default handler (#92)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
daopk and Phan Khắc Đạo authored Jun 22, 2020
1 parent 80f57f1 commit 908fa94
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 37 deletions.
1 change: 0 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ declare module 'fastify' {
| {
method: HTTPMethods;
schema: FastifySchema;
next: Router;
}
| FastifyNextCallback,
handle?: FastifyNextCallback
Expand Down
10 changes: 2 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand All @@ -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'
Expand All @@ -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)
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions pages/api/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default (req, res) => {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify({ name: 'John Doe' }))
}
59 changes: 31 additions & 28 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -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(() => {
Expand All @@ -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(() => {
Expand All @@ -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(() => {
Expand All @@ -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 => {
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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`)
Expand All @@ -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) {
Expand Down

0 comments on commit 908fa94

Please sign in to comment.