import {z} from 'zod' export type MessageParticipant = {name?: string | undefined, email: string}; export type TrackingDomainName = string; export type Metadata = {[key: string]: string}; export type UpdateApiV3GrantsGrantIdDraftsDraftIdArgs = {select?: string | undefined, body: {bcc?: Array | undefined, body?: string | undefined, cc?: Array | undefined, tracking_options?: ({opens?: boolean | undefined, thread_replies?: boolean | undefined, links?: boolean | undefined, label?: string | undefined, domain_name?: TrackingDomainName | undefined}) | undefined, attachments?: (Array<{filename?: string | undefined, content?: string | undefined, content_type?: string | undefined, content_id?: string | undefined, content_disposition?: string | undefined}>) | undefined, reply_to?: Array | undefined, starred?: boolean | undefined, subject?: string | undefined, to?: Array | undefined, metadata?: Metadata | undefined, template?: ({id?: string | undefined, strict?: boolean | undefined, variables?: {[key: string]: unknown} | undefined}) | undefined}}; export type UpdateApiV3GrantsGrantIdDraftsDraftIdOptions = {baseUrl?: string | undefined, NYLASAPIKEY?: string | undefined, fetch?: typeof fetch | undefined}; export const messageParticipant = z.object({name: z.string().optional(), email: z.string()}); export const grantId = z.string(); export const attachment = z.object({id: z.string(), content_type: z.string().min(1).optional(), filename: z.string().min(1).optional(), grant_id: grantId.optional(), content_id: z.string().optional(), content_disposition: z.string().optional(), is_inline: z.boolean().optional(), size: z.number().int().optional()}); export const messageParticipantResponse = z.object({name: z.string().optional(), email: z.string().optional()}); export const id = z.string().min(1); export const metadata = z.record(z.string(), z.string().max(500)); export const updateApiV3GrantsGrantIdDraftsDraftIdResponse = z.object({request_id: z.string().optional(), data: z.object({bcc: z.array(messageParticipant).optional(), body: z.string().optional(), cc: z.array(messageParticipant).optional(), attachments: z.array(attachment).optional(), folders: z.array(z.string()).optional(), from: z.array(messageParticipantResponse).optional(), grant_id: grantId.optional(), id: id.optional(), metadata: metadata.optional(), object: z.string().optional(), reply_to: z.array(messageParticipant).optional(), snippet: z.string().min(1).optional(), starred: z.boolean().optional(), subject: z.string().optional(), thread_id: z.string().optional(), to: z.array(messageParticipant).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 }; /** * Update a draft * * PUT /v3/grants/{grant_id}/drafts/{draft_id} * * Also accepts auth: ACCESS_TOKEN */ export const updateApiV3GrantsGrantIdDraftsDraftId = async (args: UpdateApiV3GrantsGrantIdDraftsDraftIdArgs, options?: UpdateApiV3GrantsGrantIdDraftsDraftIdOptions) => { 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))}/drafts/${encodeURIComponent(String(args.draft_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateApiV3GrantsGrantIdDraftsDraftIdResponse, await res.json()) };