Repository navigation
feat(custom-provider): add custom providers support - #472
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
📝 WalkthroughWalkthroughAdds a ChangesCustom Provider Feature
Sequence Diagram(s)sequenceDiagram
participant App
participant AppKit
participant CustomProvidersManager
participant EventEmitter
participant useCustomProvider
App->>AppKit: registerProvider({ type: 'custom', ... })
AppKit->>CustomProvidersManager: registerProvider(input)
CustomProvidersManager->>EventEmitter: emit provider:registered { type: 'custom' }
rect rgba(100, 149, 237, 0.5)
note over useCustomProvider: React subscription via useSyncExternalStore
useCustomProvider->>AppKit: watchCustomProviders(appKit, { onChange })
EventEmitter-->>useCustomProvider: provider:registered (type='custom') → onChange()
useCustomProvider->>AppKit: getCustomProvider(appKit, { id })
AppKit->>CustomProvidersManager: getProvider(id)
CustomProvidersManager-->>useCustomProvider: T | undefined
end
useCustomProvider-->>App: renders provider-ready or not-registered UI
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@demo/examples/src/appkit/hooks/providers/providers.test.tsx`:
- Around line 9-18: The mockAppKit variable is declared with a broad any type
annotation, which violates coding guidelines for non-invalid-input test
scenarios. Replace the any type on the mockAppKit variable declaration with an
explicit type that matches the actual mock structure being set up in the
beforeEach hook. Define a proper interface or type that captures the shape of
the AppKit mock object to maintain type safety throughout the test.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 9a44f57d-b67f-4233-9747-307e467a6db6
📒 Files selected for processing (24)
demo/examples/src/appkit/actions/providers/get-custom-provider.tsdemo/examples/src/appkit/actions/providers/providers.test.tsdemo/examples/src/appkit/hooks/providers/providers.test.tsxdemo/examples/src/appkit/hooks/providers/use-custom-provider.tsxpackages/appkit-react/docs/hooks.mdpackages/appkit-react/src/features/providers/hooks/use-custom-provider.tspackages/appkit-react/src/features/providers/index.tspackages/appkit-react/src/index.tspackages/appkit/docs/actions.mdpackages/appkit/src/actions/index.tspackages/appkit/src/actions/providers/get-custom-provider.tspackages/appkit/src/actions/providers/watch-custom-providers.tspackages/appkit/src/core/app-kit/services/app-kit.tspackages/appkit/src/index.tspackages/appkit/src/providers/index.tspackages/appkit/src/types/provider.tspackages/walletkit/src/core/TonWalletKit.tspackages/walletkit/src/index.tspackages/walletkit/src/providers/CustomProvider.tspackages/walletkit/src/providers/CustomProvidersManager.spec.tspackages/walletkit/src/providers/CustomProvidersManager.tspackages/walletkit/src/providers/index.tstemplate/packages/appkit-react/docs/hooks.mdtemplate/packages/appkit/docs/actions.md
| /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
|
||
| import { render, screen, cleanup } from '@testing-library/react'; | ||
| import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; | ||
|
|
||
| import { createWrapper } from '../../../__tests__/test-utils'; | ||
| import { UseCustomProviderExample } from './use-custom-provider'; | ||
|
|
||
| describe('Provider Hooks Examples', () => { | ||
| let mockAppKit: any; |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Verify this test no longer uses explicit any or disables no-explicit-any
rg -n 'no-explicit-any|:\s*any\b' demo/examples/src/appkit/hooks/providers/providers.test.tsxRepository: ton-org/kit
Length of output: 141
🏁 Script executed:
cat -n demo/examples/src/appkit/hooks/providers/providers.test.tsxRepository: ton-org/kit
Length of output: 2137
Remove broad any from the test mock type
This test file uses any at line 18 without deliberately testing invalid input. The coding guidelines require replacing any with proper types in non-invalid-input test scenarios.
Replace the any type with an explicit mock shape based on the actual mockAppKit structure defined in beforeEach:
Suggested fix
-/* eslint-disable `@typescript-eslint/no-explicit-any` */
-
import { render, screen, cleanup } from '`@testing-library/react`';
-import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
+import { describe, it, expect, vi, beforeEach, afterEach, type Mock } from 'vitest';
import { createWrapper } from '../../../__tests__/test-utils';
import { UseCustomProviderExample } from './use-custom-provider';
describe('Provider Hooks Examples', () => {
+ type MockAppKit = {
+ emitter: { on: Mock; off: Mock };
+ connectors: never[];
+ customProvidersManager: { getProvider: Mock };
+ };
- let mockAppKit: any;
+ let mockAppKit: MockAppKit;📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| /* eslint-disable @typescript-eslint/no-explicit-any */ | |
| import { render, screen, cleanup } from '@testing-library/react'; | |
| import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; | |
| import { createWrapper } from '../../../__tests__/test-utils'; | |
| import { UseCustomProviderExample } from './use-custom-provider'; | |
| describe('Provider Hooks Examples', () => { | |
| let mockAppKit: any; | |
| import { render, screen, cleanup } from '`@testing-library/react`'; | |
| import { describe, it, expect, vi, beforeEach, afterEach, type Mock } from 'vitest'; | |
| import { createWrapper } from '../../../__tests__/test-utils'; | |
| import { UseCustomProviderExample } from './use-custom-provider'; | |
| describe('Provider Hooks Examples', () => { | |
| type MockAppKit = { | |
| emitter: { on: Mock; off: Mock }; | |
| connectors: never[]; | |
| customProvidersManager: { getProvider: Mock }; | |
| }; | |
| let mockAppKit: MockAppKit; |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@demo/examples/src/appkit/hooks/providers/providers.test.tsx` around lines 9 -
18, The mockAppKit variable is declared with a broad any type annotation, which
violates coding guidelines for non-invalid-input test scenarios. Replace the any
type on the mockAppKit variable declaration with an explicit type that matches
the actual mock structure being set up in the beforeEach hook. Define a proper
interface or type that captures the shape of the AppKit mock object to maintain
type safety throughout the test.
Source: Coding guidelines
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
packages/walletkit/src/providers/CustomProvidersManager.spec.ts (1)
36-87: ⚡ Quick winConsider adding a test case documenting type-mismatch behavior.
The test suite doesn't demonstrate what happens when a provider is retrieved with a mismatched generic type parameter (e.g., registering
TestProviderbut retrieving asDifferentProvider). While TypeScript won't prevent this at compile time, a test case would document the current behavior and serve as a reference for the limitation noted in the manager implementation.Example test:
it('returns a provider even when retrieved with a mismatched type parameter', () => { interface OtherProvider extends CustomProvider { otherMethod: () => void; } manager.registerProvider(makeProvider('test')); // This compiles but returns TestProvider, not OtherProvider const provider = manager.getProvider<OtherProvider>('test'); expect(provider).toBeDefined(); expect(provider?.providerId).toBe('test'); // provider.otherMethod() would fail at runtime });🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/walletkit/src/providers/CustomProvidersManager.spec.ts` around lines 36 - 87, Add a test case to the CustomProvidersManager describe block that documents the type-mismatch behavior when a provider is retrieved with a mismatched generic type parameter. Create a new test that registers a provider as TestProvider, retrieves it using a different generic type parameter (like OtherProvider extending CustomProvider), and verify that the provider is still returned with the correct providerId despite the type mismatch. This test should document that while the retrieval succeeds at runtime, the type parameter mismatch represents a potential type safety issue that TypeScript cannot prevent.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/walletkit/src/providers/CustomProvidersManager.ts`:
- Around line 54-55: The getProvider method in CustomProvidersManager uses an
unchecked cast to return a provider with the requested generic type T, which
allows runtime type mismatches. To fix this, either implement runtime validation
by storing type metadata alongside each provider in the providers map during
registerProvider and checking that metadata in getProvider before casting, or
add prominent documentation warning users that the method does not validate type
safety and that retrieving a provider with an incorrect generic type parameter
will cause runtime errors.
---
Nitpick comments:
In `@packages/walletkit/src/providers/CustomProvidersManager.spec.ts`:
- Around line 36-87: Add a test case to the CustomProvidersManager describe
block that documents the type-mismatch behavior when a provider is retrieved
with a mismatched generic type parameter. Create a new test that registers a
provider as TestProvider, retrieves it using a different generic type parameter
(like OtherProvider extending CustomProvider), and verify that the provider is
still returned with the correct providerId despite the type mismatch. This test
should document that while the retrieval succeeds at runtime, the type parameter
mismatch represents a potential type safety issue that TypeScript cannot
prevent.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: b16a141f-9b2e-40b2-988d-c6a84b91521d
📒 Files selected for processing (9)
.changeset/custom-providers.mddemo/examples/src/appkit/actions/providers/get-custom-provider.tsdemo/examples/src/appkit/hooks/providers/use-custom-provider.tsxpackages/appkit-react/docs/hooks.mdpackages/appkit-react/src/features/providers/hooks/use-custom-provider.tspackages/appkit/docs/actions.mdpackages/appkit/src/actions/providers/get-custom-provider.tspackages/walletkit/src/providers/CustomProvidersManager.spec.tspackages/walletkit/src/providers/CustomProvidersManager.ts
✅ Files skipped from review due to trivial changes (1)
- packages/appkit/docs/actions.md
🚧 Files skipped from review as they are similar to previous changes (1)
- demo/examples/src/appkit/hooks/providers/use-custom-provider.tsx
| getProvider<T extends CustomProvider>(providerId: string): T | undefined { | ||
| return this.providers.get(providerId) as T | undefined; |
There was a problem hiding this comment.
Runtime type safety is not enforced.
The unchecked cast as T | undefined allows callers to retrieve a provider using an incorrect generic type parameter, leading to runtime errors when methods are called. TypeScript will not catch this mismatch.
Example failure:
interface ProviderA extends CustomProvider {
methodA(): void;
}
interface ProviderB extends CustomProvider {
methodB(): void;
}
// Register ProviderA
manager.registerProvider<ProviderA>({
providerId: 'my-id',
type: 'custom',
methodA: () => {}
});
// Retrieve with wrong type - TypeScript allows this
const provider = manager.getProvider<ProviderB>('my-id');
provider?.methodB(); // Runtime error: methodB is not a functionConsider adding runtime validation (e.g., storing type metadata with each provider and checking it at retrieval) or prominently documenting this limitation.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@packages/walletkit/src/providers/CustomProvidersManager.ts` around lines 54 -
55, The getProvider method in CustomProvidersManager uses an unchecked cast to
return a provider with the requested generic type T, which allows runtime type
mismatches. To fix this, either implement runtime validation by storing type
metadata alongside each provider in the providers map during registerProvider
and checking that metadata in getProvider before casting, or add prominent
documentation warning users that the method does not validate type safety and
that retrieving a provider with an incorrect generic type parameter will cause
runtime errors.
Summary by CodeRabbit
customProviders.getCustomProviderandwatchCustomProvidersfor retrieving and reacting to custom provider registrations.useCustomProviderReact hook for accessing registered custom providers byid.getCustomProvider/useCustomProviderusage guidance.