diff --git a/.eslintrc.json b/.eslintrc similarity index 84% rename from .eslintrc.json rename to .eslintrc index a5d491ff..d2f152d3 100644 --- a/.eslintrc.json +++ b/.eslintrc @@ -5,7 +5,8 @@ }, "extends": [ "eslint:recommended", - "plugin:@typescript-eslint/recommended" + "plugin:@typescript-eslint/recommended", + "prettier" ], "overrides": [], "parser": "@typescript-eslint/parser", @@ -13,9 +14,7 @@ "ecmaVersion": "latest", "sourceType": "module" }, - "plugins": [ - "@typescript-eslint" - ], + "plugins": ["@typescript-eslint"], "rules": { "@typescript-eslint/no-empty-function": "off", "@typescript-eslint/no-explicit-any": "off", diff --git a/.github/ISSUE_TEMPLATE/issue_template.md b/.github/ISSUE_TEMPLATE/issue_template.md index ba816010..1f554d6a 100644 --- a/.github/ISSUE_TEMPLATE/issue_template.md +++ b/.github/ISSUE_TEMPLATE/issue_template.md @@ -4,42 +4,49 @@ about: Create a report to help us improve title: '' labels: '' assignees: '' - --- ## Describe the Issue + A clear and concise description of what the issue is. ## Example Router Code + Please provide the itty-router code related to the issue. If possible, create a minimal, reproducible example. ```ts // your code here - ``` ## Request Details + - Method: [e.g., GET, POST, PUT, DELETE] - URL: [e.g., /api/v1/users] - Request Body: If applicable, include the request body. - Request Headers: If applicable, include the request headers. ## Steps to Reproduce + Steps to reproduce the behavior: + 1. Run '...' 2. Send request to '....' 3. See error ## Expected Behavior + A clear and concise description of what you expected to happen. ## Actual Behavior + A clear and concise description of what actually happens. Include any error messages or unexpected responses. ## Environment (please complete the following information): + - Environment: [e.g., Node, Bun, Cloudflare Workers, Service Workers, Browser] - itty-router Version: [e.g., 1.0.0] - Other Relevant Libraries and their versions: [e.g., node 14.0.0] ## Additional Context + Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/question.md b/.github/ISSUE_TEMPLATE/question.md index 3bec3ad8..4ccd552a 100644 --- a/.github/ISSUE_TEMPLATE/question.md +++ b/.github/ISSUE_TEMPLATE/question.md @@ -4,11 +4,12 @@ about: Have a question or something to discuss? title: '' labels: '' assignees: '' - --- ## Questions & Discussion -To cut down on long-standing topics that are discussion-based, rather than actual issues, please opt for one of the following paths, when possible. These have the benefit of potentially helping others after the issue (here) would have been closed. + +To cut down on long-standing topics that are discussion-based, rather than actual issues, please opt for one of the following paths, when possible. These have the benefit of potentially helping others after the issue (here) would have been closed. + 1. [Create a Discussion Topic](https://github.com/kwhitley/itty-router/discussions/new/choose) 2. [Discuss on Discord](https://discord.com/channels/832353585802903572) diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 3531b8aa..6bfc2543 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -1,23 +1,28 @@ ### Description + Please explain the changes you made here. ### Issue Related -- Link to the related issue: + +- Link to the related issue: ### Type of Change (select one and follow subtasks) + - [ ] 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? - [ ] Breaking change (fix or feature that would cause existing functionality/userland code to not work as expected) - - [ ] Explain why a breaking change is necessary: + - [ ] 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 +### Testing + Please describe the tests that you ran to verify your changes. ### Checklist + - [ ] I have read the [CONTRIBUTING](../CONTRIBUTING.md) doc diff --git a/.github/workflows/coveralls.yml b/.github/workflows/coveralls.yml index d71a5f31..d0e65e14 100644 --- a/.github/workflows/coveralls.yml +++ b/.github/workflows/coveralls.yml @@ -1,25 +1,23 @@ -on: ["push", "pull_request"] +on: ['push', 'pull_request'] name: Coveralls jobs: - build: name: Build runs-on: ubuntu-latest steps: + - uses: actions/checkout@v1 - - uses: actions/checkout@v1 - - - name: Use Node.js 16.x - uses: actions/setup-node@v3 - with: - node-version: 16.x + - name: Use Node.js 16.x + uses: actions/setup-node@v3 + with: + node-version: 16.x - - name: npm install, npm run coverage - run: | - npm install - npm run coverage + - name: npm install, npm run coverage + run: | + npm install + npm run coverage - - name: Coveralls - uses: coverallsapp/github-action@v2 + - name: Coveralls + uses: coverallsapp/github-action@v2 diff --git a/.github/workflows/verify.yml b/.github/workflows/verify.yml index d6f46f49..0fc0a5dc 100644 --- a/.github/workflows/verify.yml +++ b/.github/workflows/verify.yml @@ -2,13 +2,12 @@ name: build on: push: - branches: [ v4.x ] + branches: [v4.x] pull_request: - branches: [ v4.x ] + branches: [v4.x] jobs: build: - runs-on: ubuntu-latest steps: diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 00000000..6b9c164a --- /dev/null +++ b/.prettierrc @@ -0,0 +1,4 @@ +trailingComma: 'es5' +tabWidth: 2 +semi: false +singleQuote: true diff --git a/CHANGELOG.md b/CHANGELOG.md index 272acb06..5811333a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,9 +3,9 @@ - **v4.0.00** - Partial changelog below - BREAKING: heavy TS rewrite for core Router (thank you, ChatGPT) - added: nearly all extras from itty-router-extras - - added: createCors from itty-cors + - added: createCors from itty-cors - added: complete documentation at https://itty.dev - - feature: withParams may be used as upstream middleware (hooray!) + - feature: withParams may be used as upstream middleware (hooray!) - **v3.0.11** - changed environment build to rollup (from tsup) and code golfed the toQuery logic. (credit [@DrLoopFall](https://github.com/DrLoopFall)) - **v3.0.9** - fixes some TS issue, previously requiring you to define Router Methods to chain request definitions. (credit [@jahands](https://github.com/jahands)) - **v3.0.0** - total TS conversion with improved types, adding greedy params (credit [@markusahlstrand](https://github.com/markusahlstrand)) @@ -20,7 +20,7 @@ - **v2.4.9** - fixed the cursed "optional" file format capturing bug - RIP all the bytes lost - **v2.4.6** - fixed README issues - **v2.4.1** - fixed type errors introduced with 2.4.0 -- **v2.4.0** - HUGE internal code-golfing refactor thanks to [@taralx](https://github.com/taralx)! Super cool work on this!!! +- **v2.4.0** - HUGE internal code-golfing refactor thanks to [@taralx](https://github.com/taralx)! Super cool work on this!!! - **v2.3.10** - fix: dots now properly escaped (e.g. /image.jpg should not match /imageXjpg) - **v2.3.9** - dev fixes: [@taralx](https://github.com/taralx) improved QOL issues for test writers and dev installers - **v2.3.7** - fix: :id.:format not resolving (only conditional format would match) @@ -43,6 +43,6 @@ - **v1.0.0** - production release, stamped into gold from x0.9.7 - **v0.9.0** - added support for multiple handlers (middleware) - **v0.8.0** - deep minification pass and build steps for final module -- **v0.7.0** - removed { path } from request handler context, travis build fixed, added coveralls, improved README docs +- **v0.7.0** - removed { path } from request handler context, travis build fixed, added coveralls, improved README docs - **v0.6.0** - added types to project for vscode intellisense (thanks [@mvasigh](https://github.com/mvasigh)) - **v0.5.4** - fix: wildcard routes properly supported diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md index 73517d26..e5b3df39 100644 --- a/CODE_OF_CONDUCT.md +++ b/CODE_OF_CONDUCT.md @@ -17,23 +17,23 @@ diverse, inclusive, and healthy community. Examples of behavior that contributes to a positive environment for our community include: -* Demonstrating empathy and kindness toward other people -* Being respectful of differing opinions, viewpoints, and experiences -* Giving and gracefully accepting constructive feedback -* Accepting responsibility and apologizing to those affected by our mistakes, +- Demonstrating empathy and kindness toward other people +- Being respectful of differing opinions, viewpoints, and experiences +- Giving and gracefully accepting constructive feedback +- Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience -* Focusing on what is best not just for us as individuals, but for the +- Focusing on what is best not just for us as individuals, but for the overall community Examples of unacceptable behavior include: -* The use of sexualized language or imagery, and sexual attention or +- The use of sexualized language or imagery, and sexual attention or advances of any kind -* Trolling, insulting or derogatory comments, and personal or political attacks -* Public or private harassment -* Publishing others' private information, such as a physical or email +- Trolling, insulting or derogatory comments, and personal or political attacks +- Public or private harassment +- Publishing others' private information, such as a physical or email address, without their explicit permission -* Other conduct which could reasonably be considered inappropriate in a +- Other conduct which could reasonably be considered inappropriate in a professional setting ## Enforcement Responsibilities @@ -106,7 +106,7 @@ Violating these terms may lead to a permanent ban. ### 4. Permanent Ban **Community Impact**: Demonstrating a pattern of violation of community -standards, including sustained inappropriate behavior, harassment of an +standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals. **Consequence**: A permanent ban from any sort of public interaction within diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index bb04d4da..8ab8b4e8 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -2,8 +2,8 @@ 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: -* [How to Contribute to Open Source](https://opensource.guide/how-to-contribute/) -* [Building Welcoming Communities](https://opensource.guide/building-community/) +- [How to Contribute to Open Source](https://opensource.guide/how-to-contribute/) +- [Building Welcoming Communities](https://opensource.guide/building-community/) ## Bugs diff --git a/README.md b/README.md index 92ed225c..0c09c3e8 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,6 @@
@@ -50,28 +49,29 @@
---
-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.!
+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.!
## Features:
-- Absurdly tiny. The Router itself is ~440 bytes gzipped, and the **entire** library is under 1.5k!
-- Absurdly easy to use. We believe route code should be self-evident, obvious, and read more like poetry than code.
+
+- Absurdly tiny. The Router itself is ~440 bytes gzipped, and the **entire** library is under 1.5k!
+- Absurdly easy to use. We believe route code should be self-evident, obvious, and read more like poetry than code.
- Absurdly agnostic. We leave **you** with full control over response types, matching order, upstream/downstream effects, etc.
- Works [anywhere, in any environment](https://itty.dev/itty-router/runtimes).
- [Fully typed/TypeScript support](https://itty.dev/itty-router/typescript), including hinting.
-- Parses [route params](https://itty.dev/itty-router/route-patterns#params),
+- Parses [route params](https://itty.dev/itty-router/route-patterns#params),
[optional params](https://itty.dev/itty-router/route-patterns#optional),
- [wildcards](https://itty.dev/itty-router/route-patterns#wildcards),
+ [wildcards](https://itty.dev/itty-router/route-patterns#wildcards),
[greedy params](https://itty.dev/itty-router/route-patterns#greedy),
and [file formats](https://itty.dev/itty-router/route-patterns#file-formats).
- Automatic [query parsing](https://itty.dev/itty-router/route-patterns#query).
- Easy [error handling](https://itty.dev/itty-router/errors), including throwing errors with HTTP status codes!
-- Easy [Response](https://itty.dev/itty-router/responses) creation, with helpers for major formats (e.g.
- [json](https://itty.dev/itty-router/api#json),
- [html](https://itty.dev/itty-router/api#html),
- [png](https://itty.dev/itty-router/api#png),
+- Easy [Response](https://itty.dev/itty-router/responses) creation, with helpers for major formats (e.g.
+ [json](https://itty.dev/itty-router/api#json),
+ [html](https://itty.dev/itty-router/api#html),
+ [png](https://itty.dev/itty-router/api#png),
[jpeg](https://itty.dev/itty-router/api#jpeg), etc.)
- Deep APIs via [router nesting](https://itty.dev/itty-router/nesting).
-- Full [middleware](https://itty.dev/itty-router/middleware) support. Includes the following by default:
+- Full [middleware](https://itty.dev/itty-router/middleware) support. Includes the following by default:
- [withParams](https://itty.dev/itty-router/api#withParams) - access the params directly off the `Request` (instead of `request.params`).
- [withCookies](https://itty.dev/itty-router/api#withCookies) - access cookies in a convenient Object format.
- [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
- Fully readable regex... yeah right! 😆
## [Full Documentation](https://itty.dev/itty-router)
+
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!
## Installation
+
```
npm install itty-router
```
## Example
+
```js
-import {
- error, // creates error responses
- json, // creates JSON responses
- Router, // the ~440 byte router itself
- withParams, // middleware: puts params directly on the Request
+import {
+ error, // creates error responses
+ json, // creates JSON responses
+ Router, // the ~440 byte router itself
+ withParams, // middleware: puts params directly on the Request
} from 'itty-router'
import { todos } from './external/todos'
// create a new Router
-const router = Router()
+const router = Router()
router
// add some middleware upstream on all routes
- .all('*', withParams)
+ .all('*', withParams)
// GET list of todos
.get('/todos', () => todos)
// GET single todo, by ID
- .get('/todos/:id',
+ .get(
+ '/todos/:id',
({ id }) => todos.getById(id) || error(404, 'That todo was not found')
)
@@ -116,19 +120,22 @@ router
// Example: Cloudflare Worker module syntax
export default {
- fetch: (request, ...args) => router
- .handle(request, ...args)
- .then(json) // send as JSON
- .catch(error) // catch errors
+ fetch: (request, ...args) =>
+ router
+ .handle(request, ...args)
+ .then(json) // send as JSON
+ .catch(error), // catch errors
}
```
## Join the Discussion!
+
Have a question? Suggestion? Complaint? Want to send a gift basket?
Join us on [Discord](https://discord.com/channels/832353585802903572)!
## Testing and Contributing
+
1. Fork repo
1. Install dev dependencies via `yarn`
1. Start test runner/dev mode `yarn dev`
@@ -138,21 +145,29 @@ Join us on [Discord](https://discord.com/channels/832353585802903572)!
1. I'll add you to the credits! :)
## Special Thanks: Contributors
-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
+
+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
#### Core Concepts
+
- [@mvasigh](https://github.com/mvasigh) - proxy hack wizard behind itty, coding partner in crime, maker of the entire doc site, etc, etc.
- [@hunterloftis](https://github.com/hunterloftis) - router.handle() method now accepts extra arguments and passed them to route functions
- [@SupremeTechnopriest](https://github.com/SupremeTechnopriest) - improved TypeScript support and documentation! :D
+
#### Code Golfing
+
- [@taralx](https://github.com/taralx) - router internal code-golfing refactor for performance and character savings
- [@DrLoopFall](https://github.com/DrLoopFall) - v4.x re-minification
+
#### Fixes & Build
+
- [@taralx](https://github.com/taralx) - QOL fixes for contributing (dev dep fix and test file consistency) <3
-- [@technoyes](https://github.com/technoyes) - three kind-of-a-big-deal errors fixed. Imagine the look on my face... thanks man!! :)
+- [@technoyes](https://github.com/technoyes) - three kind-of-a-big-deal errors fixed. Imagine the look on my face... thanks man!! :)
- [@roojay520](https://github.com/roojay520) - TS interface fixes
- [@jahands](https://github.com/jahands) - v4.x TS fixes
+
#### Documentation
+
- [@arunsathiya](https://github.com/arunsathiya),
[@poacher2k](https://github.com/poacher2k),
[@ddarkr](https://github.com/ddarkr),
diff --git a/example/bun.ts b/example/bun.ts
index 7b40edab..32d1996d 100644
--- a/example/bun.ts
+++ b/example/bun.ts
@@ -1,9 +1,4 @@
-import {
- Router,
- error,
- json,
- withParams
-} from '../dist/index.js'
+import { Router, error, json, withParams } from '../dist/index.js'
const router = Router()
@@ -15,8 +10,6 @@ router
export default {
port: 3001,
- fetch: (request, env, ctx) => router
- .handle(request, env, ctx)
- .then(json)
- .catch(error)
+ fetch: (request, env, ctx) =>
+ router.handle(request, env, ctx).then(json).catch(error),
}
diff --git a/example/cors-types.ts b/example/cors-types.ts
index e73779bb..0391b93c 100644
--- a/example/cors-types.ts
+++ b/example/cors-types.ts
@@ -1,9 +1,4 @@
-import {
- IRequest,
- IRequestStrict,
- Router,
- createCors,
-} from '../src'
+import { IRequest, IRequestStrict, Router, createCors } from '../src'
type FooRequest = {
foo: string
@@ -18,10 +13,7 @@ type Env = {
KV: string
}
-type CF = [
- env: Env,
- ctx: ExecutionContext
-]
+type CF = [env: Env, ctx: ExecutionContext]
// this router defines a global signature of