1
0

4 Commits

Author SHA1 Message Date
95ccb2263a 3.1.3 2023-11-14 14:54:46 +01:00
dc88d900cb fix default extension types 2023-11-14 14:54:04 +01:00
0dca90f4f9 3.1.2 2023-11-12 18:52:30 +01:00
cb8724edc4 clean up 2023-11-12 18:48:49 +01:00
5 changed files with 73 additions and 18 deletions

View File

@@ -7,3 +7,7 @@ insert_final_newline = false
[src/**.ts]
charset = utf-8
indent_style = tab
[README.md]
indent_style = space
indent_size = 4

View File

@@ -33,6 +33,10 @@ Migrating from `v2.x.x`, check out the [Migration Guide](MIGRATION.md).
```typescript
import { Router } from '@tsndr/cloudflare-worker-router'
// Env Types
export type Var<T = string> = T
export type Secret<T = string> = T
export type Env = {
// Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
// MY_KV_NAMESPACE: KVNamespace
@@ -43,11 +47,23 @@ export type Env = {
// Example binding to R2. Learn more at https://developers.cloudflare.com/workers/runtime-apis/r2/
// MY_BUCKET: R2Bucket
SECRET_TOKEN: string
ENVIRONMENT: Var<'dev' | 'prod'>
SECRET_TOKEN: Secret
}
// Initialize router
const router = new Router<Env>()
// Request Extension
export type ExtReq = {
userId?: number
}
// Context Extension
export type ExtCtx = {
//sentry?: Toucan
}
// Initialize Router
const router = new Router<Env, ExtCtx, ExtReq>()
// Enabling build in CORS support
router.cors()
@@ -289,6 +305,10 @@ npm i -D @tsndr/cloudflare-worker-router
```typescript
import { Router } from '@tsndr/cloudflare-worker-router'
// Env Types
export type Var<T = string> = T
export type Secret<T = string> = T
export type Env = {
// Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
// MY_KV_NAMESPACE: KVNamespace
@@ -298,9 +318,35 @@ export type Env = {
//
// Example binding to R2. Learn more at https://developers.cloudflare.com/workers/runtime-apis/r2/
// MY_BUCKET: R2Bucket
//
// Example Variable
// ENVIRONMENT: Var<'dev' | 'prod'>
//
// Example Secret
// JWT_SECRET: Secret
}
const router = new Router<Env>()
// Request Extension
export type ExtReq = {
userId?: number
}
// Context Extension
export type ExtCtx = {
//sentry?: Toucan
}
// Handler Type
export type Handler = RouterHandler<Env, ExtCtx, ExtReq>
// Initialize Router
const router = new Router<Env, ExtCtx, ExtReq>()
// Enable Debug Mode
router.debug()
// Enabling build in CORS support
//router.cors()
/// Example Route
//
@@ -311,12 +357,11 @@ const router = new Router<Env>()
/// Example Route for splitting into multiple files
//
// const hiHandler: RouteHandler<Env> = async () => {
// const helloHandler: Handler = async () => {
// return new Response('Hello World')
// }
//
// router.get('/hi', hiHandler)
// router.get('/hellow', helloHandler)
// TODO: add your routes here
@@ -338,6 +383,12 @@ import { Router } from '@tsndr/cloudflare-worker-router'
const router = new Router()
// Enable Debug Mode
//router.debug()
// Enabling build in CORS support
//router.cors()
/// Example Route
//
// router.get('/hi', async () => {

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "@tsndr/cloudflare-worker-router",
"version": "3.1.1",
"version": "3.1.3",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@tsndr/cloudflare-worker-router",
"version": "3.1.1",
"version": "3.1.3",
"license": "MIT",
"devDependencies": {
"@cloudflare/workers-types": "^4.20231025.0",

View File

@@ -1,6 +1,6 @@
{
"name": "@tsndr/cloudflare-worker-router",
"version": "3.1.1",
"version": "3.1.3",
"description": "",
"main": "index.js",
"types": "index.d.ts",

View File

@@ -20,7 +20,7 @@ export type Route<TEnv, TCtx, TReq> = {
* @property {RouterRequest} req Request Object
* @property {ExecutionContext} ctx Context Object
*/
export type RouterContext<TEnv = any, TCtx = any, TReq = any> = TCtx & {
export type RouterContext<TEnv = any, TCtx = {}, TReq = {}> = TCtx & {
env: TEnv
req: RouterRequest<TReq>
dbg: boolean
@@ -80,7 +80,7 @@ export type RouterRequestQuery = {
* @param {RouterContext} ctx
* @returns {Promise<Response | void> Response | void}
*/
export type RouterHandler<TEnv = any, TCtx = any, TReq = any> = {
export type RouterHandler<TEnv = any, TCtx = {}, TReq = {}> = {
(ctx: RouterContext<TEnv, TCtx, TReq>): Promise<Response | void> | Response | void
}
@@ -120,7 +120,7 @@ export type RouterBuffer = {
* @public
* @class
*/
export class Router<TEnv = any, TCtx = any, TReq = any> {
export class Router<TEnv = any, TCtx = {}, TReq = {}> {
/**
* Router Array