itty-router 4.0.6 → 4.0.8

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/CONTRIBUTING.md CHANGED
@@ -2,8 +2,8 @@
2
2
 
3
3
  The [Open Source Guides](https://opensource.guide/) website has a collection of resources for individuals, communities, and companies. These resources help people who want to learn how to run and contribute to open source projects. Contributors and people new to open source alike will find the following guides especially useful:
4
4
 
5
- * [How to Contribute to Open Source](https://opensource.guide/how-to-contribute/)
6
- * [Building Welcoming Communities](https://opensource.guide/building-community/)
5
+ - [How to Contribute to Open Source](https://opensource.guide/how-to-contribute/)
6
+ - [Building Welcoming Communities](https://opensource.guide/building-community/)
7
7
 
8
8
  ## Bugs
9
9
 
package/README.md CHANGED
@@ -8,13 +8,12 @@
8
8
  <h2 align="center"><a href="https://itty.dev/itty-router">v4.x Documentation @ itty.dev</a>
9
9
  <br /></h2>
10
10
 
11
-
12
11
  <p align="center">
13
12
  <a href="https://npmjs.com/package/itty-router" target="_blank">
14
13
  <img src="https://img.shields.io/npm/v/itty-router.svg?style=flat-square" alt="npm version" />
15
14
  </a>
16
- <a href="https://bundlephobia.com/result?p=itty-router" target="_blank">
17
- <img src="https://img.shields.io/bundlephobia/minzip/itty-router?style=flat-square" alt="bundle size" />
15
+ <a href="https://deno.bundlejs.com/?q=itty-router/Router" target="_blank">
16
+ <img src="https://deno.bundlejs.com/?q=itty-router/Router&badge&badge-style=flat-square" alt="bundle size" />
18
17
  </a>
19
18
  <a href="https://github.com/kwhitley/itty-router/actions/workflows/verify.yml" target="_blank">
20
19
  <img src="https://img.shields.io/github/actions/workflow/status/kwhitley/itty-router/verify.yml?branch=v4.x&style=flat-square" alt="build status" />
@@ -50,48 +49,69 @@
50
49
 
51
50
  ---
52
51
 
53
- Itty aims to be the world's smallest (~440 bytes), feature-rich JavaScript router, enabling beautiful API code with a near-zero bundlesize. Designed originally for [Cloudflare Workers](https://itty.dev/itty-router/runtimes#Cloudflare%20Workers), itty can be used in browsers, Service Workers, edge functions, or standalone runtimes like [Node](https://itty.dev/itty-router/runtimes#Node), [Bun](https://itty.dev/itty-router/runtimes#Bun), etc.!
52
+ Itty aims to be the world's smallest (~440 bytes), feature-rich JavaScript router, enabling beautiful API code with a near-zero bundlesize. Designed originally for [Cloudflare Workers](https://itty.dev/itty-router/runtimes#Cloudflare%20Workers), itty can be used in browsers, Service Workers, edge functions, or standalone runtimes like [Node](https://itty.dev/itty-router/runtimes#Node), [Bun](https://itty.dev/itty-router/runtimes#Bun), etc.!
54
53
 
55
54
  ## Features:
56
- - [fully typed/TypeScript support](https://itty.dev/itty-router/typescript)
57
- - [route params](https://itty.dev/itty-router/route-patterns#params) (including [optional params](https://itty.dev/itty-router/route-patterns#optional))
58
- - [wildcards](https://itty.dev/itty-router/route-patterns#wildcards) and [greedy params](https://itty.dev/itty-router/route-patterns#greedy)
59
- - [query parsing](https://itty.dev/itty-router/route-patterns#query)
60
- - [middleware](https://itty.dev/itty-router/middleware)
61
- - [router nesting](https://itty.dev/itty-router/nesting)
62
- - lightweight, easy-to-understand route code
63
- - works in virtually [*any* runtime/environment](https://itty.dev/itty-router/runtimes) (including the browser)
55
+
56
+ - Absurdly tiny. The Router itself is ~440 bytes gzipped, and the **entire** library is under 1.5k!
57
+ - Absurdly easy to use. We believe route code should be self-evident, obvious, and read more like poetry than code.
58
+ - Absurdly agnostic. We leave **you** with full control over response types, matching order, upstream/downstream effects, etc.
59
+ - Works [anywhere, in any environment](https://itty.dev/itty-router/runtimes).
60
+ - [Fully typed/TypeScript support](https://itty.dev/itty-router/typescript), including hinting.
61
+ - Parses [route params](https://itty.dev/itty-router/route-patterns#params),
62
+ [optional params](https://itty.dev/itty-router/route-patterns#optional),
63
+ [wildcards](https://itty.dev/itty-router/route-patterns#wildcards),
64
+ [greedy params](https://itty.dev/itty-router/route-patterns#greedy),
65
+ and [file formats](https://itty.dev/itty-router/route-patterns#file-formats).
66
+ - Automatic [query parsing](https://itty.dev/itty-router/route-patterns#query).
67
+ - Easy [error handling](https://itty.dev/itty-router/errors), including throwing errors with HTTP status codes!
68
+ - Easy [Response](https://itty.dev/itty-router/responses) creation, with helpers for major formats (e.g.
69
+ [json](https://itty.dev/itty-router/api#json),
70
+ [html](https://itty.dev/itty-router/api#html),
71
+ [png](https://itty.dev/itty-router/api#png),
72
+ [jpeg](https://itty.dev/itty-router/api#jpeg), etc.)
73
+ - Deep APIs via [router nesting](https://itty.dev/itty-router/nesting).
74
+ - Full [middleware](https://itty.dev/itty-router/middleware) support. Includes the following by default:
75
+ - [withParams](https://itty.dev/itty-router/api#withParams) - access the params directly off the `Request` (instead of `request.params`).
76
+ - [withCookies](https://itty.dev/itty-router/api#withCookies) - access cookies in a convenient Object format.
77
+ - [withContent](https://itty.dev/itty-router/api#withContent) - auto-parse Request bodies as `request.content`.
78
+ - [CORS](https://itty.dev/itty-router/cors) - because we love you.
79
+ - Fully readable regex... yeah right! 😆
64
80
 
65
81
  ## [Full Documentation](https://itty.dev/itty-router)
66
- Complete docs/API are available at [itty.dev](https://itty.dev/itty-router), or join our [Discord](https://discord.com/channels/832353585802903572) channel to chat!
82
+
83
+ Complete documentation/API is available at [itty.dev](https://itty.dev/itty-router), or join our [Discord](https://discord.com/channels/832353585802903572) channel to chat with community members for quick help!
67
84
 
68
85
  ## Installation
86
+
69
87
  ```
70
88
  npm install itty-router
71
89
  ```
72
90
 
73
91
  ## Example
92
+
74
93
  ```js
75
- import {
76
- error, // creates error responses
77
- json, // creates JSON responses
78
- Router, // the ~440 byte router itself
79
- withParams, // middleware: puts params directly on the Request
94
+ import {
95
+ error, // creates error responses
96
+ json, // creates JSON responses
97
+ Router, // the ~440 byte router itself
98
+ withParams, // middleware: puts params directly on the Request
80
99
  } from 'itty-router'
81
100
  import { todos } from './external/todos'
82
101
 
83
102
  // create a new Router
84
- const router = Router()
103
+ const router = Router()
85
104
 
86
105
  router
87
106
  // add some middleware upstream on all routes
88
- .all('*', withParams)
107
+ .all('*', withParams)
89
108
 
90
109
  // GET list of todos
91
110
  .get('/todos', () => todos)
92
111
 
93
112
  // GET single todo, by ID
94
- .get('/todos/:id',
113
+ .get(
114
+ '/todos/:id',
95
115
  ({ id }) => todos.getById(id) || error(404, 'That todo was not found')
96
116
  )
97
117
 
@@ -100,44 +120,54 @@ router
100
120
 
101
121
  // Example: Cloudflare Worker module syntax
102
122
  export default {
103
- fetch: (request, ...args) => router
104
- .handle(request, ...args)
105
- .then(json) // send as JSON
106
- .catch(error) // catch errors
123
+ fetch: (request, ...args) =>
124
+ router
125
+ .handle(request, ...args)
126
+ .then(json) // send as JSON
127
+ .catch(error), // catch errors
107
128
  }
108
129
  ```
109
130
 
110
131
  ## Join the Discussion!
111
- Have a question? Suggestion? Complaint? Want to send me a gift basket?
132
+
133
+ Have a question? Suggestion? Complaint? Want to send a gift basket?
112
134
 
113
135
  Join us on [Discord](https://discord.com/channels/832353585802903572)!
114
136
 
115
137
  ## Testing and Contributing
138
+
116
139
  1. Fork repo
117
140
  1. Install dev dependencies via `yarn`
118
141
  1. Start test runner/dev mode `yarn dev`
119
142
  1. Add your code and tests if needed - do NOT remove/alter existing tests
120
- 1. Verify that tests pass once minified `yarn verify`
121
143
  1. Commit files
122
- 1. Submit PR with a detailed description of what you're doing
144
+ 1. Submit PR (and fill out the template)
123
145
  1. I'll add you to the credits! :)
124
146
 
125
147
  ## Special Thanks: Contributors
126
- These folks are the real heroes, making open source the powerhouse that it is! Help out and get your name added to this list! <3
148
+
149
+ These folks are the real heroes, making open source the powerhouse that it is! Help out and get your name added to this list! <3
127
150
 
128
151
  #### Core Concepts
152
+
129
153
  - [@mvasigh](https://github.com/mvasigh) - proxy hack wizard behind itty, coding partner in crime, maker of the entire doc site, etc, etc.
130
154
  - [@hunterloftis](https://github.com/hunterloftis) - router.handle() method now accepts extra arguments and passed them to route functions
131
155
  - [@SupremeTechnopriest](https://github.com/SupremeTechnopriest) - improved TypeScript support and documentation! :D
156
+
132
157
  #### Code Golfing
158
+
133
159
  - [@taralx](https://github.com/taralx) - router internal code-golfing refactor for performance and character savings
134
160
  - [@DrLoopFall](https://github.com/DrLoopFall) - v4.x re-minification
161
+
135
162
  #### Fixes & Build
163
+
136
164
  - [@taralx](https://github.com/taralx) - QOL fixes for contributing (dev dep fix and test file consistency) <3
137
- - [@technoyes](https://github.com/technoyes) - three kind-of-a-big-deal errors fixed. Imagine the look on my face... thanks man!! :)
165
+ - [@technoyes](https://github.com/technoyes) - three kind-of-a-big-deal errors fixed. Imagine the look on my face... thanks man!! :)
138
166
  - [@roojay520](https://github.com/roojay520) - TS interface fixes
139
167
  - [@jahands](https://github.com/jahands) - v4.x TS fixes
168
+
140
169
  #### Documentation
170
+
141
171
  - [@arunsathiya](https://github.com/arunsathiya),
142
172
  [@poacher2k](https://github.com/poacher2k),
143
173
  [@ddarkr](https://github.com/ddarkr),
package/Router.d.ts CHANGED
@@ -28,7 +28,7 @@ export type RouteHandler<I = IRequest, A extends any[] = any[]> = {
28
28
  export type RouteEntry = [string, RegExp, RouteHandler[], string];
29
29
  export type Route = <RequestType = IRequest, Args extends any[] = any[], RT = RouterType>(path: string, ...handlers: RouteHandler<RequestType, Args>[]) => RT;
30
30
  export type UniversalRoute<RequestType = IRequest, Args extends any[] = any[]> = (path: string, ...handlers: RouteHandler<RequestType, Args>[]) => RouterType<UniversalRoute<RequestType, Args>, Args>;
31
- type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2) ? true : false;
31
+ type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
32
32
  export type CustomRoutes<R = Route> = {
33
33
  [key: string]: R;
34
34
  };
package/cjs/Router.d.ts CHANGED
@@ -28,7 +28,7 @@ export type RouteHandler<I = IRequest, A extends any[] = any[]> = {
28
28
  export type RouteEntry = [string, RegExp, RouteHandler[], string];
29
29
  export type Route = <RequestType = IRequest, Args extends any[] = any[], RT = RouterType>(path: string, ...handlers: RouteHandler<RequestType, Args>[]) => RT;
30
30
  export type UniversalRoute<RequestType = IRequest, Args extends any[] = any[]> = (path: string, ...handlers: RouteHandler<RequestType, Args>[]) => RouterType<UniversalRoute<RequestType, Args>, Args>;
31
- type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2) ? true : false;
31
+ type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
32
32
  export type CustomRoutes<R = Route> = {
33
33
  [key: string]: R;
34
34
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "itty-router",
3
- "version": "4.0.6",
3
+ "version": "4.0.8",
4
4
  "description": "A tiny, zero-dependency router, designed to make beautiful APIs in any environment.",
5
5
  "type": "module",
6
6
  "main": "./index.js",
@@ -18,17 +18,17 @@
18
18
  "nested"
19
19
  ],
20
20
  "scripts": {
21
- "lint": "npx eslint src",
21
+ "dev": "yarn test",
22
+ "lint": "yarn run eslint src",
23
+ "prettier": "prettier --write src test example",
24
+ "format": "yarn lint && yarn prettier",
22
25
  "test": "vitest --coverage --reporter verbose",
23
26
  "test:once": "vitest run",
24
27
  "coverage": "vitest run --coverage",
25
28
  "coveralls": "yarn coverage && cat ./coverage/lcov.info | coveralls",
26
29
  "verify": "echo 'verifying module...' && yarn build && yarn test:once",
27
- "dev": "yarn test",
28
30
  "prerelease": "yarn verify",
29
- "prebuild": "rimraf dist && mkdir dist",
30
- "build:core": "rollup -c",
31
- "build:extras": "rollup --config rollup.config.extras.mjs",
31
+ "prebuild": "rimraf dist && mkdir dist && yarn coverage && yarn format",
32
32
  "build": "rollup -c",
33
33
  "release": "release --tag --push --patch --src=dist",
34
34
  "runtime:bun": "bun example/bun.ts",
@@ -51,21 +51,24 @@
51
51
  "@rollup/plugin-typescript": "^11.1.1",
52
52
  "@skypack/package-check": "^0.2.2",
53
53
  "@types/node": "^20.2.5",
54
+ "@typescript-eslint/eslint-plugin": "^5.59.8",
55
+ "@typescript-eslint/parser": "^5.59.8",
54
56
  "@vitejs/plugin-vue": "^4.2.3",
55
- "@vitest/coverage-c8": "^0.31.1",
56
- "@whatwg-node/server": "^0.8.0",
57
+ "@vitest/coverage-c8": "^0.31.3",
58
+ "@whatwg-node/server": "^0.8.1",
57
59
  "coveralls": "^3.1.1",
58
60
  "eslint": "^8.41.0",
59
- "eslint-plugin-jest": "^27.2.1",
61
+ "eslint-config-prettier": "^8.8.0",
60
62
  "fetch-mock": "^9.11.0",
61
63
  "fs-extra": "^11.1.1",
62
64
  "globby": "^13.1.4",
63
65
  "gzip-size": "^7.0.0",
64
66
  "http": "^0.0.1-security",
65
67
  "isomorphic-fetch": "^3.0.0",
66
- "itty-router": "^4.0.2",
68
+ "itty-router": "^4.0.6",
67
69
  "jsdom": "^22.1.0",
68
70
  "npm-run-all": "^4.1.5",
71
+ "prettier": "^2.8.8",
69
72
  "rimraf": "^5.0.1",
70
73
  "rollup": "^3.23.0",
71
74
  "rollup-plugin-bundle-size": "^1.0.3",
@@ -74,7 +77,7 @@
74
77
  "ts-node": "^10.9.1",
75
78
  "typescript": "^5.0.4",
76
79
  "vite": "^4.3.9",
77
- "vitest": "^0.31.1",
80
+ "vitest": "^0.31.3",
78
81
  "yarn": "^1.22.19",
79
82
  "yarn-release": "^1.10.5"
80
83
  }
@@ -1,128 +0,0 @@
1
- # Contributor Covenant Code of Conduct
2
-
3
- ## Our Pledge
4
-
5
- We as members, contributors, and leaders pledge to make participation in our
6
- community a harassment-free experience for everyone, regardless of age, body
7
- size, visible or invisible disability, ethnicity, sex characteristics, gender
8
- identity and expression, level of experience, education, socio-economic status,
9
- nationality, personal appearance, race, religion, or sexual identity
10
- and orientation.
11
-
12
- We pledge to act and interact in ways that contribute to an open, welcoming,
13
- diverse, inclusive, and healthy community.
14
-
15
- ## Our Standards
16
-
17
- Examples of behavior that contributes to a positive environment for our
18
- community include:
19
-
20
- * Demonstrating empathy and kindness toward other people
21
- * Being respectful of differing opinions, viewpoints, and experiences
22
- * Giving and gracefully accepting constructive feedback
23
- * Accepting responsibility and apologizing to those affected by our mistakes,
24
- and learning from the experience
25
- * Focusing on what is best not just for us as individuals, but for the
26
- overall community
27
-
28
- Examples of unacceptable behavior include:
29
-
30
- * The use of sexualized language or imagery, and sexual attention or
31
- advances of any kind
32
- * Trolling, insulting or derogatory comments, and personal or political attacks
33
- * Public or private harassment
34
- * Publishing others' private information, such as a physical or email
35
- address, without their explicit permission
36
- * Other conduct which could reasonably be considered inappropriate in a
37
- professional setting
38
-
39
- ## Enforcement Responsibilities
40
-
41
- Community leaders are responsible for clarifying and enforcing our standards of
42
- acceptable behavior and will take appropriate and fair corrective action in
43
- response to any behavior that they deem inappropriate, threatening, offensive,
44
- or harmful.
45
-
46
- Community leaders have the right and responsibility to remove, edit, or reject
47
- comments, commits, code, wiki edits, issues, and other contributions that are
48
- not aligned to this Code of Conduct, and will communicate reasons for moderation
49
- decisions when appropriate.
50
-
51
- ## Scope
52
-
53
- This Code of Conduct applies within all community spaces, and also applies when
54
- an individual is officially representing the community in public spaces.
55
- Examples of representing our community include using an official e-mail address,
56
- posting via an official social media account, or acting as an appointed
57
- representative at an online or offline event.
58
-
59
- ## Enforcement
60
-
61
- Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
- reported to the community leaders responsible for enforcement at
63
- krwhitley@gmail.com.
64
- All complaints will be reviewed and investigated promptly and fairly.
65
-
66
- All community leaders are obligated to respect the privacy and security of the
67
- reporter of any incident.
68
-
69
- ## Enforcement Guidelines
70
-
71
- Community leaders will follow these Community Impact Guidelines in determining
72
- the consequences for any action they deem in violation of this Code of Conduct:
73
-
74
- ### 1. Correction
75
-
76
- **Community Impact**: Use of inappropriate language or other behavior deemed
77
- unprofessional or unwelcome in the community.
78
-
79
- **Consequence**: A private, written warning from community leaders, providing
80
- clarity around the nature of the violation and an explanation of why the
81
- behavior was inappropriate. A public apology may be requested.
82
-
83
- ### 2. Warning
84
-
85
- **Community Impact**: A violation through a single incident or series
86
- of actions.
87
-
88
- **Consequence**: A warning with consequences for continued behavior. No
89
- interaction with the people involved, including unsolicited interaction with
90
- those enforcing the Code of Conduct, for a specified period of time. This
91
- includes avoiding interactions in community spaces as well as external channels
92
- like social media. Violating these terms may lead to a temporary or
93
- permanent ban.
94
-
95
- ### 3. Temporary Ban
96
-
97
- **Community Impact**: A serious violation of community standards, including
98
- sustained inappropriate behavior.
99
-
100
- **Consequence**: A temporary ban from any sort of interaction or public
101
- communication with the community for a specified period of time. No public or
102
- private interaction with the people involved, including unsolicited interaction
103
- with those enforcing the Code of Conduct, is allowed during this period.
104
- Violating these terms may lead to a permanent ban.
105
-
106
- ### 4. Permanent Ban
107
-
108
- **Community Impact**: Demonstrating a pattern of violation of community
109
- standards, including sustained inappropriate behavior, harassment of an
110
- individual, or aggression toward or disparagement of classes of individuals.
111
-
112
- **Consequence**: A permanent ban from any sort of public interaction within
113
- the community.
114
-
115
- ## Attribution
116
-
117
- This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
- version 2.0, available at
119
- https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
120
-
121
- Community Impact Guidelines were inspired by [Mozilla's code of conduct
122
- enforcement ladder](https://github.com/mozilla/diversity).
123
-
124
- [homepage]: https://www.contributor-covenant.org
125
-
126
- For answers to common questions about this code of conduct, see the FAQ at
127
- https://www.contributor-covenant.org/faq. Translations are available at
128
- https://www.contributor-covenant.org/translations.