Skip to content

Commit 55581a6

Browse files
committed
Update ramp-plugin-migration-guide
1 parent f59d0c0 commit 55581a6

File tree

2 files changed

+122
-2
lines changed

2 files changed

+122
-2
lines changed

AGENTS.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,5 @@ The following documentation files provide detailed guidance for specific areas o
101101

102102
### `docs/ramp-plugin-migration-guide.md`
103103

104-
**When to read**: Before migrating ramp plugins from legacy provider architecture to new ramp plugin architecture
105-
**Summary**: Comprehensive migration guide for removing FiatPluginUi abstraction and using direct API imports. Covers migration of toasts, modals, navigation, permissions (with important boolean logic inversion note), and wallet operations with before/after code examples. Essential for converting legacy fiat providers to new ramp plugins.
104+
**When to read**: Before migrating ramp plugins from legacy provider architecture to new ramp plugin architecture or when creating new ramp plugins
105+
**Summary**: Comprehensive migration guide for removing FiatPluginUi abstraction and using direct API imports. Covers migration of toasts, modals, navigation, permissions (with important boolean logic inversion note), wallet operations, and environment configuration requirements. Includes detailed steps for creating init options cleaners, validating plugin initialization, and registering plugins in envConfig. Essential for converting legacy fiat providers to new ramp plugins and ensuring proper type safety.

docs/ramp-plugin-migration-guide.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,126 @@ export interface RampApproveQuoteParams {
198198
5. **Testing**: Test all flows thoroughly after migration, especially permission flows
199199
6. **Type safety**: Use proper TypeScript types for all imported functions
200200

201+
## Environment Configuration
202+
203+
When creating a new ramp plugin, you must properly configure the plugin's initialization options to ensure type safety and centralized configuration management. This involves three steps:
204+
205+
### 1. Create Init Options Cleaner
206+
207+
In your plugin's types file (e.g., `moonpayRampTypes.ts`), create an `asInitOptions` cleaner that validates the structure of your plugin's initialization options:
208+
209+
```typescript
210+
// Init options cleaner for moonpay ramp plugin
211+
export const asInitOptions = asString // For simple API key
212+
213+
// For more complex init options:
214+
export const asInitOptions = asObject({
215+
apiKey: asString,
216+
environment: asOptional(asValue('production', 'sandbox'), 'production'),
217+
webhookUrl: asOptional(asString)
218+
})
219+
```
220+
221+
The cleaner should match the expected initialization options structure for your specific plugin.
222+
223+
### 2. Use Cleaner in Plugin Factory
224+
225+
In your plugin factory function (e.g., `moonpayRampPlugin.ts`), import and use the cleaner to validate the `initOptions` parameter:
226+
227+
```typescript
228+
import { asInitOptions } from './moonpayRampTypes'
229+
230+
export const moonpayRampPlugin: RampPluginFactory = {
231+
pluginId: 'moonpay',
232+
storeId: 'com.moonpay',
233+
displayName: 'Moonpay',
234+
235+
create: async (params: RampPluginFactoryParams): Promise<RampPlugin> => {
236+
const { initOptions } = params
237+
238+
// Validate and extract init options
239+
const apiKey = asInitOptions(initOptions)
240+
241+
// Use the validated options in your plugin
242+
// ...
243+
}
244+
}
245+
```
246+
247+
### 3. Register in envConfig
248+
249+
Add an entry to `RAMP_PLUGIN_INITS` in `src/envConfig.ts` that uses your cleaner. Import with an alias to avoid naming conflicts:
250+
251+
```typescript
252+
import { asInitOptions as asMoonpayInitOptions } from './plugins/ramps/moonpay/moonpayRampTypes'
253+
import { asInitOptions as asPaybisInitOptions } from './plugins/ramps/paybis/paybisRampTypes'
254+
255+
export const ENV_CONFIG = makeConfig(
256+
asObject({
257+
// ... other config
258+
RAMP_PLUGIN_INITS: asOptional(
259+
asObject<Record<string, unknown>>({
260+
moonpay: asOptional(asMoonpayInitOptions),
261+
paybis: asOptional(asPaybisInitOptions),
262+
// Add your plugin here with its cleaner
263+
})
264+
)
265+
})
266+
)
267+
```
268+
269+
### Why This Is Required
270+
271+
- **Type Safety**: Ensures initialization options are properly typed and validated at runtime
272+
- **Centralized Configuration**: All plugin configurations are managed in one place (`envConfig.ts`)
273+
- **Environment Management**: Allows different configurations for development, staging, and production
274+
- **Error Prevention**: Catches configuration errors early with clear error messages
275+
276+
### Complete Example: Moonpay Plugin
277+
278+
Here's how the Moonpay plugin implements environment configuration:
279+
280+
**1. In `moonpayRampTypes.ts`:**
281+
```typescript
282+
import { asString } from 'cleaners'
283+
284+
// Simple string cleaner for API key
285+
export const asInitOptions = asString
286+
```
287+
288+
**2. In `moonpayRampPlugin.ts`:**
289+
```typescript
290+
import { asInitOptions } from './moonpayRampTypes'
291+
292+
export const moonpayRampPlugin: RampPluginFactory = {
293+
// ... plugin metadata
294+
295+
create: async (params: RampPluginFactoryParams): Promise<RampPlugin> => {
296+
const { initOptions } = params
297+
const apiKey = asInitOptions(initOptions)
298+
299+
// Now apiKey is guaranteed to be a string
300+
const client = new MoonpayClient({ apiKey })
301+
// ...
302+
}
303+
}
304+
```
305+
306+
**3. In `envConfig.ts`:**
307+
```typescript
308+
import { asInitOptions as asMoonpayInitOptions } from './plugins/ramps/moonpay/moonpayRampTypes'
309+
310+
// In the ENV_CONFIG:
311+
RAMP_PLUGIN_INITS: asOptional(
312+
asObject<Record<string, unknown>>({
313+
moonpay: asOptional(asMoonpayInitOptions),
314+
// Other plugins...
315+
})
316+
)
317+
```
318+
319+
This setup ensures that when the app loads plugin configurations from environment variables or config files, they are properly validated before being passed to the plugin factories.
320+
201321
## Complete Migration Example
202322

203323
Here's a comparison showing a typical permission request migration:

0 commit comments

Comments
 (0)