@@ -6,8 +6,9 @@ import * as path from 'path'
66import { autoPrompt } from '../lib/prompt'
77import { loadDestination } from '../lib/destinations'
88import 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'
1112export default class GenerateTestPayload extends Command {
1213 private spinner : ora . Ora = ora ( )
1314
@@ -135,12 +136,19 @@ export default class GenerateTestPayload extends Command {
135136 this . spinner . start ( `Generating test payload for action: ${ actionSlug } ` )
136137
137138 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+ }
140148
141149 // 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 >
144152
145153 for ( const [ fieldKey , field ] of Object . entries ( fields ) ) {
146154 if ( field . default ) {
@@ -167,9 +175,6 @@ export default class GenerateTestPayload extends Command {
167175 this . log ( chalk . yellow ( `curl -X POST http://localhost:${ port } /${ actionSlug } \\` ) )
168176 this . log ( chalk . yellow ( ` -H "Content-Type: application/json" \\` ) )
169177 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` )
173178 } catch ( error ) {
174179 this . spinner . fail ( `Failed to generate payload for ${ actionSlug } : ${ ( error as Error ) . message } ` )
175180 }
@@ -237,26 +242,30 @@ export default class GenerateTestPayload extends Command {
237242 }
238243
239244 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+ }
257261 }
258262 }
259263
264+ // Add properties based on mapping
265+ for ( const [ key , value ] of Object . entries ( mapping ) ) {
266+ payload . properties [ key ] = value
267+ }
268+
260269 // Add properties based on mapping
261270 for ( const [ key , value ] of Object . entries ( mapping ) ) {
262271 if ( typeof value === 'string' && value . startsWith ( '$.' ) ) {
0 commit comments