h3 0.3.2 → 0.3.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/README.md CHANGED
@@ -1,24 +1,25 @@
1
- [![d](https://img.shields.io/npm/dm/h3.svg?style=flat-square)](https://npmjs.com/package/h3)
2
- [![v](https://img.shields.io/npm/v/h3/latest.svg?style=flat-square)](https://npmjs.com/package/h3)
3
- [![b](https://img.shields.io/bundlephobia/min/h3/latest.svg?style=flat-square)](https://bundlephobia.com/result?p=h3)
4
- [![a](https://img.shields.io/github/workflow/status/unjs/h3/ci/main?style=flat-square)](https://github.com/unjs/h3/actions)
5
- [![c](https://img.shields.io/codecov/c/gh/unjs/h3/main?style=flat-square)](https://codecov.io/gh/unjs/h3)
1
+ [![npm downloads](https://img.shields.io/npm/dm/h3.svg?style=flat-square)](https://npmjs.com/package/h3)
2
+ [![version](https://img.shields.io/npm/v/h3/latest.svg?style=flat-square)](https://npmjs.com/package/h3)
3
+ [![bundlephobia](https://img.shields.io/bundlephobia/min/h3/latest.svg?style=flat-square)](https://bundlephobia.com/result?p=h3)
4
+ [![build status](https://img.shields.io/github/workflow/status/unjs/h3/ci/main?style=flat-square)](https://github.com/unjs/h3/actions)
5
+ [![coverage](https://img.shields.io/codecov/c/gh/unjs/h3/main?style=flat-square)](https://codecov.io/gh/unjs/h3)
6
+ [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue?style=flat-square)](https://www.jsdocs.io/package/h3)
6
7
 
7
8
  > H3 is a minimal h(ttp) framework built for high performance and portability
8
9
 
9
10
  <!-- ![h3 - Tiny JavaScript Server](.github/banner.svg) -->
10
11
 
11
- **Features**
12
+ ## Features
12
13
 
13
- ✔️ **Portable:** Works perfectly in Serverless, Workers, and Node.js
14
+ ✔️ &nbsp;**Portable:** Works perfectly in Serverless, Workers, and Node.js
14
15
 
15
- ✔️ **Compatible:** Support connect/express middleware
16
+ ✔️ &nbsp;**Compatible:** Support connect/express middleware
16
17
 
17
- ✔️ **Minimal:** Small, tree-shakable and zero-dependency
18
+ ✔️ &nbsp;**Minimal:** Small, tree-shakable and zero-dependency
18
19
 
19
- ✔️ **Modern:** Native promise support
20
+ ✔️ &nbsp;**Modern:** Native promise support
20
21
 
21
- ✔️**Extendable:** Ships with a set of composable utilities but can be extended
22
+ ✔️ &nbsp;**Extendable:** Ships with a set of composable utilities but can be extended
22
23
 
23
24
  ## Install
24
25
 
@@ -42,7 +43,19 @@ app.use('/', () => 'Hello world!')
42
43
  createServer(app).listen(process.env.PORT || 3000)
43
44
  ```
44
45
 
45
- **Tip:** you may try [listhen](https://github.com/unjs/listhen) for a more elegant and advanced listener.
46
+ <details>
47
+ <summary>Example using <a href="https://github.com/unjs/listhen">listhen</a> for an elegant listener.</summary>
48
+
49
+ ```ts
50
+ import { createApp } from 'h3'
51
+ import { listen } from 'listhen'
52
+
53
+ const app = createApp()
54
+ app.use('/', () => 'Hello world!')
55
+
56
+ listen(app)
57
+ ```
58
+ </details>
46
59
 
47
60
  ## Examples
48
61
 
@@ -86,6 +99,8 @@ Instead of adding helpers to `req` and `res`, h3 exposes them as composable util
86
99
  - `createError({ statusCode, statusMessage, data? }`
87
100
  - `sendError(res, error, debug?)`
88
101
 
102
+ 👉 You can learn more about usage in [JSDocs Documentation](https://www.jsdocs.io/package/h3#package-functions).
103
+
89
104
  ## How it works?
90
105
 
91
106
  Using `createApp`, it returns a standard `(req, res)` handler function and internally an array called middleware stack. using`use()` method we can add an item to this internal stack.
package/dist/index.d.ts CHANGED
@@ -84,20 +84,22 @@ declare function sendError(res: ServerResponse, error: Error | H3Error, debug?:
84
84
 
85
85
  /**
86
86
  * Reads body of the request and returns encoded raw string (default) or `Buffer` if encoding if falsy.
87
- * @param req {IncomingMessage} An IncomingMessage object is created by
88
- * <a href="https://nodejs.org/api/http.html#http_class_http_server">http.Server</a>
87
+ * @param req {IncomingMessage} An IncomingMessage object is created by [http.Server](https://nodejs.org/api/http.html#http_class_http_server)
89
88
  * @param encoding {Encoding} encoding="utf-8" - The character encoding to use.
90
89
  *
91
90
  * @return {String|Buffer} Encoded raw string or raw Buffer of the body
92
91
  */
93
92
  declare function useRawBody(req: IncomingMessage, encoding?: Encoding): Encoding extends false ? Buffer : Promise<string>;
94
93
  /**
95
- * Reads request body and try to safely parse using {@link https://github.com/unjs/destr destr}
96
- * @param req {IncomingMessage} An IncomingMessage object is created by
97
- * <a href="https://nodejs.org/api/http.html#http_class_http_server">http.Server</a>
94
+ * Reads request body and try to safely parse using [destr](https://github.com/unjs/destr)
95
+ * @param req {IncomingMessage} An IncomingMessage object created by [http.Server](https://nodejs.org/api/http.html#http_class_http_server)
98
96
  * @param encoding {Encoding} encoding="utf-8" - The character encoding to use.
99
97
  *
100
- * @return {*} The Object, Array, string, number, boolean, or null value corresponding to the request JSON body
98
+ * @return {*} The `Object`, `Array`, `String`, `Number`, `Boolean`, or `null` value corresponding to the request JSON body
99
+ *
100
+ * ```ts
101
+ * const body = await useBody(req)
102
+ * ```
101
103
  */
102
104
  declare function useBody<T = any>(req: IncomingMessage): Promise<T>;
103
105
 
@@ -192,9 +194,36 @@ interface CookieSerializeOptions {
192
194
  secure?: boolean;
193
195
  }
194
196
 
197
+ /**
198
+ * Parse the request to get HTTP Cookie header string and returning an object of all cookie name-value pairs.
199
+ * @param req {IncomingMessage} An IncomingMessage object created by [http.Server](https://nodejs.org/api/http.html#http_class_http_server)
200
+ * @returns Object of cookie name-value pairs
201
+ * ```ts
202
+ * const cookies = useCookies(req)
203
+ * ```
204
+ */
195
205
  declare function useCookies(req: IncomingMessage): Record<string, string>;
206
+ /**
207
+ * Get a cookie value by name.
208
+ * @param req {IncomingMessage} An IncomingMessage object created by [http.Server](https://nodejs.org/api/http.html#http_class_http_server)
209
+ * @param name Name of the cookie to get
210
+ * @returns {*} Value of the cookie (String or undefined)
211
+ * ```ts
212
+ * const authorization = useCookie(request, 'Authorization')
213
+ * ```
214
+ */
196
215
  declare function useCookie(req: IncomingMessage, name: string): string | undefined;
197
- declare function setCookie(res: ServerResponse, name: string, value: string, serializeOptions: CookieSerializeOptions): void;
216
+ /**
217
+ * Set a cookie value by name.
218
+ * @param res {ServerResponse} A ServerResponse object created by [http.Server](https://nodejs.org/api/http.html#http_class_http_server)
219
+ * @param name Name of the cookie to set
220
+ * @param value Value of the cookie to set
221
+ * @param serializeOptions {CookieSerializeOptions} Options for serializing the cookie
222
+ * ```ts
223
+ * setCookie(res, 'Authorization', '1234567')
224
+ * ```
225
+ */
226
+ declare function setCookie(res: ServerResponse, name: string, value: string, serializeOptions?: CookieSerializeOptions): void;
198
227
 
199
228
  declare function useQuery(req: IncomingMessage): ufo.QueryObject;
200
229
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "h3",
3
- "version": "0.3.2",
3
+ "version": "0.3.3",
4
4
  "description": "Tiny JavaScript Server",
5
5
  "repository": "unjs/h3",
6
6
  "license": "MIT",