import {z} from 'zod' export type GetApiV3ContactsArgs = {'page[number]'?: number | undefined, 'page[size]'?: number | undefined, sort?: string | undefined, 'fields[contact]'?: string | undefined, include?: string | undefined, 'filter[id]'?: string | undefined, 'filter[locale]'?: string | undefined, 'filter[email]'?: string | undefined, 'filter[is_company]'?: string | undefined, 'filter[title]'?: string | undefined, 'filter[first_name]'?: string | undefined, 'filter[last_name]'?: string | undefined, 'filter[company]'?: string | undefined, 'filter[vat_nr]'?: string | undefined, 'filter[tin]'?: string | undefined, 'filter[business_registration_number]'?: string | undefined, 'filter[nationality]'?: string | undefined, 'filter[date_of_birth]'?: string | undefined, 'filter[receive_newsletter]'?: string | undefined, 'filter[bookings_marketing_consent]'?: string | undefined, 'filter[bookings_relevant_marketing_consent_until]'?: string | undefined, 'filter[address_line_1]'?: string | undefined, 'filter[address_line_2]'?: string | undefined, 'filter[postalcode]'?: string | undefined, 'filter[city]'?: string | undefined, 'filter[country_code]'?: string | undefined, 'filter[address_subdivision]'?: string | undefined, 'filter[iban]'?: string | undefined, 'filter[bic]'?: string | undefined, 'filter[phone]'?: string | undefined, 'filter[mobile_phone]'?: string | undefined, 'filter[created_at]'?: string | undefined, 'filter[updated_at]'?: string | undefined, 'filter[labels]'?: string | undefined, 'Accept-Language'?: string | undefined}; export type GetApiV3ContactsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const contact = z.object({id: z.string().optional(), type: z.string().optional(), attributes: z.object({locale: z.string().optional(), email: z.string().optional(), is_company: z.boolean().optional(), title: z.enum(["mr", "ms", "family", "mrs", "miss", "mx", "dr", "prof", "rev", "sir", "dame", "lord", "lady"]).optional(), first_name: z.string().optional(), last_name: z.string().optional(), company: z.string().optional(), vat_nr: z.string().optional(), tin: z.string().optional(), business_registration_number: z.string().optional(), nationality: z.enum(["AD", "AE", "AF", "AG", "AI", "AL", "AM", "AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AX", "AZ", "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BL", "BM", "BN", "BO", "BQ", "BR", "BS", "BT", "BV", "BW", "BY", "BZ", "CA", "CC", "CD", "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR", "CU", "CV", "CW", "CX", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO", "DZ", "EC", "EE", "EG", "EH", "ER", "ES", "ET", "FI", "FJ", "FK", "FM", "FO", "FR", "GA", "GB", "GD", "GE", "GF", "GG", "GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GS", "GT", "GU", "GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU", "ID", "IE", "IL", "IM", "IN", "IO", "IQ", "IR", "IS", "IT", "JE", "JM", "JO", "JP", "KE", "KG", "KH", "KI", "KM", "KN", "KP", "KR", "KW", "KY", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", "LV", "LY", "MA", "MC", "MD", "ME", "MF", "MG", "MH", "MK", "ML", "MM", "MN", "MO", "MP", "MQ", "MR", "MS", "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA", "NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP", "NR", "NU", "NZ", "OM", "PA", "PE", "PF", "PG", "PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT", "PW", "PY", "QA", "RE", "RO", "RS", "RU", "RW", "SA", "SB", "SC", "SD", "SE", "SG", "SH", "SI", "SJ", "SK", "SL", "SM", "SN", "SO", "SR", "SS", "ST", "SV", "SX", "SY", "SZ", "TC", "TD", "TF", "TG", "TH", "TJ", "TK", "TL", "TM", "TN", "TO", "TR", "TT", "TV", "TW", "TZ", "UA", "UG", "UM", "US", "UY", "UZ", "VA", "VC", "VE", "VG", "VI", "VN", "VU", "WF", "WS", "YE", "YT", "ZA", "ZM", "ZW", "XK", "AN", "ZZ", "WW"]).optional(), date_of_birth: z.string().optional(), gender: z.enum(["male", "female", "other", "prefer_not_to_say"]).optional(), receive_newsletter: z.boolean().optional(), bookings_marketing_consent: z.boolean().optional(), bookings_relevant_marketing_consent_until: z.string().optional(), address: z.string().optional(), address_line_1: z.string().optional(), address_line_2: z.string().optional(), postalcode: z.string().optional(), city: z.string().optional(), country_code: z.enum(["AD", "AE", "AF", "AG", "AI", "AL", "AM", "AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AX", "AZ", "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BL", "BM", "BN", "BO", "BQ", "BR", "BS", "BT", "BV", "BW", "BY", "BZ", "CA", "CC", "CD", "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR", "CU", "CV", "CW", "CX", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO", "DZ", "EC", "EE", "EG", "EH", "ER", "ES", "ET", "FI", "FJ", "FK", "FM", "FO", "FR", "GA", "GB", "GD", "GE", "GF", "GG", "GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GS", "GT", "GU", "GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU", "ID", "IE", "IL", "IM", "IN", "IO", "IQ", "IR", "IS", "IT", "JE", "JM", "JO", "JP", "KE", "KG", "KH", "KI", "KM", "KN", "KP", "KR", "KW", "KY", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", "LV", "LY", "MA", "MC", "MD", "ME", "MF", "MG", "MH", "MK", "ML", "MM", "MN", "MO", "MP", "MQ", "MR", "MS", "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA", "NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP", "NR", "NU", "NZ", "OM", "PA", "PE", "PF", "PG", "PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT", "PW", "PY", "QA", "RE", "RO", "RS", "RU", "RW", "SA", "SB", "SC", "SD", "SE", "SG", "SH", "SI", "SJ", "SK", "SL", "SM", "SN", "SO", "SR", "SS", "ST", "SV", "SX", "SY", "SZ", "TC", "TD", "TF", "TG", "TH", "TJ", "TK", "TL", "TM", "TN", "TO", "TR", "TT", "TV", "TW", "TZ", "UA", "UG", "UM", "US", "UY", "UZ", "VA", "VC", "VE", "VG", "VI", "VN", "VU", "WF", "WS", "YE", "YT", "ZA", "ZM", "ZW", "XK", "AN", "ZZ", "WW"]).optional(), address_subdivision: z.string().optional(), iban: z.string().optional(), bic: z.string().optional(), phone: z.string().optional(), mobile_phone: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}).optional(), links: z.object({url: z.string().optional()}).optional(), relationships: z.object({labels: z.object({data: z.array(z.object({id: z.string().optional(), type: z.string().optional(), meta: z.object({temp_id: z.string().optional(), method: z.string().optional()}).optional()})).optional()}).optional()}).optional(), meta: z.object({temp_id: z.string().optional()}).optional()}); export const label = z.object({id: z.string().optional(), type: z.string().optional(), attributes: z.object({name: z.string().optional(), background: z.string().optional(), foreground: z.string().optional()}).optional(), meta: z.object({temp_id: z.string().optional()}).optional()}); export const getApiV3ContactsResponse = z.object({data: z.array(contact).optional(), included: z.array(label).optional(), meta: z.object({pagination: z.object({total_records: z.number().int().optional(), page: z.number().int().optional(), size: z.number().int().optional()}).optional()}).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * GET contacts * * GET /v3/contacts * * Also accepts auth: OAuth2 */ export const getApiV3Contacts = async (args: GetApiV3ContactsArgs, options?: GetApiV3ContactsOptions) => { const {baseUrl = "https://api.bookingexperts.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (args['page[number]'] != null) query.append("page[number]", String(args['page[number]'])) if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (args.sort != null) query.append("sort", String(args.sort)) if (args['fields[contact]'] != null) query.append("fields[contact]", String(args['fields[contact]'])) if (args.include != null) query.append("include", String(args.include)) if (args['filter[id]'] != null) query.append("filter[id]", String(args['filter[id]'])) if (args['filter[locale]'] != null) query.append("filter[locale]", String(args['filter[locale]'])) if (args['filter[email]'] != null) query.append("filter[email]", String(args['filter[email]'])) if (args['filter[is_company]'] != null) query.append("filter[is_company]", String(args['filter[is_company]'])) if (args['filter[title]'] != null) query.append("filter[title]", String(args['filter[title]'])) if (args['filter[first_name]'] != null) query.append("filter[first_name]", String(args['filter[first_name]'])) if (args['filter[last_name]'] != null) query.append("filter[last_name]", String(args['filter[last_name]'])) if (args['filter[company]'] != null) query.append("filter[company]", String(args['filter[company]'])) if (args['filter[vat_nr]'] != null) query.append("filter[vat_nr]", String(args['filter[vat_nr]'])) if (args['filter[tin]'] != null) query.append("filter[tin]", String(args['filter[tin]'])) if (args['filter[business_registration_number]'] != null) query.append("filter[business_registration_number]", String(args['filter[business_registration_number]'])) if (args['filter[nationality]'] != null) query.append("filter[nationality]", String(args['filter[nationality]'])) if (args['filter[date_of_birth]'] != null) query.append("filter[date_of_birth]", String(args['filter[date_of_birth]'])) if (args['filter[receive_newsletter]'] != null) query.append("filter[receive_newsletter]", String(args['filter[receive_newsletter]'])) if (args['filter[bookings_marketing_consent]'] != null) query.append("filter[bookings_marketing_consent]", String(args['filter[bookings_marketing_consent]'])) if (args['filter[bookings_relevant_marketing_consent_until]'] != null) query.append("filter[bookings_relevant_marketing_consent_until]", String(args['filter[bookings_relevant_marketing_consent_until]'])) if (args['filter[address_line_1]'] != null) query.append("filter[address_line_1]", String(args['filter[address_line_1]'])) if (args['filter[address_line_2]'] != null) query.append("filter[address_line_2]", String(args['filter[address_line_2]'])) if (args['filter[postalcode]'] != null) query.append("filter[postalcode]", String(args['filter[postalcode]'])) if (args['filter[city]'] != null) query.append("filter[city]", String(args['filter[city]'])) if (args['filter[country_code]'] != null) query.append("filter[country_code]", String(args['filter[country_code]'])) if (args['filter[address_subdivision]'] != null) query.append("filter[address_subdivision]", String(args['filter[address_subdivision]'])) if (args['filter[iban]'] != null) query.append("filter[iban]", String(args['filter[iban]'])) if (args['filter[bic]'] != null) query.append("filter[bic]", String(args['filter[bic]'])) if (args['filter[phone]'] != null) query.append("filter[phone]", String(args['filter[phone]'])) if (args['filter[mobile_phone]'] != null) query.append("filter[mobile_phone]", String(args['filter[mobile_phone]'])) if (args['filter[created_at]'] != null) query.append("filter[created_at]", String(args['filter[created_at]'])) if (args['filter[updated_at]'] != null) query.append("filter[updated_at]", String(args['filter[updated_at]'])) if (args['filter[labels]'] != null) query.append("filter[labels]", String(args['filter[labels]'])) if (args['Accept-Language'] != null) headers.set("Accept-Language", String(args['Accept-Language'])) if (ApiKeyAuth !== undefined) headers.set("X-API-KEY", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v3/contacts${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV3ContactsResponse, await res.json()) };