@cfworker/web 2.1.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/accepts.js +81 -0
- package/dist/{application.d.ts → cjs/application.d.ts} +1 -1
- package/dist/cjs/application.js +46 -0
- package/dist/cjs/context.js +26 -0
- package/dist/cjs/cookies.js +21 -0
- package/dist/cjs/http-error.js +38 -0
- package/dist/cjs/index.js +29 -0
- package/dist/cjs/middleware.js +31 -0
- package/dist/cjs/normalize-pathname.js +22 -0
- package/dist/cjs/req.js +59 -0
- package/dist/cjs/response-builder.js +138 -0
- package/dist/cjs/router.d.ts +39 -0
- package/dist/cjs/router.js +105 -0
- package/dist/{validate.d.ts → cjs/validate.d.ts} +1 -1
- package/dist/cjs/validate.js +76 -0
- package/dist/esm/accepts.d.ts +12 -0
- package/dist/esm/application.d.ts +11 -0
- package/dist/esm/context.d.ts +15 -0
- package/dist/esm/cookies.d.ts +8 -0
- package/dist/esm/http-error.d.ts +7 -0
- package/dist/esm/index.js +12 -0
- package/dist/esm/middleware.d.ts +3 -0
- package/dist/esm/normalize-pathname.d.ts +3 -0
- package/dist/esm/req.d.ts +24 -0
- package/dist/esm/response-builder.d.ts +22 -0
- package/dist/esm/router.d.ts +39 -0
- package/dist/{router.js → esm/router.js} +0 -1
- package/dist/esm/validate.d.ts +21 -0
- package/dist/tsconfig-cjs.tsbuildinfo +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +11 -7
- package/src/application.ts +1 -1
- package/src/context.ts +1 -1
- package/src/cookies.ts +5 -1
- package/src/normalize-pathname.ts +1 -1
- package/src/req.ts +4 -4
- package/src/response-builder.ts +7 -7
- package/src/router.ts +33 -22
- package/src/validate.ts +2 -2
- package/dist/router.d.ts +0 -39
- /package/dist/{accepts.d.ts → cjs/accepts.d.ts} +0 -0
- /package/dist/{context.d.ts → cjs/context.d.ts} +0 -0
- /package/dist/{cookies.d.ts → cjs/cookies.d.ts} +0 -0
- /package/dist/{http-error.d.ts → cjs/http-error.d.ts} +0 -0
- /package/dist/{index.d.ts → cjs/index.d.ts} +0 -0
- /package/dist/{middleware.d.ts → cjs/middleware.d.ts} +0 -0
- /package/dist/{normalize-pathname.d.ts → cjs/normalize-pathname.d.ts} +0 -0
- /package/dist/{req.d.ts → cjs/req.d.ts} +0 -0
- /package/dist/{response-builder.d.ts → cjs/response-builder.d.ts} +0 -0
- /package/dist/{accepts.js → esm/accepts.js} +0 -0
- /package/dist/{application.js → esm/application.js} +0 -0
- /package/dist/{context.js → esm/context.js} +0 -0
- /package/dist/{cookies.js → esm/cookies.js} +0 -0
- /package/dist/{http-error.js → esm/http-error.js} +0 -0
- /package/dist/{index.js → esm/index.d.ts} +0 -0
- /package/dist/{middleware.js → esm/middleware.js} +0 -0
- /package/dist/{normalize-pathname.js → esm/normalize-pathname.js} +0 -0
- /package/dist/{req.js → esm/req.js} +0 -0
- /package/dist/{response-builder.js → esm/response-builder.js} +0 -0
- /package/dist/{validate.js → esm/validate.js} +0 -0
package/src/router.ts
CHANGED
|
@@ -2,26 +2,34 @@ import { Key, pathToRegexp, PathToRegexpOptions } from 'path-to-regexp';
|
|
|
2
2
|
import { Context } from './context.js';
|
|
3
3
|
import { composeMiddleware, Middleware } from './middleware.js';
|
|
4
4
|
|
|
5
|
-
export
|
|
5
|
+
export type Predicate = (context: Context) => boolean;
|
|
6
|
+
|
|
7
|
+
export const Method: (method: string) => Predicate = (method: string) => {
|
|
6
8
|
method = method.toUpperCase();
|
|
7
9
|
return ({ req }: Context) => req.method === method;
|
|
8
10
|
};
|
|
9
|
-
export const Get = Method('get');
|
|
10
|
-
export const Post = Method('post');
|
|
11
|
-
export const Put = Method('put');
|
|
12
|
-
export const Patch = Method('patch');
|
|
13
|
-
export const Delete = Method('delete');
|
|
14
|
-
export const Head = Method('head');
|
|
15
|
-
export const Options = Method('options');
|
|
16
|
-
|
|
17
|
-
export const Header
|
|
11
|
+
export const Get: Predicate = Method('get');
|
|
12
|
+
export const Post: Predicate = Method('post');
|
|
13
|
+
export const Put: Predicate = Method('put');
|
|
14
|
+
export const Patch: Predicate = Method('patch');
|
|
15
|
+
export const Delete: Predicate = Method('delete');
|
|
16
|
+
export const Head: Predicate = Method('head');
|
|
17
|
+
export const Options: Predicate = Method('options');
|
|
18
|
+
|
|
19
|
+
export const Header: (header: string, value: string) => Predicate = (
|
|
20
|
+
header: string,
|
|
21
|
+
value: string
|
|
22
|
+
) => {
|
|
18
23
|
value = value.toLowerCase();
|
|
19
24
|
return ({ req }: Context) => req.headers.get(header) === value;
|
|
20
25
|
};
|
|
21
|
-
export const Host
|
|
22
|
-
|
|
26
|
+
export const Host: (host: string) => Predicate = (host: string) =>
|
|
27
|
+
Header('host', host);
|
|
23
28
|
|
|
24
|
-
export const Path
|
|
29
|
+
export const Path: (
|
|
30
|
+
pattern: string,
|
|
31
|
+
options?: PathToRegexpOptions
|
|
32
|
+
) => Predicate = (pattern: string, options?: PathToRegexpOptions) => {
|
|
25
33
|
const regExp = pathToRegexp(pattern, options);
|
|
26
34
|
|
|
27
35
|
return ({ req: { url, params } }: Context) => {
|
|
@@ -56,37 +64,37 @@ export class Router {
|
|
|
56
64
|
this.routes = [];
|
|
57
65
|
}
|
|
58
66
|
|
|
59
|
-
public get(pathname: string, ...middleware: Middleware[]) {
|
|
67
|
+
public get(pathname: string, ...middleware: Middleware[]): Router {
|
|
60
68
|
const opts = this.options.pathToRegExpOptions;
|
|
61
69
|
return this.compose([Get, Path(pathname, opts)], ...middleware);
|
|
62
70
|
}
|
|
63
71
|
|
|
64
|
-
public post(pathname: string, ...middleware: Middleware[]) {
|
|
72
|
+
public post(pathname: string, ...middleware: Middleware[]): Router {
|
|
65
73
|
const opts = this.options.pathToRegExpOptions;
|
|
66
74
|
return this.compose([Post, Path(pathname, opts)], ...middleware);
|
|
67
75
|
}
|
|
68
76
|
|
|
69
|
-
public put(pathname: string, ...middleware: Middleware[]) {
|
|
77
|
+
public put(pathname: string, ...middleware: Middleware[]): Router {
|
|
70
78
|
const opts = this.options.pathToRegExpOptions;
|
|
71
79
|
return this.compose([Put, Path(pathname, opts)], ...middleware);
|
|
72
80
|
}
|
|
73
81
|
|
|
74
|
-
public patch(pathname: string, ...middleware: Middleware[]) {
|
|
82
|
+
public patch(pathname: string, ...middleware: Middleware[]): Router {
|
|
75
83
|
const opts = this.options.pathToRegExpOptions;
|
|
76
84
|
return this.compose([Patch, Path(pathname, opts)], ...middleware);
|
|
77
85
|
}
|
|
78
86
|
|
|
79
|
-
public delete(pathname: string, ...middleware: Middleware[]) {
|
|
87
|
+
public delete(pathname: string, ...middleware: Middleware[]): Router {
|
|
80
88
|
const opts = this.options.pathToRegExpOptions;
|
|
81
89
|
return this.compose([Delete, Path(pathname, opts)], ...middleware);
|
|
82
90
|
}
|
|
83
91
|
|
|
84
|
-
public head(pathname: string, ...middleware: Middleware[]) {
|
|
92
|
+
public head(pathname: string, ...middleware: Middleware[]): Router {
|
|
85
93
|
const opts = this.options.pathToRegExpOptions;
|
|
86
94
|
return this.compose([Head, Path(pathname, opts)], ...middleware);
|
|
87
95
|
}
|
|
88
96
|
|
|
89
|
-
public all(pathname: string, ...middleware: Middleware[]) {
|
|
97
|
+
public all(pathname: string, ...middleware: Middleware[]): Router {
|
|
90
98
|
const opts = this.options.pathToRegExpOptions;
|
|
91
99
|
return this.compose([Path(pathname, opts)], ...middleware);
|
|
92
100
|
}
|
|
@@ -101,7 +109,10 @@ export class Router {
|
|
|
101
109
|
}
|
|
102
110
|
};
|
|
103
111
|
|
|
104
|
-
public compose(
|
|
112
|
+
public compose(
|
|
113
|
+
conditions: RouteCondition[],
|
|
114
|
+
...middleware: Middleware[]
|
|
115
|
+
): Router {
|
|
105
116
|
this.routes.push({
|
|
106
117
|
conditions,
|
|
107
118
|
middleware: composeMiddleware(middleware)
|
|
@@ -109,7 +120,7 @@ export class Router {
|
|
|
109
120
|
return this;
|
|
110
121
|
}
|
|
111
122
|
|
|
112
|
-
public resolve(ctx: Context) {
|
|
123
|
+
public resolve(ctx: Context): Route | undefined {
|
|
113
124
|
return this.routes.find(
|
|
114
125
|
({ conditions }) =>
|
|
115
126
|
conditions.length === 0 || conditions.every(c => c(ctx))
|
package/src/validate.ts
CHANGED
|
@@ -30,7 +30,7 @@ const hasBody: Record<string, true> = {
|
|
|
30
30
|
function middlewareFactory(
|
|
31
31
|
schemas: RequestSchemas,
|
|
32
32
|
parser: RequestParser = Object.fromEntries,
|
|
33
|
-
lookup = Object.create(null)
|
|
33
|
+
lookup: Record<string, Schema | boolean> = Object.create(null)
|
|
34
34
|
): Middleware {
|
|
35
35
|
const {
|
|
36
36
|
headers: $headers,
|
|
@@ -118,4 +118,4 @@ function validateRequestPart(
|
|
|
118
118
|
throw new HttpError(400, errorBody);
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
-
export const validate = middlewareFactory;
|
|
121
|
+
export const validate: typeof middlewareFactory = middlewareFactory;
|
package/dist/router.d.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { PathToRegexpOptions } from 'path-to-regexp';
|
|
2
|
-
import { Context } from './context.js';
|
|
3
|
-
import { Middleware } from './middleware.js';
|
|
4
|
-
export declare const Method: (method: string) => ({ req }: Context) => boolean;
|
|
5
|
-
export declare const Get: ({ req }: Context) => boolean;
|
|
6
|
-
export declare const Post: ({ req }: Context) => boolean;
|
|
7
|
-
export declare const Put: ({ req }: Context) => boolean;
|
|
8
|
-
export declare const Patch: ({ req }: Context) => boolean;
|
|
9
|
-
export declare const Delete: ({ req }: Context) => boolean;
|
|
10
|
-
export declare const Head: ({ req }: Context) => boolean;
|
|
11
|
-
export declare const Options: ({ req }: Context) => boolean;
|
|
12
|
-
export declare const Header: (header: string, value: string) => ({ req }: Context) => boolean;
|
|
13
|
-
export declare const Host: (host: string) => ({ req }: Context) => boolean;
|
|
14
|
-
export declare const Referer: (host: string) => ({ req }: Context) => boolean;
|
|
15
|
-
export declare const Path: (pattern: string, options?: PathToRegexpOptions) => ({ req: { url, params } }: Context) => boolean;
|
|
16
|
-
export type RouteCondition = (context: Context) => boolean;
|
|
17
|
-
export interface Route {
|
|
18
|
-
conditions: RouteCondition[];
|
|
19
|
-
middleware: Middleware;
|
|
20
|
-
}
|
|
21
|
-
export interface RouterOptions {
|
|
22
|
-
pathToRegExpOptions?: PathToRegexpOptions;
|
|
23
|
-
}
|
|
24
|
-
export declare const defaultRouterOptions: RouterOptions;
|
|
25
|
-
export declare class Router {
|
|
26
|
-
private readonly options;
|
|
27
|
-
private readonly routes;
|
|
28
|
-
constructor(options?: RouterOptions);
|
|
29
|
-
get(pathname: string, ...middleware: Middleware[]): this;
|
|
30
|
-
post(pathname: string, ...middleware: Middleware[]): this;
|
|
31
|
-
put(pathname: string, ...middleware: Middleware[]): this;
|
|
32
|
-
patch(pathname: string, ...middleware: Middleware[]): this;
|
|
33
|
-
delete(pathname: string, ...middleware: Middleware[]): this;
|
|
34
|
-
head(pathname: string, ...middleware: Middleware[]): this;
|
|
35
|
-
all(pathname: string, ...middleware: Middleware[]): this;
|
|
36
|
-
middleware: Middleware;
|
|
37
|
-
compose(conditions: RouteCondition[], ...middleware: Middleware[]): this;
|
|
38
|
-
resolve(ctx: Context): Route | undefined;
|
|
39
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|