@serwist/routing 8.4.3 → 9.0.0-preview.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 (42) hide show
  1. package/dist/NavigationRoute.d.ts +2 -2
  2. package/dist/NavigationRoute.d.ts.map +1 -0
  3. package/dist/RegExpRoute.d.ts +2 -2
  4. package/dist/RegExpRoute.d.ts.map +1 -0
  5. package/dist/Route.d.ts +2 -2
  6. package/dist/Route.d.ts.map +1 -0
  7. package/dist/Router.d.ts +2 -2
  8. package/dist/Router.d.ts.map +1 -0
  9. package/dist/_types.d.ts +1 -0
  10. package/dist/_types.d.ts.map +1 -0
  11. package/dist/index.d.ts +3 -1
  12. package/dist/index.d.ts.map +1 -0
  13. package/dist/index.js +19 -17
  14. package/dist/registerRoute.d.ts +3 -3
  15. package/dist/registerRoute.d.ts.map +1 -0
  16. package/dist/setCatchHandler.d.ts +2 -2
  17. package/dist/setCatchHandler.d.ts.map +1 -0
  18. package/dist/setDefaultHandler.d.ts +3 -3
  19. package/dist/setDefaultHandler.d.ts.map +1 -0
  20. package/dist/unregisterRoute.d.ts +8 -0
  21. package/dist/unregisterRoute.d.ts.map +1 -0
  22. package/dist/utils/constants.d.ts +3 -6
  23. package/dist/utils/constants.d.ts.map +1 -0
  24. package/dist/utils/getOrCreateDefaultRouter.d.ts +1 -0
  25. package/dist/utils/getOrCreateDefaultRouter.d.ts.map +1 -0
  26. package/dist/utils/normalizeHandler.d.ts +1 -0
  27. package/dist/utils/normalizeHandler.d.ts.map +1 -0
  28. package/package.json +18 -16
  29. package/src/NavigationRoute.ts +119 -0
  30. package/src/RegExpRoute.ts +74 -0
  31. package/src/Route.ts +68 -0
  32. package/src/Router.ts +436 -0
  33. package/src/_types.ts +62 -0
  34. package/{dist/index.d.cts → src/index.ts} +12 -1
  35. package/src/registerRoute.ts +89 -0
  36. package/src/setCatchHandler.ts +22 -0
  37. package/src/setDefaultHandler.ts +25 -0
  38. package/src/unregisterRoute.ts +12 -0
  39. package/src/utils/constants.ts +24 -0
  40. package/src/utils/getOrCreateDefaultRouter.ts +29 -0
  41. package/src/utils/normalizeHandler.ts +40 -0
  42. package/dist/index.cjs +0 -728
@@ -0,0 +1,24 @@
1
+ /*
2
+ Copyright 2018 Google LLC
3
+
4
+ Use of this source code is governed by an MIT-style
5
+ license that can be found in the LICENSE file or at
6
+ https://opensource.org/licenses/MIT.
7
+ */
8
+
9
+ export type HTTPMethod = "DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT";
10
+
11
+ /**
12
+ * The default HTTP method, 'GET', used when there's no specific method
13
+ * configured for a route.
14
+ *
15
+ * @private
16
+ */
17
+ export const defaultMethod = "GET" satisfies HTTPMethod;
18
+
19
+ /**
20
+ * The list of valid HTTP methods associated with requests that could be routed.
21
+ *
22
+ * @private
23
+ */
24
+ export const validMethods = ["DELETE", "GET", "HEAD", "PATCH", "POST", "PUT"] satisfies HTTPMethod[];
@@ -0,0 +1,29 @@
1
+ /*
2
+ Copyright 2019 Google LLC
3
+
4
+ Use of this source code is governed by an MIT-style
5
+ license that can be found in the LICENSE file or at
6
+ https://opensource.org/licenses/MIT.
7
+ */
8
+
9
+ import { Router } from "../Router.js";
10
+
11
+ let defaultRouter: Router;
12
+
13
+ /**
14
+ * Creates a new, singleton Router instance if one does not exist. If one
15
+ * does already exist, that instance is returned.
16
+ *
17
+ * @private
18
+ * @returns
19
+ */
20
+ export const getOrCreateDefaultRouter = (): Router => {
21
+ if (!defaultRouter) {
22
+ defaultRouter = new Router();
23
+
24
+ // The helpers that use the default Router assume these listeners exist.
25
+ defaultRouter.addFetchListener();
26
+ defaultRouter.addCacheListener();
27
+ }
28
+ return defaultRouter;
29
+ };
@@ -0,0 +1,40 @@
1
+ /*
2
+ Copyright 2018 Google LLC
3
+
4
+ Use of this source code is governed by an MIT-style
5
+ license that can be found in the LICENSE file or at
6
+ https://opensource.org/licenses/MIT.
7
+ */
8
+
9
+ import type { RouteHandler, RouteHandlerObject } from "@serwist/core";
10
+ import { assert } from "@serwist/core/internal";
11
+
12
+ /**
13
+ * @param handler Either a function, or an object with a
14
+ * 'handle' method.
15
+ * @returns An object with a handle method.
16
+ *
17
+ * @private
18
+ */
19
+ export const normalizeHandler = (handler: RouteHandler): RouteHandlerObject => {
20
+ if (handler && typeof handler === "object") {
21
+ if (process.env.NODE_ENV !== "production") {
22
+ assert!.hasMethod(handler, "handle", {
23
+ moduleName: "@serwist/routing",
24
+ className: "Route",
25
+ funcName: "constructor",
26
+ paramName: "handler",
27
+ });
28
+ }
29
+ return handler;
30
+ }
31
+ if (process.env.NODE_ENV !== "production") {
32
+ assert!.isType(handler, "function", {
33
+ moduleName: "@serwist/routing",
34
+ className: "Route",
35
+ funcName: "constructor",
36
+ paramName: "handler",
37
+ });
38
+ }
39
+ return { handle: handler };
40
+ };