@ahoo-wang/fetcher-react 3.4.6 → 3.5.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.
@@ -0,0 +1,80 @@
1
+ import { UseSecurityReturn } from './useSecurity';
2
+ import { ReactNode } from 'react';
3
+ import { TokenStorage } from '@ahoo-wang/fetcher-cosec';
4
+ /**
5
+ * Type alias for the security context value, which is the same as the return type of useSecurity hook.
6
+ * Provides access to current user information, authentication status, and authentication methods.
7
+ */
8
+ export type SecurityContext = UseSecurityReturn;
9
+ /**
10
+ * React context for managing authentication state across the component tree.
11
+ * This context provides a way to share authentication state and methods between components
12
+ * without prop drilling. It should be used with SecurityProvider to wrap the application.
13
+ */
14
+ export declare const SecurityContext: import('react').Context<UseSecurityReturn | undefined>;
15
+ /**
16
+ * Props for the SecurityProvider component.
17
+ */
18
+ export interface SecurityContextOptions {
19
+ /**
20
+ * The token storage instance used to manage authentication tokens.
21
+ * This should be a valid TokenStorage implementation that handles token persistence and retrieval.
22
+ */
23
+ tokenStorage: TokenStorage;
24
+ /**
25
+ * The child components that will have access to the security context.
26
+ */
27
+ children: ReactNode;
28
+ }
29
+ /**
30
+ * Provider component that supplies authentication state and methods to its child components.
31
+ * This component wraps the application or a portion of it to provide access to authentication
32
+ * functionality through the useSecurityContext hook.
33
+ *
34
+ * @param tokenStorage - The token storage instance for managing authentication tokens.
35
+ * @param children - The child components that will have access to the security context.
36
+ * @returns A React element that provides the security context to its children.
37
+ * @throws {Error} May throw errors if tokenStorage operations fail during initialization.
38
+ * @example
39
+ * ```tsx
40
+ * import { SecurityProvider } from '@ahoo-wang/fetcher-react/cosec';
41
+ * import { tokenStorage } from './tokenStorage';
42
+ *
43
+ * function App() {
44
+ * return (
45
+ * <SecurityProvider tokenStorage={tokenStorage}>
46
+ * <MyApp />
47
+ * </SecurityProvider>
48
+ * );
49
+ * }
50
+ * ```
51
+ */
52
+ export declare function SecurityProvider({ tokenStorage, children, }: SecurityContextOptions): import("react/jsx-runtime").JSX.Element;
53
+ /**
54
+ * Hook to access the security context within components wrapped by SecurityProvider.
55
+ * This hook provides reactive access to authentication state and methods throughout the component tree.
56
+ *
57
+ * @returns The security context containing currentUser, authenticated status, signIn, and signOut methods.
58
+ * @throws {Error} Throws an error if used outside of a SecurityProvider component.
59
+ * @example
60
+ * ```tsx
61
+ * import { useSecurityContext } from '@ahoo-wang/fetcher-react/cosec';
62
+ *
63
+ * function UserProfile() {
64
+ * const { currentUser, authenticated, signOut } = useSecurityContext();
65
+ *
66
+ * if (!authenticated) {
67
+ * return <div>Please sign in</div>;
68
+ * }
69
+ *
70
+ * return (
71
+ * <div>
72
+ * <p>Welcome, {currentUser?.sub}!</p>
73
+ * <button onClick={signOut}>Sign Out</button>
74
+ * </div>
75
+ * );
76
+ * }
77
+ * ```
78
+ */
79
+ export declare function useSecurityContext(): SecurityContext;
80
+ //# sourceMappingURL=SecurityContext.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SecurityContext.d.ts","sourceRoot":"","sources":["../../src/cosec/SecurityContext.tsx"],"names":[],"mappings":"AAaA,OAAO,EAAe,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAC/D,OAAO,EAAiB,SAAS,EAAc,MAAM,OAAO,CAAC;AAC7D,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAExD;;;GAGG;AACH,MAAM,MAAM,eAAe,GAAG,iBAAiB,CAAC;AAEhD;;;;GAIG;AACH,eAAO,MAAM,eAAe,wDAE3B,CAAC;AAEF;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC;;;OAGG;IACH,YAAY,EAAE,YAAY,CAAC;IAE3B;;OAEG;IACH,QAAQ,EAAE,SAAS,CAAC;CACrB;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,gBAAgB,CAAC,EAC/B,YAAY,EACZ,QAAQ,GACT,EAAE,sBAAsB,2CAOxB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,kBAAkB,IAAI,eAAe,CAQpD"}
@@ -0,0 +1,3 @@
1
+ export * from './SecurityContext';
2
+ export * from './useSecurity';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/cosec/index.ts"],"names":[],"mappings":"AAaA,cAAc,mBAAmB,CAAC;AAClC,cAAc,eAAe,CAAC"}
@@ -0,0 +1,65 @@
1
+ import { TokenStorage, CompositeToken, CoSecJwtPayload } from '@ahoo-wang/fetcher-cosec';
2
+ /**
3
+ * Return type for the useSecurity hook.
4
+ */
5
+ export interface UseSecurityReturn {
6
+ /**
7
+ * The current authenticated user's JWT payload, or null if not authenticated.
8
+ * Contains user information extracted from the access token.
9
+ */
10
+ currentUser: CoSecJwtPayload | null;
11
+ /**
12
+ * Boolean indicating whether the user is currently authenticated.
13
+ * True if a valid token exists and the user is signed in, false otherwise.
14
+ */
15
+ authenticated: boolean;
16
+ /**
17
+ * Function to sign in with a composite token.
18
+ * @param compositeToken - The composite token containing access and refresh tokens.
19
+ */
20
+ signIn: (compositeToken: CompositeToken) => void;
21
+ /**
22
+ * Function to sign out the current user.
23
+ */
24
+ signOut: () => void;
25
+ }
26
+ /**
27
+ * Hook for managing authentication state and operations using CoSec tokens.
28
+ *
29
+ * This hook provides reactive access to the current user information, authentication status,
30
+ * and methods to sign in and sign out. It integrates with the TokenStorage to persist tokens
31
+ * and updates the state reactively when tokens change.
32
+ *
33
+ * @param tokenStorage - The token storage instance used to manage authentication tokens.
34
+ * This should be a valid TokenStorage implementation that handles
35
+ * token persistence and retrieval.
36
+ * @returns An object containing:
37
+ * - currentUser: The current authenticated user's JWT payload, or null if not authenticated.
38
+ * - authenticated: Boolean indicating whether the user is currently authenticated.
39
+ * - signIn: Function to authenticate with a composite token.
40
+ * - signOut: Function to sign out the current user.
41
+ * @throws {Error} May throw errors if tokenStorage operations fail, such as invalid tokens
42
+ * or storage access issues (implementation dependent).
43
+ * @example
44
+ * ```typescript
45
+ * import { useSecurity } from '@ahoo-wang/fetcher-react/cosec';
46
+ * import { tokenStorage } from './tokenStorage';
47
+ *
48
+ * function App() {
49
+ * const { currentUser, authenticated, signIn, signOut } = useSecurity(tokenStorage);
50
+ *
51
+ * if (!authenticated) {
52
+ * return <button onClick={() => signIn(compositeToken)}>Sign In</button>;
53
+ * }
54
+ *
55
+ * return (
56
+ * <div>
57
+ * <p>Welcome, {currentUser?.sub}!</p>
58
+ * <button onClick={signOut}>Sign Out</button>
59
+ * </div>
60
+ * );
61
+ * }
62
+ * ```
63
+ */
64
+ export declare function useSecurity(tokenStorage: TokenStorage): UseSecurityReturn;
65
+ //# sourceMappingURL=useSecurity.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useSecurity.d.ts","sourceRoot":"","sources":["../../src/cosec/useSecurity.ts"],"names":[],"mappings":"AAeA,OAAO,EACL,YAAY,EACZ,cAAc,EACd,eAAe,EAChB,MAAM,0BAA0B,CAAC;AAElC;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC;;;OAGG;IACH,WAAW,EAAE,eAAe,GAAG,IAAI,CAAC;IAEpC;;;OAGG;IACH,aAAa,EAAE,OAAO,CAAC;IAEvB;;;OAGG;IACH,MAAM,EAAE,CAAC,cAAc,EAAE,cAAc,KAAK,IAAI,CAAC;IAEjD;;OAEG;IACH,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,wBAAgB,WAAW,CAAC,YAAY,EAAE,YAAY,GAAG,iBAAiB,CAezE"}
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './core';
2
+ export * from './cosec';
2
3
  export * from './storage';
3
4
  export * from './fetcher';
4
5
  export * from './wow';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAaA,cAAc,QAAQ,CAAC;AACvB,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,OAAO,CAAC;AACtB,cAAc,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAaA,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,OAAO,CAAC;AACtB,cAAc,YAAY,CAAC"}