h3 1.0.0 → 1.0.2

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Pooya Parsa <pooya@pi0.io>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,38 +1,164 @@
1
- # h3-js
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)
2
7
 
3
- A better HTMLElement constructor
8
+ > H3 is a minimal h(ttp) framework built for high performance and portability
4
9
 
5
- ## Example
10
+ <!-- ![h3 - Tiny JavaScript Server](.github/banner.svg) -->
6
11
 
7
- ```javascript
8
- var body = h('div', 'example',
9
- h('h1', null, 'Example'),
10
- h('label', {htmlFor: 'task'},
11
- h('input', {id: 'task', type: 'checkbox', checked: true}),
12
- 'Check out h3-js'
13
- )
14
- );
12
+ ## Features
13
+
14
+ ✔️ &nbsp;**Portable:** Works perfectly in Serverless, Workers, and Node.js
15
+
16
+ ✔️ &nbsp;**Minimal:** Small and tree-shakable
17
+
18
+ ✔️ &nbsp;**Modern:** Native promise support
19
+
20
+ ✔️ &nbsp;**Extendable:** Ships with a set of composable utilities but can be extended
21
+
22
+ ✔️ &nbsp;**Router:** Super fast route matching using [unjs/radix3](https://github.com/unjs/radix3)
23
+
24
+ ✔️ &nbsp;**Compatible:** Compatibility layer with node/connect/express middleware
25
+
26
+ ## Install
27
+
28
+ ```bash
29
+ # Using npm
30
+ npm install h3
31
+
32
+ # Using yarn
33
+ yarn add h3
34
+
35
+ # Using pnpm
36
+ pnpm add h3
15
37
  ```
16
38
 
17
- ## API
39
+ ## Usage
40
+
41
+ ```ts
42
+ import { createServer } from 'http'
43
+ import { createApp, eventHandler, toNodeListener } from 'h3'
44
+
45
+ const app = createApp()
46
+ app.use('/', eventHandler(() => 'Hello world!'))
47
+
48
+ createServer(toNodeListener(app)).listen(process.env.PORT || 3000)
49
+ ```
50
+
51
+ <details>
52
+ <summary>Example using <a href="https://github.com/unjs/listhen">listhen</a> for an elegant listener.</summary>
53
+
54
+ ```ts
55
+ import { createApp, toNodeListener } from 'h3'
56
+ import { listen } from 'listhen'
57
+
58
+ const app = createApp()
59
+ app.use('/', eventHandler(() => 'Hello world!'))
60
+
61
+ listen(toNodeListener(app))
62
+ ```
63
+ </details>
64
+
65
+ ## Router
66
+
67
+ The `app` instance created by `h3` uses a middleware stack (see [how it works](#how-it-works)) with the ability to match route prefix and apply matched middleware.
68
+
69
+ To opt-in using a more advanced and convenient routing system, we can create a router instance and register it to app instance.
70
+
71
+ ```ts
72
+ import { createApp, eventHandler, createRouter } from 'h3'
73
+
74
+ const app = createApp()
75
+
76
+ const router = createRouter()
77
+ .get('/', eventHandler(() => 'Hello World!'))
78
+ .get('/hello/:name', eventHandler(event => `Hello ${event.context.params.name}!`))
79
+
80
+ app.use(router)
81
+ ```
82
+
83
+ **Tip:** We can register same route more than once with different methods.
84
+
85
+ Routes are internally stored in a [Radix Tree](https://en.wikipedia.org/wiki/Radix_tree) and matched using [unjs/radix3](https://github.com/unjs/radix3).
86
+
87
+ ## More app usage examples
88
+
89
+ ```js
90
+ // Handle can directly return object or Promise<object> for JSON response
91
+ app.use('/api', eventHandler((event) => ({ url: event.node.req.url }))
92
+
93
+ // We can have better matching other than quick prefix match
94
+ app.use('/odd', eventHandler(() => 'Is odd!'), { match: url => url.substr(1) % 2 })
95
+
96
+ // Handle can directly return string for HTML response
97
+ app.use(eventHandler(() => '<h1>Hello world!</h1>'))
98
+
99
+ // We can chain calls to .use()
100
+ app.use('/1', eventHandler(() => '<h1>Hello world!</h1>'))
101
+ .use('/2', eventHandler(() => '<h1>Goodbye!</h1>'))
102
+
103
+ // Legacy middleware with 3rd argument are automatically promisified
104
+ app.use(fromNodeMiddleware((req, res, next) => { req.setHeader('x-foo', 'bar'); next() }))
105
+
106
+ // Lazy loaded routes using { lazy: true }
107
+ app.use('/big', () => import('./big-handler'), { lazy: true })
108
+ ```
109
+
110
+ ## Utilities
111
+
112
+ H3 has concept of compasable utilities that accept `event` (from `eventHandler((event) => {})`) as their first argument. This has several performance benefits over injecting them to `event` or `app` instances and global middleware commonly used in Node.js frameworks such as Express, which Only required code is evaluated and bundled and rest of utils can be tree-shaken when not used.
113
+
114
+ ### Built-in
115
+
116
+ - `readRawBody(event, encoding?)`
117
+ - `readBody(event)`
118
+ - `parseCookies(event)`
119
+ - `getCookie(event, name)`
120
+ - `setCookie(event, name, value, opts?)`
121
+ - `deleteCookie(event, name, opts?)`
122
+ - `getQuery(event)`
123
+ - `getRouterParams(event)`
124
+ - `send(event, data, type?)`
125
+ - `sendRedirect(event, location, code=302)`
126
+ - `getRequestHeaders(event, headers)` (alias: `getHeaders`)
127
+ - `getRequestHeader(event, name)` (alias: `getHeader`)
128
+ - `setResponseHeaders(event, headers)` (alias: `setHeaders`)
129
+ - `setResponseHeader(event, name, value)` (alias: `setHeader`)
130
+ - `appendResponseHeaders(event, headers)` (alias: `appendHeaders`)
131
+ - `appendResponseHeader(event, name, value)` (alias: `appendHeader`)
132
+ - `writeEarlyHints(event, links, callback)`
133
+ - `sendStream(event, data)`
134
+ - `sendError(event, error, debug?)`
135
+ - `getMethod(event, default?)`
136
+ - `isMethod(event, expected, allowHead?)`
137
+ - `assertMethod(event, expected, allowHead?)`
138
+ - `createError({ statusCode, statusMessage, data? })`
139
+ - `sendProxy(event, { target, headers?, fetchOptions?, fetch?, sendStream? })`
140
+ - `proxyRequest(event, { target, headers?, fetchOptions?, fetch?, sendStream? })`
18
141
 
19
- ### h(tagName, props, ...children)
142
+ 👉 You can learn more about usage in [JSDocs Documentation](https://www.jsdocs.io/package/h3#package-functions).
20
143
 
21
- Creates and returns a new HTMLElement.
144
+ ## Community Packages
22
145
 
23
- Primary interface: `tagName` sets the tag name, each key-value pair of `props` is copied onto the result, and each of `children` is appended to the result.
146
+ You can use more h3 event utilities made by the community.
24
147
 
25
- Conveniences:
148
+ Please check their READMEs for more details.
26
149
 
27
- - `props.style` sets `result.style.cssText`
28
- - If `props` is falsy then it is ignored
29
- - If `props` is a string then it instead sets `result.className`
30
- - Children that are falsy are ignored
31
- - Children that are strings are converted to `Text` instances
32
- - Children that are Arrays are flattened into `children`
150
+ PRs are welcome to add your packages.
33
151
 
34
- ## Setup
152
+ - [h3-cors](https://github.com/NozomuIkuta/h3-cors)
153
+ - `defineCorsEventHandler(options)`
154
+ - `isPreflight(event)`
155
+ - [h3-typebox](https://github.com/kevinmarrec/h3-typebox)
156
+ - `validateBody(event, schema)`
157
+ - `validateQuery(event, schema)`
158
+ - [h3-zod](https://github.com/wobsoriano/h3-zod)
159
+ - `useValidatedBody(event, schema)`
160
+ - `useValidatedQuery(event, schema)`
35
161
 
36
- Install: `npm install h3`
162
+ ## License
37
163
 
38
- Import: `const h = require('h3');`
164
+ MIT