1 import { API_CODES } from '../../constants';
2 import type { Api } from '../../interfaces';
3 import type { GetCanonicalAddressesApiResponse } from '../../interfaces/calendar';
4 import type { SimpleMap } from '../../interfaces/utils';
5 import { getCanonicalAddresses } from '../addresses';
7 export const getCanonicalEmailMap = async (emails: string[] = [], api: Api) => {
8 const map: SimpleMap<string> = {};
10 const encodedEmails = emails.map((email) => encodeURIComponent(email));
11 const { Responses, Code } = await api<GetCanonicalAddressesApiResponse>(getCanonicalAddresses(encodedEmails));
12 if (Code !== API_CODES.GLOBAL_SUCCESS) {
13 throw new Error('Canonicalize operation failed');
15 Responses.forEach(({ Email, Response: { Code, CanonicalEmail } }) => {
16 if (Code !== API_CODES.SINGLE_SUCCESS) {
17 throw new Error('Canonicalize operation failed');
19 map[Email] = CanonicalEmail;