Skip to content

Commit

Permalink
Merge branch 'v4.x' into 175-status-and-response
Browse files Browse the repository at this point in the history
  • Loading branch information
kwhitley committed Jun 21, 2023
2 parents 061aeab + af17894 commit 1463289
Show file tree
Hide file tree
Showing 8 changed files with 325 additions and 193 deletions.
19 changes: 4 additions & 15 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
### Description

Please explain the changes you made here.

### Issue Related

- Link to the related issue:
### Related Issue
Link to the related issue:

### Type of Change (select one and follow subtasks)

- [ ] Documentation (README.md)
- [ ] Maintenance or repo-level work (e.g. linting, build, tests, refactoring, etc.)
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Is this a mainstream benefit or an edge case?
- [ ] Is it worth the bytes?
- [ ] I have included test coverage
- [ ] Breaking change (fix or feature that would cause existing functionality/userland code to not work as expected)
- [ ] Explain why a breaking change is necessary:
- [ ] This change requires (or is) a documentation update
- [ ] I have added necessary local documentation (if appropriate)
- [ ] I have added necessary [itty.dev](https://github.com/kwhitley/itty.dev) documentation (if appropriate)

### Testing

Please describe the tests that you ran to verify your changes.

### Checklist

- [ ] I have read the [CONTRIBUTING](../CONTRIBUTING.md) doc
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 example/node-require.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { createServerAdapter } = require('@whatwg-node/server')
const { createServer } = require('http')
require('isomorphic-fetch')
const { Router, error, json } = require('itty-router')
const { Router, error, json } = require('../dist/index.js')

const router = Router()

Expand Down
119 changes: 103 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,97 @@
{
"name": "itty-router",
"version": "4.0.11",
"version": "4.0.12",
"description": "A tiny, zero-dependency router, designed to make beautiful APIs in any environment.",
"type": "module",
"main": "./cjs/index.js",
"module": "./index.js",
"main": "./index.js",
"module": "./index.mjs",
"types": "./index.d.ts",
"exports": {
".": {
"import": "./index.mjs",
"require": "./index.js",
"types": "./index.d.ts"
},
"./createCors": {
"import": "./createCors.mjs",
"require": "./createCors.js",
"types": "./createCors.d.ts"
},
"./createResponse": {
"import": "./createResponse.mjs",
"require": "./createResponse.js",
"types": "./createResponse.d.ts"
},
"./error": {
"import": "./error.mjs",
"require": "./error.js",
"types": "./error.d.ts"
},
"./html": {
"import": "./html.mjs",
"require": "./html.js",
"types": "./html.d.ts"
},
"./jpeg": {
"import": "./jpeg.mjs",
"require": "./jpeg.js",
"types": "./jpeg.d.ts"
},
"./json": {
"import": "./json.mjs",
"require": "./json.js",
"types": "./json.d.ts"
},
"./png": {
"import": "./png.mjs",
"require": "./png.js",
"types": "./png.d.ts"
},
"./Router": {
"import": "./Router.mjs",
"require": "./Router.js",
"types": "./Router.d.ts"
},
"./status": {
"import": "./status.mjs",
"require": "./status.js",
"types": "./status.d.ts"
},
"./StatusError": {
"import": "./StatusError.mjs",
"require": "./StatusError.js",
"types": "./StatusError.d.ts"
},
"./text": {
"import": "./text.mjs",
"require": "./text.js",
"types": "./text.d.ts"
},
"./webp": {
"import": "./webp.mjs",
"require": "./webp.js",
"types": "./webp.d.ts"
},
"./websocket": {
"import": "./websocket.mjs",
"require": "./websocket.js",
"types": "./websocket.d.ts"
},
"./withContent": {
"import": "./withContent.mjs",
"require": "./withContent.js",
"types": "./withContent.d.ts"
},
"./withCookies": {
"import": "./withCookies.mjs",
"require": "./withCookies.js",
"types": "./withCookies.d.ts"
},
"./withParams": {
"import": "./withParams.mjs",
"require": "./withParams.js",
"types": "./withParams.d.ts"
}
},
"keywords": [
"api",
"router",
Expand All @@ -30,6 +116,7 @@
"coveralls": "yarn coverage && cat ./coverage/lcov.info | coveralls",
"verify": "echo 'verifying module...' && yarn build && yarn test:once",
"prerelease": "yarn verify",
"prerelease:next": "yarn verify",
"prebuild": "rimraf dist && mkdir dist",
"build": "rollup -c",
"release": "release --tag --push --patch --src=dist",
Expand All @@ -53,35 +140,35 @@
"@rollup/plugin-terser": "^0.4.3",
"@rollup/plugin-typescript": "^11.1.1",
"@skypack/package-check": "^0.2.2",
"@types/node": "^20.2.5",
"@typescript-eslint/eslint-plugin": "^5.59.8",
"@typescript-eslint/parser": "^5.59.8",
"@types/node": "^20.3.1",
"@typescript-eslint/eslint-plugin": "^5.60.0",
"@typescript-eslint/parser": "^5.60.0",
"@vitejs/plugin-vue": "^4.2.3",
"@vitest/coverage-c8": "^0.31.3",
"@whatwg-node/server": "^0.8.1",
"@vitest/coverage-c8": "^0.32.2",
"@whatwg-node/server": "^0.8.12",
"coveralls": "^3.1.1",
"eslint": "^8.41.0",
"eslint": "^8.43.0",
"eslint-config-prettier": "^8.8.0",
"fetch-mock": "^9.11.0",
"fs-extra": "^11.1.1",
"globby": "^13.1.4",
"globby": "^13.2.0",
"gzip-size": "^7.0.0",
"http": "^0.0.1-security",
"isomorphic-fetch": "^3.0.0",
"itty-router": "^4.0.10-next.4",
"itty-router": "^4.0.11",
"jsdom": "^22.1.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.8.8",
"rimraf": "^5.0.1",
"rollup": "^3.23.0",
"rollup": "^3.25.1",
"rollup-plugin-bundle-size": "^1.0.3",
"rollup-plugin-copy": "^3.4.0",
"rollup-plugin-multi-input": "^1.4.1",
"ts-node": "^10.9.1",
"typescript": "^5.0.4",
"typescript": "^5.1.3",
"vite": "^4.3.9",
"vitest": "^0.31.3",
"vitest": "^0.32.2",
"yarn": "^1.22.19",
"yarn-release": "^1.10.5"
"yarn-release": "^1.10.6"
}
}
42 changes: 31 additions & 11 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,38 @@ import typescript from '@rollup/plugin-typescript'
import { globby } from 'globby'
import bundleSize from 'rollup-plugin-bundle-size'
import copy from 'rollup-plugin-copy'
import fs from 'fs-extra'

export default async () => {
const files = (await globby('./src/*.ts', {
ignore: ['**/*.spec.ts', 'example'],
})).map(path => ({
path,
shortPath: path.replace(/(\/src)|(\.ts)/g, '').replace('./index', '.'),
esm: path.replace('/src/', '/dist/').replace('.ts', '.js'),
cjs: path.replace('/src/', '/dist/cjs/').replace('.ts', '.js'),
types: path.replace('/src/', '/dist/').replace('.ts', '.d.ts'),
}))
// scan files to build
const files = (await globby('./src/*.ts', {
ignore: ['**/*.spec.ts', 'example'],
})).map(path => ({
path,
shortPath: path.replace(/(\/src)|(\.ts)/g, '').replace('./index', '.'),
esm: path.replace('/src/', '/dist/').replace('.ts', '.mjs'),
cjs: path.replace('/src/', '/dist/').replace('.ts', '.js'),
types: path.replace('/src/', '/dist/').replace('.ts', '.d.ts'),
})).sort((a, b) => a.shortPath.toLowerCase() < b.shortPath.toLowerCase() ? -1 : 1)


// read original package.json
const pkg = await fs.readJSON('./package.json')

// create updated exports list from build files
pkg.exports = files.reduce((acc, file) => {
acc[file.shortPath] = {
import: file.esm.replace('/dist', ''),
require: file.cjs.replace('/dist', ''),
types: file.types.replace('/dist', ''),
}

return acc
}, {})

// write updated package.json
await fs.writeJSON('./package.json', pkg, { spaces: 2 })

export default async () => {
console.log(files.map(f => f.path))

return files.map(file => ({
Expand All @@ -33,7 +53,7 @@ export default async () => {
],
plugins: [
typescript({ sourceMap: true }),
// terser(),
terser(),
bundleSize(),
copy({
targets: [
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
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"lib": ["esnext", "dom", "dom.iterable"],
"listEmittedFiles": false,
"listFiles": false,
"moduleResolution": "node",
"moduleResolution": "nodeNext",
"noFallthroughCasesInSwitch": true,
"pretty": true,
"resolveJsonModule": true,
Expand Down
Loading

0 comments on commit 1463289

Please sign in to comment.