@serwist/routing 9.0.0-preview.17 → 9.0.0-preview.18
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/index.d.ts +2 -12
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -77
- package/package.json +3 -14
- package/src/index.ts +2 -22
- package/dist/NavigationRoute.d.ts +0 -57
- package/dist/NavigationRoute.d.ts.map +0 -1
- package/dist/RegExpRoute.d.ts +0 -24
- package/dist/RegExpRoute.d.ts.map +0 -1
- package/dist/Route.d.ts +0 -33
- package/dist/Route.d.ts.map +0 -1
- package/dist/Router.d.ts +0 -124
- package/dist/Router.d.ts.map +0 -1
- package/dist/_types.d.ts +0 -48
- package/dist/_types.d.ts.map +0 -1
- package/dist/chunks/parseRoute.js +0 -401
- package/dist/index.internal.d.ts +0 -4
- package/dist/index.internal.d.ts.map +0 -1
- package/dist/index.internal.js +0 -2
- package/dist/registerRoute.d.ts +0 -15
- package/dist/registerRoute.d.ts.map +0 -1
- package/dist/setCatchHandler.d.ts +0 -9
- package/dist/setCatchHandler.d.ts.map +0 -1
- package/dist/setDefaultHandler.d.ts +0 -12
- package/dist/setDefaultHandler.d.ts.map +0 -1
- package/dist/unregisterRoute.d.ts +0 -8
- package/dist/unregisterRoute.d.ts.map +0 -1
- package/dist/utils/constants.d.ts +0 -15
- package/dist/utils/constants.d.ts.map +0 -1
- package/dist/utils/getOrCreateDefaultRouter.d.ts +0 -10
- package/dist/utils/getOrCreateDefaultRouter.d.ts.map +0 -1
- package/dist/utils/normalizeHandler.d.ts +0 -10
- package/dist/utils/normalizeHandler.d.ts.map +0 -1
- package/dist/utils/parseRoute.d.ts +0 -5
- package/dist/utils/parseRoute.d.ts.map +0 -1
- package/src/NavigationRoute.ts +0 -119
- package/src/RegExpRoute.ts +0 -74
- package/src/Route.ts +0 -68
- package/src/Router.ts +0 -432
- package/src/_types.ts +0 -62
- package/src/index.internal.ts +0 -4
- package/src/registerRoute.ts +0 -33
- package/src/setCatchHandler.ts +0 -22
- package/src/setDefaultHandler.ts +0 -25
- package/src/unregisterRoute.ts +0 -12
- package/src/utils/constants.ts +0 -24
- package/src/utils/getOrCreateDefaultRouter.ts +0 -29
- package/src/utils/normalizeHandler.ts +0 -40
- package/src/utils/parseRoute.ts +0 -67
package/src/setDefaultHandler.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
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 type { RouteHandler } from "@serwist/core";
|
|
10
|
-
|
|
11
|
-
import { getOrCreateDefaultRouter } from "./utils/getOrCreateDefaultRouter.js";
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Defines a default `handler` that's called when no routes explicitly
|
|
15
|
-
* match the incoming request.
|
|
16
|
-
*
|
|
17
|
-
* Without a default handler, unmatched requests will go against the
|
|
18
|
-
* network as if there were no service worker present.
|
|
19
|
-
*
|
|
20
|
-
* @param handler A callback function that returns a Promise resulting in a Response.
|
|
21
|
-
*/
|
|
22
|
-
export const setDefaultHandler = (handler: RouteHandler): void => {
|
|
23
|
-
const defaultRouter = getOrCreateDefaultRouter();
|
|
24
|
-
defaultRouter.setDefaultHandler(handler);
|
|
25
|
-
};
|
package/src/unregisterRoute.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { Route } from "./Route.js";
|
|
2
|
-
import { getOrCreateDefaultRouter } from "./utils/getOrCreateDefaultRouter.js";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Unregisters a route from the singleton Router instance.
|
|
6
|
-
*
|
|
7
|
-
* @param route The route to unregister.
|
|
8
|
-
*/
|
|
9
|
-
export const unregisterRoute = (route: Route): void => {
|
|
10
|
-
const defaultRouter = getOrCreateDefaultRouter();
|
|
11
|
-
defaultRouter.unregisterRoute(route);
|
|
12
|
-
};
|
package/src/utils/constants.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
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[];
|
|
@@ -1,29 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,40 +0,0 @@
|
|
|
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
|
-
};
|
package/src/utils/parseRoute.ts
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import type { RouteHandler, RouteMatchCallback } from "@serwist/core";
|
|
2
|
-
import { SerwistError, logger } from "@serwist/core/internal";
|
|
3
|
-
|
|
4
|
-
import { RegExpRoute } from "../RegExpRoute.js";
|
|
5
|
-
import { Route } from "../Route.js";
|
|
6
|
-
import type { HTTPMethod } from "./constants.js";
|
|
7
|
-
|
|
8
|
-
export const parseRoute = (capture: RegExp | string | RouteMatchCallback | Route, handler?: RouteHandler, method?: HTTPMethod): Route => {
|
|
9
|
-
let route: Route;
|
|
10
|
-
|
|
11
|
-
if (typeof capture === "string") {
|
|
12
|
-
const captureUrl = new URL(capture, location.href);
|
|
13
|
-
|
|
14
|
-
if (process.env.NODE_ENV !== "production") {
|
|
15
|
-
if (!(capture.startsWith("/") || capture.startsWith("http"))) {
|
|
16
|
-
throw new SerwistError("invalid-string", {
|
|
17
|
-
moduleName: "@serwist/routing",
|
|
18
|
-
funcName: "registerRoute",
|
|
19
|
-
paramName: "capture",
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// We want to check if Express-style wildcards are in the pathname only.
|
|
24
|
-
// TODO: Remove this log message in v4.
|
|
25
|
-
const valueToCheck = capture.startsWith("http") ? captureUrl.pathname : capture;
|
|
26
|
-
|
|
27
|
-
// See https://github.com/pillarjs/path-to-regexp#parameters
|
|
28
|
-
const wildcards = "[*:?+]";
|
|
29
|
-
if (new RegExp(`${wildcards}`).exec(valueToCheck)) {
|
|
30
|
-
logger.debug(
|
|
31
|
-
`The '$capture' parameter contains an Express-style wildcard character (${wildcards}). Strings are now always interpreted as exact matches; use a RegExp for partial or wildcard matches.`,
|
|
32
|
-
);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const matchCallback: RouteMatchCallback = ({ url }) => {
|
|
37
|
-
if (process.env.NODE_ENV !== "production") {
|
|
38
|
-
if (url.pathname === captureUrl.pathname && url.origin !== captureUrl.origin) {
|
|
39
|
-
logger.debug(
|
|
40
|
-
`${capture} only partially matches the cross-origin URL ${url.toString()}. This route will only handle cross-origin requests if they match the entire URL.`,
|
|
41
|
-
);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return url.href === captureUrl.href;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
// If `capture` is a string then `handler` and `method` must be present.
|
|
49
|
-
route = new Route(matchCallback, handler!, method);
|
|
50
|
-
} else if (capture instanceof RegExp) {
|
|
51
|
-
// If `capture` is a `RegExp` then `handler` and `method` must be present.
|
|
52
|
-
route = new RegExpRoute(capture, handler!, method);
|
|
53
|
-
} else if (typeof capture === "function") {
|
|
54
|
-
// If `capture` is a function then `handler` and `method` must be present.
|
|
55
|
-
route = new Route(capture, handler!, method);
|
|
56
|
-
} else if (capture instanceof Route) {
|
|
57
|
-
route = capture;
|
|
58
|
-
} else {
|
|
59
|
-
throw new SerwistError("unsupported-route-type", {
|
|
60
|
-
moduleName: "@serwist/routing",
|
|
61
|
-
funcName: "registerRoute",
|
|
62
|
-
paramName: "capture",
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
return route;
|
|
67
|
-
};
|