Skip to content

Commit 9c82e81

Browse files
authored
Merge pull request #472 from mCodex/feat/addHookSupport
Feat/add hook support
2 parents 78fdf42 + 96af4f5 commit 9c82e81

30 files changed

Lines changed: 2768 additions & 807 deletions

.github/workflows/android-build.yml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ on:
1414
- 'nitrogen/generated/android/**'
1515
- 'cpp/**'
1616
- 'android/**'
17-
- '**/bun.lock'
17+
- '**/yarn.lock'
1818
- '**/react-native.config.js'
1919
- '**/nitro.json'
2020
pull_request:
@@ -25,7 +25,7 @@ on:
2525
- '**/nitrogen/generated/android/**'
2626
- 'cpp/**'
2727
- 'android/**'
28-
- '**/bun.lock'
28+
- '**/yarn.lock'
2929
- '**/react-native.config.js'
3030
- '**/nitro.json'
3131
workflow_dispatch:
@@ -44,14 +44,19 @@ jobs:
4444
arch: [new, old]
4545
steps:
4646
- uses: actions/checkout@v4
47-
- uses: oven-sh/setup-bun@v2
47+
- uses: actions/setup-node@v4
48+
with:
49+
node-version: '22'
50+
cache: 'yarn'
51+
52+
- name: Install dependencies (yarn)
53+
run: yarn install --frozen-lockfile
4854

49-
- name: Install dependencies (bun)
50-
run: bun install
55+
- name: Install example dependencies (yarn)
56+
run: cd example && yarn install --frozen-lockfile
5157

5258
- name: Generate Nitro modules (codegen)
53-
run: |
54-
npm run codegen
59+
run: yarn codegen
5560

5661
- name: Disable new architecture in gradle.properties
5762
if: matrix.arch == 'old'

.github/workflows/ios-build.yml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ on:
1717
- 'cpp/**'
1818
- 'ios/**'
1919
- '**/Podfile.lock'
20-
- '**/bun.lock'
20+
- '**/yarn.lock'
2121
- '**/*.podspec'
2222
- '**/react-native.config.js'
2323
- '**/nitro.json'
@@ -32,7 +32,7 @@ on:
3232
- 'cpp/**'
3333
- 'ios/**'
3434
- '**/Podfile.lock'
35-
- '**/bun.lock'
35+
- '**/yarn.lock'
3636
- '**/*.podspec'
3737
- '**/react-native.config.js'
3838
- '**/nitro.json'
@@ -55,18 +55,20 @@ jobs:
5555
arch: [new, old]
5656
steps:
5757
- uses: actions/checkout@v4
58-
- uses: oven-sh/setup-bun@v2
58+
- uses: actions/setup-node@v4
59+
with:
60+
node-version: '22'
61+
cache: 'yarn'
5962
- name: Setup Xcode
6063
uses: maxim-lobanov/setup-xcode@v1
6164
with:
6265
xcode-version: 16.4
6366

64-
- name: Install dependencies (bun)
65-
run: bun install
67+
- name: Install dependencies (yarn)
68+
run: yarn install --frozen-lockfile
6669

6770
- name: Generate Nitro modules (codegen)
68-
run: |
69-
npm run codegen
71+
run: yarn codegen
7072

7173
- name: Disable new architecture in Podfile
7274
if: matrix.arch == 'old'

.github/workflows/release.yml

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,17 @@ jobs:
2424
uses: actions/checkout@v4
2525
with:
2626
fetch-depth: 0
27-
- name: Setup Bun.js
28-
uses: oven-sh/setup-bun@v2
27+
- name: Setup Node.js
28+
uses: actions/setup-node@v4
2929
with:
30-
bun-version: latest
31-
- name: Cache bun dependencies
32-
id: bun-cache
33-
uses: actions/cache@v4
34-
with:
35-
path: ~/.bun/install/cache
36-
key: ${{ runner.os }}-bun-${{ hashFiles('**/bun.lockb') }}
37-
restore-keys: |
38-
${{ runner.os }}-bun-
30+
node-version: '22'
31+
cache: 'yarn'
3932

40-
- name: Install npm dependencies (bun)
41-
run: bun install
33+
- name: Install npm dependencies (yarn)
34+
run: yarn install --frozen-lockfile
4235

4336
- name: Build lib
44-
run: bun run build
37+
run: yarn build
4538

4639
- name: Release
4740
env:
@@ -52,4 +45,4 @@ jobs:
5245
GIT_AUTHOR_EMAIL: '${{ github.actor }}@users.noreply.github.com'
5346
GIT_COMMITTER_NAME: ${{ github.actor }}
5447
GIT_COMMITTER_EMAIL: '${{ github.actor }}@users.noreply.github.com'
55-
run: bun release
48+
run: yarn release

README.md

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,86 @@ No manual linking is required. Nitro handles platform registration via autolinki
101101
> [!TIP]
102102
> Use `includeValue: false` during reads when you only care about metadata—this keeps plaintext out of memory and speeds up list views.
103103
104-
## ⚡️ Quick start
104+
## ⚛️ React Hooks API (Recommended)
105+
106+
For a modern, reactive approach with automatic memory management and loading states, use the dedicated hooks:
107+
108+
```tsx
109+
import { Text, View, ActivityIndicator } from 'react-native'
110+
import {
111+
useSecureStorage,
112+
useSecurityAvailability,
113+
} from 'react-native-sensitive-info'
114+
115+
// Use hooks directly in any component - no provider needed!
116+
function YourComponent() {
117+
// Fetch and manage all secrets in a service (with CRUD)
118+
const {
119+
items,
120+
isLoading,
121+
error,
122+
saveSecret,
123+
removeSecret,
124+
} = useSecureStorage({ service: 'myapp', includeValues: true })
125+
126+
// Query device security capabilities (cached automatically)
127+
const { data: capabilities } = useSecurityAvailability()
128+
129+
if (isLoading) return <ActivityIndicator />
130+
if (error) return <Text>Error: {error.message}</Text>
131+
132+
return (
133+
<View>
134+
{items.map(item => (
135+
<Text key={item.key}>
136+
{item.key}: {item.value} ({item.metadata.securityLevel})
137+
</Text>
138+
))}
139+
<Text>
140+
Biometry available: {capabilities?.biometry ? 'Yes' : 'No'}
141+
</Text>
142+
</View>
143+
)
144+
}
145+
```
146+
147+
### Key hooks
148+
149+
| Hook | Use Case | Returns |
150+
| --- | --- | --- |
151+
| `useSecureStorage()` | Manage all secrets in a service (list, add, remove) | `{ items, isLoading, error, saveSecret, removeSecret, clearAll, refreshItems }` |
152+
| `useSecretItem()` | Fetch a single secret | `{ data, isLoading, error, refetch }` |
153+
| `useSecret()` | Single secret + mutations | `{ data, isLoading, error, saveSecret, deleteSecret, refetch }` |
154+
| `useHasSecret()` | Check if secret exists (lightweight) | `{ data (boolean), isLoading, error, refetch }` |
155+
| `useSecurityAvailability()` | Query device capabilities (cached) | `{ data, isLoading, error, refetch }` |
156+
157+
### Best practices
158+
159+
- **Memory leak prevention** — All hooks automatically cancel requests and clean up resources on unmount.
160+
- **Conditional fetching** — Use `skip: true` to prevent unnecessary operations:
161+
162+
```tsx
163+
const { data } = useSecretItem('token', { skip: !isAuthenticated })
164+
```
165+
166+
- **Optimize list views** — Fetch metadata only to avoid decryption overhead:
167+
168+
```tsx
169+
const { items } = useSecureStorage({ includeValues: false })
170+
```
171+
172+
- **Share capabilities** — Query independently and results are cached automatically:
173+
174+
```tsx
175+
// Each component queries independently (results cached automatically)
176+
const { data: capabilities1 } = useSecurityAvailability()
177+
const { data: capabilities2 } = useSecurityAvailability()
178+
// Same cached result, no duplicate native calls
179+
```
180+
181+
For comprehensive examples and advanced patterns, see [`HOOKS.md`](./HOOKS.md).
182+
183+
## Imperative API
105184

106185
```tsx
107186
import React, { useEffect } from 'react'
@@ -137,8 +216,6 @@ export function SecureTokenExample() {
137216
void SensitiveInfo.clearService({ service: 'auth' })
138217
```
139218

140-
## 📚 API reference
141-
142219
All functions live at the top level export and return Promises.
143220

144221
| Method | Signature | Description |
@@ -161,7 +238,7 @@ All functions live at the top level export and return Promises.
161238

162239
Android automatically enforces Class 3 biometrics whenever the hardware supports them, falling back to the strongest available authenticator on older devices.
163240

164-
See `src/views/sensitive-info.nitro.ts` for full TypeScript definitions.
241+
See `src/sensitive-info.nitro.ts` for full TypeScript definitions.
165242

166243
## 🔐 Access control & metadata
167244

babel.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module.exports = {
2+
plugins: ['babel-plugin-react-compiler'],
23
presets: ['module:@react-native/babel-preset'],
34
}

0 commit comments

Comments
 (0)