@sudobility/consumables_client 0.0.3 → 0.0.4
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/CLAUDE.md +35 -0
- package/package.json +3 -3
package/CLAUDE.md
CHANGED
|
@@ -67,3 +67,38 @@ bun run typecheck # TypeScript check
|
|
|
67
67
|
- useConsumableProducts(offeringId) — available packages
|
|
68
68
|
- usePurchaseCredits() — purchase flow
|
|
69
69
|
- usePurchaseHistory() / useUsageHistory() — audit trails
|
|
70
|
+
|
|
71
|
+
## Related Projects
|
|
72
|
+
|
|
73
|
+
- **consumables_pages** (`@sudobility/consumables_pages`) — UI components for the credits store and history pages. Depends on this package for hooks and data.
|
|
74
|
+
- **consumables_service** (`@sudobility/consumables_service`) — Backend counterpart that manages balances, purchases, and usage in the database. This client calls its API endpoints.
|
|
75
|
+
|
|
76
|
+
Dependency direction: `consumables_pages` --> `consumables_client` --> `consumables_service` (via HTTP)
|
|
77
|
+
|
|
78
|
+
## Coding Patterns
|
|
79
|
+
|
|
80
|
+
- **Singleton pattern (no React Context)**: The library uses a module-level singleton (`initializeConsumables()`) instead of React Context. All hooks read from this singleton. Never introduce a Context provider.
|
|
81
|
+
- **Adapter pattern for RevenueCat**: Platform-specific SDK logic lives in adapters that implement `ConsumablesAdapter`. To support a new platform, add a new adapter file -- do not modify existing adapters.
|
|
82
|
+
- **Event-driven balance updates**: Balance changes are broadcast via `onConsumablesBalanceChange()` listeners. Hooks subscribe to these events. When modifying balance-related logic, always emit the balance change event so subscribers stay in sync.
|
|
83
|
+
- **Lazy-loaded adapters**: Adapters dynamically import their underlying SDK (`@revenuecat/purchases-js` or `react-native-purchases`) to keep initial bundle size small.
|
|
84
|
+
|
|
85
|
+
## Gotchas
|
|
86
|
+
|
|
87
|
+
- **Adapters lazy-load the SDK**: The RevenueCat SDK is imported dynamically inside the adapter, not at module level. This reduces bundle size but means the adapter is not ready synchronously -- always `await` initialization.
|
|
88
|
+
- **Singleton must be initialized before hooks work**: Calling `useBalance()` or any hook before `initializeConsumables()` will throw or return undefined. Ensure initialization happens at app startup (e.g., in a root layout or entry file).
|
|
89
|
+
- **Offerings cache survives user change, but balance cache does not**: When `setConsumablesUserId()` is called, the balance is refetched for the new user, but cached offerings (credit packages) are kept because they are not user-specific. Do not clear offerings on user change.
|
|
90
|
+
- **Two adapters, one interface**: Web and React Native adapters have different underlying SDKs with different APIs. Always code against `ConsumablesAdapter`, never against a specific SDK directly.
|
|
91
|
+
|
|
92
|
+
## Testing
|
|
93
|
+
|
|
94
|
+
- Run tests: `bun test` (uses vitest)
|
|
95
|
+
- Tests use **mocked adapters** -- they do not call real RevenueCat or backend APIs.
|
|
96
|
+
- When adding new functionality, write tests using the mock adapter pattern found in existing test files.
|
|
97
|
+
- Test hooks by verifying they respond correctly to singleton events (balance changes, purchase completions).
|
|
98
|
+
|
|
99
|
+
## Publishing
|
|
100
|
+
|
|
101
|
+
- Package: `@sudobility/consumables_client` (public on npm)
|
|
102
|
+
- Build before publish: `bun run build` produces ESM output in `dist/`
|
|
103
|
+
- Bump version in `package.json`, then `npm publish --access public`
|
|
104
|
+
- Consumers (e.g., `consumables_pages`) should be tested against the new version before publishing downstream
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sudobility/consumables_client",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "Cross-platform consumable credits client with RevenueCat adapter pattern",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"prepublishOnly": "bun run build"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
|
-
"@sudobility/types": "^1.9.
|
|
32
|
+
"@sudobility/types": "^1.9.52",
|
|
33
33
|
"react": "^18.0.0 || ^19.0.0",
|
|
34
34
|
"@revenuecat/purchases-js": "^1.0.0",
|
|
35
35
|
"react-native-purchases": ">=7.0.0"
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"@eslint/js": "^10.0.1",
|
|
47
47
|
"@revenuecat/purchases-js": "^1.1.3",
|
|
48
|
-
"@sudobility/types": "^1.9.
|
|
48
|
+
"@sudobility/types": "^1.9.52",
|
|
49
49
|
"@types/bun": "^1.2.8",
|
|
50
50
|
"@types/node": "^22.0.0",
|
|
51
51
|
"@types/react": "^19.2.5",
|