import {z} from 'zod' export type GetApiAttachmentsAttachmentIdArgs = {attachment_id: number}; export type GetApiAttachmentsAttachmentIdOptions = {baseUrl?: string | undefined, Auth0?: string | undefined, fetch?: typeof fetch | undefined}; export const attachmentBase = z.object({id: z.number().int().gte(1), entity_type: z.string().min(1), target_id: z.number().gte(0), strategy_id: z.number().gte(1), restriction: z.enum(["INCLUDE", "EXCLUDE"]).min(1), operator: z.enum(["OR", "AND"]).min(1), created_on: z.string(), updated_on: z.string(), version: z.number(), or_better: z.boolean().optional()}); export const singleMetadata = z.object({status: z.string()}); export const getApiAttachmentsAttachmentIdResponse = z.object({data: attachmentBase.optional(), meta: singleMetadata.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 }; /** * Get an Attachment * * GET /attachments/{attachment_id} */ export const getApiAttachmentsAttachmentId = async (args: GetApiAttachmentsAttachmentIdArgs, options?: GetApiAttachmentsAttachmentIdOptions) => { const {baseUrl = "https://api.mediamath.com/api/v3.0", fetch: fetchFn = fetch, Auth0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (Auth0 !== undefined) headers.set('authorization', `Bearer ${Auth0}`) const url = `${baseUrl.replace(/\/+$/, '')}/attachments/${encodeURIComponent(String(args.attachment_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(getApiAttachmentsAttachmentIdResponse, await res.json()) };