@cfworker/web 2.1.0 → 4.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.
Files changed (60) hide show
  1. package/dist/cjs/accepts.js +81 -0
  2. package/dist/{application.d.ts → cjs/application.d.ts} +1 -1
  3. package/dist/cjs/application.js +46 -0
  4. package/dist/cjs/context.js +26 -0
  5. package/dist/{cookies.d.ts → cjs/cookies.d.ts} +2 -2
  6. package/dist/cjs/cookies.js +21 -0
  7. package/dist/cjs/http-error.js +38 -0
  8. package/dist/cjs/index.js +29 -0
  9. package/dist/cjs/middleware.js +31 -0
  10. package/dist/cjs/normalize-pathname.js +22 -0
  11. package/dist/cjs/req.js +59 -0
  12. package/dist/cjs/response-builder.js +138 -0
  13. package/dist/cjs/router.d.ts +39 -0
  14. package/dist/cjs/router.js +105 -0
  15. package/dist/{validate.d.ts → cjs/validate.d.ts} +1 -1
  16. package/dist/cjs/validate.js +76 -0
  17. package/dist/esm/accepts.d.ts +12 -0
  18. package/dist/esm/application.d.ts +11 -0
  19. package/dist/esm/context.d.ts +15 -0
  20. package/dist/esm/cookies.d.ts +8 -0
  21. package/dist/esm/http-error.d.ts +7 -0
  22. package/dist/esm/index.js +12 -0
  23. package/dist/esm/middleware.d.ts +3 -0
  24. package/dist/esm/normalize-pathname.d.ts +3 -0
  25. package/dist/esm/req.d.ts +24 -0
  26. package/dist/esm/response-builder.d.ts +22 -0
  27. package/dist/esm/router.d.ts +39 -0
  28. package/dist/{router.js → esm/router.js} +4 -5
  29. package/dist/esm/validate.d.ts +21 -0
  30. package/dist/tsconfig-cjs.tsbuildinfo +1 -0
  31. package/dist/tsconfig.tsbuildinfo +1 -0
  32. package/package.json +21 -17
  33. package/src/application.ts +1 -1
  34. package/src/context.ts +1 -1
  35. package/src/cookies.ts +3 -3
  36. package/src/normalize-pathname.ts +1 -1
  37. package/src/req.ts +4 -4
  38. package/src/response-builder.ts +7 -7
  39. package/src/router.ts +37 -26
  40. package/src/validate.ts +2 -2
  41. package/dist/router.d.ts +0 -39
  42. /package/dist/{accepts.d.ts → cjs/accepts.d.ts} +0 -0
  43. /package/dist/{context.d.ts → cjs/context.d.ts} +0 -0
  44. /package/dist/{http-error.d.ts → cjs/http-error.d.ts} +0 -0
  45. /package/dist/{index.d.ts → cjs/index.d.ts} +0 -0
  46. /package/dist/{middleware.d.ts → cjs/middleware.d.ts} +0 -0
  47. /package/dist/{normalize-pathname.d.ts → cjs/normalize-pathname.d.ts} +0 -0
  48. /package/dist/{req.d.ts → cjs/req.d.ts} +0 -0
  49. /package/dist/{response-builder.d.ts → cjs/response-builder.d.ts} +0 -0
  50. /package/dist/{accepts.js → esm/accepts.js} +0 -0
  51. /package/dist/{application.js → esm/application.js} +0 -0
  52. /package/dist/{context.js → esm/context.js} +0 -0
  53. /package/dist/{cookies.js → esm/cookies.js} +0 -0
  54. /package/dist/{http-error.js → esm/http-error.js} +0 -0
  55. /package/dist/{index.js → esm/index.d.ts} +0 -0
  56. /package/dist/{middleware.js → esm/middleware.js} +0 -0
  57. /package/dist/{normalize-pathname.js → esm/normalize-pathname.js} +0 -0
  58. /package/dist/{req.js → esm/req.js} +0 -0
  59. /package/dist/{response-builder.js → esm/response-builder.js} +0 -0
  60. /package/dist/{validate.js → esm/validate.js} +0 -0
package/src/router.ts CHANGED
@@ -2,34 +2,42 @@ 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 const Method = (method: string) => {
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 = (header: string, value: string) => {
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 = (host: string) => Header('host', host);
22
- export const Referer = (host: string) => Header('referer', host);
26
+ export const Host: (host: string) => Predicate = (host: string) =>
27
+ Header('host', host);
23
28
 
24
- export const Path = (pattern: string, options?: PathToRegexpOptions) => {
25
- const regExp = pathToRegexp(pattern, options);
29
+ export const Path: (
30
+ pattern: string,
31
+ options?: PathToRegexpOptions
32
+ ) => Predicate = (pattern: string, options?: PathToRegexpOptions) => {
33
+ const { regexp, keys } = pathToRegexp(pattern, options);
26
34
 
27
35
  return ({ req: { url, params } }: Context) => {
28
- const match = url.pathname.match(regExp);
36
+ const match = url.pathname.match(regexp);
29
37
  if (!match) {
30
38
  return false;
31
39
  }
32
- collectParameters(regExp.keys, match, params);
40
+ collectParameters(keys, match, params);
33
41
  return true;
34
42
  };
35
43
  };
@@ -46,7 +54,7 @@ export interface RouterOptions {
46
54
  }
47
55
 
48
56
  export const defaultRouterOptions: RouterOptions = {
49
- pathToRegExpOptions: { strict: true }
57
+ pathToRegExpOptions: {}
50
58
  };
51
59
 
52
60
  export class Router {
@@ -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(conditions: RouteCondition[], ...middleware: Middleware[]) {
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