1
- import { MatchHandler , router , Routes } from "https://crux.land/router@0.0.4" ;
1
+ import { MatchHandler , router , Routes } from "https://crux.land/router@0.0.5" ;
2
+
3
+ export type { MatchHandler } ;
2
4
3
5
class UnhandledRouteError extends Error {
4
6
routes : Routes ;
@@ -9,10 +11,12 @@ class UnhandledRouteError extends Error {
9
11
const method = request . method ;
10
12
const reqPath = new URL ( request . url ) . pathname ;
11
13
const routesNumber = Object . entries ( routes ) . length ;
12
- const routePlural = routesNumber === 1 ? "route" : "routes" ;
14
+ const routePlural = routesNumber === 1
15
+ ? "route has a handler"
16
+ : "routes have handlers" ;
13
17
14
18
// deno-fmt-ignore
15
- super ( `${ method } ${ reqPath } (${ routesNumber } ${ routePlural } have handlers )` ) ;
19
+ super ( `${ method } ${ reqPath } (${ routesNumber } ${ routePlural } )` ) ;
16
20
17
21
this . name = this . constructor . name ;
18
22
if ( Error . captureStackTrace ) {
@@ -24,25 +28,24 @@ class UnhandledRouteError extends Error {
24
28
}
25
29
}
26
30
27
- type MockFetch = {
31
+ export interface MockFetch {
28
32
fetch : typeof globalThis . fetch ;
29
33
mock : ( route : string , handler : MatchHandler ) => void ;
30
34
remove : ( route : string ) => void ;
31
35
reset : ( ) => void ;
32
- } ;
36
+ }
33
37
34
38
/**
35
- * Create a stateful version of the global functions that do not contain
36
- * any global state.
37
- *
38
- * The returned object can be destructured.
39
- *
40
- * ```
41
- * const { fetch, mock, remove, reset } = sandbox()
42
- * ```
43
- */
39
+ * Get a set of functions that do not share any state with the globals.
40
+ *
41
+ * The returned object can be destructured.
42
+ *
43
+ * ```
44
+ * const { fetch, mock, remove, reset } = sandbox()
45
+ * ```
46
+ */
44
47
export function sandbox ( ) : MockFetch {
45
- const routeStore : Map < string , MatchHandler > = new Map ( ) ;
48
+ const routeStore = new Map < string , MatchHandler > ( ) ;
46
49
47
50
async function fetch (
48
51
input : string | Request | URL ,
@@ -99,14 +102,15 @@ export const mockedFetch = globalMockFetch.fetch;
99
102
/**
100
103
* Mock a new route, or override an existing handler.
101
104
*
102
- * The route uses path-to-regexp syntax, with the additional extension of
103
- * (optional) method routing (prefix with METHOD@, eg. `POST@/user/:id`).
105
+ * The route uses URLPattern syntax, with the additional extension of
106
+ * (optional) method routing by prefixing with the method,
107
+ * eg. `"POST@/user/:id"`.
104
108
*
105
- * The handler function can either be a function or an async function .
109
+ * The handler function may be asynchronous .
106
110
*
107
111
* ```
108
- * mock("GET@/users/:id", async (_req, match ) => {
109
- * const id = parseInt(match. params["id"]);
112
+ * mock("GET@/users/:id", async (_req, params ) => {
113
+ * const id = parseInt(params["id"]);
110
114
* const data = await magicallyGetMyUserData(id);
111
115
* return new Response(JSON.stringify(data));
112
116
* })
@@ -123,17 +127,20 @@ export const reset = globalMockFetch.reset;
123
127
// Store the original fetch so it can be restored later
124
128
const originalFetch = globalThis . fetch ;
125
129
130
+ // The functions below are `const` for consistency.
131
+
126
132
/**
127
- * Replace `window.fetch` with a mock that routes requests to a matching handler.
133
+ * Replace `globalThis.fetch` with `mockedFetch` (or another function that
134
+ * matches the `fetch` signature)
128
135
*
129
- * To reset `window .fetch`, call `uninstall()`.
136
+ * To restore the original `globalThis .fetch`, call `uninstall()`.
130
137
*/
131
- export const install = ( ) => {
132
- globalThis . fetch = mockedFetch ;
138
+ export const install = ( replacement ?: typeof fetch ) => {
139
+ globalThis . fetch = replacement ?? mockedFetch ;
133
140
} ;
134
141
135
142
/**
136
- * Restore `window .fetch` to what it was before `install()` was called .
143
+ * Restore `globalThis .fetch` to what it was before this library was imported .
137
144
*/
138
145
export const uninstall = ( ) => {
139
146
globalThis . fetch = originalFetch ;
0 commit comments