Skip to content

Commit

Permalink
feat: custom matcher and improved docs
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Nov 21, 2020
1 parent 7625c53 commit 1c4f9d1
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 4 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
- Tree-shakable and zero dependency
- Promise and `aync/await` support
- Lazy Loading
- Basic Router
- Quick prefix router
- Custom route matcher

> See [un](https://github.com/nuxt-contrib/un) for workers support
Expand All @@ -33,10 +34,20 @@ const { createApp } = require('@nuxt/h2')

const app = createApp()

// Handle can directly return object or Promise<object> for JSON response
app.use('/api', (req) => ({ url: req.url }))
app.use(() => 'Hello world!')

// You can have better matching other than quick prefix match
app.use('/odd', () => 'Is odd!', { match: url => url.substr(1) % 2 })

// Handle can directly return string for HTML response
app.use(() => '<h1>Hello world!</h1>')

// If handle is already async, using useAsync to avoid unnecessary promisify wrapper
// (Shrotcut to pass { proimsify: false })
// app.useAsync(async () => {})

// Lazy loading routes using { lazy: true }
// app.use('/big', () => import('./big'), { lazy: true })

const port = process.env.PORT || 3000
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"eslint": "latest",
"express": "^4.17.1",
"jest": "^26.6.3",
"jiti": "^0.1.12",
"siroc": "latest",
"standard-version": "latest",
"ts-jest": "^26.4.4",
Expand Down
5 changes: 5 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export function use (

export function createHandle (stack: Stack): PHandle {
return async function handle (req: IncomingMessage, res: ServerResponse) {
req.url = req.url || '/'
const originalUrl = (req as any).originalUrl = (req as any).originalUrl || req.url || '/'
for (const layer of stack) {
if (layer.route.length) {
Expand All @@ -56,6 +57,9 @@ export function createHandle (stack: Stack): PHandle {
}
req.url = originalUrl.substr(layer.route.length) || '/'
}
if (layer.match && !layer.match(req.url as string, req)) {
continue
}
const val = await layer.handle(req, res)
if (res.writableEnded) {
break
Expand All @@ -78,6 +82,7 @@ export function createHandle (stack: Stack): PHandle {
function normalizeLayer (layer: InputLayer) {
return {
route: stripTrailingSlash(layer.route).toLocaleLowerCase(),
match: layer.match,
handle: layer.lazy
? lazyHandle(layer.handle as LazyHandle, layer.promisify)
: (layer.promisify !== false ? promisifyHandle(layer.handle) : layer.handle)
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ export type PHandle = (req: IncomingMessage, res: ServerResponse) => Promise<any

export type Middleware = (req: IncomingMessage, res: ServerResponse, next: (err?: Error) => any) => any

export type Matcher = (url: string, req?: IncomingMessage) => Boolean

export interface Layer {
route: string
match?: Matcher
handle: Handle
}

Expand All @@ -17,6 +20,7 @@ export type LazyHandle = () => Handle | Promise<Handle>

export interface InputLayer {
route?: string
match?: Matcher
handle: Handle | LazyHandle
lazy?: boolean
promisify?: boolean
Expand Down
3 changes: 2 additions & 1 deletion test/fixture/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { Server } = require('http')
const express = require('express')
const { createApp } = require('../..')
const { createApp } = require('jiti')(__dirname)('../../src')

function createExpress () {
const app = express()
Expand All @@ -15,6 +15,7 @@ const app = createApp()
app.use('/api/hello', req => ({ url: req.url }))
app.use('/api/express', createExpress())
app.use('/api', (_req, res) => { res.end('API root') })
app.use('/odd', () => 'Is odd!', { match: url => url.substr(1) % 2 })
app.use('/', () => 'Hello world!')

const port = process.env.PORT || 3000
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3742,7 +3742,7 @@ jest@^26.6.3:
import-local "^3.0.2"
jest-cli "^26.6.3"

jiti@^0.1.11:
jiti@^0.1.11, jiti@^0.1.12:
version "0.1.12"
resolved "https://registry.yarnpkg.com/jiti/-/jiti-0.1.12.tgz#ffe4a09266d739b77721170fb73115c50b680330"
integrity sha512-rWtqsaABBGNzTzfQkfP7DUvE9KGEIQUd6s0nITw5rDW1L9OwwtBQ5dz9hMq4YrMZqrSJr8ArrMvZ1mV1/7S3Fg==
Expand Down

0 comments on commit 1c4f9d1

Please sign in to comment.