UNPKG

4.16 kBJavaScriptView Raw
1// @flow
2
3import { NativeModules, Platform } from 'react-native';
4
5type FieldType =
6 | typeof PHONE_NUMBERS
7 | typeof EMAILS
8 | typeof ADDRESSES
9 | typeof IMAGE
10 | typeof THUMBNAIL
11 | typeof NOTE
12 | typeof BIRTHDAY
13 | typeof NON_GREGORIAN_BIRTHDAY
14 | typeof NAME_PREFIX
15 | typeof NAME_SUFFIX
16 | typeof PHONETIC_FIRST_NAME
17 | typeof PHONETIC_MIDDLE_NAME
18 | typeof PHONETIC_LAST_NAME
19 | typeof SOCIAL_PROFILES
20 | typeof IM_ADDRESSES
21 | typeof URLS
22 | typeof DATES
23 | typeof RELATIONSHIPS;
24
25type Options = {
26 id?: string,
27 pageSize?: number,
28 pageOffset?: number,
29 fields?: FieldType[],
30};
31
32type Contact = {
33 id: string,
34 contactType: string,
35 name: string,
36 firstName?: string,
37 middleName?: string,
38 lastName?: string,
39 previousLastName?: string,
40 namePrefix?: string,
41 nameSuffix?: string,
42 nickname?: string,
43 phoneticFirstName?: string,
44 phoneticMiddleName?: string,
45 phoneticLastName?: string,
46 birthday?: {
47 day?: number,
48 month?: number,
49 year?: number,
50 },
51 nonGregorianBirthday?: {
52 day?: number,
53 month?: number,
54 year?: number,
55 },
56 emails?: {
57 email?: string,
58 primary?: boolean,
59 label: string,
60 id: string,
61 }[],
62 phoneNumbers?: {
63 number?: string,
64 primary?: boolean,
65 digits?: string,
66 countryCode?: string,
67 label: string,
68 id: string,
69 }[],
70 addresses?: {
71 street?: string,
72 city?: string,
73 country?: string,
74 region?: string,
75 neighborhood?: string,
76 postalCode?: string,
77 poBox?: string,
78 isoCountryCode?: string,
79 label: string,
80 id: string,
81 }[],
82 socialProfiles?: {
83 service?: string,
84 localizedProfile?: string,
85 url?: string,
86 username?: string,
87 userId?: string,
88 label: string,
89 id: string,
90 }[],
91 instantMessageAddresses?: {
92 service?: string,
93 username?: string,
94 localizedService?: string,
95 label: string,
96 id: string,
97 }[],
98 urls?: {
99 label: string,
100 url?: string,
101 id: string,
102 }[],
103 company?: string,
104 jobTitle?: string,
105 department?: string,
106 imageAvailable?: boolean,
107 image?: {
108 uri?: string,
109 },
110 thumbnail?: {
111 uri?: string,
112 },
113 note?: string,
114 dates?: {
115 day?: number,
116 month?: number,
117 year?: number,
118 id: string,
119 label: string,
120 }[],
121 relationships?: {
122 label: string,
123 name?: string,
124 id: string,
125 }[],
126};
127
128type Response = {
129 data: Contact[],
130 total: number,
131 hasNextPage: boolean,
132 hasPreviousPage: boolean,
133};
134
135const DEFAULT_PAGE_SIZE = 100;
136
137export async function getContactsAsync(
138 { pageSize = DEFAULT_PAGE_SIZE, pageOffset = 0, fields = [] }: Options = {}
139): Promise<Response> {
140 if (Platform.OS === 'ios' && (fields.includes(IMAGE) || fields.includes(THUMBNAIL))) {
141 console.warn(
142 'Mind that fetching images for all contacts might be time and resource consuming. ' +
143 'Consider using getContactByIdAsync() to get data for a single contact.'
144 );
145 }
146 return await NativeModules.ExponentContacts.getContactsAsync({
147 pageSize,
148 pageOffset,
149 fields,
150 });
151}
152
153export async function getContactByIdAsync({ fields = [], id }: Options = {}): Promise<Response> {
154 if (id == null) {
155 throw new Error('Please pass an ID as a parameter');
156 } else {
157 return await NativeModules.ExponentContacts.getContactsAsync({
158 pageSize: 1,
159 pageOffset: 0,
160 fields,
161 id,
162 });
163 }
164}
165
166export const PHONE_NUMBERS = 'phoneNumbers';
167export const EMAILS = 'emails';
168export const ADDRESSES = 'addresses';
169export const IMAGE = 'image';
170export const THUMBNAIL = 'thumbnail';
171export const NOTE = 'note';
172export const BIRTHDAY = 'birthday';
173export const NON_GREGORIAN_BIRTHDAY = 'nonGregorianBirthday';
174export const NAME_PREFIX = 'namePrefix';
175export const NAME_SUFFIX = 'nameSuffix';
176export const PHONETIC_FIRST_NAME = 'phoneticFirstName';
177export const PHONETIC_MIDDLE_NAME = 'phoneticMiddleName';
178export const PHONETIC_LAST_NAME = 'phoneticLastName';
179export const SOCIAL_PROFILES = 'socialProfiles';
180export const IM_ADDRESSES = 'instantMessageAddresses';
181export const URLS = 'urlAddresses';
182export const DATES = 'dates';
183export const RELATIONSHIPS = 'relationships';