Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
95ccb2263a
|
|||
|
dc88d900cb
|
|||
|
0dca90f4f9
|
|||
|
cb8724edc4
|
@@ -7,3 +7,7 @@ insert_final_newline = false
|
|||||||
[src/**.ts]
|
[src/**.ts]
|
||||||
charset = utf-8
|
charset = utf-8
|
||||||
indent_style = tab
|
indent_style = tab
|
||||||
|
|
||||||
|
[README.md]
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 4
|
||||||
|
|||||||
65
README.md
65
README.md
@@ -33,6 +33,10 @@ Migrating from `v2.x.x`, check out the [Migration Guide](MIGRATION.md).
|
|||||||
```typescript
|
```typescript
|
||||||
import { Router } from '@tsndr/cloudflare-worker-router'
|
import { Router } from '@tsndr/cloudflare-worker-router'
|
||||||
|
|
||||||
|
// Env Types
|
||||||
|
export type Var<T = string> = T
|
||||||
|
export type Secret<T = string> = T
|
||||||
|
|
||||||
export type Env = {
|
export type Env = {
|
||||||
// Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
|
// Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
|
||||||
// MY_KV_NAMESPACE: KVNamespace
|
// 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/
|
// Example binding to R2. Learn more at https://developers.cloudflare.com/workers/runtime-apis/r2/
|
||||||
// MY_BUCKET: R2Bucket
|
// MY_BUCKET: R2Bucket
|
||||||
|
|
||||||
SECRET_TOKEN: string
|
ENVIRONMENT: Var<'dev' | 'prod'>
|
||||||
|
|
||||||
|
SECRET_TOKEN: Secret
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize router
|
// Request Extension
|
||||||
const router = new Router<Env>()
|
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
|
// Enabling build in CORS support
|
||||||
router.cors()
|
router.cors()
|
||||||
@@ -289,6 +305,10 @@ npm i -D @tsndr/cloudflare-worker-router
|
|||||||
```typescript
|
```typescript
|
||||||
import { Router } from '@tsndr/cloudflare-worker-router'
|
import { Router } from '@tsndr/cloudflare-worker-router'
|
||||||
|
|
||||||
|
// Env Types
|
||||||
|
export type Var<T = string> = T
|
||||||
|
export type Secret<T = string> = T
|
||||||
|
|
||||||
export type Env = {
|
export type Env = {
|
||||||
// Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
|
// Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
|
||||||
// MY_KV_NAMESPACE: KVNamespace
|
// 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/
|
// Example binding to R2. Learn more at https://developers.cloudflare.com/workers/runtime-apis/r2/
|
||||||
// MY_BUCKET: R2Bucket
|
// 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
|
/// Example Route
|
||||||
//
|
//
|
||||||
@@ -311,12 +357,11 @@ const router = new Router<Env>()
|
|||||||
|
|
||||||
/// Example Route for splitting into multiple files
|
/// Example Route for splitting into multiple files
|
||||||
//
|
//
|
||||||
// const hiHandler: RouteHandler<Env> = async () => {
|
// const helloHandler: Handler = async () => {
|
||||||
// return new Response('Hello World')
|
// return new Response('Hello World')
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// router.get('/hi', hiHandler)
|
// router.get('/hellow', helloHandler)
|
||||||
|
|
||||||
|
|
||||||
// TODO: add your routes here
|
// TODO: add your routes here
|
||||||
|
|
||||||
@@ -338,6 +383,12 @@ import { Router } from '@tsndr/cloudflare-worker-router'
|
|||||||
|
|
||||||
const router = new Router()
|
const router = new Router()
|
||||||
|
|
||||||
|
// Enable Debug Mode
|
||||||
|
//router.debug()
|
||||||
|
|
||||||
|
// Enabling build in CORS support
|
||||||
|
//router.cors()
|
||||||
|
|
||||||
/// Example Route
|
/// Example Route
|
||||||
//
|
//
|
||||||
// router.get('/hi', async () => {
|
// router.get('/hi', async () => {
|
||||||
|
|||||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@tsndr/cloudflare-worker-router",
|
"name": "@tsndr/cloudflare-worker-router",
|
||||||
"version": "3.1.1",
|
"version": "3.1.3",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@tsndr/cloudflare-worker-router",
|
"name": "@tsndr/cloudflare-worker-router",
|
||||||
"version": "3.1.1",
|
"version": "3.1.3",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@cloudflare/workers-types": "^4.20231025.0",
|
"@cloudflare/workers-types": "^4.20231025.0",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@tsndr/cloudflare-worker-router",
|
"name": "@tsndr/cloudflare-worker-router",
|
||||||
"version": "3.1.1",
|
"version": "3.1.3",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"types": "index.d.ts",
|
"types": "index.d.ts",
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ export type Route<TEnv, TCtx, TReq> = {
|
|||||||
* @property {RouterRequest} req Request Object
|
* @property {RouterRequest} req Request Object
|
||||||
* @property {ExecutionContext} ctx Context 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
|
env: TEnv
|
||||||
req: RouterRequest<TReq>
|
req: RouterRequest<TReq>
|
||||||
dbg: boolean
|
dbg: boolean
|
||||||
@@ -80,7 +80,7 @@ export type RouterRequestQuery = {
|
|||||||
* @param {RouterContext} ctx
|
* @param {RouterContext} ctx
|
||||||
* @returns {Promise<Response | void> Response | void}
|
* @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
|
(ctx: RouterContext<TEnv, TCtx, TReq>): Promise<Response | void> | Response | void
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,7 +120,7 @@ export type RouterBuffer = {
|
|||||||
* @public
|
* @public
|
||||||
* @class
|
* @class
|
||||||
*/
|
*/
|
||||||
export class Router<TEnv = any, TCtx = any, TReq = any> {
|
export class Router<TEnv = any, TCtx = {}, TReq = {}> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Router Array
|
* Router Array
|
||||||
|
|||||||
Reference in New Issue
Block a user