Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
307a93d86c
|
|||
|
3a955af1bc
|
|||
|
db22422968
|
|||
|
0a73e4c32e
|
|||
|
8bd8e50ee5
|
|||
|
e4ec06bf47
|
|||
|
2ad75dc3fb
|
|||
|
c2b6189a86
|
|||
|
33cafbf17c
|
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "@tsndr/cloudflare-worker-router",
|
||||
"version": "3.0.0-7",
|
||||
"version": "3.0.0-11",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@tsndr/cloudflare-worker-router",
|
||||
"version": "3.0.0-7",
|
||||
"version": "3.0.0-11",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@cloudflare/workers-types": "^4.20230115.0",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@tsndr/cloudflare-worker-router",
|
||||
"version": "3.0.0-7",
|
||||
"version": "3.0.0-11",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"types": "index.d.ts",
|
||||
|
||||
32
src/index.ts
32
src/index.ts
@@ -36,7 +36,7 @@ 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 {Request} raw Raw Request Object
|
||||
* @property {IncomingRequestCfProperties} [cf] object containing custom Cloudflare properties. (https://developers.cloudflare.com/workers/examples/accessing-the-cloudflare-object)
|
||||
*/
|
||||
export type RouterRequest<TExt> = {
|
||||
@@ -45,10 +45,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 +392,14 @@ export class Router<TEnv = any, TExt = any> {
|
||||
raw: request,
|
||||
params: {},
|
||||
query: {},
|
||||
body: '',
|
||||
bearer: () => request.headers.get('Authorization')?.replace('Bearer ', '').trim() ?? '',
|
||||
arrayBuffer: (): Promise<ArrayBuffer> => request.arrayBuffer(),
|
||||
text: (): Promise<string> => request.text(),
|
||||
json: <T>(): Promise<T> => request.json<T>(),
|
||||
formData: (): Promise<FormData> => request.formData(),
|
||||
blob: (): Promise<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 +407,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
|
||||
|
||||
@@ -421,8 +429,10 @@ export class Router<TEnv = any, TExt = any> {
|
||||
if (!response)
|
||||
return new Response(this.debugMode ? 'Handler did not return a Response!' : null, { status: 404 })
|
||||
|
||||
if (this.corsEnabled)
|
||||
if (this.corsEnabled) {
|
||||
response = new Response(response.body, response)
|
||||
this.setCorsHeaders(response.headers)
|
||||
}
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user