1
0

4 Commits

Author SHA1 Message Date
0a73e4c32e 3.0.0-10 2023-02-05 02:11:19 +01:00
8bd8e50ee5 update 2023-02-05 02:11:09 +01:00
e4ec06bf47 3.0.0-9 2023-02-05 01:46:53 +01:00
2ad75dc3fb move things around, update body behavior 2023-02-05 01:46:20 +01:00
3 changed files with 19 additions and 12 deletions

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "@tsndr/cloudflare-worker-router",
"version": "3.0.0-8",
"version": "3.0.0-10",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@tsndr/cloudflare-worker-router",
"version": "3.0.0-8",
"version": "3.0.0-10",
"license": "MIT",
"devDependencies": {
"@cloudflare/workers-types": "^4.20230115.0",

View File

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

View File

@@ -36,7 +36,6 @@ export type RouterContext<TEnv = any, TExt = any> = {
* @property {RouterRequestParams} params Object containing all parameters defined in the url string
* @property {RouterRequestQuery} query Object containing all query parameters
* @property {Headers} headers Request headers object
* @property {string | any} body Only available if method is `POST`, `PUT`, `PATCH` or `DELETE`. Contains either the received body string or a parsed object if valid JSON was sent.
* @property {IncomingRequestCfProperties} [cf] object containing custom Cloudflare properties. (https://developers.cloudflare.com/workers/examples/accessing-the-cloudflare-object)
*/
export type RouterRequest<TExt> = {
@@ -45,10 +44,14 @@ export type RouterRequest<TExt> = {
params: RouterRequestParams
query: RouterRequestQuery
headers: Headers
body: string | any
raw: Request
cf?: IncomingRequestCfProperties
arrayBuffer(): Promise<ArrayBuffer>
text(): Promise<string>
json<T>(): Promise<T>
formData(): Promise<FormData>
blob(): Promise<Blob>
bearer: () => string
cf?: IncomingRequestCfProperties
} & TExt
/**
@@ -388,15 +391,14 @@ export class Router<TEnv = any, TExt = any> {
raw: request,
params: {},
query: {},
body: '',
arrayBuffer: request.arrayBuffer,
text: request.text,
json: <T>(): Promise<T> => request.json<T>(),
formData: request.formData,
blob: request.blob,
bearer: () => request.headers.get('Authorization')?.replace(/^(B|b)earer /, '').trim() ?? '',
} as RouterRequest<TExt>
const route = this.getRoute(req)
if (!route)
return new Response(this.debugMode ? 'Route not found!' : null, { status: 404 })
if (this.corsEnabled && req.method === 'OPTIONS') {
return new Response(null, {
headers: this.setCorsHeaders(),
@@ -404,6 +406,11 @@ export class Router<TEnv = any, TExt = any> {
})
}
const route = this.getRoute(req)
if (!route)
return new Response(this.debugMode ? 'Route not found!' : null, { status: 404 })
const handlers = [...this.globalHandlers, ...route.handlers]
const dbg = this.debugMode