Skip to content

Commit f939948

Browse files
committedJan 18, 2023
Run prettier on files
1 parent 4beeaf7 commit f939948

36 files changed

+514
-513
lines changed
 

‎.devcontainer/devcontainer.json

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
// See https://containers.dev/implementors/json_reference/ for configuration reference
22
{
3-
"name": "Untitled Node.js project",
4-
"build": {
5-
"dockerfile": "Dockerfile"
6-
},
7-
"remoteUser": "node",
8-
"customizations": {
9-
"vscode": {
10-
"extensions": [
11-
"eamodio.gitlens"
12-
]
13-
}
14-
}
3+
"name": "Untitled Node.js project",
4+
"build": {
5+
"dockerfile": "Dockerfile"
6+
},
7+
"remoteUser": "node",
8+
"customizations": {
9+
"vscode": {
10+
"extensions": ["eamodio.gitlens"]
11+
}
12+
}
1513
}

‎.github/workflows/release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ jobs:
3232
publish: npm run publish
3333
env:
3434
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
35-
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
35+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

‎.github/workflows/tests.yml

+10-11
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,24 @@ name: Tests
22

33
on:
44
push:
5-
branches: [ main ]
5+
branches: [main]
66
pull_request:
7-
branches: [ main ]
7+
branches: [main]
88

99
jobs:
1010
build:
11-
1211
runs-on: ubuntu-latest
1312

1413
strategy:
1514
matrix:
1615
node-version: [16.x, 18.x]
1716

1817
steps:
19-
- uses: actions/checkout@v2
20-
- name: Use Node.js ${{ matrix.node-version }}
21-
uses: actions/setup-node@v1
22-
with:
23-
node-version: ${{ matrix.node-version }}
24-
- run: npm ci
25-
- run: npm run build
26-
- run: npm test
18+
- uses: actions/checkout@v2
19+
- name: Use Node.js ${{ matrix.node-version }}
20+
uses: actions/setup-node@v1
21+
with:
22+
node-version: ${{ matrix.node-version }}
23+
- run: npm ci
24+
- run: npm run build
25+
- run: npm test

‎.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist

‎README.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -166,23 +166,23 @@ This makes it really easy to test our code. You can even test your hooks by leve
166166

167167
```ts
168168
import { runHookContext } from "@backhooks/core";
169-
import { configureHeadersHook } from '@backhooks/http';
170-
import { useAuthorizationHeader } from './hooks/useAuthorizationHeader'
169+
import { configureHeadersHook } from "@backhooks/http";
170+
import { useAuthorizationHeader } from "./hooks/useAuthorizationHeader";
171171

172-
test('it should return the authorization header', async () => {
172+
test("it should return the authorization header", async () => {
173173
runHookContext(() => {
174-
configureHeadersHook(state => {
174+
configureHeadersHook((state) => {
175175
return {
176176
...state,
177177
headers: {
178-
authorization: 'def'
179-
}
180-
}
181-
})
182-
const authorizationHeader = useAuthorizationHeader()
183-
expect(authorizationHeader).toBe('def') // true
184-
})
185-
})
178+
authorization: "def",
179+
},
180+
};
181+
});
182+
const authorizationHeader = useAuthorizationHeader();
183+
expect(authorizationHeader).toBe("def"); // true
184+
});
185+
});
186186
```
187187

188188
## Global context

‎packages/core/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ npm install @backhooks/core
1414

1515
This allows you to create a hook. Example of creating a simple hook:
1616

17-
```ts
17+
````ts
1818
const [useCount, configureCountHook] = createHook({
1919
data () {
2020
return {
@@ -43,4 +43,4 @@ runHookContext(() => {
4343
console.log(useCount()) // 2
4444
console.log(useCount()) // 3
4545
})
46-
```
46+
````

‎packages/core/jest.config.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/** @type {import('ts-jest').JestConfigWithTsJest} */
22
module.exports = {
3-
preset: 'ts-jest',
4-
testEnvironment: 'node',
5-
};
3+
preset: "ts-jest",
4+
testEnvironment: "node",
5+
};

‎packages/core/src/core.ts

+55-37
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,79 @@
1-
import {AsyncLocalStorage} from 'node:async_hooks'
2-
import * as crypto from 'node:crypto'
3-
import type {Optional} from 'utility-types'
1+
import { AsyncLocalStorage } from "node:async_hooks";
2+
import * as crypto from "node:crypto";
3+
import type { Optional } from "utility-types";
44

5-
const asyncLocalStorage = new AsyncLocalStorage()
5+
const asyncLocalStorage = new AsyncLocalStorage();
66

7-
const globalStore = {}
7+
const globalStore = {};
88

9-
interface CreateHookOptions<State extends Record<string, any>, ExecuteResult, HookOptions> {
10-
name: string
11-
data: () => State
12-
execute: (state: State, options: HookOptions) => ExecuteResult
9+
interface CreateHookOptions<
10+
State extends Record<string, any>,
11+
ExecuteResult,
12+
HookOptions
13+
> {
14+
name: string;
15+
data: () => State;
16+
execute: (state: State, options: HookOptions) => ExecuteResult;
1317
}
1418

1519
const getCurrentStore = () => {
16-
return asyncLocalStorage.getStore() || globalStore
17-
}
20+
return asyncLocalStorage.getStore() || globalStore;
21+
};
1822

19-
const generateHookFunction = <State, ExecuteResult, HookOptions>(options: CreateHookOptions<State, ExecuteResult, HookOptions>) => {
23+
const generateHookFunction = <State, ExecuteResult, HookOptions>(
24+
options: CreateHookOptions<State, ExecuteResult, HookOptions>
25+
) => {
2026
return (parameters?: HookOptions) => {
21-
const store = getCurrentStore()
27+
const store = getCurrentStore();
2228
if (!store[options.name]) {
23-
store[options.name] = options.data()
29+
store[options.name] = options.data();
2430
}
25-
const hookStore = store[options.name] as State
26-
return options.execute(hookStore, parameters)
27-
}
28-
}
31+
const hookStore = store[options.name] as State;
32+
return options.execute(hookStore, parameters);
33+
};
34+
};
2935

30-
const generateUpdateHookStateFunction = <State, ExecuteResult, HookOptions>(options: CreateHookOptions<State, ExecuteResult, HookOptions>) => {
36+
const generateUpdateHookStateFunction = <State, ExecuteResult, HookOptions>(
37+
options: CreateHookOptions<State, ExecuteResult, HookOptions>
38+
) => {
3139
return (fn: (currentState: State) => State) => {
32-
const store = getCurrentStore()
40+
const store = getCurrentStore();
3341
if (!store[options.name]) {
34-
store[options.name] = options.data()
42+
store[options.name] = options.data();
3543
}
36-
const hookStore = store[options.name] as State
37-
store[options.name] = fn(hookStore)
38-
}
39-
}
44+
const hookStore = store[options.name] as State;
45+
store[options.name] = fn(hookStore);
46+
};
47+
};
4048

41-
export const createHook = <State, ExecuteResult, HookOptions>(options: Optional<Omit<CreateHookOptions<State, ExecuteResult, HookOptions>, 'name'>, 'data'>): [(parameters?: HookOptions) => ExecuteResult, (fn: (currentState: State) => State) => void] => {
42-
const name = crypto.randomUUID()
43-
const data = options.data || (() => ({} as State))
49+
export const createHook = <State, ExecuteResult, HookOptions>(
50+
options: Optional<
51+
Omit<CreateHookOptions<State, ExecuteResult, HookOptions>, "name">,
52+
"data"
53+
>
54+
): [
55+
(parameters?: HookOptions) => ExecuteResult,
56+
(fn: (currentState: State) => State) => void
57+
] => {
58+
const name = crypto.randomUUID();
59+
const data = options.data || (() => ({} as State));
4460
return [
4561
generateHookFunction({
4662
...options,
4763
name,
48-
data
64+
data,
4965
}),
5066
generateUpdateHookStateFunction({
5167
...options,
5268
name,
53-
data
54-
})
55-
]
56-
}
69+
data,
70+
}),
71+
];
72+
};
5773

58-
export const runHookContext = async <T>(fn: () => Promise<T> | T): Promise<T> => {
59-
const result = await asyncLocalStorage.run({}, fn) as T
60-
return result
61-
}
74+
export const runHookContext = async <T>(
75+
fn: () => Promise<T> | T
76+
): Promise<T> => {
77+
const result = (await asyncLocalStorage.run({}, fn)) as T;
78+
return result;
79+
};

‎packages/core/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './core'
1+
export * from "./core";

0 commit comments

Comments
 (0)
Please sign in to comment.