Skip to content

Commit

Permalink
Merge branch 'main' into pr/tobiasdiez/250
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Dec 15, 2022
2 parents ad06571 + 4e52381 commit b697c77
Show file tree
Hide file tree
Showing 36 changed files with 1,799 additions and 864 deletions.
8 changes: 2 additions & 6 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
"eslint-config-unjs"
],
"rules": {
"unicorn/no-null": 0,
"unicorn/prevent-abbreviations": 0,
"no-undef": 0,
"no-use-before-define": 0,
"unicorn/no-await-expression-member": 0,
"unicorn/no-useless-undefined": 0
"unicorn/no-null": "off",
"unicorn/number-literal-case": "off"
}
}
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
37 changes: 37 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,43 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## v1.0.2

[compare changes](https://github.com/unjs/h3/compare/v1.0.1...v1.0.2)


### 🩹 Fixes

- Correct types for `readRawBody` ([#277](https://github.com/unjs/h3/pull/277))
- **readBody:** Handle top-level arrays in url-encoded data ([#278](https://github.com/unjs/h3/pull/278))

### 💅 Refactors

- Update `@deprecated` comment ([#245](https://github.com/unjs/h3/pull/245))
- **createRouter:** Deprecate misspelled `preemptive` option ([#256](https://github.com/unjs/h3/pull/256))

### 📖 Documentation

- Fix deprecated methods ([#238](https://github.com/unjs/h3/pull/238))

### 🏡 Chore

- Add section to readme for community packages ([#262](https://github.com/unjs/h3/pull/262))
- Update eslint config ([0812e81](https://github.com/unjs/h3/commit/0812e81))
- Format with prettier ([a0e21c1](https://github.com/unjs/h3/commit/a0e21c1))
- Fix type issue ([a9b3187](https://github.com/unjs/h3/commit/a9b3187))

### ✅ Tests

- Fix legacy middleware test ([408f3f2](https://github.com/unjs/h3/commit/408f3f2))

### ❤️ Contributors

- Pooya Parsa <[email protected]>
- Daniel Roe <[email protected]>
- Nozomu Ikuta <[email protected]>
- Larry Williamson <[email protected]>

### [1.0.1](https://github.com/unjs/h3/compare/v1.0.0...v1.0.1) (2022-11-15)

## [1.0.0](https://github.com/unjs/h3/compare/v0.8.6...v1.0.0) (2022-11-15)
Expand Down
73 changes: 48 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,32 @@ pnpm add h3
## Usage

```ts
import { createServer } from 'http'
import { createApp, eventHandler, toNodeListener } from 'h3'
import { createServer } from "node:http";
import { createApp, eventHandler, toNodeListener } from "h3";

const app = createApp()
app.use('/', eventHandler(() => 'Hello world!'))
const app = createApp();
app.use(
"/",
eventHandler(() => "Hello world!")
);

createServer(toNodeListener(app)).listen(process.env.PORT || 3000)
createServer(toNodeListener(app)).listen(process.env.PORT || 3000);
```

<details>
<summary>Example using <a href="https://github.com/unjs/listhen">listhen</a> for an elegant listener.</summary>
Example using <a href="https://github.com/unjs/listhen">listhen</a> for an elegant listener:

```ts
import { createApp, toNodeListener } from 'h3'
import { listen } from 'listhen'
import { createApp, eventHandler, toNodeListener } from "h3";
import { listen } from "listhen";

const app = createApp()
app.use('/', eventHandler(() => 'Hello world!'))
const app = createApp();
app.use(
"/",
eventHandler(() => "Hello world!")
);

listen(toNodeListener(app))
listen(toNodeListener(app));
```
</details>

## Router

Expand All @@ -69,15 +73,21 @@ The `app` instance created by `h3` uses a middleware stack (see [how it works](#
To opt-in using a more advanced and convenient routing system, we can create a router instance and register it to app instance.

```ts
import { createApp, eventHandler, createRouter } from 'h3'
import { createApp, eventHandler, createRouter } from "h3";

const app = createApp()
const app = createApp();

const router = createRouter()
.get('/', eventHandler(() => 'Hello World!'))
.get('/hello/:name', eventHandler(event => `Hello ${event.context.params.name}!`))

app.use(router)
.get(
"/",
eventHandler(() => "Hello World!")
)
.get(
"/hello/:name",
eventHandler((event) => `Hello ${event.context.params.name}!`)
);

app.use(router);
```

**Tip:** We can register same route more than once with different methods.
Expand Down Expand Up @@ -139,17 +149,30 @@ H3 has concept of compasable utilities that accept `event` (from `eventHandler((
- `sendProxy(event, { target, headers?, fetchOptions?, fetch?, sendStream? })`
- `proxyRequest(event, { target, headers?, fetchOptions?, fetch?, sendStream? })`
- `sendNoContent(event, code = 204)`
- `setResponseStatus(event, status)`
- `getResponseStatus(event)`
- `getResponseStatusText(event)`
- `readMultipartFormData(event)`
👉 You can learn more about usage in [JSDocs Documentation](https://www.jsdocs.io/package/h3#package-functions).
### Add-ons
## Community Packages
You can use more h3 event utilities made by the community.
Please check their READMEs for more details.
More composable utilities can be found in community packages.
PRs are welcome to add your packages.
- `validateBody(event, schema)` from [h3-typebox](https://github.com/kevinmarrec/h3-typebox)
- `validateQuery(event, schema)` from [h3-typebox](https://github.com/kevinmarrec/h3-typebox)
- `useValidatedBody(event, schema)` from [h3-zod](https://github.com/wobsoriano/h3-zod)
- `useValidatedQuery(event, schema)` from [h3-zod](https://github.com/wobsoriano/h3-zod)
- [h3-cors](https://github.com/NozomuIkuta/h3-cors)
- `defineCorsEventHandler(options)`
- `isPreflight(event)`
- [h3-typebox](https://github.com/kevinmarrec/h3-typebox)
- `validateBody(event, schema)`
- `validateQuery(event, schema)`
- [h3-zod](https://github.com/wobsoriano/h3-zod)
- `useValidatedBody(event, schema)`
- `useValidatedQuery(event, schema)`
## License
Expand Down
31 changes: 16 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "h3",
"version": "1.0.1",
"version": "1.0.2",
"description": "Tiny JavaScript Server",
"repository": "unjs/h3",
"license": "MIT",
Expand All @@ -22,38 +22,39 @@
"scripts": {
"build": "unbuild",
"dev": "vitest",
"lint": "eslint --ext ts,mjs,cjs .",
"lint": "eslint --ext ts,mjs,cjs . && prettier -c src test playground",
"play": "jiti ./playground/index.ts",
"profile": "0x -o -D .profile -P 'autocannon -c 100 -p 10 -d 40 http://localhost:$PORT' ./playground/server.cjs",
"release": "pnpm test && pnpm build && changelogen --release && pnpm publish && git push --follow-tags",
"test": "pnpm lint && vitest run --coverage"
},
"dependencies": {
"cookie-es": "^0.5.0",
"destr": "^1.2.1",
"destr": "^1.2.2",
"radix3": "^1.0.0",
"ufo": "^1.0.0"
"ufo": "^1.0.1"
},
"devDependencies": {
"0x": "^5.4.1",
"@types/express": "^4.17.14",
"@types/node": "^18.11.9",
"@types/express": "^4.17.15",
"@types/node": "^18.11.15",
"@types/supertest": "^2.0.12",
"@vitest/coverage-c8": "^0.25.3",
"@vitest/coverage-c8": "^0.25.8",
"autocannon": "^7.10.0",
"changelogen": "^0.4.0",
"connect": "^3.7.0",
"eslint": "^8.28.0",
"eslint-config-unjs": "^0.0.2",
"eslint": "^8.29.0",
"eslint-config-unjs": "^0.0.3",
"express": "^4.18.2",
"get-port": "^6.1.2",
"jiti": "^1.16.0",
"listhen": "^1.0.0",
"listhen": "^1.0.1",
"node-fetch-native": "^1.0.1",
"supertest": "^6.3.1",
"typescript": "^4.9.3",
"unbuild": "^1.0.1",
"vitest": "^0.25.3"
"prettier": "^2.8.1",
"supertest": "^6.3.3",
"typescript": "^4.9.4",
"unbuild": "^1.0.2",
"vitest": "^0.25.8"
},
"packageManager": "pnpm@7.17.0"
"packageManager": "pnpm@7.18.2"
}
41 changes: 31 additions & 10 deletions playground/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
import { listen } from "listhen";
import { fetch } from "node-fetch-native";
import { createApp, createRouter, eventHandler, toNodeListener, parseCookies, createError, proxyRequest } from "../src";
import {
createApp,
createRouter,
eventHandler,
toNodeListener,
parseCookies,
createError,
proxyRequest,
} from "../src";

const app = createApp({ debug: true });
const router = createRouter()
.get("/", eventHandler(event => proxyRequest(event, "http://icanhazip.com", {
fetch
})))
.get("/error/:code", eventHandler((event) => {
throw createError({ statusCode: Number.parseInt(event.context.params.code) });
}))
.get("/hello/:name", eventHandler((event) => {
return `Hello ${parseCookies(event)}!`;
}));
.get(
"/",
eventHandler((event) =>
proxyRequest(event, "http://icanhazip.com", {
fetch,
})
)
)
.get(
"/error/:code",
eventHandler((event) => {
throw createError({
statusCode: Number.parseInt(event.context.params.code),
});
})
)
.get(
"/hello/:name",
eventHandler((event) => {
return `Hello ${parseCookies(event)}!`;
})
);

app.use(router);

Expand Down
Loading

0 comments on commit b697c77

Please sign in to comment.