@hypequery/react 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -1
- package/dist/createHooks.d.ts +3 -1
- package/dist/createHooks.d.ts.map +1 -1
- package/dist/createHooks.js +7 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -63,7 +63,7 @@ Factory function that creates type-safe `useQuery` and `useMutation` hooks for y
|
|
|
63
63
|
interface CreateHooksConfig<TApi> {
|
|
64
64
|
baseUrl: string; // Required: API base URL (e.g., '/api' or 'https://api.example.com')
|
|
65
65
|
fetchFn?: typeof fetch; // Optional: Custom fetch implementation (defaults to global fetch)
|
|
66
|
-
headers?: Record<string, string> | (() => Record<string, string>); // Optional: Default headers or a resolver function
|
|
66
|
+
headers?: Record<string, string | undefined> | (() => Record<string, string | undefined>); // Optional: Default headers or a resolver function
|
|
67
67
|
config?: Record<string, QueryMethodConfig>; // Optional: Per-route HTTP method overrides
|
|
68
68
|
api?: TApi; // Optional: API object to auto-extract HTTP methods
|
|
69
69
|
}
|
|
@@ -301,6 +301,18 @@ const { useQuery } = createHooks<Api>({
|
|
|
301
301
|
});
|
|
302
302
|
```
|
|
303
303
|
|
|
304
|
+
### Dynamic headers
|
|
305
|
+
|
|
306
|
+
```ts
|
|
307
|
+
const { useQuery } = createHooks<Api>({
|
|
308
|
+
baseUrl: '/api',
|
|
309
|
+
headers: () => {
|
|
310
|
+
const key = getTenantKey();
|
|
311
|
+
return key ? { 'x-tenant-key': key } : {};
|
|
312
|
+
},
|
|
313
|
+
});
|
|
314
|
+
```
|
|
315
|
+
|
|
304
316
|
### HTTP method configuration
|
|
305
317
|
|
|
306
318
|
```ts
|
package/dist/createHooks.d.ts
CHANGED
|
@@ -4,13 +4,15 @@ import { HttpError } from './errors.js';
|
|
|
4
4
|
export interface QueryMethodConfig {
|
|
5
5
|
method?: string;
|
|
6
6
|
}
|
|
7
|
+
type HeaderMap = Record<string, string | undefined>;
|
|
8
|
+
type HeadersInput = HeaderMap | (() => HeaderMap);
|
|
7
9
|
export interface CreateHooksConfig<TApi = Record<string, {
|
|
8
10
|
input: unknown;
|
|
9
11
|
output: unknown;
|
|
10
12
|
}>> {
|
|
11
13
|
baseUrl: string;
|
|
12
14
|
fetchFn?: typeof fetch;
|
|
13
|
-
headers?:
|
|
15
|
+
headers?: HeadersInput;
|
|
14
16
|
config?: Record<string, QueryMethodConfig>;
|
|
15
17
|
api?: TApi;
|
|
16
18
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createHooks.d.ts","sourceRoot":"","sources":["../src/createHooks.tsx"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,eAAe,IAAI,uBAAuB,EAC/C,KAAK,kBAAkB,IAAI,0BAA0B,EACrD,KAAK,iBAAiB,EACtB,KAAK,cAAc,EACpB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACxE,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,MAAM,WAAW,iBAAiB;IAChC,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,iBAAiB,CAAC,IAAI,GAAG,MAAM,CAAC,MAAM,EAAE;IAAE,KAAK,EAAE,OAAO,CAAC;IAAC,MAAM,EAAE,OAAO,CAAA;CAAE,CAAC;IAC3F,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,OAAO,KAAK,CAAC;IACvB,OAAO,CAAC,EAAE,
|
|
1
|
+
{"version":3,"file":"createHooks.d.ts","sourceRoot":"","sources":["../src/createHooks.tsx"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,eAAe,IAAI,uBAAuB,EAC/C,KAAK,kBAAkB,IAAI,0BAA0B,EACrD,KAAK,iBAAiB,EACtB,KAAK,cAAc,EACpB,MAAM,uBAAuB,CAAC;AA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IAAI,kIAET,iBAAiB,CAAC,WAAW,CAAC,GAAG,EAAE,IAAI,CAAC,EAAE,SAAS,EAAE,UAAU,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;EAW/E"}
|
package/dist/createHooks.js
CHANGED
|
@@ -52,6 +52,12 @@ const looksLikeQueryOptions = (value) => {
|
|
|
52
52
|
const matches = optionKeys.filter((key) => key in value).length;
|
|
53
53
|
return matches >= 2;
|
|
54
54
|
};
|
|
55
|
+
const resolveHeaders = (headers) => {
|
|
56
|
+
if (!headers)
|
|
57
|
+
return {};
|
|
58
|
+
const raw = typeof headers === 'function' ? headers() : headers;
|
|
59
|
+
return Object.fromEntries(Object.entries(raw).filter(([, value]) => value !== undefined));
|
|
60
|
+
};
|
|
55
61
|
export function createHooks(config) {
|
|
56
62
|
const { baseUrl, fetchFn = fetch, headers = {}, config: explicitConfig = {}, api } = config;
|
|
57
63
|
const finalConfig = { ...deriveMethodConfig(api), ...explicitConfig };
|
|
@@ -78,7 +84,7 @@ export function createHooks(config) {
|
|
|
78
84
|
else if (input !== undefined) {
|
|
79
85
|
body = JSON.stringify(input);
|
|
80
86
|
}
|
|
81
|
-
const resolvedHeaders =
|
|
87
|
+
const resolvedHeaders = resolveHeaders(headers);
|
|
82
88
|
const res = await fetchFn(finalUrl, {
|
|
83
89
|
method,
|
|
84
90
|
headers: {
|