Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT: "AutoRouter" to reduce router initialization boilerplate #195

Closed
wants to merge 39 commits into from
Closed
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
5f495a9
testing flow
kwhitley Aug 15, 2023
70f5901
released v4.1.0-next.0 - initial flow release
kwhitley Aug 16, 2023
69d1e19
released v4.1.0-next.1 - flow should be exported from index
kwhitley Aug 16, 2023
496679f
added flow examples
kwhitley Aug 27, 2023
a5e9789
released v4.1.0-next.2 - updated package.json
kwhitley Sep 13, 2023
d6727b6
released v4.1.0-next.3 - changing API of flow
kwhitley Sep 13, 2023
902e668
released v4.1.0-next.4 - code golf optimization in flow
kwhitley Sep 13, 2023
668f402
flow work
kwhitley Oct 28, 2023
87e708d
flow working with tests - TODO: finalize API
kwhitley Nov 25, 2023
aead16c
fixed merge conflicts
kwhitley Nov 25, 2023
ab1364f
ts-error should have description
kwhitley Nov 25, 2023
acd93ae
fixing lint errors
kwhitley Nov 25, 2023
16e31e4
Merge remote-tracking branch 'origin/v4.x' into flow
kwhitley Nov 25, 2023
b210c31
update to CHANGELOG
kwhitley Nov 25, 2023
497da62
Merge remote-tracking branch 'origin/v4.x' into flow
kwhitley Nov 25, 2023
b3fab6f
deps update
kwhitley Nov 25, 2023
8195b92
update actions to use Node v20
kwhitley Nov 25, 2023
d0866d1
mock Response for websocket test
kwhitley Nov 25, 2023
fd13151
fix node version
kwhitley Nov 25, 2023
5169efa
flow signatures added and tests fixed
kwhitley Dec 6, 2023
7e8bf0c
remove support for router.fetch
kwhitley Dec 6, 2023
575460b
resolved merge conflicts
kwhitley Dec 6, 2023
811c166
deps update
kwhitley Dec 6, 2023
873f794
testing after and before handlers in flow
kwhitley Dec 13, 2023
5c25e1a
tweak bun flow advanced example
kwhitley Dec 13, 2023
762c489
default CORS option is now to allow all methods
kwhitley Dec 13, 2023
3ae06c3
CHANGELOG added for createCors fix
kwhitley Dec 13, 2023
5858f48
fixed #202 - typescript generics not being used at router level if IR…
kwhitley Dec 14, 2023
2559423
fixed request/args TS flow?!
kwhitley Dec 15, 2023
9539603
modified changelog and Router types
kwhitley Dec 15, 2023
52915f7
checking in
kwhitley Dec 15, 2023
5c3d570
updating flow spec
kwhitley Dec 15, 2023
143892c
after honors return if given
kwhitley Dec 16, 2023
de4a44d
shorter way to write the after bit
kwhitley Dec 16, 2023
ea83f52
Bugfix/ts expect error is a bug (#205)
Crisfole Dec 16, 2023
d96a320
merge conflicts
kwhitley Dec 17, 2023
4af4b54
AutoRouter
kwhitley Dec 17, 2023
d1772b9
passing all tests
kwhitley Dec 17, 2023
cf7efa7
conflicts
kwhitley Jan 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/coveralls.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ jobs:
steps:
- uses: actions/checkout@v1

- name: Use Node.js 16.x
- name: Use Node.js 20.10.0
uses: actions/setup-node@v3
with:
node-version: 16.x
node-version: 20.10.0

- name: npm install, npm run coverage
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Use Node.js
uses: actions/setup-node@v1
with:
node-version: '16.x'
node-version: '20.10.0'
- name: Install dependencies
run: yarn
- run: yarn lint
2 changes: 1 addition & 1 deletion .github/workflows/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Use Node.js
uses: actions/setup-node@v1
with:
node-version: '16.x'
node-version: '20.10.0'
- name: Install dependencies
run: yarn
- run: yarn verify
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
- **v4.1.0**
- added: `HasContent<ContentType>` type to `withContent` (credit [@alexrosenfeld10](https://github.com/alexrosenfeld10))
- added: Adds basic text/formData support to supplement the native JSON support of `withContent`
- added: `origin` in `CorsOptions` now can accept a function (credit [@leaysgur])
- added: `CorsOptions` now exported from `createCors`
- added: `flow(router)` to reduce initilization boilerplate
- fixed: `createCors()` now correctly defaults to all methods "*" unless specified, not just GET
- added: router generics have now been overhauled to allow greater flexibility (may use router-level and route-level together)
- **v4.0.00** - Partial changelog below
- BREAKING: heavy TS rewrite for core Router (thank you, ChatGPT)
- added: nearly all extras from itty-router-extras
Expand Down
37 changes: 37 additions & 0 deletions example/bun-flow-advanced.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// WITH FLOW (overriding options, and enabling CORS)
import { Router, error, flow } from '../src'
import { IRequest, IRequestStrict } from '../src'

type Env = {
KV: object
}

type CustomRequest = {
foo: string
} & IRequestStrict

type RouterArgs = [env: Env]

const router = Router()

router
.all('*', (request) => { request.start = Date.now() })
.get('/test', () => 'Success!')
.get('/foo/:bar/:baz?', ({ bar, baz }) => ({ bar, baz }))

export default flow(router, {
before: (request) => request.start = Date.now(),
after: (response, request) => {
const { method, start } = request
const { status } = response
const duration = Date.now() - start
const url = new URL(request.url)
console.log(`${status} ${method} ${url.pathname+(url.search || '')} - ${duration}ms`)
},
cors: true,
notFound: () => error(404, 'Are you sure about that?'),
})

const flowed = flow(router)

flowed.fetch()
6 changes: 6 additions & 0 deletions example/bun-flow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Router, flow } from '../src'

const app = Router()
app.get('/', () => 'Hello Itty!')

export default flow(app)
9 changes: 6 additions & 3 deletions example/bun.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Router, error, json, withParams } from '../dist/index.js'
// WITHOUT FLOW
import { IRequest, Router, error, json, withParams } from 'itty-router'

const router = Router()

Expand All @@ -10,6 +11,8 @@ router

export default {
port: 3001,
fetch: (request, env, ctx) =>
router.handle(request, env, ctx).then(json).catch(error),
fetch: (request: IRequest) => router
.handle(request)
.then(json)
.catch(error),
}
32 changes: 32 additions & 0 deletions example/e2e middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { IRequest, IRequestStrict, Router } from 'Router'

type HasFoo = {
foo: string
} & IRequestStrict

type HasBar = {
bar: string
} & IRequestStrict

const router = Router()

// MIDDLEWARE: adds foo to the request
const withFoo = (request: HasFoo) => {
request.foo = 'bar' // no return = next handler gets fired (making this middleware)
}

// MIDDLEWARE: adds foo to the request
const withBar = (request: HasBar) => {
request.bar = 'baz' // no return = next handler gets fired (making this middleware)
}

// ADD ROUTES
router
.all<HasFoo & HasBar>('*', withFoo, withBar, (request) => {
request.bar // this can be infered
request.foo // this can be infered
})

.get('/', withFoo, withBar, (request) => {
return request
})
50 changes: 50 additions & 0 deletions example/universal-vs-overrides.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// WITH FLOW (overriding options, and enabling CORS)
import { Router, error, flow } from '../src'
import { IRequest, IRequestStrict } from '../src'

type Env = {
KV: object
}

type CustomRequest = {
foo: string
} & IRequestStrict

type RouterArgs = [env: Env]
type RouterArgs2 = [env: Env, ctx: ExecutionContext]

const universalRouter =
Router<CustomRequest, RouterArgs>()
.get('/type-check', (request, env) => {
request.foo // should be found
env.KV // should be found
env.KB // should NOT be found
})
.get<IRequestStrict>('/type-check', (request, env) => {
request.foo // should NOT be found
env.KV // should be found
env.KB // should NOT be found
})
.puppy('/type-check', (request, env) => {
request.foo // should be found
env.KV // should be found
env.KB // should NOT be found
})
.puppy<CustomRequest, RouterArgs2>('/type-check', (request, env) => {
request.foo // should be found
env.KV // should be found
env.KB // should NOT be found
})

// Router without generics will use IRequest by default
const overrideRouter =
Router()
.get('/type-check', (request, env, ctx) => {
request.whatever // should ignore
env.whatever // should ignore
ctx.whatever // should ignore
})
.get<IRequestStrict, RouterArgs2>('/type-check', (request, env, ctx) => {
request.foo // should NOT be found
ctx.waitUntil // should infer
})
Loading
Loading