Skip to content

Commit 3dc9dfc

Browse files
committed
Add JSDoc to security hooks
Add comprehensive JSDoc comments to useHasSecret, useKeyRotation, useSecureOperation, and useSecurityAvailability. The updates document params, return types, remarks, examples, and related links; clarify behaviors such as avoiding biometric prompts when only checking existence, lazy vs eager key rotation and its biometric implications, execute() swallowing errors and exposing AuthenticationCanceledError as a cause, and caching/refetch semantics for security capability queries. Improves developer guidance and API discoverability.
1 parent d6beaaa commit 3dc9dfc

4 files changed

Lines changed: 80 additions & 1 deletion

File tree

src/hooks/useHasSecret.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,26 @@ export interface UseHasSecretResult extends AsyncState<boolean> {
1717

1818
/**
1919
* Checks if a secure item exists without fetching its payload.
20+
*
21+
* @param key - Identifier to look up. Changing the key triggers a fresh check.
22+
* @param options - Storage scoping plus a `skip` flag.
23+
* @returns A {@link UseHasSecretResult} where `data` is the boolean existence flag and `refetch`
24+
* re-runs the check on demand.
25+
*
26+
* @remarks Prefer this over {@link useSecret} / {@link useSecretItem} when you only need to know
27+
* whether a key exists \u2014 it never decrypts and never triggers biometric prompts.
28+
*
29+
* @example
30+
* ```tsx
31+
* const { data: hasToken, isLoading } = useHasSecret('session-token', {
32+
* service: 'com.example.auth',
33+
* })
34+
*
35+
* if (isLoading) return null
36+
* return hasToken ? <Dashboard /> : <Onboarding />
37+
* ```
38+
*
39+
* @see {@link hasItem}
2040
*/
2141
export function useHasSecret(
2242
key: string,

src/hooks/useKeyRotation.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,30 @@ export interface UseKeyRotationResult {
4545
/**
4646
* Provides a minimal wrapper around {@link rotateKeys} / {@link getKeyVersion} with loading,
4747
* result, and error state wired up for UI consumption.
48+
*
49+
* @param options - Storage scoping plus a `reEncryptEagerly` default for the `rotate()` helper.
50+
* @returns A {@link UseKeyRotationResult} with `lastResult`, `error`, `isRotating`, and the
51+
* imperative `rotate` / `readVersion` helpers.
52+
*
53+
* @remarks
54+
* - `rotate({ reEncryptEagerly: true })` may trigger one biometric prompt **per protected entry**.
55+
* Prefer the default lazy rotation unless you have a compliance reason to migrate ciphertext
56+
* immediately.
57+
* - `rotate()` never throws \u2014 it resolves with a {@link HookMutationResult}.
58+
*
59+
* @example
60+
* ```tsx
61+
* const { rotate, readVersion, isRotating, lastResult } = useKeyRotation({
62+
* service: 'com.example.auth',
63+
* })
64+
*
65+
* await rotate() // lazy: returns immediately
66+
* await rotate({ reEncryptEagerly: true }) // eager: re-encrypts every entry now
67+
* const version = await readVersion()
68+
* ```
69+
*
70+
* @see {@link rotateKeys}
71+
* @see {@link getKeyVersion}
4872
*/
4973
export function useKeyRotation(
5074
options?: UseKeyRotationOptions

src/hooks/useSecureOperation.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,23 @@ export interface UseSecureOperationResult extends VoidAsyncState {
1313
/**
1414
* Wraps a single asynchronous procedure (such as clearing a service) with loading and error state.
1515
*
16+
* @returns A {@link UseSecureOperationResult} with `error`/`isLoading`/`isPending` flags and an
17+
* `execute(operation)` helper that runs the supplied async callback while tracking lifecycle.
18+
*
19+
* @remarks
20+
* - `execute()` swallows the underlying throw and surfaces the failure as `error` (a typed
21+
* {@link HookError}). It never re-throws.
22+
* - User-cancelled biometric prompts surface as a `HookError` whose `cause` is an
23+
* {@link AuthenticationCanceledError} \u2014 inspect `.cause` if you need to differentiate.
24+
*
1625
* @example
1726
* ```tsx
1827
* const secureLogout = useSecureOperation()
1928
*
20-
* const handleLogout = () => secureLogout.execute(() => SensitiveInfo.clearService({ service: 'auth' }))
29+
* const handleLogout = () =>
30+
* secureLogout.execute(() => clearService({ service: 'com.example.auth' }))
31+
*
32+
* if (secureLogout.error) toast(secureLogout.error.message)
2133
* ```
2234
*/
2335
export function useSecureOperation(): UseSecureOperationResult {

src/hooks/useSecurityAvailability.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,29 @@ export interface UseSecurityAvailabilityResult
1111

1212
/**
1313
* Queries which security primitives are available on the current device and caches the outcome.
14+
*
15+
* @returns A {@link UseSecurityAvailabilityResult} with `data` (the latest snapshot),
16+
* `error`/`isLoading`/`isPending` flags, and a `refetch` helper that bypasses the cache.
17+
*
18+
* @remarks
19+
* - The hook caches the first successful response per component instance \u2014 subsequent renders
20+
* reuse the cached value without hitting the native module.
21+
* - `refetch()` forces a fresh native call \u2014 use it after the user changes biometric enrollment
22+
* in system settings.
23+
* - On error, the previously cached `data` is preserved so you can render fallback UI without
24+
* losing capability info.
25+
*
26+
* @example
27+
* ```tsx
28+
* const { data: caps, isLoading } = useSecurityAvailability()
29+
*
30+
* if (isLoading || !caps) return null
31+
* return caps.biometry
32+
* ? <EnableFaceIdToggle />
33+
* : <Text>Biometrics unavailable on this device.</Text>
34+
* ```
35+
*
36+
* @see {@link getSupportedSecurityLevels}
1437
*/
1538
export function useSecurityAvailability(): UseSecurityAvailabilityResult {
1639
const cacheRef = useRef<SecurityAvailability | null>(null)

0 commit comments

Comments
 (0)