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

v5.x DRAFT #222

Merged
merged 18 commits into from
Mar 24, 2024
Merged

v5.x DRAFT #222

merged 18 commits into from
Mar 24, 2024

Conversation

kwhitley
Copy link
Owner

@kwhitley kwhitley commented Mar 14, 2024

Release Progress

  • Finalize API
  • 100% test coverage
  • Fully type additions
  • Document @ itty.dev
    • add section for Routers
    • add comparison chart for Router features
    • add to API page
    • add examples for each Router type
  • Document @ README.md
    • rebrand to itty.dev
    • modify examples
    • simplify feature set

Changes

Router (adding features) ~550 bytes

  • OBJECTIVE: by adding these stages as config-based, we allow clean exports leveraging the default router.fetch signature to match up to various runtimes (e.g. Bun, Cloudflare Workers). We also make the possibility of AutoRouter that adds sane defaults. Previously, this control flow was only possible using the .then chain after router.fetch, or by monkey-patching/intercepting the fetch method itself.
  • fully backwards compatible with the previous Router (now IttyRouter)
  • added option before - array of before handlers/middleware
  • added option catch - a single error handler to catch thrown exceptions
  • added option finally - array of response handlers
    • each after handler is passed the following args: (response: any, request: IRequest, ...args), where ...args are anything additional passed to the original fetch function. This allows the after stage to easily have access to Cloudflare environment and context.
    • each of these may optionally modify the response
    • if nothing is returned, the previous successful response is passed through - this allows pass-through functions (e.g. logging) to function without forcing a return

Example:

import { Router, error, json, withParams } from 'itty-router'

const router = Router({ 
  port: 3001,
  before: [withParams],
  catch: error,
  finally: [json],  
})

router
  .get('/basic', () => new Response('Success!'))
  .get('/text', () => 'Success!')
  .get('/params/:foo', ({ foo }) => foo)
  .get('/json', () => ({ foo: 'bar' }))
  .get('/throw', (a) => a.b.c)
  .all('*', () => error(404))

export default router

AutoRouter (new) ~1kB

  • creates Router with sane defaults and a few modified behaviors
  • added option: format (a single response formatter, such as json) to control default formatter
    • to skip this stage, enter a no-op: format: () => {}
  • added option: missing (a single request handler that will be passed the request and any additional args)
    • to skip this stage, enter a no-op: missing: () => {}
  • added withParams as a forced before stage entry
  • modified option: finally always uses the following pattern:
    finally: [
      (r: any, ...args) => r ?? missing(r, ...args),
      format,
      ...finally,
    ],
    This means you lose complete control over replacing the missing and format stage.

Example:

import { AutoRouter } from 'itty-router'

const router = AutoRouter({ port: 3001 })

router
  .get('/basic', () => new Response('Success!'))
  .get('/text', () => 'Success!')
  .get('/params/:foo', ({ foo }) => foo)
  .get('/json', () => ({ foo: 'bar' }))
  .get('/throw', (a) => a.b.c)

export default router

IttyRouter (new) ~460 bytes

  • moves the original Router to IttyRouter
  • NOTE - .handle is fully deprecated here (in favor of router.fetch) to save bytes

Example:

import { IttyRouter, error, json, withParams } from 'itty-router'

const router = IttyRouter({ port: 3001 })

router
  .all('*', withParams)
  .get('/basic', () => new Response('Success!'))
  .get('/text', () => 'Success!')
  .get('/params/:foo', ({ foo }) => foo)
  .get('/json', () => ({ foo: 'bar' }))
  .get('/throw', (a) => a.b.c)
  .all('*', () => error(404))

export default {
  fetch: (request, ...args) => router
                                 .fetch(request, ...args)
                                 .then(json)
                                 .catch(error)
}

Type of Change (select one and follow subtasks)

  • Documentation (README.md)
  • 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)
    • I have included test coverage
  • Breaking change (fix or feature that would cause existing functionality/userland code to not work as expected)
    • 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 documentation (if appropriate)

@kwhitley kwhitley self-assigned this Mar 15, 2024
@kwhitley kwhitley marked this pull request as draft March 15, 2024 06:31
@kwhitley kwhitley added new feature New feature or request in progress I'm working on it... allegedly... Feedback Requested API Changes, interface questions, etc... labels Mar 15, 2024
@kwhitley kwhitley changed the title 4.3x - FlowRouter + AutoRouter DRAFT: 4.3x - FlowRouter + AutoRouter Mar 15, 2024
@kwhitley kwhitley changed the title DRAFT: 4.3x - FlowRouter + AutoRouter DRAFT: 4.3x - IttyRouter + Router + AutoRouter Mar 15, 2024
@kwhitley kwhitley added the PENDING RELEASE Final stages before release! :D label Mar 19, 2024
@kwhitley kwhitley changed the title DRAFT: 4.3x - IttyRouter + Router + AutoRouter 4.3x Draft: IttyRouter + Router + AutoRouter Mar 19, 2024
@kwhitley kwhitley changed the title 4.3x Draft: IttyRouter + Router + AutoRouter 4.3x DRAFT: IttyRouter + Router + AutoRouter Mar 19, 2024
* replaced after stage with finally

* fixed potential bug in createResponse to prevent pollution if passed a Request as second param, like in v4.3 stages

* createResponse simplified and safe against using as ResponseHandler
@kwhitley kwhitley changed the base branch from v4.x to v5.x March 24, 2024 20:07
@kwhitley kwhitley marked this pull request as ready for review March 24, 2024 20:08
@kwhitley kwhitley changed the title 4.3x DRAFT: IttyRouter + Router + AutoRouter v5.x DRAFT Mar 24, 2024
@kwhitley kwhitley merged commit 4d56b22 into v5.x Mar 24, 2024
4 checks passed
@kwhitley kwhitley deleted the auto-router-v2 branch March 24, 2024 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feedback Requested API Changes, interface questions, etc... in progress I'm working on it... allegedly... new feature New feature or request PENDING RELEASE Final stages before release! :D
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant