Skip to content

Commit

Permalink
released v4.0.11-next.2 - added middleware test
Browse files Browse the repository at this point in the history
  • Loading branch information
kwhitley committed Jun 21, 2023
1 parent d7469f1 commit df4c44d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
17 changes: 12 additions & 5 deletions example/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { withParams } from 'itty-router'
import {
Router, // the router itself
IRequest, // lightweight/generic Request type
Expand All @@ -8,9 +9,10 @@ import {
error,
} from '../src'

// declare a custom Router type with used methods
interface CustomRouter extends RouterType {
puppy: Route
const myCustomMiddleware = (request: IRequest) => {
if (true) {
return false
}
}

// declare a custom Request type to allow request injection from middleware
Expand All @@ -28,14 +30,19 @@ const { corsify, preflight } = createCors()
const router = Router({ base: '/' })

router
.all('*', preflight)
.get<CustomRouter>('/authors', withAuthors, (request: RequestWithAuthors) => {
.all('*', preflight, withParams)
.get('/authors', withAuthors, (request: RequestWithAuthors) => {
return request.authors?.[0]
})
.puppy('/:name', (request) => {
const name = request.params.name
const foo = request.query.foo
})
.get('/whatever/:foo/:bar',
myCustomMiddleware,
myCustomMiddleware,
({ foo, bar }) => ({ foo, bar })
)
.all('*', () => error(404))

// CF ES6 module syntax
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "itty-router",
"version": "4.0.11-next.1",
"version": "4.0.11-next.2",
"description": "A tiny, zero-dependency router, designed to make beautiful APIs in any environment.",
"main": "./index.js",
"module": "./index.mjs",
Expand Down
17 changes: 17 additions & 0 deletions src/Router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,23 @@ describe('Router', () => {
})
})

describe('MIDDLEWARE', () => {
it('calls any handler until a return', async () => {
const router = Router()
const h1 = vi.fn()
const h2 = vi.fn()
const h3 = vi.fn(() => true)

router.get('*', h1, h2, h3)

const results = await router.handle(buildRequest({ path: '/' }))
expect(h1).toHaveBeenCalled()
expect(h2).toHaveBeenCalled()
expect(h3).toHaveBeenCalled()
expect(results).toBe(true)
})
})

describe('ROUTE MATCHING', () => {
describe('allowed characters', () => {
const chars = `/foo/-.abc!@%&_=:;',~|/bar`
Expand Down

0 comments on commit df4c44d

Please sign in to comment.