import {z} from 'zod' export type GetApiV3GrantsGrantIdContactsContactIdArgs = {select?: string | undefined, profile_picture?: boolean | undefined}; export type GetApiV3GrantsGrantIdContactsContactIdOptions = {baseUrl?: string | undefined, NYLASAPIKEY?: string | undefined, fetch?: typeof fetch | undefined}; export const contactBirthday = z.string(); export const contactCompanyName = z.string(); export const contactEmail = z.object({email: z.string().max(255), type: z.enum(["work", "home", "other"]).optional()}); export const contactGivenName = z.string(); export const grantId = z.string(); export const id = z.string().min(1); export const contactGroupId = z.object({id: id}); export const contactImAddress = z.object({im_address: z.string().max(255), type: z.string().optional()}); export const contactJobTitle = z.string(); export const contactManagerName = z.string(); export const metadata = z.record(z.string(), z.string().max(500)); export const contactMiddleName = z.string(); export const contactNickname = z.string(); export const contactNotes = z.string(); export const contactOfficeLocation = z.string(); export const contactPhoneNumber = z.object({number: z.string(), type: z.enum(["work", "home", "mobile", "other"]).optional()}); export const contactPhysicalAddress = z.object({city: z.string().optional(), country: z.string().optional(), postal_code: z.string().optional(), state: z.string().optional(), street_address: z.string().optional(), type: z.enum(["work", "home", "other"])}); export const contactPictureUrl = z.string(); export const contactSource = z.enum(["address_book", "domain", "inbox"]); export const contactSuffix = z.string(); export const contactSurname = z.string(); export const contactWebPage = z.object({url: z.string(), type: z.enum(["work", "home", "other"]).optional()}); export const contactPicture = z.string(); export const getApiV3GrantsGrantIdContactsContactIdResponse = z.object({request_id: z.string().optional(), data: z.object({birthday: contactBirthday.optional(), company_name: contactCompanyName.optional(), emails: z.array(contactEmail).nullable().optional(), given_name: contactGivenName.optional(), grant_id: grantId.optional(), groups: z.array(contactGroupId).nullable().optional(), id: id.optional(), im_addresses: z.array(contactImAddress).nullable().optional(), job_title: contactJobTitle.optional(), manager_name: contactManagerName.optional(), metadata: metadata.optional(), middle_name: contactMiddleName.optional(), nickname: contactNickname.optional(), notes: contactNotes.optional(), object: z.string().optional(), office_location: contactOfficeLocation.optional(), phone_numbers: z.array(contactPhoneNumber).nullable().optional(), physical_addresses: z.array(contactPhysicalAddress).nullable().optional(), picture_url: contactPictureUrl.optional(), source: contactSource.optional(), suffix: contactSuffix.optional(), surname: contactSurname.optional(), web_pages: z.array(contactWebPage).nullable().optional(), picture: contactPicture.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 }; /** * Return a contact * * GET /v3/grants/{grant_id}/contacts/{contact_id} * * Also accepts auth: ACCESS_TOKEN */ export const getApiV3GrantsGrantIdContactsContactId = async (args: GetApiV3GrantsGrantIdContactsContactIdArgs, options?: GetApiV3GrantsGrantIdContactsContactIdOptions) => { const {baseUrl = "https://api.us.nylas.com", fetch: fetchFn = fetch, NYLASAPIKEY} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.select != null) query.append("select", String(args.select)) if (args.profile_picture != null) query.append("profile_picture", String(args.profile_picture)) if (NYLASAPIKEY !== undefined) headers.set('authorization', `Bearer ${NYLASAPIKEY}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/grants/${encodeURIComponent(String(args.grant_id))}/contacts/${encodeURIComponent(String(args.contact_id))}${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(getApiV3GrantsGrantIdContactsContactIdResponse, await res.json()) };