Skip to content

Commit

Permalink
fix: correctly expose route and middleware types (#10)
Browse files Browse the repository at this point in the history
* fix: correctly expose route and middleware types

* test: add test for non-matching async route

* includes commented-out test to match #11

* feat: allow returning undefined to a non-middleware function

closes #11

* fix: remove support for returning undefined from handler

* chore: remove ts-ignore
  • Loading branch information
danielroe committed Nov 23, 2020
1 parent 091481d commit bb6cd4c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ export interface InputLayer {
export type InputStack = InputLayer[]

export interface AppUse {
(route: string, handle: Handle, options?: Partial<InputLayer>): App
(handle: Handle, options?: Partial<InputLayer>): App
(route: string | string[], handle: Middleware | Middleware[], options?: Partial<InputLayer & { promisify: true }>): App
(route: string | string[], handle: Handle | Handle[], options?: Partial<InputLayer>): App
(handle: Middleware | Middleware[], options?: Partial<InputLayer & { promisify: true }>): App
(handle: Handle | Handle[], options?: Partial<InputLayer>): App
(options: InputLayer): App
(route: string, handles: Handle[]): App
(handles: Handle[]): App
}

export interface App {
Expand Down
27 changes: 21 additions & 6 deletions test/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ describe('app', () => {
return await Promise.resolve('42')
})

// @ts-ignore
// eslint-disable-next-line
app.useAsync(async (_req, _res, next) => {
next()
Expand All @@ -66,8 +65,7 @@ describe('app', () => {
})

it('can use route arrays', async () => {
// TODO: this should not require type annotation
app.use(['/1', '/2'] as any, () => 'valid')
app.use(['/1', '/2'], () => 'valid')

const responses = [
await request.get('/1'),
Expand All @@ -77,14 +75,31 @@ describe('app', () => {
})

it('can use handler arrays', async () => {
// TODO: fix type issue
// @ts-ignore
app.use('/', [(_req, _res, next) => { next() }, () => 'valid'])
app.use('/', [
(_req, _res, next) => { next() },
(_req, _res, next) => next(),
// eslint-disable-next-line
async (_req, _res, next) => { next() },
() => 'valid'
])

const response = await request.get('/')
expect(response.text).toEqual('valid')
})

it('prohibits use of next() in non-promisified handlers', () => {
// @ts-expect-error
app.use('/', (_req, _res, next) => next(), { promisify: false })
})

it('handles next() call with no routes matching', async () => {
app.use('/', (_req, _res, next) => next())
app.use('/', () => {}, { promisify: false })

const response = await request.get('/')
expect(response.status).toEqual(404)
})

it('can take an object', async () => {
app.use({ route: '/', handle: () => 'valid' })

Expand Down
3 changes: 1 addition & 2 deletions test/integrations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ describe('integrations with other frameworks', () => {

it('can be used as express middleware', async () => {
const expressApp = express()
// @ts-ignore - remove when #10 is merged
app.use('/api/hello', (_req, res, next) => {
res.prop = '42'
;(res as any).prop = '42'
next()
})
app.use('/api/hello', (req, res) => ({ url: req.url, prop: (res as any).prop }))
Expand Down

0 comments on commit bb6cd4c

Please sign in to comment.