make-fetch 3.1.2 → 3.1.3
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/index.js +3 -2
- package/package.json +1 -1
- package/test.js +30 -0
package/index.js
CHANGED
|
@@ -7,7 +7,8 @@ export function makeFetch (handler, {
|
|
|
7
7
|
Response = globalThis.Response
|
|
8
8
|
} = {}) {
|
|
9
9
|
return async function fetch (...requestOptions) {
|
|
10
|
-
const
|
|
10
|
+
const isAlreadyRequest = requestOptions[0] instanceof Request
|
|
11
|
+
const request = isAlreadyRequest ? requestOptions[0] : new Request(...requestOptions)
|
|
11
12
|
|
|
12
13
|
const { body = null, ...responseOptions } = await handler(request)
|
|
13
14
|
|
|
@@ -150,7 +151,7 @@ function matches (request, route, property) {
|
|
|
150
151
|
return areEqual(route.hostname, new URL(request.url).hostname)
|
|
151
152
|
} else if (property === 'protocol') {
|
|
152
153
|
return areEqual(route.protocol, new URL(request.url).protocol)
|
|
153
|
-
} else if(property === 'method') {
|
|
154
|
+
} else if (property === 'method') {
|
|
154
155
|
return areEqual(route.method, request.method.toUpperCase())
|
|
155
156
|
} else {
|
|
156
157
|
const routeProperty = route[property]
|
package/package.json
CHANGED
package/test.js
CHANGED
|
@@ -31,6 +31,36 @@ test('Basic makeFetch test', async (t) => {
|
|
|
31
31
|
t.end()
|
|
32
32
|
})
|
|
33
33
|
|
|
34
|
+
test.only('Basic makeFetch test with Request object', async (t) => {
|
|
35
|
+
const fetch = makeFetch(({ url }) => {
|
|
36
|
+
return {
|
|
37
|
+
status: 200,
|
|
38
|
+
statusText: 'OK',
|
|
39
|
+
headers: {
|
|
40
|
+
url
|
|
41
|
+
},
|
|
42
|
+
body: intoAsyncIterable(url)
|
|
43
|
+
}
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
const toFetch = new Request('example://hostname/pathname')
|
|
47
|
+
|
|
48
|
+
const response = await fetch(toFetch)
|
|
49
|
+
|
|
50
|
+
t.ok(response.ok, 'response was OK')
|
|
51
|
+
|
|
52
|
+
const body = await response.text()
|
|
53
|
+
|
|
54
|
+
t.equal(body, toFetch.url, 'got expected response body')
|
|
55
|
+
|
|
56
|
+
t.equal(response.headers.get('url'), toFetch.url, 'got expected response headers')
|
|
57
|
+
|
|
58
|
+
t.equal(response.statusText, 'OK', 'got expected status text')
|
|
59
|
+
|
|
60
|
+
t.end()
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
|
|
34
64
|
test('Basic router tests', async (t) => {
|
|
35
65
|
const { fetch, router } = makeRoutedFetch()
|
|
36
66
|
|