File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -94,6 +94,12 @@ describe('Mailchimp request bodies', () => {
9494 expect ( bodyOf ( mailchimpAddMemberTagsTool , { ...params , tags : '2024' } ) ) . toEqual ( {
9595 tags : [ { name : '2024' , status : 'active' } ] ,
9696 } )
97+ expect ( bodyOf ( mailchimpAddMemberTagsTool , { ...params , tags : '1e3' } ) ) . toEqual ( {
98+ tags : [ { name : '1e3' , status : 'active' } ] ,
99+ } )
100+ expect ( ( ) =>
101+ bodyOf ( mailchimpAddMemberTagsTool , { ...params , tags : '[{"name":"VIP"}' } )
102+ ) . toThrow ( )
97103 expect ( ( ) => bodyOf ( mailchimpAddMemberTagsTool , { ...params , tags : '[]' } ) ) . toThrow ( )
98104 } )
99105
Original file line number Diff line number Diff line change @@ -389,19 +389,24 @@ export function mailchimpMemberSegment(value: string): string {
389389
390390/**
391391 * Normalizes the tags input for the member tags endpoint, which requires `{ name, status }`
392- * objects. Accepts a JSON array of tag objects or names, or a comma-separated list of names;
393- * a tag without a status gets `defaultStatus`.
392+ * objects. Accepts a JSON array (or object) of tag objects or names, or a comma-separated list of
393+ * names kept as text; a tag without a status gets `defaultStatus`.
394394 */
395395export function buildMemberTags (
396396 raw : unknown ,
397397 defaultStatus : 'active' | 'inactive'
398398) : Array < { name : string ; status : string } > {
399399 let parsed : unknown = raw
400400 if ( typeof raw === 'string' ) {
401- try {
402- parsed = JSON . parse ( raw )
403- } catch {
404- parsed = raw . split ( ',' )
401+ const trimmed = raw . trim ( )
402+ if ( trimmed . startsWith ( '[' ) || trimmed . startsWith ( '{' ) ) {
403+ try {
404+ parsed = JSON . parse ( trimmed )
405+ } catch {
406+ throw new Error ( 'Tags look like JSON but could not be parsed' )
407+ }
408+ } else {
409+ parsed = trimmed . split ( ',' )
405410 }
406411 }
407412 const items = Array . isArray ( parsed ) ? parsed : [ parsed ]
You can’t perform that action at this time.
0 commit comments