Skip to content

Commit edff842

Browse files
committed
chores: reorganize packages
1 parent 078ca3f commit edff842

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+5028
-3095
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@backhooks/examples": minor
3+
"@backhooks/hooks": minor
4+
"@backhooks/core": minor
5+
"@backhooks/express": minor
6+
"@backhooks/fastify": minor
7+
---
8+
9+
Reorganize packages

README.md

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,33 @@ Backhook is a type safe, plug and play and funny way of injecting dependencies a
3434

3535
It's compatible with major frameworks like ExpressJS, Fastify, and can also be run standalone in a very simple way.
3636

37-
This project allows you to choose between three packages.
37+
This project allows you to choose between two packages.
3838

3939
- `@backhooks/core`: For a minimalist dependency injection framework
40-
- `@backhooks/http`: For builtin hooks and middlewares related to http contexts
4140
- `@backhooks/hooks`: For a complete set of builtin hooks that can help you with your daily work. (WIP)
4241

4342
## Get started
4443

4544
### Install dependency
4645

4746
```
48-
npm install @backhooks/core @backhooks/http
47+
npm install @backhooks/hooks
4948
```
5049

5150
### ExpressJS usage
5251

5352
```
54-
npm install @backhooks/http
53+
npm install @backhooks/express @backhooks/hooks
5554
```
5655

5756
```ts
5857
import express from "express";
59-
import { useHeaders, hooksMiddleware } from "@backhooks/http";
58+
import middleware from "@backhooks/express";
59+
import { useHeaders } from "@backhooks/hooks";
6060

6161
const app = express();
6262

63-
app.use(hooksMiddleware());
63+
app.use(middleware());
6464

6565
app.get("/", (req, res) => {
6666
const headers = useHeaders(); // <- This is a hook
@@ -95,7 +95,7 @@ runHookContext(() => {
9595

9696
### Using the `createHook` function
9797

98-
A hook holds a `state` that lives through the entire life of a **hook context**. For the Express application, this context lives through the entire request lifecycle thanks to the `hooksMidleware`. For a standalone app, this context lives in all the functions called (even deeper function calls) from the `runHookContext` function.
98+
A hook holds a `state` that lives through the entire life of a **hook context**. For the Express application, this context lives through the entire request lifecycle thanks to the express (or other runtime) `middleware`. For a standalone app, this context lives in all the functions called (even deeper function calls) from the `runHookContext` function.
9999

100100
This is made possible thanks to the [`AsyncLocalStorage`](https://nodejs.org/dist/latest-v18.x/docs/api/async_context.html#class-asynclocalstorage) nodeJS API
101101

@@ -173,12 +173,12 @@ Hook states can be updated at runtime. If the counter has to start at 50, it can
173173
```ts
174174
import { createHook, runHookContext } from "@backhooks/core";
175175

176-
const [useCount, updateCount] = createHook({
176+
const [useCount, setCount] = createHook({
177177
...
178178
});
179179

180180
runHookContext(() => {
181-
updateCount(() => {
181+
setCount(() => {
182182
return {
183183
count: 50,
184184
};
@@ -187,7 +187,7 @@ runHookContext(() => {
187187
});
188188
```
189189

190-
This is exactly how the `hooksMiddleware` works. It runs a context for the current request, uses the state updater of the `useHeaders` hook and attach the values from the `req` express object.
190+
This is exactly how the `middleware` works. It runs a context for the current request, uses the state updater of the `useHeaders` hook and attach the values from the `req` express object.
191191

192192
The `useHeaders()` function can now magically return the values of the request headers through the entire request lifecycle.
193193

@@ -198,18 +198,17 @@ Don't hesitate to open an issue if you want to use hooks with another framework.
198198
### Fastify
199199

200200
```
201-
npm install @backhooks/http
201+
npm install @backhooks/fastify @backhooks/hooks
202202
```
203203

204204
```ts
205205
import Fastify from "fastify";
206-
import { hooksMiddleware, useHeaders } from "@backhooks/http";
206+
import hooksPreHandler from "@backhooks/fastify";
207+
import { useHeaders } from "@backhooks/hooks";
207208

208-
const fastify = Fastify({
209-
logger: true,
210-
});
209+
const fastify = Fastify();
211210

212-
fastify.addHook("preHandler", hooksMiddleware());
211+
fastify.addHook("preHandler", hooksPreHandler());
213212

214213
// Declare a route
215214
fastify.get("/", () => {
@@ -259,7 +258,7 @@ Let's try out to write a `useAuthorizationHeader` hook:
259258

260259
```ts
261260
import { createHook } from "@backhooks/core";
262-
import { useHeaders } from "@backhooks/http";
261+
import { useHeaders } from "@backhooks/hooks";
263262

264263
const [useAuthorizationHeader, updateAuthorizationHeader] = createHook({
265264
data() {
@@ -294,12 +293,12 @@ Being able to update the hook state, makes it very easy to unit test our hooks o
294293

295294
```ts
296295
import { runHookContext } from "@backhooks/core";
297-
import { configureHeadersHook } from "@backhooks/http";
296+
import { setHeaders } from "@backhooks/hooks";
298297
import { useAuthorizationHeader } from "./hooks/useAuthorizationHeader";
299298

300299
test("it should return the authorization header", () => {
301300
return runHookContext(() => {
302-
configureHeadersHook((state) => {
301+
setHeaders(() => {
303302
return {
304303
headers: {
305304
authorization: "def",
@@ -389,10 +388,7 @@ import { MyProvider } from "./MyProvider";
389388
export const [useMyProvider] = createHook({
390389
data() {
391390
return new MyProvider();
392-
},
393-
execute(state) {
394-
return state;
395-
},
391+
}
396392
});
397393
```
398394

0 commit comments

Comments
 (0)