Skip to content

Commit 5ea897e

Browse files
authored
feat: add types for getting and setting contact property values (#730)
1 parent e3a9da7 commit 5ea897e

File tree

5 files changed

+119
-2
lines changed

5 files changed

+119
-2
lines changed

src/contacts/contacts.spec.ts

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ describe('Contacts', () => {
3131
it('creates a contact', async () => {
3232
const payload: CreateContactOptions = {
3333
34-
audienceId: '3d4a472d-bc6d-4dd2-aa9d-d3d50ce87222',
34+
properties: {
35+
country: 'Canada',
36+
edition: 1,
37+
},
3538
};
3639
const response: CreateContactResponseSuccess = {
3740
object: 'contact',
@@ -368,6 +371,7 @@ describe('Contacts', () => {
368371
last_name: '',
369372
created_at: '2024-01-16T18:12:26.514Z',
370373
unsubscribed: false,
374+
properties: {},
371375
};
372376

373377
fetchMock.mockOnce(JSON.stringify(response), {
@@ -393,6 +397,7 @@ describe('Contacts', () => {
393397
"id": "fd61172c-cafc-40f5-b049-b45947779a29",
394398
"last_name": "",
395399
"object": "contact",
400+
"properties": {},
396401
"unsubscribed": false,
397402
},
398403
"error": null,
@@ -412,6 +417,7 @@ describe('Contacts', () => {
412417
last_name: '',
413418
created_at: '2024-01-16T18:12:26.514Z',
414419
unsubscribed: false,
420+
properties: {},
415421
};
416422

417423
fetchMock.mockOnce(JSON.stringify(response), {
@@ -437,6 +443,7 @@ describe('Contacts', () => {
437443
"id": "fd61172c-cafc-40f5-b049-b45947779a29",
438444
"last_name": "",
439445
"object": "contact",
446+
"properties": {},
440447
"unsubscribed": false,
441448
},
442449
"error": null,
@@ -457,6 +464,16 @@ describe('Contacts', () => {
457464
last_name: '',
458465
created_at: '2024-01-16T18:12:26.514Z',
459466
unsubscribed: false,
467+
properties: {
468+
country: {
469+
type: 'string',
470+
value: 'Canada',
471+
},
472+
edition: {
473+
type: 'number',
474+
value: 1,
475+
},
476+
},
460477
};
461478

462479
fetchMock.mockOnce(JSON.stringify(response), {
@@ -481,6 +498,16 @@ describe('Contacts', () => {
481498
"id": "fd61172c-cafc-40f5-b049-b45947779a29",
482499
"last_name": "",
483500
"object": "contact",
501+
"properties": {
502+
"country": {
503+
"type": "string",
504+
"value": "Canada",
505+
},
506+
"edition": {
507+
"type": "number",
508+
"value": 1,
509+
},
510+
},
484511
"unsubscribed": false,
485512
},
486513
"error": null,
@@ -500,6 +527,16 @@ describe('Contacts', () => {
500527
last_name: '',
501528
created_at: '2024-01-16T18:12:26.514Z',
502529
unsubscribed: false,
530+
properties: {
531+
country: {
532+
type: 'string',
533+
value: 'Canada',
534+
},
535+
edition: {
536+
type: 'number',
537+
value: 1,
538+
},
539+
},
503540
};
504541

505542
fetchMock.mockOnce(JSON.stringify(response), {
@@ -524,6 +561,16 @@ describe('Contacts', () => {
524561
"id": "fd61172c-cafc-40f5-b049-b45947779a29",
525562
"last_name": "",
526563
"object": "contact",
564+
"properties": {
565+
"country": {
566+
"type": "string",
567+
"value": "Canada",
568+
},
569+
"edition": {
570+
"type": "number",
571+
"value": 1,
572+
},
573+
},
527574
"unsubscribed": false,
528575
},
529576
"error": null,
@@ -541,6 +588,16 @@ describe('Contacts', () => {
541588
first_name: '',
542589
last_name: '',
543590
created_at: '2024-01-16T18:12:26.514Z',
591+
properties: {
592+
country: {
593+
type: 'string',
594+
value: 'Canada',
595+
},
596+
edition: {
597+
type: 'number',
598+
value: 1,
599+
},
600+
},
544601
unsubscribed: false,
545602
};
546603

@@ -563,6 +620,16 @@ describe('Contacts', () => {
563620
"id": "fd61172c-cafc-40f5-b049-b45947779a29",
564621
"last_name": "",
565622
"object": "contact",
623+
"properties": {
624+
"country": {
625+
"type": "string",
626+
"value": "Canada",
627+
},
628+
"edition": {
629+
"type": "number",
630+
"value": 1,
631+
},
632+
},
566633
"unsubscribed": false,
567634
},
568635
"error": null,
@@ -589,6 +656,16 @@ describe('Contacts', () => {
589656
first_name: '',
590657
last_name: '',
591658
created_at: '2024-01-16T18:12:26.514Z',
659+
properties: {
660+
country: {
661+
type: 'string',
662+
value: 'Canada',
663+
},
664+
edition: {
665+
type: 'number',
666+
value: 1,
667+
},
668+
},
592669
unsubscribed: false,
593670
};
594671

@@ -611,6 +688,16 @@ describe('Contacts', () => {
611688
"id": "fd61172c-cafc-40f5-b049-b45947779a29",
612689
"last_name": "",
613690
"object": "contact",
691+
"properties": {
692+
"country": {
693+
"type": "string",
694+
"value": "Canada",
695+
},
696+
"edition": {
697+
"type": "number",
698+
"value": 1,
699+
},
700+
},
614701
"unsubscribed": false,
615702
},
616703
"error": null,
@@ -635,8 +722,11 @@ describe('Contacts', () => {
635722
it('updates a contact', async () => {
636723
const payload: UpdateContactOptions = {
637724
id: '3d4a472d-bc6d-4dd2-aa9d-d3d50ce87223',
638-
audienceId: '3d4a472d-bc6d-4dd2-aa9d-d3d50ce87222',
639725
firstName: 'Bu',
726+
properties: {
727+
country: 'Canada',
728+
edition: 1,
729+
},
640730
};
641731
const response = {
642732
id: '3d4a472d-bc6d-4dd2-aa9d-d3d50ce87223',

src/contacts/contacts.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export class Contacts {
5151
email: payload.email,
5252
first_name: payload.firstName,
5353
last_name: payload.lastName,
54+
properties: payload.properties,
5455
},
5556
options,
5657
);
@@ -137,6 +138,7 @@ export class Contacts {
137138
unsubscribed: options.unsubscribed,
138139
first_name: options.firstName,
139140
last_name: options.lastName,
141+
properties: options.properties,
140142
},
141143
);
142144
return data;

src/contacts/interfaces/create-contact-options.interface.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@ import type { PostOptions } from '../../common/interfaces';
22
import type { Response } from '../../interfaces';
33
import type { Contact } from './contact';
44

5+
interface CreateContactPropertiesOptions {
6+
[key: string]: string | number | null;
7+
}
8+
59
export interface CreateContactOptions {
610
audienceId?: string;
711
email: string;
812
unsubscribed?: boolean;
913
firstName?: string;
1014
lastName?: string;
15+
properties?: CreateContactPropertiesOptions;
1116
}
1217

1318
export interface CreateContactRequestOptions extends PostOptions {}

src/contacts/interfaces/get-contact.interface.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,27 @@ export type GetContactOptions =
77
audienceId?: string;
88
} & SelectingField);
99

10+
type ContactPropertyValue =
11+
| {
12+
type: 'string';
13+
value: string;
14+
}
15+
| {
16+
type: 'number';
17+
value: number;
18+
};
19+
20+
interface ContactProperties {
21+
[key: string]: ContactPropertyValue;
22+
}
23+
1024
export interface GetContactResponseSuccess
1125
extends Pick<
1226
Contact,
1327
'id' | 'email' | 'created_at' | 'first_name' | 'last_name' | 'unsubscribed'
1428
> {
1529
object: 'contact';
30+
properties: ContactProperties;
1631
}
1732

1833
export type GetContactResponse = Response<GetContactResponseSuccess>;

src/contacts/interfaces/update-contact.interface.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import type { Response } from '../../interfaces';
22
import type { Contact, SelectingField } from './contact';
33

4+
interface UpdateContactPropertiesOptions {
5+
[key: string]: string | number | null;
6+
}
7+
48
export type UpdateContactOptions = {
59
audienceId?: string;
610
unsubscribed?: boolean;
711
firstName?: string;
812
lastName?: string;
13+
properties?: UpdateContactPropertiesOptions;
914
} & SelectingField;
1015

1116
export type UpdateContactResponseSuccess = Pick<Contact, 'id'> & {

0 commit comments

Comments
 (0)