import {z} from 'zod' export type GetApiMediaArgs = {id?: string | undefined, take?: number | undefined, page?: number | undefined, group_id?: Array | undefined, group_name?: Array | undefined}; export type GetApiMediaOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const media = z.object({name: z.string().nullable().optional(), group_id: z.string().nullable().optional(), tags: z.string().nullable().optional(), advertiser_id: z.string().nullable().optional(), is_shared: z.boolean().nullable().optional(), start_date: z.string().nullable().optional(), end_date: z.string().nullable().optional(), id: z.string().nullable().optional(), group_name: z.string().nullable().optional(), mime_type: z.string().nullable().optional(), file_size: z.number().int().optional(), file_name: z.string().nullable().optional(), file_url: z.string().nullable().optional(), thumbnail_url: z.string().nullable().optional(), uploaded_on: z.string().nullable().optional(), uploaded_by_id: z.string().nullable().optional(), sha_512: z.string().nullable().optional(), width: z.number().int().nullable().optional(), height: z.number().int().nullable().optional(), is_approved: z.boolean().nullable().optional(), approved_on: z.string().nullable().optional(), declined_on: z.string().nullable().optional(), declined_reason: z.string().nullable().optional()}); export const getApiMediaResponse = z.array(media); 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 }; /** * Gets all media in an account. * * GET /media * * Also accepts auth: ApiKeyInHeader, ApiKeyInQuery */ export const getApiMedia = async (args: GetApiMediaArgs, options?: GetApiMediaOptions) => { const {baseUrl = "https://api.reveldigital.com", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.id != null) query.append("id", String(args.id)) if (args.take != null) query.append("take", String(args.take)) if (args.page != null) query.append("page", String(args.page)) for (const item of args.group_id ?? []) query.append("group_id", String(item)) for (const item of args.group_name ?? []) query.append("group_name", String(item)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/media${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(getApiMediaResponse, await res.json()) };