make-fetch 3.1.1 → 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.
Files changed (3) hide show
  1. package/index.js +4 -1
  2. package/package.json +1 -1
  3. 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 request = new Request(...requestOptions)
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,6 +151,8 @@ 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)
154
+ } else if (property === 'method') {
155
+ return areEqual(route.method, request.method.toUpperCase())
153
156
  } else {
154
157
  const routeProperty = route[property]
155
158
  const requestProperty = request[property]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "make-fetch",
3
- "version": "3.1.1",
3
+ "version": "3.1.3",
4
4
  "description": "Implement your own `fetch()` with node.js streams",
5
5
  "main": "index.js",
6
6
  "type": "module",
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