make-fetch 3.0.0 → 3.1.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.
package/LICENSE CHANGED
File without changes
package/README.md CHANGED
@@ -73,12 +73,14 @@ Instead of having a separate parameter for the body and the response options, th
73
73
 
74
74
  This will then return a standard [fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch) API which takes request info, and returns responses.
75
75
 
76
- `makeRoutedFetch({onNotFound}) => {router: Router, fetch}`
76
+ `makeRoutedFetch({onNotFound, onError}) => {router: Router, fetch}`
77
77
 
78
78
  If you want to have an easier way of routing different methods/hostnames/paths, you can use a routed make-fetch which can make it easier to register handlers for different routes.
79
79
  This will creat a Router, and a `fetch()` instance for that router.
80
80
  Handlers you add on the router will be useful to match URLs+methods from the fetch request and will use the matched handler to generate the response.
81
81
  You can optionally supply a `onNotFound` handler to be invoked if no other routes match.
82
+ You can optionally supply a `onError` handler to be invoked when an error is thrown from your handlers which will take the `Error` instance and the `Request` instance as arguments.
83
+ The default `onError` handler will print the stack trace to the body with a `500` status code.
82
84
 
83
85
  `router.add(method, urlPattern, handler) => Router`
84
86
 
package/index.js CHANGED
@@ -18,7 +18,8 @@ export function makeFetch (handler, {
18
18
  }
19
19
 
20
20
  export function makeRoutedFetch ({
21
- onNotFound = DEFAULT_NOT_FOUND
21
+ onNotFound = DEFAULT_NOT_FOUND,
22
+ onError = DEFAULT_ON_ERROR
22
23
  } = {}) {
23
24
  const router = new Router()
24
25
 
@@ -27,7 +28,11 @@ export function makeRoutedFetch ({
27
28
  if (!route) {
28
29
  return onNotFound(request)
29
30
  }
30
- return route.handler(request)
31
+ try {
32
+ return route.handler(request)
33
+ } catch (e) {
34
+ return await onError(e, request)
35
+ }
31
36
  })
32
37
 
33
38
  return { fetch, router }
@@ -37,6 +42,16 @@ export function DEFAULT_NOT_FOUND () {
37
42
  return { status: 404, statusText: 'Invalid URL' }
38
43
  }
39
44
 
45
+ export function DEFAULT_ON_ERROR (e) {
46
+ return {
47
+ status: 500,
48
+ headers: {
49
+ 'Content-Type': 'text/plain; charset=utf-8'
50
+ },
51
+ body: e.stack
52
+ }
53
+ }
54
+
40
55
  export class Router {
41
56
  constructor () {
42
57
  this.routes = []
@@ -116,7 +131,7 @@ function matches (request, route, property) {
116
131
 
117
132
  const routeSegment = routeSegments[i]
118
133
  const requestSegment = requestSegments[i]
119
- const routeWild = routeSegments === WILDCARD
134
+ const routeWild = routeSegment === WILDCARD
120
135
  const matches = routeWild || (routeSegment === requestSegment)
121
136
 
122
137
  if (routeLast) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "make-fetch",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "Implement your own `fetch()` with node.js streams",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/test.js CHANGED
File without changes