File tree Expand file tree Collapse file tree 8 files changed +75
-29
lines changed Expand file tree Collapse file tree 8 files changed +75
-29
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ " @backhooks/examples " : patch
3
+ " @backhooks/http " : patch
4
+ ---
5
+
6
+ - Updated types for headers and body hooks
7
+ - Added useQuery hook
Original file line number Diff line number Diff line change 2
2
"name" : " backhooks" ,
3
3
"scripts" : {
4
4
"build" : " npm run build --workspaces --if-present" ,
5
- "test" : " npm run test --workspaces --if-present" ,
5
+ "test" : " npm run build && npm run test --workspaces --if-present" ,
6
6
"publish" : " npm run build && changeset publish" ,
7
7
"make:toc" : " markdown-toc -i README.md" ,
8
8
"format:code" : " prettier -w ." ,
Original file line number Diff line number Diff line change @@ -8,11 +8,7 @@ import {
8
8
App ,
9
9
} from "h3" ;
10
10
import { listen } from "listhen" ;
11
- import {
12
- configureBodyHook ,
13
- configureHeadersHook ,
14
- useHeaders ,
15
- } from "@backhooks/http" ;
11
+ import { configureBodyHook , configureHeadersHook } from "@backhooks/http" ;
16
12
import { runHookContext } from "@backhooks/core" ;
17
13
import { mainHandler } from "../handlers" ;
18
14
@@ -23,15 +19,13 @@ const makeHookableApp = (h3App: App) => {
23
19
h3App . handler = async ( event : H3Event ) => {
24
20
return runHookContext ( ( ) => {
25
21
const headers = getHeaders ( event ) ;
26
- configureHeadersHook ( ( state ) => {
22
+ configureHeadersHook ( ( ) => {
27
23
return {
28
- ...state ,
29
24
headers,
30
25
} ;
31
26
} ) ;
32
- configureBodyHook ( ( state ) => {
27
+ configureBodyHook ( ( ) => {
33
28
return {
34
- ...state ,
35
29
fetch ( ) {
36
30
return readBody ( event ) ;
37
31
} ,
Original file line number Diff line number Diff line change 1
1
import { createHook } from "@backhooks/core" ;
2
2
3
+ interface BodyHookState {
4
+ body ?: any ;
5
+ fetch ?: ( ) => any ;
6
+ }
7
+
3
8
const [ useBody , configureBodyHook ] = createHook ( {
4
- data ( ) {
9
+ data ( ) : BodyHookState {
5
10
return {
6
- body : undefined as any ,
11
+ body : undefined ,
7
12
fetch ( ) {
8
- return undefined as any ;
13
+ return undefined ;
9
14
} ,
10
15
} ;
11
16
} ,
Original file line number Diff line number Diff line change 1
1
import { createHook } from "@backhooks/core" ;
2
2
3
- const useHeadersKey = "@backhooks/http/useHeaders" ;
4
-
5
- export interface HeadersHookOptions {
3
+ export interface HeadersHookState {
6
4
headers ?: Record < string , string > ;
7
5
fetch ?: ( ) => Record < string , string > ;
8
6
}
9
7
10
8
const [ useHeaders , configureHeadersHook ] = createHook ( {
11
- data ( ) {
9
+ data ( ) : HeadersHookState {
12
10
return {
13
- headers : undefined as Record < string , string > ,
11
+ headers : undefined as undefined | Record < string , string > ,
14
12
fetch : ( ) => {
15
13
return { } ;
16
14
} ,
Original file line number Diff line number Diff line change
1
+ import { createHook } from "@backhooks/core" ;
2
+
3
+ export interface QueryHookState {
4
+ query ?: Record < string , string > ;
5
+ fetch ?: ( ) => Record < string , string > ;
6
+ }
7
+
8
+ const [ useQuery , setQuery ] = createHook ( {
9
+ data ( ) : QueryHookState {
10
+ return {
11
+ query : undefined as undefined | Record < string , string > ,
12
+ fetch : ( ) => {
13
+ return { } ;
14
+ } ,
15
+ } ;
16
+ } ,
17
+ execute ( state ) {
18
+ if ( state . query ) {
19
+ return state . query ;
20
+ }
21
+ if ( state . fetch ) {
22
+ state . query = state . fetch ( ) ;
23
+ }
24
+ return ( state . query || { } ) as Record < string , string > ;
25
+ } ,
26
+ } ) ;
27
+
28
+ export { useQuery , setQuery } ;
Original file line number Diff line number Diff line change 1
1
import { runHookContext } from "@backhooks/core" ;
2
2
import { configureHeadersHook } from "../hooks/headers" ;
3
3
import { configureBodyHook } from "../hooks/body" ;
4
+ import { setQuery } from "../hooks/query" ;
4
5
5
6
export const hooksMiddleware = ( ) => {
6
7
return ( req , res , next ) => {
7
8
runHookContext ( async ( ) => {
8
- configureHeadersHook ( ( currentState ) => {
9
+ configureHeadersHook ( ( ) => {
9
10
return {
10
- ...currentState ,
11
11
fetch ( ) {
12
12
return req . headers ;
13
13
} ,
14
14
} ;
15
- } ) ,
16
- configureBodyHook ( ( currentState ) => {
17
- return {
18
- ...currentState ,
19
- fetch ( ) {
20
- return req . body ;
21
- } ,
22
- } ;
23
- } ) ;
15
+ } ) ;
16
+ configureBodyHook ( ( ) => {
17
+ return {
18
+ fetch ( ) {
19
+ return req . body ;
20
+ } ,
21
+ } ;
22
+ } ) ;
23
+ setQuery ( ( ) => {
24
+ return {
25
+ query : req . query ,
26
+ } ;
27
+ } ) ;
24
28
next ( ) ;
25
29
} ) . catch ( ( error ) => {
26
30
next ( error ) ;
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import { json } from "body-parser";
5
5
import { useBody } from "../src/hooks/body" ;
6
6
import { useHeaders } from "../src/hooks/headers" ;
7
7
import { hooksMiddleware } from "../src/middlewares/server" ;
8
+ import { useQuery } from "../src/hooks/query" ;
8
9
9
10
test ( "it should work with express" , async ( ) => {
10
11
const app = express ( ) ;
@@ -18,6 +19,12 @@ test("it should work with express", async () => {
18
19
body,
19
20
} ) ;
20
21
} ) ;
22
+ app . get ( "/bar" , ( req , res ) => {
23
+ const query = useQuery ( ) ;
24
+ res . send ( {
25
+ query,
26
+ } ) ;
27
+ } ) ;
21
28
const res = await request ( app )
22
29
. post ( "/" )
23
30
. send ( {
@@ -28,6 +35,9 @@ test("it should work with express", async () => {
28
35
} ) ;
29
36
expect ( res . body . headers . foo ) . toBe ( "bar" ) ;
30
37
expect ( res . body . body . hello ) . toBe ( "world" ) ;
38
+
39
+ const res2 = await request ( app ) . get ( "/bar?some=thing" ) ;
40
+ expect ( res2 . body . query . some ) . toBe ( "thing" ) ;
31
41
} ) ;
32
42
33
43
test ( "it should work with fastify" , async ( ) => {
You can’t perform that action at this time.
0 commit comments