itty-router 4.0.7 → 4.0.9-next.0
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/README.md +42 -27
- package/package.json +88 -5
- package/CONTRIBUTING.md +0 -92
package/README.md
CHANGED
|
@@ -8,7 +8,6 @@
|
|
|
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" />
|
|
@@ -50,28 +49,29 @@
|
|
|
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.
|
|
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.!
|
|
53
|
+
|
|
54
|
+
## Features
|
|
54
55
|
|
|
55
|
-
|
|
56
|
-
-
|
|
57
|
-
-
|
|
58
|
-
- Absurdly agnostic. We leave **you** with full control over response types, matching order, upstream/downstream effects, etc.
|
|
56
|
+
- Tiny. The Router itself is ~440 bytes gzipped, and the **entire** library is under 1.6k!
|
|
57
|
+
- Easy to use. We believe route code should be self-evident, obvious, and read more like poetry than code.
|
|
58
|
+
- Agnostic. We leave **you** with full control over response types, matching order, upstream/downstream effects, etc.
|
|
59
59
|
- Works [anywhere, in any environment](https://itty.dev/itty-router/runtimes).
|
|
60
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),
|
|
61
|
+
- Parses [route params](https://itty.dev/itty-router/route-patterns#params),
|
|
62
62
|
[optional params](https://itty.dev/itty-router/route-patterns#optional),
|
|
63
|
-
[wildcards](https://itty.dev/itty-router/route-patterns#wildcards),
|
|
63
|
+
[wildcards](https://itty.dev/itty-router/route-patterns#wildcards),
|
|
64
64
|
[greedy params](https://itty.dev/itty-router/route-patterns#greedy),
|
|
65
65
|
and [file formats](https://itty.dev/itty-router/route-patterns#file-formats).
|
|
66
66
|
- Automatic [query parsing](https://itty.dev/itty-router/route-patterns#query).
|
|
67
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),
|
|
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
72
|
[jpeg](https://itty.dev/itty-router/api#jpeg), etc.)
|
|
73
73
|
- Deep APIs via [router nesting](https://itty.dev/itty-router/nesting).
|
|
74
|
-
- Full [middleware](https://itty.dev/itty-router/middleware) support.
|
|
74
|
+
- Full [middleware](https://itty.dev/itty-router/middleware) support. Includes the following by default:
|
|
75
75
|
- [withParams](https://itty.dev/itty-router/api#withParams) - access the params directly off the `Request` (instead of `request.params`).
|
|
76
76
|
- [withCookies](https://itty.dev/itty-router/api#withCookies) - access cookies in a convenient Object format.
|
|
77
77
|
- [withContent](https://itty.dev/itty-router/api#withContent) - auto-parse Request bodies as `request.content`.
|
|
@@ -79,35 +79,39 @@ Itty aims to be the world's smallest (~440 bytes), feature-rich JavaScript route
|
|
|
79
79
|
- Fully readable regex... yeah right! 😆
|
|
80
80
|
|
|
81
81
|
## [Full Documentation](https://itty.dev/itty-router)
|
|
82
|
+
|
|
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!
|
|
83
84
|
|
|
84
85
|
## Installation
|
|
86
|
+
|
|
85
87
|
```
|
|
86
88
|
npm install itty-router
|
|
87
89
|
```
|
|
88
90
|
|
|
89
91
|
## Example
|
|
92
|
+
|
|
90
93
|
```js
|
|
91
|
-
import {
|
|
92
|
-
error,
|
|
93
|
-
json,
|
|
94
|
-
Router,
|
|
95
|
-
withParams,
|
|
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
|
|
96
99
|
} from 'itty-router'
|
|
97
100
|
import { todos } from './external/todos'
|
|
98
101
|
|
|
99
102
|
// create a new Router
|
|
100
|
-
const router = Router()
|
|
103
|
+
const router = Router()
|
|
101
104
|
|
|
102
105
|
router
|
|
103
106
|
// add some middleware upstream on all routes
|
|
104
|
-
.all('*', withParams)
|
|
107
|
+
.all('*', withParams)
|
|
105
108
|
|
|
106
109
|
// GET list of todos
|
|
107
110
|
.get('/todos', () => todos)
|
|
108
111
|
|
|
109
112
|
// GET single todo, by ID
|
|
110
|
-
.get(
|
|
113
|
+
.get(
|
|
114
|
+
'/todos/:id',
|
|
111
115
|
({ id }) => todos.getById(id) || error(404, 'That todo was not found')
|
|
112
116
|
)
|
|
113
117
|
|
|
@@ -116,19 +120,22 @@ router
|
|
|
116
120
|
|
|
117
121
|
// Example: Cloudflare Worker module syntax
|
|
118
122
|
export default {
|
|
119
|
-
fetch: (request, ...args) =>
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
+
fetch: (request, ...args) =>
|
|
124
|
+
router
|
|
125
|
+
.handle(request, ...args)
|
|
126
|
+
.then(json) // send as JSON
|
|
127
|
+
.catch(error), // catch errors
|
|
123
128
|
}
|
|
124
129
|
```
|
|
125
130
|
|
|
126
131
|
## Join the Discussion!
|
|
132
|
+
|
|
127
133
|
Have a question? Suggestion? Complaint? Want to send a gift basket?
|
|
128
134
|
|
|
129
135
|
Join us on [Discord](https://discord.com/channels/832353585802903572)!
|
|
130
136
|
|
|
131
137
|
## Testing and Contributing
|
|
138
|
+
|
|
132
139
|
1. Fork repo
|
|
133
140
|
1. Install dev dependencies via `yarn`
|
|
134
141
|
1. Start test runner/dev mode `yarn dev`
|
|
@@ -138,21 +145,29 @@ Join us on [Discord](https://discord.com/channels/832353585802903572)!
|
|
|
138
145
|
1. I'll add you to the credits! :)
|
|
139
146
|
|
|
140
147
|
## Special Thanks: Contributors
|
|
141
|
-
|
|
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
|
|
142
150
|
|
|
143
151
|
#### Core Concepts
|
|
152
|
+
|
|
144
153
|
- [@mvasigh](https://github.com/mvasigh) - proxy hack wizard behind itty, coding partner in crime, maker of the entire doc site, etc, etc.
|
|
145
154
|
- [@hunterloftis](https://github.com/hunterloftis) - router.handle() method now accepts extra arguments and passed them to route functions
|
|
146
155
|
- [@SupremeTechnopriest](https://github.com/SupremeTechnopriest) - improved TypeScript support and documentation! :D
|
|
156
|
+
|
|
147
157
|
#### Code Golfing
|
|
158
|
+
|
|
148
159
|
- [@taralx](https://github.com/taralx) - router internal code-golfing refactor for performance and character savings
|
|
149
160
|
- [@DrLoopFall](https://github.com/DrLoopFall) - v4.x re-minification
|
|
161
|
+
|
|
150
162
|
#### Fixes & Build
|
|
163
|
+
|
|
151
164
|
- [@taralx](https://github.com/taralx) - QOL fixes for contributing (dev dep fix and test file consistency) <3
|
|
152
|
-
- [@technoyes](https://github.com/technoyes) - three kind-of-a-big-deal errors fixed.
|
|
165
|
+
- [@technoyes](https://github.com/technoyes) - three kind-of-a-big-deal errors fixed. Imagine the look on my face... thanks man!! :)
|
|
153
166
|
- [@roojay520](https://github.com/roojay520) - TS interface fixes
|
|
154
167
|
- [@jahands](https://github.com/jahands) - v4.x TS fixes
|
|
168
|
+
|
|
155
169
|
#### Documentation
|
|
170
|
+
|
|
156
171
|
- [@arunsathiya](https://github.com/arunsathiya),
|
|
157
172
|
[@poacher2k](https://github.com/poacher2k),
|
|
158
173
|
[@ddarkr](https://github.com/ddarkr),
|
package/package.json
CHANGED
|
@@ -1,9 +1,88 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "itty-router",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.9-next.0",
|
|
4
4
|
"description": "A tiny, zero-dependency router, designed to make beautiful APIs in any environment.",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"module": "index.js",
|
|
8
|
+
"types": "index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./index.js",
|
|
12
|
+
"require": "./cjs/index.js",
|
|
13
|
+
"types": "./index.d.ts"
|
|
14
|
+
},
|
|
15
|
+
"./createCors": {
|
|
16
|
+
"import": "./createCors.js",
|
|
17
|
+
"require": "./cjs/createCors.js",
|
|
18
|
+
"types": "./createCors.d.ts"
|
|
19
|
+
},
|
|
20
|
+
"./createResponse": {
|
|
21
|
+
"import": "./createResponse.js",
|
|
22
|
+
"require": "./cjs/createResponse.js",
|
|
23
|
+
"types": "./createResponse.d.ts"
|
|
24
|
+
},
|
|
25
|
+
"./error": {
|
|
26
|
+
"import": "./error.js",
|
|
27
|
+
"require": "./cjs/error.js",
|
|
28
|
+
"types": "./error.d.ts"
|
|
29
|
+
},
|
|
30
|
+
"./html": {
|
|
31
|
+
"import": "./html.js",
|
|
32
|
+
"require": "./cjs/html.js",
|
|
33
|
+
"types": "./html.d.ts"
|
|
34
|
+
},
|
|
35
|
+
"./jpeg": {
|
|
36
|
+
"import": "./jpeg.js",
|
|
37
|
+
"require": "./cjs/jpeg.js",
|
|
38
|
+
"types": "./jpeg.d.ts"
|
|
39
|
+
},
|
|
40
|
+
"./png": {
|
|
41
|
+
"import": "./png.js",
|
|
42
|
+
"require": "./cjs/png.js",
|
|
43
|
+
"types": "./png.d.ts"
|
|
44
|
+
},
|
|
45
|
+
"./Router": {
|
|
46
|
+
"import": "./Router.js",
|
|
47
|
+
"require": "./cjs/Router.js",
|
|
48
|
+
"types": "./Router.d.ts"
|
|
49
|
+
},
|
|
50
|
+
"./status": {
|
|
51
|
+
"import": "./status.js",
|
|
52
|
+
"require": "./cjs/status.js",
|
|
53
|
+
"types": "./status.d.ts"
|
|
54
|
+
},
|
|
55
|
+
"./text": {
|
|
56
|
+
"import": "./text.js",
|
|
57
|
+
"require": "./cjs/text.js",
|
|
58
|
+
"types": "./text.d.ts"
|
|
59
|
+
},
|
|
60
|
+
"./webp": {
|
|
61
|
+
"import": "./webp.js",
|
|
62
|
+
"require": "./cjs/webp.js",
|
|
63
|
+
"types": "./webp.d.ts"
|
|
64
|
+
},
|
|
65
|
+
"./websocket": {
|
|
66
|
+
"import": "./websocket.js",
|
|
67
|
+
"require": "./cjs/websocket.js",
|
|
68
|
+
"types": "./websocket.d.ts"
|
|
69
|
+
},
|
|
70
|
+
"./withContent": {
|
|
71
|
+
"import": "./withContent.js",
|
|
72
|
+
"require": "./cjs/withContent.js",
|
|
73
|
+
"types": "./withContent.d.ts"
|
|
74
|
+
},
|
|
75
|
+
"./withCookies": {
|
|
76
|
+
"import": "./withCookies.js",
|
|
77
|
+
"require": "./cjs/withCookies.js",
|
|
78
|
+
"types": "./withCookies.d.ts"
|
|
79
|
+
},
|
|
80
|
+
"./withParams": {
|
|
81
|
+
"import": "./withParams.js",
|
|
82
|
+
"require": "./cjs/withParams.js",
|
|
83
|
+
"types": "./withParams.d.ts"
|
|
84
|
+
}
|
|
85
|
+
},
|
|
7
86
|
"keywords": [
|
|
8
87
|
"api",
|
|
9
88
|
"router",
|
|
@@ -19,16 +98,18 @@
|
|
|
19
98
|
],
|
|
20
99
|
"scripts": {
|
|
21
100
|
"dev": "yarn test",
|
|
22
|
-
"lint": "
|
|
101
|
+
"lint": "yarn run eslint src",
|
|
102
|
+
"prettier": "prettier --write src test example",
|
|
103
|
+
"format": "yarn lint && yarn prettier",
|
|
23
104
|
"test": "vitest --coverage --reporter verbose",
|
|
24
105
|
"test:once": "vitest run",
|
|
25
106
|
"coverage": "vitest run --coverage",
|
|
26
107
|
"coveralls": "yarn coverage && cat ./coverage/lcov.info | coveralls",
|
|
27
108
|
"verify": "echo 'verifying module...' && yarn build && yarn test:once",
|
|
28
109
|
"prerelease": "yarn verify",
|
|
29
|
-
"prebuild": "rimraf dist && mkdir dist && yarn coverage && yarn
|
|
110
|
+
"prebuild": "rimraf dist && mkdir dist && yarn coverage && yarn format",
|
|
30
111
|
"build": "rollup -c",
|
|
31
|
-
"release": "release --tag --push --
|
|
112
|
+
"release": "release --tag --push --type=next --src=dist",
|
|
32
113
|
"runtime:bun": "bun example/bun.ts",
|
|
33
114
|
"runtime:node": "node example/node.js"
|
|
34
115
|
},
|
|
@@ -56,6 +137,7 @@
|
|
|
56
137
|
"@whatwg-node/server": "^0.8.1",
|
|
57
138
|
"coveralls": "^3.1.1",
|
|
58
139
|
"eslint": "^8.41.0",
|
|
140
|
+
"eslint-config-prettier": "^8.8.0",
|
|
59
141
|
"fetch-mock": "^9.11.0",
|
|
60
142
|
"fs-extra": "^11.1.1",
|
|
61
143
|
"globby": "^13.1.4",
|
|
@@ -65,6 +147,7 @@
|
|
|
65
147
|
"itty-router": "^4.0.6",
|
|
66
148
|
"jsdom": "^22.1.0",
|
|
67
149
|
"npm-run-all": "^4.1.5",
|
|
150
|
+
"prettier": "^2.8.8",
|
|
68
151
|
"rimraf": "^5.0.1",
|
|
69
152
|
"rollup": "^3.23.0",
|
|
70
153
|
"rollup-plugin-bundle-size": "^1.0.3",
|
package/CONTRIBUTING.md
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
# Contributing to Itty
|
|
2
|
-
|
|
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
|
-
|
|
5
|
-
* [How to Contribute to Open Source](https://opensource.guide/how-to-contribute/)
|
|
6
|
-
* [Building Welcoming Communities](https://opensource.guide/building-community/)
|
|
7
|
-
|
|
8
|
-
## Bugs
|
|
9
|
-
|
|
10
|
-
We use [GitHub issues](https://github.com/kwhitley/itty-router/issues) for our public bugs. If you would like to report a problem, take a look around and see if someone already opened an issue about it. If you are certain this is a new unreported bug, you can submit a [bug report](#reporting-new-issues).
|
|
11
|
-
|
|
12
|
-
If you have questions about using itty, [contact us on Discord](https://discord.com/channels/832353585802903572), and we will do our best to answer your questions.
|
|
13
|
-
|
|
14
|
-
### Reporting new issues
|
|
15
|
-
|
|
16
|
-
When [opening a new issue](https://github.com/kwhitley/itty-router/issues/new/choose), always make sure to fill out the issue template. **This step is very important!** Not doing so may result in your issue not being managed in a timely fashion. Don't take this personally if this happens, and feel free to open a new issue once you've gathered all the information required by the template.
|
|
17
|
-
|
|
18
|
-
- **One issue, one bug:** Please report a single bug per issue.
|
|
19
|
-
- **Provide reproduction steps:** List all the steps necessary to reproduce the issue. The person reading your bug report should be able to follow these steps to reproduce your issue with minimal effort.
|
|
20
|
-
|
|
21
|
-
### Proposing a change
|
|
22
|
-
|
|
23
|
-
If you would like to request a new feature or enhancement but are not yet thinking about opening a pull request, you can also file an issue with [feature template](https://github.com/kwhitley/itty-router/issues/new?template=feature_request.yml).
|
|
24
|
-
|
|
25
|
-
If you're only fixing a bug, it's fine to submit a pull request right away, but we still recommend that you file an issue detailing what you're fixing. This is helpful in case we don't accept that specific fix but want to keep track of the issue.
|
|
26
|
-
|
|
27
|
-
Small pull requests are much easier to review and more likely to get merged.
|
|
28
|
-
|
|
29
|
-
### Installation
|
|
30
|
-
|
|
31
|
-
1. Ensure you have [npm](https://www.npmjs.com/get-npm) installed.
|
|
32
|
-
1. Ensure you have [yarn](https://classic.yarnpkg.com/lang/en/docs/install) installed.
|
|
33
|
-
1. After cloning the repository, run `yarn` in the root of the repository.
|
|
34
|
-
1. To start development, run `yarn dev`.
|
|
35
|
-
|
|
36
|
-
### Creating a branch
|
|
37
|
-
|
|
38
|
-
Fork [the repository](https://github.com/kwhitley/itty-router) and create your branch from `v4.x`. If you've never sent a GitHub pull request before, you can learn how from [this free video series](https://egghead.io/courses/how-to-contribute-to-an-open-source-project-on-github).
|
|
39
|
-
|
|
40
|
-
### Testing
|
|
41
|
-
|
|
42
|
-
A good test plan has the exact commands you ran and their output, provides screenshots or videos if the pull request changes UI.
|
|
43
|
-
|
|
44
|
-
- If you've changed APIs, update the documentation, including at the appropriate places within [itty.dev](https://itty.dev/itty-router).
|
|
45
|
-
|
|
46
|
-
#### Writing tests
|
|
47
|
-
|
|
48
|
-
All tests are located in adjacent `.spec.ts` files, next to the file being tested.
|
|
49
|
-
|
|
50
|
-
#### Running tests
|
|
51
|
-
|
|
52
|
-
1. To run test, run `yarn test`, or `yarn dev` for continuous testing (includes `--watch`).
|
|
53
|
-
|
|
54
|
-
### Style guide
|
|
55
|
-
|
|
56
|
-
[Eslint](https://eslint.org) will catch most styling issues that may exist in your code. You can check the status of your code styling by simply running `yarn lint`.
|
|
57
|
-
|
|
58
|
-
#### Code conventions
|
|
59
|
-
|
|
60
|
-
- `camelCase` for public variable names and methods.
|
|
61
|
-
- No abbreviated variable names - maximize readability and let the minification process do its thing later.
|
|
62
|
-
|
|
63
|
-
### Sending your pull request
|
|
64
|
-
|
|
65
|
-
Please make sure the following is done when submitting a pull request:
|
|
66
|
-
|
|
67
|
-
1. Describe your **test plan** in your pull request description. Make sure to test your changes.
|
|
68
|
-
1. Make sure your code lints (`yarn lint`).
|
|
69
|
-
1. Make sure your tests pass (`yarn test`).
|
|
70
|
-
|
|
71
|
-
All pull requests should be opened against the `v4.x` branch. Make sure the PR does only one thing, otherwise please split it.
|
|
72
|
-
|
|
73
|
-
#### Breaking changes
|
|
74
|
-
|
|
75
|
-
When adding a new breaking change, follow this template in your pull request:
|
|
76
|
-
|
|
77
|
-
```md
|
|
78
|
-
### New breaking change here
|
|
79
|
-
|
|
80
|
-
- **Who does this affect**:
|
|
81
|
-
- **How to migrate**:
|
|
82
|
-
- **Why make this breaking change**:
|
|
83
|
-
- **Severity (number of people affected x effort)**:
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
## License
|
|
87
|
-
|
|
88
|
-
By contributing to itty, you agree that your contributions will be licensed under its [MIT license](https://github.com/kwhitley/itty-router/blob/master/LICENSE).
|
|
89
|
-
|
|
90
|
-
## Questions
|
|
91
|
-
|
|
92
|
-
Feel free to ask in [#itty-router](https://discord.com/channels/832353585802903572) on [Discord](https://discord.com/channels/832353585802903572) if you have questions about our process, how to proceed, etc.
|