Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nextjs): obsolete a 2nd arg of handle #43

Merged
merged 1 commit into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 6 additions & 8 deletions src/nextjs.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { Hono } from 'hono'
import type { Env, Hono } from 'hono'
import { getRequestListener } from './listener'
import { HandleInterface } from './types'
import { NextApiHandler } from 'next/types'

// <E extends Hono<any, any>
export const handle: HandleInterface = <E extends Hono<any, any>>(
subApp: E,
path: string = '/'
) => {
return getRequestListener(new Hono().route(path, subApp).fetch)
export const handle = <E extends Env, S extends {}, BasePath extends string>(
app: Hono<E, S, BasePath>
): NextApiHandler => {
return getRequestListener(app.fetch)
}
6 changes: 0 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { Hono } from 'hono'
import type { NextApiHandler } from 'next/types'
import type { createServer } from 'node:http'

export type FetchCallback = (request: Request) => Promise<unknown> | unknown
Expand All @@ -15,7 +13,3 @@ export type Options = {
serverOptions?: Object
createServer?: typeof createServer
}

export interface HandleInterface {
<E extends Hono<any, any>>(subApp: E, path?: string): NextApiHandler
}
40 changes: 21 additions & 19 deletions test/nextjs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { installGlobals } from '../src/globals'
installGlobals()

describe('Basic', () => {
const app = new Hono()
const app = new Hono().basePath('/api')
app.get('/', (c) => c.text('Hello! Node!'))

app.get('/posts', (c) => {
Expand All @@ -19,7 +19,7 @@ describe('Basic', () => {
return c.text(`DELETE ${c.req.param('id')}`)
})

const server = handle(app, '/api')
const server = handle(app)

it('Should return 200 response - GET /api', async () => {
const res = await request(server).get('/api')
Expand Down Expand Up @@ -49,36 +49,38 @@ describe('Basic', () => {

describe('Routing', () => {
describe('Nested Route', () => {
const book = new Hono()
book.get('/', (c) => c.text('get /book'))
const book = new Hono().basePath('/api')
book.get('/', (c) => c.text('get /api'))
book.get('/:id', (c) => {
return c.text('get /book/' + c.req.param('id'))
return c.text('get /api/' + c.req.param('id'))
})
book.post('/', (c) => c.text('post /book'))
book.post('/', (c) => c.text('post /api'))

const app = new Hono()
app.route('/book', book)
app.route('/v2', book)

const server = handle(app, '/api')
app.showRoutes()

it('Should return responses from `/api/book/*`', async () => {
let res = await request(server).get('/api/book')
const server = handle(app)

it('Should return responses from `/v2/api/*`', async () => {
let res = await request(server).get('/v2/api')
expect(res.status).toBe(200)
expect(res.text).toBe(`get /book`)
expect(res.text).toBe(`get /api`)

res = await request(server).get('/api/book/123')
res = await request(server).get('/v2/api/123')
expect(res.status).toBe(200)
expect(res.text).toBe(`get /book/123`)
expect(res.text).toBe(`get /api/123`)

res = await request(server).post('/api/book')
res = await request(server).post('/v2/api')
expect(res.status).toBe(200)
expect(res.text).toBe(`post /book`)
expect(res.text).toBe(`post /api`)
})
})
})

describe('Request body', () => {
const app = new Hono()
const app = new Hono().basePath('/api')
app.post('/json', async (c) => {
const data = await c.req.json()
return c.json(data)
Expand All @@ -87,7 +89,7 @@ describe('Request body', () => {
const data = await c.req.parseBody()
return c.json(data)
})
const server = handle(app, '/api')
const server = handle(app)

it('Should handle JSON body', async () => {
const res = await request(server)
Expand All @@ -107,14 +109,14 @@ describe('Request body', () => {
})

describe('Response body', () => {
const app = new Hono()
const app = new Hono().basePath('/api')
app.get('/json', (c) => {
return c.json({ foo: 'bar' })
})
app.get('/html', (c) => {
return c.html('<h1>Hello!</h1>')
})
const server = handle(app, '/api')
const server = handle(app)

it('Should return JSON body', async () => {
const res = await request(server).get('/api/json')
Expand Down