diff --git a/.editorconfig b/.editorconfig index 29385e8..5648cbb 100644 --- a/.editorconfig +++ b/.editorconfig @@ -7,3 +7,7 @@ insert_final_newline = false [src/**.ts] charset = utf-8 indent_style = tab + +[README.md] +indent_style = space +indent_size = 4 diff --git a/README.md b/README.md index 807194d..57fe741 100644 --- a/README.md +++ b/README.md @@ -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 +export type Secret = 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() +// Request Extension +export type ExtReq = { + userId?: number +} + +// Context Extension +export type ExtCtx = { + //sentry?: Toucan +} + +// Initialize Router +const router = new Router() // 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 +export type Secret = T + export type Env = { // Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/ // MY_KV_NAMESPACE: KVNamespace @@ -298,32 +318,57 @@ 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() +// Request Extension +export type ExtReq = { + userId?: number +} + +// Context Extension +export type ExtCtx = { + //sentry?: Toucan +} + +// Handler Type +export type Handler = RouterHandler + +// Initialize Router +const router = new Router() + +// Enable Debug Mode +router.debug() + +// Enabling build in CORS support +//router.cors() /// Example Route // // router.get('/hi', async () => { -// return new Response('Hello World') -//}) +// return new Response('Hello World') +// }) /// Example Route for splitting into multiple files // -// const hiHandler: RouteHandler = async () => { +// const helloHandler: Handler = async () => { // return new Response('Hello World') // } // -// router.get('/hi', hiHandler) - +// router.get('/hellow', helloHandler) // TODO: add your routes here export default { - async fetch(request: Request, env: Env, ctx: ExecutionContext) { - return router.handle(request, env, ctx) - } + async fetch(request: Request, env: Env, ctx: ExecutionContext) { + return router.handle(request, env, ctx) + } } ``` @@ -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 () => {