itty-router 2.5.2 → 2.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,9 @@ Until this library makes it to a production release of v1.x, **minor versions ma
3
3
 
4
4
  [@SupremeTechnopriest)(https://github.com/SupremeTechnopriest) - improved TypeScript support and documentation! :D\
5
5
 
6
+ - **v2.6.1** - fixes named export in ESM/mjs export
7
+ - **v2.6.0** - package now is hybrid export, supporting both ESM (.mjs) and CJS (.js) minified versions
8
+ - **v2.5.3** - corrects type for router.handle to return Promise<any>
6
9
  - **v2.5.2** - fixes issue with arrow functions in CommonJS named exports (rare issue)
7
10
  - **v2.5.1** - added context to Cloudflare ES module syntax example (credit [@jcapogna](https://github.com/jcapogna))
8
11
  - **v2.5.0** - improved TypeScript docs/types (thanks [@SupremeTechnopriest](https://github.com/SupremeTechnopriest)!)
package/README.md CHANGED
@@ -19,7 +19,7 @@
19
19
  <img alt="Join the discussion on Github" src="https://img.shields.io/badge/Github%20Discussions%20%26%20Support-Chat%20now!-blue" />
20
20
  </a>-->
21
21
 
22
- It's an itty bitty router, designed for express-like routing within [Cloudflare Workers](https://developers.cloudflare.com/workers/) (or anywhere else). Like... it's super tiny ([~500 bytes](https://bundlephobia.com/package/itty-router)), with zero dependencies. For reals.
22
+ Tiny, zero-dependency router with route param and query parsing - built for [Cloudflare Workers](https://developers.cloudflare.com/workers/), but works everywhere!
23
23
 
24
24
  ### Addons & Related Libraries
25
25
  1. [itty-router-extras](https://www.npmjs.com/package/itty-router-extras) - adds quality-of-life improvements and utility functions for simplifying request/response code (e.g. middleware, cookies, body parsing, json handling, errors, and an itty version with automatic exception handling)!
@@ -27,9 +27,9 @@ It's an itty bitty router, designed for express-like routing within [Cloudflare
27
27
 
28
28
  ## Features
29
29
  - [x] Tiny ([~500 bytes](https://bundlephobia.com/package/itty-router) compressed), with zero dependencies.
30
- - [x] [TypeScript support](#typescript)!
31
- - [x] Full sync/async support. Use it when you need it!
32
- - [x] Route params, with wildcards and optionals (e.g. `/api/:collection/:id?`)
30
+ - [x] [Fully typed/TypeScript support](#typescript)
31
+ - [x] Supports sync/async handlers/middleware.
32
+ - [x] Parses route params, with wildcards and optionals (e.g. `/api/:collection/:id?`)
33
33
  - [x] Query parsing (e.g. `?page=3&foo=bar`)
34
34
  - [x] [Middleware support](#middleware). Any number of sync/async handlers may be passed to a route.
35
35
  - [x] [Nestable](#nested-routers-with-404-handling). Supports nesting routers for API branching.
@@ -105,9 +105,9 @@ router.get('/todos/:user/:item?', (req) => {
105
105
  ```
106
106
 
107
107
  ### 3. Handle Incoming Request(s)
108
- ##### `async router.handle(request.proxy: Proxy || request: Request, ...anything else)`
108
+ ##### `async router.handle(request.proxy: Proxy || request: Request, ...anything else) => Promise<any>`
109
109
  Requests (doesn't have to be a real Request class) should have both a **method** and full **url**.
110
- The `handle` method will then return the first matching route handler that returns something (or nothing at all if no match).
110
+ The `handle` method will then return a Promise, resolving with the first matching route handler that returns something (or nothing at all if no match).
111
111
 
112
112
  ```js
113
113
  router.handle({
@@ -301,7 +301,7 @@ export default {
301
301
  export default {
302
302
  fetch: (...args) => router
303
303
  .handle(...args)
304
- .then(response =>
304
+ .then(response => {
305
305
  // can modify response here before final return, e.g. CORS headers
306
306
 
307
307
  return response
@@ -505,42 +505,6 @@ router.puppy('/', request => {}) // Strongly typed
505
505
  1. Submit PR with a detailed description of what you're doing
506
506
  1. I'll add you to the credits! :)
507
507
 
508
- ### The Entire Code (for more legibility, [see src on GitHub](https://github.com/kwhitley/itty-router/blob/v2.x/src/itty-router.js))
509
- ```js
510
- function Router({ base = '', routes = [] } = {}) {
511
- return {
512
- __proto__: new Proxy({}, {
513
- get: (t, k, c) => (p, ...H) =>
514
- routes.push([
515
- k.toUpperCase(),
516
- RegExp(`^${(base + p)
517
- .replace(/(\/?)\*/g, '($1.*)?')
518
- .replace(/\/$/, '')
519
- .replace(/:(\w+)(\?)?(\.)?/g, '$2(?<$1>[^/]+)$2$3')
520
- .replace(/\.(?=[\w(])/, '\\.')
521
- .replace(/\)\.\?\(([^\[]+)\[\^/g, '?)\\.?($1(?<=\\.)[^\\.') // RIP all the bytes lost :'(
522
- }/*$`),
523
- H,
524
- ]) && c
525
- }),
526
- routes,
527
- async handle (q, ...a) {
528
- let s, m,
529
- u = new URL(q.url)
530
- q.query = Object.fromEntries(u.searchParams)
531
- for (let [M, p, H] of routes) {
532
- if ((M === q.method || M === 'ALL') && (m = u.pathname.match(p))) {
533
- q.params = m.groups
534
- for (let h of H) {
535
- if ((s = await h(q.proxy || q, ...a)) !== undefined) return s
536
- }
537
- }
538
- }
539
- }
540
- }
541
- }
542
- ```
543
-
544
508
  ## Special Thanks
545
509
  This repo goes out to my past and present colleagues at Arundo - who have brought me such inspiration, fun,
546
510
  and drive over the last couple years. In particular, the absurd brevity of this code is thanks to a
@@ -42,7 +42,7 @@ export interface IHTTPMethods {
42
42
  }
43
43
 
44
44
  export type Router<TRequest = Request, TMethods = {}> = {
45
- handle: (request: TRequest, ...extra: any) => any
45
+ handle: (request: TRequest, ...extra: any) => Promise<any>
46
46
  routes: RouteEntry<TRequest>[]
47
47
  all: Route
48
48
  } & TMethods & {
@@ -0,0 +1 @@
1
+ function e({base:t="",routes:n=[]}={}){return{__proto__:new Proxy({},{get:(e,a,o)=>(e,...r)=>n.push([a.toUpperCase(),RegExp(`^${(t+e).replace(/(\/?)\*/g,"($1.*)?").replace(/\/$/,"").replace(/:(\w+)(\?)?(\.)?/g,"$2(?<$1>[^/]+)$2$3").replace(/\.(?=[\w(])/,"\\.").replace(/\)\.\?\(([^\[]+)\[\^/g,"?)\\.?($1(?<=\\.)[^\\.")}/*$`),r])&&o}),routes:n,async handle(e,...r){let a,o,t=new URL(e.url);e.query=Object.fromEntries(t.searchParams);for(var[p,s,u]of n)if((p===e.method||"ALL"===p)&&(o=t.pathname.match(s))){e.params=o.groups;for(var c of u)if(void 0!==(a=await c(e.proxy||e,...r)))return a}}}}export default{Router:e};export{e as Router};
package/package.json CHANGED
@@ -1,13 +1,21 @@
1
1
  {
2
2
  "name": "itty-router",
3
- "version": "2.5.2",
3
+ "version": "2.6.1",
4
4
  "description": "Tiny, zero-dependency router with route param and query parsing - built for Cloudflare Workers, but works everywhere!",
5
- "main": "./dist/itty-router.min.js",
6
- "types": "./dist/itty-router.d.ts",
5
+ "sourceType": "module",
7
6
  "files": [
8
7
  "dist/itty-router.min.js",
8
+ "dist/itty-router.min.mjs",
9
9
  "dist/itty-router.d.ts"
10
10
  ],
11
+ "main": "./dist/itty-router.min.js",
12
+ "exports": {
13
+ ".": {
14
+ "require": "./dist/itty-router.min.js",
15
+ "import": "./dist/itty-router.min.mjs"
16
+ }
17
+ },
18
+ "types": "./dist/itty-router.d.ts",
11
19
  "keywords": [
12
20
  "router",
13
21
  "cloudflare",
@@ -47,7 +55,11 @@
47
55
  "coveralls": "cat ./coverage/lcov.info | node node_modules/.bin/coveralls",
48
56
  "prerelease": "yarn verify",
49
57
  "prebuild": "rimraf dist && mkdir dist && node prebuild.js && cp src/itty-router.d.ts dist",
50
- "build": "uglifyjs dist/itty-router.js -c -m --toplevel > dist/itty-router.min.js",
58
+ "build": "yarn uglify",
59
+ "uglify": "yarn uglify:cjs && yarn uglify:esm",
60
+ "uglify:esm": "uglifyjs dist/itty-router.mjs -c -m --toplevel > dist/itty-router.min.mjs",
61
+ "uglify:cjs": "uglifyjs src/itty-router.js -c -m --toplevel > dist/itty-router.min.js",
62
+ "build:esm": "esbuild src/itty-router.js --format=esm --outfile=./dist/itty-router.mjs",
51
63
  "postbuild": "node check-size.js",
52
64
  "release": "release --tag --push"
53
65
  },
@@ -62,20 +74,20 @@
62
74
  },
63
75
  "homepage": "https://itty-router.dev",
64
76
  "devDependencies": {
65
- "@vitejs/plugin-vue": "^1.4.0",
66
- "@vue/compiler-sfc": "^3.2.2",
67
- "chalk": "^4.1.2",
77
+ "@vitejs/plugin-vue": "^2.2.4",
68
78
  "coveralls": "^3.1.1",
69
- "eslint": "^7.32.0",
70
- "eslint-plugin-jest": "^24.4.0",
71
- "fs-extra": "^10.0.0",
79
+ "esbuild": "^0.14.27",
80
+ "eslint": "^8.11.0",
81
+ "eslint-plugin-jest": "^26.1.2",
82
+ "fs-extra": "^10.0.1",
72
83
  "gzip-size": "^6.0.0",
73
84
  "isomorphic-fetch": "^3.0.0",
74
- "jest": "^27.0.6",
85
+ "jest": "^27.5.1",
75
86
  "rimraf": "^3.0.2",
76
- "uglify-js": "^3.14.1",
77
- "vite": "^2.4.4",
78
- "yarn": "^1.22.11",
87
+ "uglify-js": "^3.15.3",
88
+ "vite": "^2.8.6",
89
+ "vue": "^3.2.31",
90
+ "yarn": "^1.22.18",
79
91
  "yarn-release": "^1.10.3"
80
92
  }
81
93
  }