import {z} from 'zod' export type MediaUpdate = {folder?: string | undefined, alt_text?: string | undefined, metadata?: {[key: string]: unknown} | undefined, trigger_webhook?: boolean | undefined}; export type PatchApiBucketsSlugMediaMediaIdArgs = {body: MediaUpdate}; export type PatchApiBucketsSlugMediaMediaIdOptions = {baseUrl?: string | undefined, writeKey?: string | undefined, fetch?: typeof fetch | undefined}; export const media = z.object({id: z.string().optional(), name: z.string().optional(), original_name: z.string().optional(), size: z.union([z.number().int(), z.string()]).optional(), type: z.string().optional(), bucket: z.string().optional(), folder: z.string().nullable().optional(), alt_text: z.string().nullable().optional(), width: z.number().int().nullable().optional(), height: z.number().int().nullable().optional(), url: z.string().optional(), imgix_url: z.string().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), created_at: z.string().optional()}).and(z.record(z.string(), z.unknown())); export const mediaResponse = z.object({media: 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 }; /** * Update media metadata * * PATCH /buckets/{slug}/media/{media_id} */ export const patchApiBucketsSlugMediaMediaId = async (args: PatchApiBucketsSlugMediaMediaIdArgs, options?: PatchApiBucketsSlugMediaMediaIdOptions) => { const {baseUrl = "https://api.cosmicjs.com/v3", fetch: fetchFn = fetch, writeKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (writeKey !== undefined) headers.set('authorization', `Bearer ${writeKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/buckets/${encodeURIComponent(String(args.slug))}/media/${encodeURIComponent(String(args.media_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(mediaResponse, await res.json()) };