import {z} from 'zod' export type ContactBirthday = string; export type ContactCompanyName = string; export type ContactEmail = {email: string, type?: ('work' | 'home' | 'other') | undefined}; export type ContactGivenName = string; export type Id = string; export type ContactGroupId = {id: Id}; export type ContactImAddress = {im_address: string, type?: string | undefined}; export type ContactJobTitle = string; export type ContactManagerName = string; export type Metadata = {[key: string]: string}; export type ContactMiddleName = string; export type ContactNickname = string; export type ContactNotes = string; export type ContactOfficeLocation = string; export type ContactPhoneNumber = {number: string, type?: ('work' | 'home' | 'mobile' | 'other') | undefined}; export type ContactPhysicalAddress = {city?: string | undefined, country?: string | undefined, postal_code?: string | undefined, state?: string | undefined, street_address?: string | undefined, type: 'work' | 'home' | 'other'}; export type ContactSuffix = string; export type ContactSurname = string; export type ContactWebPage = {url: string, type?: ('work' | 'home' | 'other') | undefined}; export type CreateApiV3GrantsGrantIdContactsArgs = {select?: string | undefined, body: {birthday?: ContactBirthday | undefined, company_name?: ContactCompanyName | undefined, emails?: Array | undefined, given_name: ContactGivenName, groups?: Array | undefined, im_addresses?: Array | undefined, job_title?: ContactJobTitle | undefined, manager_name?: ContactManagerName | undefined, metadata?: Metadata | undefined, middle_name?: ContactMiddleName | undefined, nickname?: ContactNickname | undefined, notes?: ContactNotes | undefined, office_location?: ContactOfficeLocation | undefined, phone_numbers?: Array | undefined, physical_addresses?: Array | undefined, suffix?: ContactSuffix | undefined, source?: 'address_book' | undefined, surname?: ContactSurname | undefined, web_pages?: Array | undefined}}; export type CreateApiV3GrantsGrantIdContactsOptions = {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 createApiV3GrantsGrantIdContactsResponse = 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()}).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 }; /** * Create contact * * POST /v3/grants/{grant_id}/contacts * * Also accepts auth: ACCESS_TOKEN */ export const createApiV3GrantsGrantIdContacts = async (args: CreateApiV3GrantsGrantIdContactsArgs, options?: CreateApiV3GrantsGrantIdContactsOptions) => { const {baseUrl = "https://api.us.nylas.com", fetch: fetchFn = fetch, NYLASAPIKEY} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.select != null) query.append("select", String(args.select)) if (NYLASAPIKEY !== undefined) headers.set('authorization', `Bearer ${NYLASAPIKEY}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/grants/${encodeURIComponent(String(args.grant_id))}/contacts${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV3GrantsGrantIdContactsResponse, await res.json()) };