@@ -11,7 +11,8 @@ import {
11
11
GlobalSetting ,
12
12
isDirective ,
13
13
DestinationDefinition as CloudModeDestinationDefinition ,
14
- InputField
14
+ InputField ,
15
+ AudienceDestinationDefinition
15
16
} from '@segment/actions-core'
16
17
import Chance from 'chance'
17
18
import { getRawKeys } from '@segment/actions-core/mapping-kit/value-keys'
@@ -132,7 +133,7 @@ export default class GenerateTestPayload extends Command {
132
133
133
134
if ( actionToGenerate === 'all' ) {
134
135
for ( const [ slug , action ] of actions ) {
135
- await this . generatePayloadForAction ( destination , slug , action , flags . port )
136
+ await this . generatePayloadForAction ( destination , slug , action )
136
137
}
137
138
} else {
138
139
const action = actions . find ( ( [ slug ] ) => slug === actionToGenerate )
@@ -142,13 +143,13 @@ export default class GenerateTestPayload extends Command {
142
143
return
143
144
}
144
145
145
- await this . generatePayloadForAction ( destination , action [ 0 ] , action [ 1 ] , flags . port )
146
+ await this . generatePayloadForAction ( destination , action [ 0 ] , action [ 1 ] )
146
147
}
147
148
148
149
this . log ( chalk . green ( `\nDone generating test payloads! 🎉` ) )
149
150
}
150
151
151
- async generatePayloadForAction ( destination : DestinationDefinition , actionSlug : string , action : any , port : number ) {
152
+ async generatePayloadForAction ( destination : DestinationDefinition , actionSlug : string , action : any ) {
152
153
this . spinner . start ( `Generating test payload for action: ${ actionSlug } ` )
153
154
154
155
try {
@@ -170,19 +171,32 @@ export default class GenerateTestPayload extends Command {
170
171
}
171
172
}
172
173
174
+ const audienceSettings = {
175
+ ...( destination as AudienceDestinationDefinition ) ?. audienceFields
176
+ }
177
+
173
178
// Generate sample mapping based on action fields
174
179
const mapping = { } as Record < string , any >
175
180
const fields = ( action . fields || { } ) as Record < string , InputField >
176
181
177
182
for ( const [ fieldKey , field ] of Object . entries ( fields ) ) {
178
183
if ( field . default ) {
179
184
mapping [ fieldKey ] = field . default
185
+ } else if ( field . choices ) {
186
+ // if choices is array of string, pick the first one
187
+ // if choices is array of {label: string, value: string}, then pick the value of the first one
188
+ mapping [ fieldKey ] = typeof field . choices [ 0 ] === 'string' ? field . choices [ 0 ] : field . choices [ 0 ] . value
180
189
}
181
190
}
182
191
183
192
// Generate sample payload based on the fields
184
193
const payload = this . generateSamplePayloadFromMapping ( mapping )
185
194
195
+ // if audience settings exist, add them to the payload
196
+ if ( audienceSettings ) {
197
+ set ( payload , 'context.personas.audience_settings' , this . generateSampleFromSchema ( audienceSettings || { } ) )
198
+ }
199
+
186
200
// Generate final sample request
187
201
const sampleRequest = {
188
202
settings,
@@ -194,7 +208,7 @@ export default class GenerateTestPayload extends Command {
194
208
195
209
// Print the curl command to the terminal
196
210
this . log ( chalk . cyan ( `\n# Test payload for ${ chalk . bold ( destination . name ) } - ${ chalk . bold ( actionSlug ) } ` ) )
197
- this . log ( chalk . yellow ( `curl -X POST http://localhost:${ port } /${ actionSlug } \\` ) )
211
+ this . log ( chalk . yellow ( `curl -X POST http://localhost:3000 /${ actionSlug } \\` ) )
198
212
this . log ( chalk . yellow ( ` -H "Content-Type: application/json" \\` ) )
199
213
this . log ( chalk . yellow ( ` -d '${ JSON . stringify ( sampleRequest ) . replace ( / ' / g, "\\'" ) } '` ) )
200
214
} catch ( error ) {
@@ -207,7 +221,7 @@ export default class GenerateTestPayload extends Command {
207
221
for ( const [ propName , setting ] of Object . entries ( schema ) ) {
208
222
if ( setting . default !== undefined ) {
209
223
result [ propName ] = setting . default
210
- } else if ( setting . required ) {
224
+ } else {
211
225
result [ propName ] = this . generatePlaceholderForSchema ( setting )
212
226
}
213
227
}
@@ -223,13 +237,13 @@ export default class GenerateTestPayload extends Command {
223
237
if ( schema . choices ) {
224
238
return schema . choices [ 0 ]
225
239
}
226
- return `YOUR_ ${ schema . label || 'VALUE' } `
240
+ return `< ${ schema . label || 'VALUE' } > `
227
241
case 'number' :
228
242
return 0
229
243
case 'boolean' :
230
244
return false
231
245
case 'password' :
232
- return `YOUR_ ${ schema . label || 'PASSWORD' } `
246
+ return `< ${ schema . label || 'PASSWORD' } > `
233
247
default :
234
248
return null
235
249
}
0 commit comments