@openapi-qraft/react 1.8.0 → 1.10.0
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/CHANGELOG.md +10 -0
- package/README.md +61 -55
- package/dist/Unstable_QraftSecureRequestFn.d.ts +81 -0
- package/dist/Unstable_QraftSecureRequestFn.d.ts.map +1 -0
- package/dist/Unstable_QraftSecureRequestFn.js +187 -0
- package/dist/Unstable_QraftSecureRequestFn.js.map +1 -0
- package/dist/lib/jwt-decode/index.d.ts +29 -0
- package/dist/lib/jwt-decode/index.d.ts.map +1 -0
- package/dist/lib/jwt-decode/index.js +61 -0
- package/dist/lib/jwt-decode/index.js.map +1 -0
- package/dist/lib/jwt-decode/index.test.d.ts +2 -0
- package/dist/lib/jwt-decode/index.test.d.ts.map +1 -0
- package/dist/lib/jwt-decode/index.test.js +99 -0
- package/dist/lib/jwt-decode/index.test.js.map +1 -0
- package/dist/lib/requestFn.d.ts +3 -1
- package/dist/lib/requestFn.d.ts.map +1 -1
- package/dist/lib/requestFn.js +1 -1
- package/dist/lib/requestFn.js.map +1 -1
- package/package.json +5 -5
- package/src/Unstable_QraftSecureRequestFn.ts +365 -0
- package/src/lib/jwt-decode/index.test.ts +131 -0
- package/src/lib/jwt-decode/index.ts +105 -0
- package/src/lib/requestFn.ts +4 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# @openapi-qraft/react
|
|
2
2
|
|
|
3
|
+
## 1.10.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 67c1432: Added the `<QraftSecureRequestFn />` component to enhance secure request handling. This component supports
|
|
8
|
+
multiple authentication methods including API Key, Basic, Bearer, and Cookie authentication. Integrates seamlessly with
|
|
9
|
+
QraftContext for secure API requests.
|
|
10
|
+
|
|
11
|
+
## 1.9.0
|
|
12
|
+
|
|
3
13
|
## 1.8.0
|
|
4
14
|
|
|
5
15
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -149,12 +149,53 @@ function App() {
|
|
|
149
149
|
> The Qraft is designed to be as modular as possible, enabling you to integrate your own request client and serializers.
|
|
150
150
|
> To ensure optimal tree-shaking, we do not include default serializers in the functions.
|
|
151
151
|
|
|
152
|
+
## Supported Hooks
|
|
153
|
+
|
|
154
|
+
- [x] [`useQuery(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/hooks/useQuery)
|
|
155
|
+
- [x] [`useMutation(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/hooks/useMutation)
|
|
156
|
+
- [x] [`useInfiniteQuery(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/hooks/useInfiniteQuery)
|
|
157
|
+
- [x] [`useQueries(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/hooks/useQueries)
|
|
158
|
+
- [x] [`useSuspenseQuery(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/hooks/useSuspenseQuery)
|
|
159
|
+
- [x] [`useSuspenseInfiniteQuery(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/hooks/useSuspenseInfiniteQuery)
|
|
160
|
+
- [x] [`useSuspenseQueries(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/hooks/useSuspenseQueries)
|
|
161
|
+
- [x] [`useIsFetching(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/hooks/useIsFetching)
|
|
162
|
+
- [x] [`useMutationState(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/hooks/useMutationState)
|
|
163
|
+
- [x] [`useIsMutating(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/hooks/useIsMutating)
|
|
164
|
+
|
|
165
|
+
## `QueryClient` methods
|
|
166
|
+
|
|
167
|
+
- [x] [`fetchQuery(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/query-client/fetchQuery)
|
|
168
|
+
- [x] [`fetchInfiniteQuery(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/query-client/fetchInfiniteQuery)
|
|
169
|
+
- [x] [`prefetchQuery(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/query-client/prefetchQuery)
|
|
170
|
+
- [x] [`prefetchInfiniteQuery(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/query-client/prefetchInfiniteQuery)
|
|
171
|
+
- [x] [`getQueryData(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/query-client/getQueryData)
|
|
172
|
+
- [x] [`getQueriesData(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/query-client/getQueriesData)
|
|
173
|
+
- [x] [`setQueryData(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/query-client/setQueryData)
|
|
174
|
+
- [x] [`getQueryState(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/query-client/getQueryState)
|
|
175
|
+
- [x] [`setQueriesData(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/query-client/setQueriesData)
|
|
176
|
+
- [x] [`invalidateQueries(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/query-client/invalidateQueries)
|
|
177
|
+
- [x] [`refetchQueries(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/query-client/refetchQueries)
|
|
178
|
+
- [x] [`cancelQueries(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/query-client/cancelQueries)
|
|
179
|
+
- [x] [`removeQueries(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/query-client/removeQueries)
|
|
180
|
+
- [x] [`resetQueries(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/query-client/resetQueries)
|
|
181
|
+
- [x] [`isFetching(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/query-client/isFetching)
|
|
182
|
+
- [x] [`isMutating(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/query-client/isMutating)
|
|
183
|
+
|
|
184
|
+
## Qraft Utility Functions
|
|
185
|
+
|
|
186
|
+
- [x] [`getQueryKey(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/query-client/getQueryKey)
|
|
187
|
+
- [x] [`setInfiniteQueryData(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/query-client/setInfiniteQueryData)
|
|
188
|
+
- [x] [`getInfiniteQueryKey(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/query-client/getInfiniteQueryKey)
|
|
189
|
+
- [x] [`getInfiniteQueryData(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/query-client/getInfiniteQueryData)
|
|
190
|
+
- [x] [`getInfiniteQueryState(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/query-client/getInfiniteQueryState)
|
|
191
|
+
- [x] [`getMutationKey(...)`](https://openapi-qraft.github.io/openapi-qraft/docs/query-client/getMutationKey)
|
|
192
|
+
|
|
152
193
|
## Usage
|
|
153
194
|
|
|
154
195
|
With the client set up, you can now use the generated Hooks in your React Components to fetch data, execute mutations,
|
|
155
196
|
and more.
|
|
156
197
|
|
|
157
|
-
### [useQuery(...) 🔗](https://
|
|
198
|
+
### [useQuery(...) 🔗](https://openapi-qraft.github.io/openapi-qraft/docs/hooks/useQuery)
|
|
158
199
|
|
|
159
200
|
```ts
|
|
160
201
|
/**
|
|
@@ -173,7 +214,7 @@ const { data, error, isPending } = qraft.entities.getEntities.useQuery({
|
|
|
173
214
|
});
|
|
174
215
|
```
|
|
175
216
|
|
|
176
|
-
### [useMutation(...) 🔗](https://
|
|
217
|
+
### [useMutation(...) 🔗](https://openapi-qraft.github.io/openapi-qraft/docs/hooks/useMutation)
|
|
177
218
|
|
|
178
219
|
#### With predefined parameters
|
|
179
220
|
|
|
@@ -233,7 +274,7 @@ mutation.mutate({
|
|
|
233
274
|
});
|
|
234
275
|
```
|
|
235
276
|
|
|
236
|
-
### [useMutationState(...) 🔗](https://
|
|
277
|
+
### [useMutationState(...) 🔗](https://openapi-qraft.github.io/openapi-qraft/docs/hooks/useMutationState)
|
|
237
278
|
|
|
238
279
|
Mutation state is a helper hook that provides the current state of a mutation globally.
|
|
239
280
|
It could be used to track the status of a mutation in any component, which is useful for showing loading spinners or
|
|
@@ -261,7 +302,7 @@ useEffect(() => {
|
|
|
261
302
|
}, [mutate, mutationState?.status]);
|
|
262
303
|
```
|
|
263
304
|
|
|
264
|
-
### [useInfiniteQuery(...) 🔗](https://
|
|
305
|
+
### [useInfiniteQuery(...) 🔗](https://openapi-qraft.github.io/openapi-qraft/docs/hooks/useInfiniteQuery)
|
|
265
306
|
|
|
266
307
|
#### _Offset-based_
|
|
267
308
|
|
|
@@ -338,7 +379,7 @@ infiniteQuery.fetchNextPage();
|
|
|
338
379
|
infiniteQuery.fetchPreviousPage();
|
|
339
380
|
```
|
|
340
381
|
|
|
341
|
-
### [useQueries(...) 🔗](https://
|
|
382
|
+
### [useQueries(...) 🔗](https://openapi-qraft.github.io/openapi-qraft/docs/hooks/useQueries)
|
|
342
383
|
|
|
343
384
|
```ts
|
|
344
385
|
/**
|
|
@@ -377,7 +418,7 @@ qraft.entities.getEntities.useQueries({
|
|
|
377
418
|
});
|
|
378
419
|
```
|
|
379
420
|
|
|
380
|
-
### [useIsFetching(...) 🔗](https://
|
|
421
|
+
### [useIsFetching(...) 🔗](https://openapi-qraft.github.io/openapi-qraft/docs/hooks/useIsFetching)
|
|
381
422
|
|
|
382
423
|
```tsx
|
|
383
424
|
function FetchStatus() {
|
|
@@ -405,9 +446,19 @@ function FetchStatus() {
|
|
|
405
446
|
}
|
|
406
447
|
```
|
|
407
448
|
|
|
408
|
-
|
|
449
|
+
## [`QueryClient`](https://tanstack.com/query/latest/docs/reference/QueryClient) Methods
|
|
450
|
+
|
|
451
|
+
#### [setQueryData(...) 🔗](https://openapi-qraft.github.io/openapi-qraft/docs/query-client/setQueryData)
|
|
452
|
+
|
|
453
|
+
```ts
|
|
454
|
+
qraft.pet.getPetById.setQueryData(
|
|
455
|
+
{ path: { petId: 123 } }, // Query Key parameters
|
|
456
|
+
{ id: 123, name: 'Rex' }, // Data to set
|
|
457
|
+
queryClient
|
|
458
|
+
);
|
|
459
|
+
```
|
|
409
460
|
|
|
410
|
-
#### [invalidateQueries(...) 🔗](https://
|
|
461
|
+
#### [invalidateQueries(...) 🔗](https://openapi-qraft.github.io/openapi-qraft/docs/query-client/invalidateQueries)
|
|
411
462
|
|
|
412
463
|
[Queries Invalidation 🔗](https://tanstack.com/query/latest/docs/framework/react/guides/query-invalidation) is possible
|
|
413
464
|
using `<operation>.invalidateQueries(...)` method.
|
|
@@ -511,25 +562,7 @@ qraft.entities.getEntities.invalidateQueries(
|
|
|
511
562
|
);
|
|
512
563
|
```
|
|
513
564
|
|
|
514
|
-
#### [
|
|
515
|
-
|
|
516
|
-
[Query Cancellation](https://tanstack.com/query/latest/docs/framework/react/guides/query-cancellation) has the
|
|
517
|
-
same interface as `invalidateQueries(...)`, but it will cancel the queries instead of invalidating them.
|
|
518
|
-
|
|
519
|
-
#### [resetQueries(...) 🔗](https://tanstack.com/query/latest/docs/reference/QueryClient#queryclientresetqueries)
|
|
520
|
-
|
|
521
|
-
The interface is the same as `invalidateQueries(...)`, but it will reset the queries instead of invalidating them.
|
|
522
|
-
|
|
523
|
-
#### [refetchQueries(...) 🔗](https://tanstack.com/query/latest/docs/reference/QueryClient#queryclientrefetchqueries)
|
|
524
|
-
|
|
525
|
-
The interface is the same as `invalidateQueries(...)`, but it will refetch the queries instead of invalidating them.
|
|
526
|
-
|
|
527
|
-
#### [isFetching(...) 🔗](https://tanstack.com/query/latest/docs/reference/QueryClient#queryclientisfetching)
|
|
528
|
-
|
|
529
|
-
The interface is the same as `invalidateQueries(...), but it will check if the queries are fetching, and there are no
|
|
530
|
-
options.
|
|
531
|
-
|
|
532
|
-
#### [fetchQuery(...) 🔗](https://tanstack.com/query/latest/docs/reference/QueryClient#queryclientfetchquery)
|
|
565
|
+
#### [fetchQuery(...) 🔗](https://openapi-qraft.github.io/openapi-qraft/docs/query-client/fetchQuery)
|
|
533
566
|
|
|
534
567
|
```ts
|
|
535
568
|
/**
|
|
@@ -557,11 +590,7 @@ const posts = qraft.posts.getPosts.fetchQuery(
|
|
|
557
590
|
);
|
|
558
591
|
```
|
|
559
592
|
|
|
560
|
-
#### [
|
|
561
|
-
|
|
562
|
-
The interface is the same as `fetchQuery(...)`, but returns empty Promise.
|
|
563
|
-
|
|
564
|
-
#### [fetchInfiniteQuery(...) 🔗](https://tanstack.com/query/latest/docs/reference/QueryClient#queryclientfetchinfinitequery)
|
|
593
|
+
#### [fetchInfiniteQuery(...) 🔗](https://openapi-qraft.github.io/openapi-qraft/docs/query-client/fetchInfiniteQuery)
|
|
565
594
|
|
|
566
595
|
```ts
|
|
567
596
|
/**
|
|
@@ -604,29 +633,6 @@ console.log(
|
|
|
604
633
|
);
|
|
605
634
|
```
|
|
606
635
|
|
|
607
|
-
#### [prefetchInfiniteQuery(...) 🔗](https://tanstack.com/query/latest/docs/reference/QueryClient#queryclientprefetchinfinitequery)
|
|
608
|
-
|
|
609
|
-
The interface is the same as `fetchInfiniteQuery(...)`, but returns empty Promise.
|
|
610
|
-
|
|
611
|
-
### Suspense Queries
|
|
612
|
-
|
|
613
|
-
Supported Suspense Queries are:
|
|
614
|
-
|
|
615
|
-
- [useSuspenseQuery(...) 🔗](https://tanstack.com/query/latest/docs/framework/react/reference/useSuspenseQuery)
|
|
616
|
-
- [useSuspenseInfiniteQuery(...) 🔗](https://tanstack.com/query/latest/docs/framework/react/reference/useSuspenseInfiniteQuery)
|
|
617
|
-
- [useSuspenseQueries(...) 🔗](https://tanstack.com/query/latest/docs/framework/react/reference/useSuspenseQueries)
|
|
618
|
-
|
|
619
|
-
### Documentation in Progress 🚧
|
|
620
|
-
|
|
621
|
-
- `getQueryKey(parameters)`
|
|
622
|
-
- `getQueryData(parameters)`
|
|
623
|
-
- `setQueryData(parameters, updater)`
|
|
624
|
-
- `getInfiniteQueryKey(parameters)`
|
|
625
|
-
- `getInfiniteQueryData(parameters)`
|
|
626
|
-
- `setInfiniteQueryData(parameters, updater)`
|
|
627
|
-
- `schema` - object with schema: `{method, url, mediaType}`
|
|
628
|
-
- `types` - object with types: `{parameters, data, error, body}`
|
|
629
|
-
|
|
630
636
|
### Authorization and Custom Requests
|
|
631
637
|
|
|
632
638
|
To override any request, you can provide a custom `request` function to the `QraftContext`.
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
import { QueryClient } from '@tanstack/react-query';
|
|
3
|
+
import type { OperationSchema, RequestFnInfo, RequestFnOptions } from './lib/requestFn.js';
|
|
4
|
+
interface QraftSecureRequestFnBaseProps {
|
|
5
|
+
requestFn<T>(schema: OperationSchema, requestInfo: RequestFnInfo, options?: RequestFnOptions): Promise<T>;
|
|
6
|
+
securitySchemes: SecuritySchemeHandlers<string>;
|
|
7
|
+
}
|
|
8
|
+
type RequestFn = QraftSecureRequestFnBaseProps['requestFn'];
|
|
9
|
+
export interface QraftSecureRequestFnProps extends QraftSecureRequestFnBaseProps {
|
|
10
|
+
children(secureRequestFn: QraftSecureRequestFnBaseProps['requestFn']): ReactNode;
|
|
11
|
+
queryClient?: QueryClient;
|
|
12
|
+
}
|
|
13
|
+
export declare function QraftSecureRequestFn({ children, requestFn, securitySchemes, queryClient: inQueryClient, }: QraftSecureRequestFnProps): import("react").FunctionComponentElement<{
|
|
14
|
+
children?: ReactNode;
|
|
15
|
+
}>;
|
|
16
|
+
export declare const useSecuritySchemeAuth: ({ securitySchemes, requestFn, queryClient, }: QraftSecureRequestFnBaseProps & {
|
|
17
|
+
queryClient: QueryClient;
|
|
18
|
+
}) => RequestFn;
|
|
19
|
+
/**
|
|
20
|
+
* Create a secure `requestFn` that manages security schemes.
|
|
21
|
+
* It will automatically fetch and refresh tokens when needed
|
|
22
|
+
* using QueryClient and the provided security schemes.
|
|
23
|
+
*
|
|
24
|
+
* @param securitySchemes OpenAPI security schemes.
|
|
25
|
+
* @param requestFn Qraft `requestFn`
|
|
26
|
+
* @param queryClient QueryClient instance.
|
|
27
|
+
*/
|
|
28
|
+
export declare function createSecureRequestFn(securitySchemes: SecuritySchemeHandlers<string>, requestFn: RequestFn, queryClient: QueryClient): RequestFn;
|
|
29
|
+
/**
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
export declare function shallowEqualObjects(newObj: Record<string, any> | string, prevObj: Record<string, any> | string | undefined): boolean;
|
|
33
|
+
type SecurityScheme = SecuritySchemeBearer | SecuritySchemeBasic | SecuritySchemeAPIKey | SecuritySchemeCookie;
|
|
34
|
+
export type SecuritySchemeBearer = string /** JWT Bearer token. **/ | {
|
|
35
|
+
/** Refresh interval in milliseconds. */
|
|
36
|
+
refreshInterval: number;
|
|
37
|
+
/** Token to be used for authentication. */
|
|
38
|
+
token: string;
|
|
39
|
+
};
|
|
40
|
+
export interface SecuritySchemeBasic {
|
|
41
|
+
/** Credentials to be used for authentication. */
|
|
42
|
+
credentials: string;
|
|
43
|
+
/** Refresh interval in milliseconds. */
|
|
44
|
+
refreshInterval: number;
|
|
45
|
+
}
|
|
46
|
+
export type SecuritySchemeAPIKey = {
|
|
47
|
+
/** Where the secret should be placed. */
|
|
48
|
+
in: 'header' | 'query';
|
|
49
|
+
/** Name of the secret key. */
|
|
50
|
+
name: string;
|
|
51
|
+
/** Secret to be used for authentication. */
|
|
52
|
+
value: string;
|
|
53
|
+
/** Refresh interval in milliseconds. */
|
|
54
|
+
refreshInterval?: number;
|
|
55
|
+
};
|
|
56
|
+
export type SecuritySchemeCookie = {
|
|
57
|
+
/** Where the secret should be placed. */
|
|
58
|
+
in: 'cookie';
|
|
59
|
+
/** Refresh interval in milliseconds. */
|
|
60
|
+
refreshInterval?: number;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Type definition for a handler function that manages security schemes.
|
|
64
|
+
* This function is invoked when a token needs to be fetched or refreshed.
|
|
65
|
+
*
|
|
66
|
+
* @param props - The properties for the handler function.
|
|
67
|
+
* @param props.isRefreshing - A boolean indicating the state of the token.
|
|
68
|
+
* It's `false` on the initial "fetch token" request, and `true` if the token is stale or updating using `refreshInterval`.
|
|
69
|
+
* @param props.signal - An AbortSignal that can be used to abort the request.
|
|
70
|
+
*
|
|
71
|
+
* @returns A SecurityScheme, which contains the necessary data for authentication.
|
|
72
|
+
*/
|
|
73
|
+
type SecuritySchemeHandler = (props: {
|
|
74
|
+
isRefreshing: boolean;
|
|
75
|
+
signal: AbortSignal;
|
|
76
|
+
}) => SecurityScheme | Promise<SecurityScheme>;
|
|
77
|
+
export type SecuritySchemeHandlers<SecuritySchemeName extends string> = {
|
|
78
|
+
[key in SecuritySchemeName]: SecuritySchemeHandler;
|
|
79
|
+
};
|
|
80
|
+
export {};
|
|
81
|
+
//# sourceMappingURL=Unstable_QraftSecureRequestFn.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Unstable_QraftSecureRequestFn.d.ts","sourceRoot":"","sources":["../src/Unstable_QraftSecureRequestFn.ts"],"names":[],"mappings":"AAEA,OAAO,EAA2B,SAAS,EAAsB,MAAM,OAAO,CAAC;AAE/E,OAAO,EACL,WAAW,EAIZ,MAAM,uBAAuB,CAAC;AAG/B,OAAO,KAAK,EACV,eAAe,EACf,aAAa,EACb,gBAAgB,EACjB,MAAM,oBAAoB,CAAC;AAE5B,UAAU,6BAA6B;IACrC,SAAS,CAAC,CAAC,EACT,MAAM,EAAE,eAAe,EACvB,WAAW,EAAE,aAAa,EAC1B,OAAO,CAAC,EAAE,gBAAgB,GACzB,OAAO,CAAC,CAAC,CAAC,CAAC;IACd,eAAe,EAAE,sBAAsB,CAAC,MAAM,CAAC,CAAC;CACjD;AAED,KAAK,SAAS,GAAG,6BAA6B,CAAC,WAAW,CAAC,CAAC;AAE5D,MAAM,WAAW,yBACf,SAAQ,6BAA6B;IACrC,QAAQ,CACN,eAAe,EAAE,6BAA6B,CAAC,WAAW,CAAC,GAC1D,SAAS,CAAC;IACb,WAAW,CAAC,EAAE,WAAW,CAAC;CAC3B;AAED,wBAAgB,oBAAoB,CAAC,EACnC,QAAQ,EACR,SAAS,EACT,eAAe,EACf,WAAW,EAAE,aAAa,GAC3B,EAAE,yBAAyB;;GAkC3B;AAED,eAAO,MAAM,qBA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AAM,EAAE,WAAW,CAAC;CACrB,KAAK,cAAc,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;AAE/C,MAAM,MAAM,sBAAsB,CAAC,kBAAkB,SAAS,MAAM,IAAI;KACrE,GAAG,IAAI,kBAAkB,GAAG,qBAAqB;CACnD,CAAC"}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { createElement, Fragment, useEffect, useMemo } from 'react';
|
|
3
|
+
import { QueryClient, useQueries, } from '@tanstack/react-query';
|
|
4
|
+
import { jwtDecode } from './lib/jwt-decode/index.js';
|
|
5
|
+
export function QraftSecureRequestFn({ children, requestFn, securitySchemes, queryClient: inQueryClient, }) {
|
|
6
|
+
const queryClient = useMemo(() => inQueryClient ??
|
|
7
|
+
new QueryClient({
|
|
8
|
+
defaultOptions: {
|
|
9
|
+
queries: {
|
|
10
|
+
staleTime: 300_000,
|
|
11
|
+
gcTime: 300_000,
|
|
12
|
+
refetchOnWindowFocus: false,
|
|
13
|
+
refetchOnMount: false,
|
|
14
|
+
refetchOnReconnect: false,
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
}), [inQueryClient]);
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
queryClient.mount();
|
|
20
|
+
return () => {
|
|
21
|
+
return void queryClient.unmount();
|
|
22
|
+
};
|
|
23
|
+
}, [queryClient]);
|
|
24
|
+
const secureRequestFn = useSecuritySchemeAuth({
|
|
25
|
+
securitySchemes,
|
|
26
|
+
requestFn,
|
|
27
|
+
queryClient,
|
|
28
|
+
});
|
|
29
|
+
return createElement(Fragment, {
|
|
30
|
+
children: children(secureRequestFn),
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
export const useSecuritySchemeAuth = ({ securitySchemes, requestFn, queryClient, }) => {
|
|
34
|
+
useQueries({
|
|
35
|
+
queries: Object.entries(securitySchemes).map(([securitySchemeName]) => {
|
|
36
|
+
const queryKey = [securitySchemeName];
|
|
37
|
+
return {
|
|
38
|
+
enabled: Boolean(queryClient.getQueryData(queryKey)),
|
|
39
|
+
queryKey: queryKey,
|
|
40
|
+
queryFn: ({ signal, }) => {
|
|
41
|
+
const securitySchemeHandler = securitySchemes[securitySchemeName];
|
|
42
|
+
return securitySchemeHandler({ signal, isRefreshing: true });
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}),
|
|
46
|
+
}, queryClient);
|
|
47
|
+
return useMemo(() => createSecureRequestFn(securitySchemes, requestFn, queryClient), [securitySchemes, requestFn, queryClient]);
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Create a secure `requestFn` that manages security schemes.
|
|
51
|
+
* It will automatically fetch and refresh tokens when needed
|
|
52
|
+
* using QueryClient and the provided security schemes.
|
|
53
|
+
*
|
|
54
|
+
* @param securitySchemes OpenAPI security schemes.
|
|
55
|
+
* @param requestFn Qraft `requestFn`
|
|
56
|
+
* @param queryClient QueryClient instance.
|
|
57
|
+
*/
|
|
58
|
+
export function createSecureRequestFn(securitySchemes, requestFn, queryClient) {
|
|
59
|
+
return async function secureRequestFn(schema, requestInfo, options) {
|
|
60
|
+
const availableSecuritySchemes = Object.keys(securitySchemes);
|
|
61
|
+
const securitySchemeName = schema.security?.find((security) => availableSecuritySchemes.includes(security));
|
|
62
|
+
if (!securitySchemeName) {
|
|
63
|
+
return requestFn(schema, requestInfo, options);
|
|
64
|
+
}
|
|
65
|
+
const securitySchemeHandler = securitySchemes[securitySchemeName];
|
|
66
|
+
return requestFn(schema, await createSecureRequestInfo(securitySchemeHandler, [securitySchemeName], requestInfo, queryClient), options);
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Calculate the interval at which a token should be refreshed.
|
|
71
|
+
*/
|
|
72
|
+
function getJwtTokenRefreshInterval(token) {
|
|
73
|
+
const parsedToken = jwtDecode(token);
|
|
74
|
+
if (!parsedToken.iat || !parsedToken.exp) {
|
|
75
|
+
throw new Error("JWT token must contain both 'iat' and 'exp' fields.");
|
|
76
|
+
}
|
|
77
|
+
return (parsedToken.exp - parsedToken.iat) * 1000;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Refresh the token if it is about to expire
|
|
81
|
+
* @returns The string to be used as the `Authorization` header or the object to be used in the request.
|
|
82
|
+
*/
|
|
83
|
+
async function createSecureRequestInfo(handler, queryKey, requestInfo, queryClient) {
|
|
84
|
+
const prevSecurityResult = queryClient.getQueryData(queryKey);
|
|
85
|
+
const securityResult = await queryClient.fetchQuery({
|
|
86
|
+
queryKey,
|
|
87
|
+
queryFn: ({ signal }) => handler({
|
|
88
|
+
signal,
|
|
89
|
+
isRefreshing: Boolean(prevSecurityResult),
|
|
90
|
+
}),
|
|
91
|
+
});
|
|
92
|
+
if (!shallowEqualObjects(securityResult, prevSecurityResult)) {
|
|
93
|
+
const securityRefreshInterval = getSecurityRefreshInterval(securityResult);
|
|
94
|
+
if (securityRefreshInterval !== undefined && securityRefreshInterval > 0) {
|
|
95
|
+
// remove the already fetched query, before setting new defaults
|
|
96
|
+
queryClient.removeQueries({ queryKey, exact: true });
|
|
97
|
+
// set query new defaults (staleTime and gcTime)
|
|
98
|
+
setSecurityQueryDefaults(queryClient, queryKey, securityRefreshInterval);
|
|
99
|
+
// Set query data with the new token, since the old one was removed.
|
|
100
|
+
// New query data will follow the new defaults staleTime and gcTime.
|
|
101
|
+
queryClient.setQueryData(queryKey, securityResult);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
const securityParameters = createSecureRequestParameters(securityResult);
|
|
105
|
+
if (!securityParameters)
|
|
106
|
+
return requestInfo;
|
|
107
|
+
return {
|
|
108
|
+
...requestInfo,
|
|
109
|
+
parameters: {
|
|
110
|
+
...requestInfo.parameters,
|
|
111
|
+
[securityParameters.in]: {
|
|
112
|
+
[securityParameters.name]: securityParameters.value,
|
|
113
|
+
...requestInfo.parameters?.[securityParameters.in],
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
function setSecurityQueryDefaults(queryClient, queryKey, refreshInterval) {
|
|
119
|
+
// set staleTime and gcTime based on token lifetime
|
|
120
|
+
queryClient.setQueryDefaults(queryKey, {
|
|
121
|
+
// stale the token 90% before it expires
|
|
122
|
+
staleTime: isFinite(refreshInterval)
|
|
123
|
+
? refreshInterval * 0.9
|
|
124
|
+
: refreshInterval,
|
|
125
|
+
// garbage collect the token after it expires
|
|
126
|
+
gcTime: isFinite(refreshInterval) ? refreshInterval : refreshInterval,
|
|
127
|
+
// refetch the token 80% before it expires
|
|
128
|
+
refetchInterval: isFinite(refreshInterval)
|
|
129
|
+
? refreshInterval * 0.8
|
|
130
|
+
: refreshInterval,
|
|
131
|
+
// always refetch the token, even in the background
|
|
132
|
+
refetchIntervalInBackground: true,
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
function createSecureRequestParameters(securityResult) {
|
|
136
|
+
if (typeof securityResult === 'string' || 'token' in securityResult) {
|
|
137
|
+
return {
|
|
138
|
+
in: 'header',
|
|
139
|
+
name: 'Authorization',
|
|
140
|
+
value: `Bearer ${typeof securityResult === 'string' ? securityResult : securityResult.token}`,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
if ('credentials' in securityResult) {
|
|
144
|
+
return {
|
|
145
|
+
in: 'header',
|
|
146
|
+
name: 'Authorization',
|
|
147
|
+
value: `Basic ${securityResult.credentials}`,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
if ('in' in securityResult && securityResult.in !== 'cookie') {
|
|
151
|
+
return securityResult;
|
|
152
|
+
}
|
|
153
|
+
throw new Error('Security scheme must be a string, an object with a token property, an object with a credentials property, or an object with an in property that is not equal to "cookie".');
|
|
154
|
+
}
|
|
155
|
+
function getSecurityRefreshInterval(securityResult) {
|
|
156
|
+
if (typeof securityResult === 'string') {
|
|
157
|
+
return getJwtTokenRefreshInterval(securityResult);
|
|
158
|
+
}
|
|
159
|
+
if ('refreshInterval' in securityResult) {
|
|
160
|
+
return securityResult.refreshInterval;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* @internal
|
|
165
|
+
*/
|
|
166
|
+
export function shallowEqualObjects(newObj, prevObj) {
|
|
167
|
+
if (typeof newObj !== typeof prevObj)
|
|
168
|
+
return false;
|
|
169
|
+
if (typeof newObj === 'string')
|
|
170
|
+
return newObj === prevObj;
|
|
171
|
+
if (typeof newObj !== 'object' ||
|
|
172
|
+
newObj === null ||
|
|
173
|
+
typeof prevObj !== 'object' ||
|
|
174
|
+
prevObj === null ||
|
|
175
|
+
prevObj === undefined)
|
|
176
|
+
return false;
|
|
177
|
+
if (Object.keys(newObj).length !== Object.keys(prevObj).length)
|
|
178
|
+
return false;
|
|
179
|
+
for (const key in newObj) {
|
|
180
|
+
if (!Object.prototype.hasOwnProperty.call(newObj, key))
|
|
181
|
+
return false;
|
|
182
|
+
if (newObj[key] !== prevObj[key])
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
return true;
|
|
186
|
+
}
|
|
187
|
+
//# sourceMappingURL=Unstable_QraftSecureRequestFn.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Unstable_QraftSecureRequestFn.js","sourceRoot":"","sources":["../src/Unstable_QraftSecureRequestFn.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAa,SAAS,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAE/E,OAAO,EACL,WAAW,EAGX,UAAU,GACX,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AA0BtD,MAAM,UAAU,oBAAoB,CAAC,EACnC,QAAQ,EACR,SAAS,EACT,eAAe,EACf,WAAW,EAAE,aAAa,GACA;IAC1B,MAAM,WAAW,GAAG,OAAO,CACzB,GAAG,EAAE,CACH,aAAa;QACb,IAAI,WAAW,CAAC;YACd,cAAc,EA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?: string;
|
|
6
|
+
alg?: string;
|
|
7
|
+
kid?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface JwtPayload {
|
|
10
|
+
iss?: string;
|
|
11
|
+
sub?: string;
|
|
12
|
+
aud?: string[] | string;
|
|
13
|
+
exp?: number;
|
|
14
|
+
nbf?: number;
|
|
15
|
+
iat?: number;
|
|
16
|
+
jti?: string;
|
|
17
|
+
}
|
|
18
|
+
export declare class InvalidTokenError extends Error {
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Decode a base64 string to a UTF-8 string.
|
|
22
|
+
* Could be used in Node.js or in the browser.
|
|
23
|
+
*/
|
|
24
|
+
export declare function b64DecodeUnicode(data: string): string;
|
|
25
|
+
export declare function jwtDecode<T = JwtHeader>(token: string, options: JwtDecodeOptions & {
|
|
26
|
+
header: true;
|
|
27
|
+
}): T;
|
|
28
|
+
export declare function jwtDecode<T = JwtPayload>(token: string, options?: JwtDecodeOptions): T;
|
|
29
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/jwt-decode/index.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,gBAAgB;IAC/B,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED,MAAM,WAAW,SAAS;IACxB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,UAAU;IACzB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC;IACxB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,qBAAa,iBAAkB,SAAQ,KAAK;CAAG;AAI/C;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAKrD;AAwBD,wBAAgB,SAAS,CAAC,CAAC,GAAG,SAAS,EACrC,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,gBAAgB,GAAG;IAAE,MAAM,EAAE,IAAI,CAAA;CAAE,GAC3C,CAAC,CAAC;AACL,wBAAgB,SAAS,CAAC,CAAC,GAAG,UAAU,EACtC,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE,gBAAgB,GACzB,CAAC,CAAC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export class InvalidTokenError extends Error {
|
|
2
|
+
}
|
|
3
|
+
InvalidTokenError.prototype.name = 'InvalidTokenError';
|
|
4
|
+
/**
|
|
5
|
+
* Decode a base64 string to a UTF-8 string.
|
|
6
|
+
* Could be used in Node.js or in the browser.
|
|
7
|
+
*/
|
|
8
|
+
export function b64DecodeUnicode(data) {
|
|
9
|
+
if (typeof atob === 'function')
|
|
10
|
+
return decodeURIComponent(Array.from(atob(data), byteToPercent).join(''));
|
|
11
|
+
return Buffer.from(data, 'base64').toString('utf-8');
|
|
12
|
+
}
|
|
13
|
+
function base64UrlDecode(str) {
|
|
14
|
+
let output = str.replace(/-/g, '+').replace(/_/g, '/');
|
|
15
|
+
switch (output.length % 4) {
|
|
16
|
+
case 0:
|
|
17
|
+
break;
|
|
18
|
+
case 2:
|
|
19
|
+
output += '==';
|
|
20
|
+
break;
|
|
21
|
+
case 3:
|
|
22
|
+
output += '=';
|
|
23
|
+
break;
|
|
24
|
+
default:
|
|
25
|
+
throw new Error('base64 string is not of the correct length');
|
|
26
|
+
}
|
|
27
|
+
try {
|
|
28
|
+
return b64DecodeUnicode(output);
|
|
29
|
+
}
|
|
30
|
+
catch (err) {
|
|
31
|
+
return atob(output);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export function jwtDecode(token, options) {
|
|
35
|
+
if (typeof token !== 'string') {
|
|
36
|
+
throw new InvalidTokenError('Invalid token specified: must be a string');
|
|
37
|
+
}
|
|
38
|
+
options ||= {};
|
|
39
|
+
const pos = options.header === true ? 0 : 1;
|
|
40
|
+
const part = token.split('.')[pos];
|
|
41
|
+
if (typeof part !== 'string') {
|
|
42
|
+
throw new InvalidTokenError(`Invalid token specified: missing part #${pos + 1}`);
|
|
43
|
+
}
|
|
44
|
+
let decoded;
|
|
45
|
+
try {
|
|
46
|
+
decoded = base64UrlDecode(part);
|
|
47
|
+
}
|
|
48
|
+
catch (e) {
|
|
49
|
+
throw new InvalidTokenError(`Invalid token specified: invalid base64 for part #${pos + 1} (${e.message})`);
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
return JSON.parse(decoded);
|
|
53
|
+
}
|
|
54
|
+
catch (e) {
|
|
55
|
+
throw new InvalidTokenError(`Invalid token specified: invalid json for part #${pos + 1} (${e.message})`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function byteToPercent(b) {
|
|
59
|
+
return `%${`00${b.charCodeAt(0).toString(16)}`.slice(-2)}`;
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/jwt-decode/index.ts"],"names":[],"mappings":"AAoBA,MAAM,OAAO,iBAAkB,SAAQ,KAAK;CAAG;AAE/C,iBAAiB,CAAC,SAAS,CAAC,IAAI,GAAG,mBAAmB,CAAC;AAEvD;;;GAGG;AACH,MAAM,UAAU,gBAAgB,CAAC,IAAY;IAC3C,IAAI,OAAO,IAAI,KAAK,UAAU;QAC5B,OAAO,kBAAkB,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,aAAa,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC;IAE5E,OAAO,MAAM,C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