import {z} from 'zod' export type UpdateApiBoxesKeyThreadsArgs = {key: string, body: {boxKey: string, threadGmailId: string}}; export type UpdateApiBoxesKeyThreadsOptions = {baseUrl?: string | undefined, sec0?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const updateApiBoxesKeyThreadsResponse = z.array(z.object({creatorKey: z.string().optional(), boxKey: z.string().optional(), pipelineKey: z.string().optional(), creationTimestamp: z.number().int().optional(), lastUpdatedTimestamp: z.number().int().optional(), numberOfEmails: z.number().int().optional(), names: z.array(z.unknown()).optional(), emailAddresses: z.array(z.unknown()).optional(), threadGmailId: z.string().optional(), fileKeys: z.array(z.unknown()).optional(), gmailThreadKey: z.string().optional(), key: z.string().optional(), lastSavedTimestamp: z.number().int().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 }; /** * Put an email in a box * * PUT /boxes/{key}/threads */ export const updateApiBoxesKeyThreads = async (args: UpdateApiBoxesKeyThreadsArgs, options?: UpdateApiBoxesKeyThreadsOptions) => { const {baseUrl = "https://api.streak.com/api/v1", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/x-www-form-urlencoded") headers.set('accept', "application/json") if (sec0 !== undefined) headers.set('authorization', `Basic ${btoa(`${sec0.username}:${sec0.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/boxes/${encodeURIComponent(String(args.key))}/threads${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: new URLSearchParams(Object.entries(args.body).flatMap(([key, value]) => (value == null ? [] : [[key, String(value)]])))}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(updateApiBoxesKeyThreadsResponse, await res.json()) };