Skip to content

Commit d6beaaa

Browse files
committed
Add docs for useSecret and useSecretItem hooks
Add comprehensive JSDoc to useSecret and useSecretItem to clarify parameters, return shapes, and behavior. Highlights: useSecret now documents that it combines a read subscription with saveSecret/deleteSecret that refresh the cache on success and that mutation helpers return a discriminated HookMutationResult (do not throw). useSecretItem docs explain includeValue:false (metadata-only, avoids iOS biometric prompt), that NotFoundError is surfaced as data:null, and include usage examples and @see references. No functional changes — only documentation improvements.
1 parent 0beacd7 commit d6beaaa

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

src/hooks/useSecret.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,32 @@ const normalizeMutationOptions = (
4545
/**
4646
* Maintains a secure item while exposing imperative helpers to mutate or refresh it.
4747
*
48+
* Combines a read subscription (via {@link useSecretItem}) with `saveSecret` / `deleteSecret`
49+
* helpers that automatically refresh the cached entry on success.
50+
*
51+
* @param key - Identifier of the secret to track. Changing the key triggers a fresh fetch.
52+
* @param options - Storage scoping plus hook flags (`skip`, `includeValue`).
53+
* @returns A {@link UseSecretResult} with `data`/`error`/`isLoading`/`isPending` state and the
54+
* `saveSecret`, `deleteSecret`, `refetch` helpers.
55+
*
56+
* @remarks
57+
* - Mutation helpers never throw \u2014 they resolve with a {@link HookMutationResult} discriminated
58+
* union. Branch with `if (!result.success)`.
59+
* - If you only need read access, prefer {@link useSecretItem} \u2014 lighter result shape.
60+
*
4861
* @example
4962
* ```tsx
50-
* const secret = useSecret('refreshToken', { service: 'com.example.session' })
63+
* const { data, isLoading, saveSecret, deleteSecret } = useSecret('refreshToken', {
64+
* service: 'com.example.session',
65+
* })
66+
*
67+
* await saveSecret(nextToken)
68+
* await deleteSecret()
5169
* ```
70+
*
71+
* @see {@link useSecretItem}
72+
* @see {@link setItem}
73+
* @see {@link deleteItem}
5274
*/
5375
export function useSecret(
5476
key: string,

src/hooks/useSecretItem.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,31 @@ export interface UseSecretItemResult extends AsyncState<SensitiveInfoItem> {
2626

2727
/**
2828
* Fetches a single entry from the secure store and keeps the result in sync with the component.
29+
*
30+
* @param key - Identifier of the entry to fetch. Changing the key triggers a fresh request.
31+
* @param options - Storage scoping plus hook-only flags (`includeValue`, `skip`).
32+
* @returns A {@link UseSecretItemResult} with `data`/`error`/`isLoading`/`isPending` state and a
33+
* `refetch` helper.
34+
*
35+
* @remarks
36+
* - Pass `includeValue: false` to fetch metadata only \u2014 cheaper, and on iOS this avoids the
37+
* biometric prompt. Use {@link useSecret} instead when you also need mutation helpers.
38+
* - The hook absorbs `NotFoundError` from the underlying {@link getItem} call and surfaces it as
39+
* `data: null` \u2014 every other error appears in `error` as a {@link HookError}.
40+
*
41+
* @example
42+
* ```tsx
43+
* const { data, isLoading, refetch } = useSecretItem('session-token', {
44+
* service: 'com.example.auth',
45+
* })
46+
*
47+
* if (isLoading) return <Spinner />
48+
* if (!data) return <SignInScreen />
49+
* return <Dashboard token={data.value!} backend={data.metadata.backend} />
50+
* ```
51+
*
52+
* @see {@link getItem}
53+
* @see {@link useSecret}
2954
*/
3055
export function useSecretItem(
3156
key: string,

0 commit comments

Comments
 (0)