import {z} from 'zod' export type CreateApiApiV1AttachmentsArgs = {body: {ItemId?: string | undefined, Name?: string | undefined, Content?: string | undefined, RequestId?: string | undefined}}; export type CreateApiApiV1AttachmentsOptions = {baseUrl: string, TeamhoodAPIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const eyleanDomainEnumsFileSourceType = z.literal("SharePoint"); export const externalAPIContractsResponsesV1EntitiesExternalAttachment = z.object({source: eyleanDomainEnumsFileSourceType, url: z.string().nullable()}); export const externalAPIContractsResponsesV1EntitiesAttachment = z.object({id: z.string(), itemId: z.string(), name: z.string().nullable(), mimeType: z.string().nullable(), size: z.number().int(), external: externalAPIContractsResponsesV1EntitiesExternalAttachment}); 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 }; /** * Adds a new attachment to the specified item. * Supports direct file uploads only; external source attachments are not supported. * * POST /api/v1/attachments */ export const createApiApiV1Attachments = async (args: CreateApiApiV1AttachmentsArgs, options: CreateApiApiV1AttachmentsOptions) => { const {baseUrl, fetch: fetchFn = fetch, TeamhoodAPIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (TeamhoodAPIKey !== undefined) headers.set("X-ApiKey", TeamhoodAPIKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/attachments${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(externalAPIContractsResponsesV1EntitiesAttachment, await res.json()) };