@@ -6,8 +6,9 @@ import * as path from 'path'
6
6
import { autoPrompt } from '../lib/prompt'
7
7
import { loadDestination } from '../lib/destinations'
8
8
import type { JSONSchema7 } from 'json-schema'
9
- import { DestinationDefinition } from '@segment/actions-core'
10
-
9
+ import { DestinationDefinition } from '../lib/destinations'
10
+ import { BrowserDestinationDefinition } from '@segment/destinations-manifest'
11
+ import type { DestinationDefinition as CloudModeDestinationDefinition , InputField } from '@segment/actions-core'
11
12
export default class GenerateTestPayload extends Command {
12
13
private spinner : ora . Ora = ora ( )
13
14
@@ -135,12 +136,19 @@ export default class GenerateTestPayload extends Command {
135
136
this . spinner . start ( `Generating test payload for action: ${ actionSlug } ` )
136
137
137
138
try {
138
- // Generate sample settings based on destination settings schema
139
- const settings = this . generateSampleFromSchema ( destination . settings || { } )
139
+ let settings : unknown
140
+ if ( ( destination as BrowserDestinationDefinition ) . mode == 'device' ) {
141
+ // Generate sample settings based on destination settings schema
142
+ const destinationSettings = ( destination as BrowserDestinationDefinition ) . settings
143
+ settings = this . generateSampleFromSchema ( destinationSettings || { } )
144
+ } else if ( ( destination as CloudModeDestinationDefinition ) . mode == 'cloud' ) {
145
+ const destinationSettings = ( destination as CloudModeDestinationDefinition ) . authentication ?. fields
146
+ settings = this . generateSampleFromSchema ( destinationSettings || { } )
147
+ }
140
148
141
149
// Generate sample mapping based on action fields
142
- const mapping = { }
143
- const fields = action . fields || { }
150
+ const mapping = { } as Record < string , any >
151
+ const fields = ( action . fields || { } ) as Record < string , InputField >
144
152
145
153
for ( const [ fieldKey , field ] of Object . entries ( fields ) ) {
146
154
if ( field . default ) {
@@ -167,9 +175,6 @@ export default class GenerateTestPayload extends Command {
167
175
this . log ( chalk . yellow ( `curl -X POST http://localhost:${ port } /${ actionSlug } \\` ) )
168
176
this . log ( chalk . yellow ( ` -H "Content-Type: application/json" \\` ) )
169
177
this . log ( chalk . yellow ( ` -d '${ JSON . stringify ( sampleRequest ) . replace ( / ' / g, "\\'" ) } '` ) )
170
- this . log ( chalk . green ( `\n# Pretty version (save to a file and use with curl -d "@payload.json"):` ) )
171
- this . log ( chalk . white ( JSON . stringify ( sampleRequest , null , 2 ) ) )
172
- this . log ( `\n${ chalk . grey ( '------------------------------------------------------' ) } \n` )
173
178
} catch ( error ) {
174
179
this . spinner . fail ( `Failed to generate payload for ${ actionSlug } : ${ ( error as Error ) . message } ` )
175
180
}
@@ -237,26 +242,30 @@ export default class GenerateTestPayload extends Command {
237
242
}
238
243
239
244
generateSamplePayloadFromMapping ( mapping : Record < string , any > ) : Record < string , any > {
240
- const payload : Record < string , any > = { }
241
-
242
- // Basic sample with common fields
243
- payload . userId = 'user123'
244
- payload . anonymousId = 'anon456'
245
- payload . event = 'Example Event'
246
- payload . type = 'track'
247
- payload . timestamp = new Date ( ) . toISOString ( )
248
- payload . properties = { }
249
- payload . context = {
250
- ip : '127.0.0.1' ,
251
- userAgent : 'Mozilla/5.0' ,
252
- page : {
253
- path : '/' ,
254
- url : 'https://example.com/' ,
255
- referrer : '' ,
256
- title : 'Example Page'
245
+ const payload : Record < string , any > = {
246
+ userId : 'user123' ,
247
+ anonymousId : 'anon456' ,
248
+ event : 'Example Event' ,
249
+ type : 'track' ,
250
+ timestamp : new Date ( ) . toISOString ( ) ,
251
+ properties : { } ,
252
+ context : {
253
+ ip : '127.0.0.1' ,
254
+ userAgent : 'Mozilla/5.0' ,
255
+ page : {
256
+ path : '/' ,
257
+ url : 'https://example.com/' ,
258
+ referrer : '' ,
259
+ title : 'Example Page'
260
+ }
257
261
}
258
262
}
259
263
264
+ // Add properties based on mapping
265
+ for ( const [ key , value ] of Object . entries ( mapping ) ) {
266
+ payload . properties [ key ] = value
267
+ }
268
+
260
269
// Add properties based on mapping
261
270
for ( const [ key , value ] of Object . entries ( mapping ) ) {
262
271
if ( typeof value === 'string' && value . startsWith ( '$.' ) ) {
0 commit comments