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 22 "name" : " backhooks" ,
33 "scripts" : {
44 "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" ,
66 "publish" : " npm run build && changeset publish" ,
77 "make:toc" : " markdown-toc -i README.md" ,
88 "format:code" : " prettier -w ." ,
Original file line number Diff line number Diff line change @@ -8,11 +8,7 @@ import {
88 App ,
99} from "h3" ;
1010import { listen } from "listhen" ;
11- import {
12- configureBodyHook ,
13- configureHeadersHook ,
14- useHeaders ,
15- } from "@backhooks/http" ;
11+ import { configureBodyHook , configureHeadersHook } from "@backhooks/http" ;
1612import { runHookContext } from "@backhooks/core" ;
1713import { mainHandler } from "../handlers" ;
1814
@@ -23,15 +19,13 @@ const makeHookableApp = (h3App: App) => {
2319 h3App . handler = async ( event : H3Event ) => {
2420 return runHookContext ( ( ) => {
2521 const headers = getHeaders ( event ) ;
26- configureHeadersHook ( ( state ) => {
22+ configureHeadersHook ( ( ) => {
2723 return {
28- ...state ,
2924 headers,
3025 } ;
3126 } ) ;
32- configureBodyHook ( ( state ) => {
27+ configureBodyHook ( ( ) => {
3328 return {
34- ...state ,
3529 fetch ( ) {
3630 return readBody ( event ) ;
3731 } ,
Original file line number Diff line number Diff line change 11import { createHook } from "@backhooks/core" ;
22
3+ interface BodyHookState {
4+ body ?: any ;
5+ fetch ?: ( ) => any ;
6+ }
7+
38const [ useBody , configureBodyHook ] = createHook ( {
4- data ( ) {
9+ data ( ) : BodyHookState {
510 return {
6- body : undefined as any ,
11+ body : undefined ,
712 fetch ( ) {
8- return undefined as any ;
13+ return undefined ;
914 } ,
1015 } ;
1116 } ,
Original file line number Diff line number Diff line change 11import { createHook } from "@backhooks/core" ;
22
3- const useHeadersKey = "@backhooks/http/useHeaders" ;
4-
5- export interface HeadersHookOptions {
3+ export interface HeadersHookState {
64 headers ?: Record < string , string > ;
75 fetch ?: ( ) => Record < string , string > ;
86}
97
108const [ useHeaders , configureHeadersHook ] = createHook ( {
11- data ( ) {
9+ data ( ) : HeadersHookState {
1210 return {
13- headers : undefined as Record < string , string > ,
11+ headers : undefined as undefined | Record < string , string > ,
1412 fetch : ( ) => {
1513 return { } ;
1614 } ,
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 11import { runHookContext } from "@backhooks/core" ;
22import { configureHeadersHook } from "../hooks/headers" ;
33import { configureBodyHook } from "../hooks/body" ;
4+ import { setQuery } from "../hooks/query" ;
45
56export const hooksMiddleware = ( ) => {
67 return ( req , res , next ) => {
78 runHookContext ( async ( ) => {
8- configureHeadersHook ( ( currentState ) => {
9+ configureHeadersHook ( ( ) => {
910 return {
10- ...currentState ,
1111 fetch ( ) {
1212 return req . headers ;
1313 } ,
1414 } ;
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+ } ) ;
2428 next ( ) ;
2529 } ) . catch ( ( error ) => {
2630 next ( error ) ;
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import { json } from "body-parser";
55import { useBody } from "../src/hooks/body" ;
66import { useHeaders } from "../src/hooks/headers" ;
77import { hooksMiddleware } from "../src/middlewares/server" ;
8+ import { useQuery } from "../src/hooks/query" ;
89
910test ( "it should work with express" , async ( ) => {
1011 const app = express ( ) ;
@@ -18,6 +19,12 @@ test("it should work with express", async () => {
1819 body,
1920 } ) ;
2021 } ) ;
22+ app . get ( "/bar" , ( req , res ) => {
23+ const query = useQuery ( ) ;
24+ res . send ( {
25+ query,
26+ } ) ;
27+ } ) ;
2128 const res = await request ( app )
2229 . post ( "/" )
2330 . send ( {
@@ -28,6 +35,9 @@ test("it should work with express", async () => {
2835 } ) ;
2936 expect ( res . body . headers . foo ) . toBe ( "bar" ) ;
3037 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" ) ;
3141} ) ;
3242
3343test ( "it should work with fastify" , async ( ) => {
You can’t perform that action at this time.
0 commit comments