Skip to content

Commit de61e4f

Browse files
committed
feat: Created hooks package
1 parent bb48f27 commit de61e4f

File tree

12 files changed

+163
-3
lines changed

12 files changed

+163
-3
lines changed

.changeset/gold-cobras-work.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@backhooks/hooks": patch
3+
---
4+
5+
Created package, useAuthorizationHeader, useBearerToken, useRequestId

package-lock.json

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"workspaces": [
99
"packages/core",
1010
"packages/http",
11+
"packages/hooks",
1112
"packages/examples"
1213
],
1314
"devDependencies": {

packages/hooks/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist

packages/hooks/jest.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/** @type {import('ts-jest').JestConfigWithTsJest} */
2+
module.exports = {
3+
preset: 'ts-jest',
4+
testEnvironment: 'node'
5+
};

packages/hooks/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "@backhooks/hooks",
3+
"version": "0.0.1",
4+
"scripts": {
5+
"build": "tsc",
6+
"test": "jest"
7+
},
8+
"dependencies": {
9+
"@backhooks/core": "^0.1.2",
10+
"@backhooks/http": "^0.1.1"
11+
},
12+
"devDependencies": {
13+
"typescript": "^4.9.4"
14+
}
15+
}

packages/hooks/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './useAuthorizationHeader'
2+
export * from './useBearerToken'
3+
export * from './useRequestId'
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {createHook} from '@backhooks/core'
2+
import {useHeaders} from '@backhooks/http'
3+
4+
const [
5+
useAuthorizationHeader,
6+
configureAuthorizationHeaderHook
7+
] = createHook({
8+
name: '@backhooks/hooks/useAuthorizationHeader',
9+
data () {
10+
const headers = useHeaders()
11+
return {
12+
header: headers['authorization']
13+
}
14+
},
15+
execute (state) {
16+
return state.header
17+
}
18+
})
19+
20+
export {
21+
useAuthorizationHeader,
22+
configureAuthorizationHeaderHook
23+
}

packages/examples/src/hooks/useBearerToken.ts renamed to packages/hooks/src/useBearerToken.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { createHook } from "@backhooks/core"
2-
import { useHeaders } from "@backhooks/http"
2+
import { useAuthorizationHeader } from "./useAuthorizationHeader"
33

44
const [useBearerToken, configureBearerTokenHook] = createHook({
55
name: 'useBearerToken',
66
data () {
7-
const headers = useHeaders()
8-
const bearerToken = headers['authorization']?.match(/^Bearer (.*)$/)
7+
const authorizationHeader = useAuthorizationHeader()
8+
const bearerToken = authorizationHeader?.match(/^Bearer (.*)$/)
99
return {
1010
token: bearerToken?.[1]
1111
}

packages/hooks/src/useRequestId.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { createHook } from "@backhooks/core";
2+
import * as crypto from 'node:crypto'
3+
4+
const [
5+
useRequestId,
6+
configureRequestIdHook
7+
] = createHook({
8+
name: '@backhooks/hooks/useRequestId',
9+
data () {
10+
return {
11+
requestId: crypto.randomUUID()
12+
}
13+
},
14+
execute (state) {
15+
return state.requestId
16+
}
17+
})
18+
19+
export {
20+
useRequestId,
21+
configureRequestIdHook
22+
}

0 commit comments

Comments
 (0)