@visulima/connect 1.1.0 → 1.1.1
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/CHANGELOG.md +7 -0
- package/README.md +25 -25
- package/package.json +1 -1
- package/src/types.d.ts +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
## @visulima/connect [1.1.1](https://github.com/visulima/visulima/compare/@visulima/connect@1.1.0...@visulima/connect@1.1.1) (2022-11-09)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* **connect:** added missing type to the types.d ([33f7fa8](https://github.com/visulima/visulima/commit/33f7fa8282d23e00cd3bb4aeb7691f4b615b9afc))
|
|
7
|
+
|
|
1
8
|
## @visulima/connect [1.1.0](https://github.com/visulima/visulima/compare/@visulima/connect@1.0.1...@visulima/connect@1.1.0) (2022-11-07)
|
|
2
9
|
|
|
3
10
|
|
package/README.md
CHANGED
|
@@ -111,7 +111,7 @@ router
|
|
|
111
111
|
}
|
|
112
112
|
);
|
|
113
113
|
|
|
114
|
-
export default router.
|
|
114
|
+
export default router.nodeHandler();
|
|
115
115
|
```
|
|
116
116
|
|
|
117
117
|
### Next.js getServerSideProps
|
|
@@ -220,7 +220,7 @@ router
|
|
|
220
220
|
});
|
|
221
221
|
});
|
|
222
222
|
|
|
223
|
-
export default router.
|
|
223
|
+
export default router.nodeHandler();
|
|
224
224
|
```
|
|
225
225
|
|
|
226
226
|
### Next.js Middleware
|
|
@@ -285,7 +285,7 @@ Create an instance Node.js router.
|
|
|
285
285
|
router1.use(async (req, res, next) => {
|
|
286
286
|
req.hello = "world";
|
|
287
287
|
await next(); // call to proceed to the next in chain
|
|
288
|
-
console.log("request is done"); // call after all downstream
|
|
288
|
+
console.log("request is done"); // call after all downstream nodeHandler has run
|
|
289
289
|
});
|
|
290
290
|
|
|
291
291
|
// Or include a base
|
|
@@ -338,19 +338,19 @@ router.get((req, res, next) => {
|
|
|
338
338
|
```
|
|
339
339
|
|
|
340
340
|
> **Note**
|
|
341
|
-
> You should understand Next.js [file-system based routing](https://nextjs.org/docs/routing/introduction). For example, having a `router.put("/api/foo",
|
|
341
|
+
> You should understand Next.js [file-system based routing](https://nextjs.org/docs/routing/introduction). For example, having a `router.put("/api/foo", nodeHandler)` inside `page/api/index.js` _does not_ serve that nodeHandler at `/api/foo`.
|
|
342
342
|
|
|
343
343
|
### router.all(pattern, ...fns)
|
|
344
344
|
|
|
345
345
|
Same as [.METHOD](#methodpattern-fns) but accepts _any_ methods.
|
|
346
346
|
|
|
347
|
-
### router.
|
|
347
|
+
### router.nodeHandler(options)
|
|
348
348
|
|
|
349
|
-
Create a
|
|
349
|
+
Create a nodeHandler to handle incoming requests.
|
|
350
350
|
|
|
351
351
|
**options.onError**
|
|
352
352
|
|
|
353
|
-
Accepts a function as a catch-all error
|
|
353
|
+
Accepts a function as a catch-all error nodeHandler; executed whenever a nodeHandler throws an error.
|
|
354
354
|
By default, it responds with a generic `500 Internal Server Error` while logging the error to `console`.
|
|
355
355
|
|
|
356
356
|
```javascript
|
|
@@ -363,12 +363,12 @@ function onError(err, req, res) {
|
|
|
363
363
|
|
|
364
364
|
const router = createNodeRouter({onError});
|
|
365
365
|
|
|
366
|
-
export default router.
|
|
366
|
+
export default router.nodeHandler();
|
|
367
367
|
```
|
|
368
368
|
|
|
369
369
|
**options.onNoMatch**
|
|
370
370
|
|
|
371
|
-
Accepts a function of `(req, res)` as a
|
|
371
|
+
Accepts a function of `(req, res)` as a nodeHandler when no route is matched.
|
|
372
372
|
By default, it responds with a `404` status and a `Route [Method] [Url] not found` body.
|
|
373
373
|
|
|
374
374
|
```javascript
|
|
@@ -378,7 +378,7 @@ function onNoMatch(req, res) {
|
|
|
378
378
|
|
|
379
379
|
const router = createNodeRouter({onNoMatch});
|
|
380
380
|
|
|
381
|
-
export default router.
|
|
381
|
+
export default router.nodeHandler();
|
|
382
382
|
```
|
|
383
383
|
|
|
384
384
|
### router.run(req, res)
|
|
@@ -460,10 +460,10 @@ router
|
|
|
460
460
|
});
|
|
461
461
|
```
|
|
462
462
|
|
|
463
|
-
Another issue is that the
|
|
463
|
+
Another issue is that the nodeHandler would resolve before all the code in each layer runs.
|
|
464
464
|
|
|
465
465
|
```javascript
|
|
466
|
-
const
|
|
466
|
+
const nodeHandler = router
|
|
467
467
|
.use(async (req, res, next) => {
|
|
468
468
|
next(); // this is not returned or await
|
|
469
469
|
})
|
|
@@ -473,9 +473,9 @@ const handler = router
|
|
|
473
473
|
res.send("ok");
|
|
474
474
|
console.log("request is completed");
|
|
475
475
|
})
|
|
476
|
-
.
|
|
476
|
+
.nodeHandler();
|
|
477
477
|
|
|
478
|
-
await
|
|
478
|
+
await nodeHandler(req, res);
|
|
479
479
|
console.log("finally"); // this will run before the get layer gets to finish
|
|
480
480
|
|
|
481
481
|
// This will result in:
|
|
@@ -492,12 +492,12 @@ export default createNodeRouter().use(a).use(b);
|
|
|
492
492
|
// api/foo.js
|
|
493
493
|
import router from "api-libs/base";
|
|
494
494
|
|
|
495
|
-
export default router.get(x).
|
|
495
|
+
export default router.get(x).nodeHandler();
|
|
496
496
|
|
|
497
497
|
// api/bar.js
|
|
498
498
|
import router from "api-libs/base";
|
|
499
499
|
|
|
500
|
-
export default router.get(y).
|
|
500
|
+
export default router.get(y).nodeHandler();
|
|
501
501
|
```
|
|
502
502
|
|
|
503
503
|
This is because, in each API Route, the same router instance is mutated, leading to undefined behaviors.
|
|
@@ -510,19 +510,19 @@ export default createNodeRouter().use(a).use(b);
|
|
|
510
510
|
// api/foo.js
|
|
511
511
|
import router from "api-libs/base";
|
|
512
512
|
|
|
513
|
-
export default router.clone().get(x).
|
|
513
|
+
export default router.clone().get(x).nodeHandler();
|
|
514
514
|
|
|
515
515
|
// api/bar.js
|
|
516
516
|
import router from "api-libs/base";
|
|
517
517
|
|
|
518
|
-
export default router.clone().get(y).
|
|
518
|
+
export default router.clone().get(y).nodeHandler();
|
|
519
519
|
```
|
|
520
520
|
|
|
521
521
|
3. **DO NOT** use response function like `res.(s)end` or `res.redirect` inside `getServerSideProps`.
|
|
522
522
|
|
|
523
523
|
```javascript
|
|
524
524
|
// page/index.js
|
|
525
|
-
const
|
|
525
|
+
const nodeHandler = createNodeRouter()
|
|
526
526
|
.use((req, res) => {
|
|
527
527
|
// BAD: res.redirect is not a function (not defined in `getServerSideProps`)
|
|
528
528
|
// See https://github.com/hoangvvo/@visulima/connect/issues/194#issuecomment-1172961741 for a solution
|
|
@@ -541,15 +541,15 @@ export async function getServerSideProps({req, res}) {
|
|
|
541
541
|
}
|
|
542
542
|
```
|
|
543
543
|
|
|
544
|
-
|
|
544
|
+
4. **DO NOT** use `nodeHandler()` directly in `getServerSideProps`.
|
|
545
545
|
|
|
546
546
|
```javascript
|
|
547
547
|
// page/index.js
|
|
548
548
|
const router = createNodeRouter().use(foo).use(bar);
|
|
549
|
-
const
|
|
549
|
+
const nodeHandler = router.nodeHandler();
|
|
550
550
|
|
|
551
551
|
export async function getServerSideProps({req, res}) {
|
|
552
|
-
await
|
|
552
|
+
await nodeHandler(req, res); // BAD: You should call router.run(req, res);
|
|
553
553
|
return {
|
|
554
554
|
props: {},
|
|
555
555
|
};
|
|
@@ -563,7 +563,7 @@ export async function getServerSideProps({req, res}) {
|
|
|
563
563
|
<details>
|
|
564
564
|
<summary>Match multiple routes</summary>
|
|
565
565
|
|
|
566
|
-
If you created the file `/api/<specific route>.js` folder, the
|
|
566
|
+
If you created the file `/api/<specific route>.js` folder, the nodeHandler will only run on that specific route.
|
|
567
567
|
|
|
568
568
|
If you need to create all handlers for all routes in one file (similar to `Express.js`). You can use [Optional catch-all API routes](https://nextjs.org/docs/api-routes/dynamic-api-routes#optional-catch-all-api-routes).
|
|
569
569
|
|
|
@@ -577,10 +577,10 @@ const router = createNodeRouter()
|
|
|
577
577
|
res.send(`Hello ${req.params.userId}`);
|
|
578
578
|
});
|
|
579
579
|
|
|
580
|
-
export default router.
|
|
580
|
+
export default router.nodeHandler();
|
|
581
581
|
```
|
|
582
582
|
|
|
583
|
-
While this allows quick migration from Express.js, consider
|
|
583
|
+
While this allows quick migration from Express.js, consider separating routes into different files (`/api/user/[userId].js`, `/api/hello.js`) in the future.
|
|
584
584
|
|
|
585
585
|
</details>
|
|
586
586
|
|
package/package.json
CHANGED
package/src/types.d.ts
CHANGED