@@ -18,6 +18,19 @@ interface CTAParams {
1818 ref_style ?: string
1919}
2020
21+ const CTA_PARAM_KEYS : ( keyof CTAParams ) [ ] = [ 'ref_product' , 'ref_plan' , 'ref_type' , 'ref_style' ]
22+
23+ interface CTASchemaProperty {
24+ type : string
25+ name : string
26+ description : string
27+ enum : string [ ]
28+ }
29+
30+ type CTASchemaProperties = {
31+ [ K in keyof CTAParams ] -?: CTASchemaProperty
32+ }
33+
2134// Conversion mappings from old CTA format to new schema
2235const ctaToTypeMapping : Record < string , string > = {
2336 'GHEC trial' : 'trial' ,
@@ -153,27 +166,40 @@ function extractCTAParams(url: string): CTAParams {
153166 const urlObj = new URL ( url )
154167 const ctaParams : CTAParams = { }
155168 for ( const [ key , value ] of urlObj . searchParams . entries ( ) ) {
156- if ( key . startsWith ( 'ref_' ) ) {
157- ; ( ctaParams as any ) [ key ] = value
169+ if ( key . startsWith ( 'ref_' ) && CTA_PARAM_KEYS . includes ( key as keyof CTAParams ) ) {
170+ ctaParams [ key as keyof CTAParams ] = value
158171 }
159172 }
160173 return ctaParams
161174}
162175
176+ interface AjvErrorParams {
177+ missingProperty ?: string
178+ allowedValues ?: string [ ]
179+ additionalProperty ?: string
180+ }
181+
182+ interface AjvError {
183+ keyword : string
184+ instancePath : string
185+ message ?: string
186+ params : AjvErrorParams
187+ }
188+
163189// Process AJV validation errors into readable messages
164- function formatValidationErrors ( ctaParams : CTAParams , errors : any [ ] ) : string [ ] {
190+ function formatValidationErrors ( ctaParams : CTAParams , errors : AjvError [ ] ) : string [ ] {
165191 const errorMessages : string [ ] = [ ]
166192 for ( const error of errors ) {
167193 let message = ''
168194 if ( error . keyword === 'required' ) {
169- message = `Missing required parameter: ${ ( error . params as any ) ? .missingProperty } `
195+ message = `Missing required parameter: ${ error . params . missingProperty } `
170196 } else if ( error . keyword === 'enum' ) {
171197 const paramName = error . instancePath . substring ( 1 )
172198 const invalidValue = ctaParams [ paramName as keyof CTAParams ]
173- const allowedValues = ( error . params as any ) ? .allowedValues || [ ]
199+ const allowedValues = error . params . allowedValues || [ ]
174200 message = `Invalid value for ${ paramName } : "${ invalidValue } ". Valid values are: ${ allowedValues . join ( ', ' ) } `
175201 } else if ( error . keyword === 'additionalProperties' ) {
176- message = `Unexpected parameter: ${ ( error . params as any ) ? .additionalProperty } `
202+ message = `Unexpected parameter: ${ error . params . additionalProperty } `
177203 } else {
178204 message = `Validation error: ${ error . message } `
179205 }
@@ -191,7 +217,7 @@ function validateCTAParams(params: CTAParams): { isValid: boolean; errors: strin
191217 return { isValid : true , errors : [ ] }
192218 }
193219
194- const errors = formatValidationErrors ( params , ajvErrors )
220+ const errors = formatValidationErrors ( params , ajvErrors as unknown as AjvError [ ] )
195221 return {
196222 isValid : false ,
197223 errors,
@@ -382,12 +408,14 @@ async function interactiveBuilder(): Promise<void> {
382408
383409 // Required parameters
384410 console . log ( chalk . white ( `\nRequired parameters:` ) )
411+ const schemaProps = ctaSchema . properties as CTASchemaProperties
385412
386413 for ( const requiredParam of ctaSchema . required ) {
387- ; ( params as any ) [ requiredParam ] = await selectFromOptions (
414+ const paramKey = requiredParam as keyof CTAParams
415+ params [ paramKey ] = await selectFromOptions (
388416 requiredParam ,
389- ( ctaSchema . properties as any ) [ requiredParam ] . description ,
390- ( ctaSchema . properties as any ) [ requiredParam ] . enum ,
417+ schemaProps [ paramKey ] . description ,
418+ schemaProps [ paramKey ] . enum ,
391419 prompt ,
392420 )
393421 }
@@ -399,15 +427,16 @@ async function interactiveBuilder(): Promise<void> {
399427 const optionalProperties = allProperties . filter ( ( prop ) => ! ctaSchema . required . includes ( prop ) )
400428
401429 for ( const optionalParam of optionalProperties ) {
430+ const paramKey = optionalParam as keyof CTAParams
402431 const includeParam = await confirmChoice (
403- `Include ${ ( ctaSchema . properties as any ) [ optionalParam ] . name . toLowerCase ( ) } ?` ,
432+ `Include ${ schemaProps [ paramKey ] . name . toLowerCase ( ) } ?` ,
404433 prompt ,
405434 )
406435 if ( includeParam ) {
407- ; ( params as any ) [ optionalParam ] = await selectFromOptions (
436+ params [ paramKey ] = await selectFromOptions (
408437 optionalParam ,
409- ( ctaSchema . properties as any ) [ optionalParam ] . description ,
410- ( ctaSchema . properties as any ) [ optionalParam ] . enum ,
438+ schemaProps [ paramKey ] . description ,
439+ schemaProps [ paramKey ] . enum ,
411440 prompt ,
412441 )
413442 }
0 commit comments