@visulima/connect 1.1.0 → 1.2.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/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## @visulima/connect [1.2.0](https://github.com/visulima/visulima/compare/@visulima/connect@1.1.1...@visulima/connect@1.2.0) (2022-11-15)
2
+
3
+
4
+ ### Features
5
+
6
+ * added new packages for faster api creation ([#14](https://github.com/visulima/visulima/issues/14)) ([eb64fcf](https://github.com/visulima/visulima/commit/eb64fcf33f2a75ea48262ad6e71f80e159a93972))
7
+
8
+ ## @visulima/connect [1.1.1](https://github.com/visulima/visulima/compare/@visulima/connect@1.1.0...@visulima/connect@1.1.1) (2022-11-09)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * **connect:** added missing type to the types.d ([33f7fa8](https://github.com/visulima/visulima/commit/33f7fa8282d23e00cd3bb4aeb7691f4b615b9afc))
14
+
1
15
  ## @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
16
 
3
17
 
package/README.md CHANGED
@@ -59,7 +59,7 @@ pnpm add @visulima/connect
59
59
  >
60
60
  > Although `@visulima/connect` is initially written for Next.js, it can be used in [http server](https://nodejs.org/api/http.html#httpcreateserveroptions-requestlistener), [Vercel](https://vercel.com/docs/concepts/functions/serverless-functions). See [Examples](./examples/) for more integrations.
61
61
 
62
- Below are some use cases.
62
+ Below are use cases.
63
63
 
64
64
  ### Next.js API Routes
65
65
 
@@ -111,7 +111,7 @@ router
111
111
  }
112
112
  );
113
113
 
114
- export default router.handler();
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.handler();
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 handler has run
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", handler)` inside `page/api/index.js` _does not_ serve that handler at `/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.handler(options)
347
+ ### router.nodeHandler(options)
348
348
 
349
- Create a handler to handle incoming requests.
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 handler; executed whenever a handler throws an 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.handler();
366
+ export default router.nodeHandler();
367
367
  ```
368
368
 
369
369
  **options.onNoMatch**
370
370
 
371
- Accepts a function of `(req, res)` as a handler when no route is matched.
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.handler();
381
+ export default router.nodeHandler();
382
382
  ```
383
383
 
384
384
  ### router.run(req, res)
@@ -415,7 +415,7 @@ await router.run(req, res);
415
415
 
416
416
  ## Common errors
417
417
 
418
- There are some pitfalls in using `@visulima/connect`. Below are things to keep in mind to use it correctly.
418
+ There are pitfalls in using `@visulima/connect`. Below are things to keep in mind to use it correctly.
419
419
 
420
420
  1. **Always** `await next()`
421
421
 
@@ -460,10 +460,10 @@ router
460
460
  });
461
461
  ```
462
462
 
463
- Another issue is that the handler would resolve before all the code in each layer runs.
463
+ Another issue is that the nodeHandler would resolve before all the code in each layer runs.
464
464
 
465
465
  ```javascript
466
- const handler = router
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
- .handler();
476
+ .nodeHandler();
477
477
 
478
- await handler(req, res);
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).handler();
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).handler();
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).handler();
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).handler();
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 handler = createNodeRouter()
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
- 3. **DO NOT** use `handler()` directly in `getServerSideProps`.
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 handler = router.handler();
549
+ const nodeHandler = router.nodeHandler();
550
550
 
551
551
  export async function getServerSideProps({req, res}) {
552
- await handler(req, res); // BAD: You should call router.run(req, res);
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 handler will only run on that specific route.
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.handler();
580
+ export default router.nodeHandler();
581
581
  ```
582
582
 
583
- While this allows quick migration from Express.js, consider seperating routes into different files (`/api/user/[userId].js`, `/api/hello.js`) in the future.
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
 
@@ -603,7 +603,7 @@ router.use(expressWrapper(someExpressMiddleware));
603
603
  ## Supported Node.js Versions
604
604
 
605
605
  Libraries in this ecosystem make the best effort to track
606
- [Node.js' release schedule](https://nodejs.org/en/about/releases/). Here's [a
606
+ [Node.js release schedule](https://github.com/nodejs/release#release-schedule). Heres [a
607
607
  post on why we think this is important](https://medium.com/the-node-js-collection/maintainers-should-consider-following-node-js-release-schedule-ab08ed4de71a).
608
608
 
609
609
  ## Contributing
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@visulima/connect",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "The minimal router and middleware layer for Next.js, Micro, Vercel, or Node.js http/http2 with support for zod validation.",
5
5
  "keywords": [
6
6
  "javascript",
@@ -64,7 +64,7 @@
64
64
  "clean": "rimraf node_modules dist",
65
65
  "coverage": "vitest run --coverage",
66
66
  "dev": "pnpm predev && pnpm run build --watch",
67
- "lint:eslint": "cross-env NO_LOGS=true eslint --ext js,jsx,ts,tsx --max-warnings=0 --config .eslintrc.cjs --cache --cache-strategy content .",
67
+ "lint:eslint": "cross-env NO_LOGS=true eslint --ext js,jsx,ts,tsx --max-warnings=0 --config .eslintrc.cjs",
68
68
  "lint:eslint:fix": "pnpm run lint:eslint --fix",
69
69
  "test": "vitest"
70
70
  },
package/src/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { AnyZodObject } from "zod";
1
+ import type { AnyZodObject } from "zod";
2
2
 
3
3
  export type HttpMethod = "GET" | "HEAD" | "POST" | "PUT" | "PATCH" | "DELETE";
4
4