import {z} from 'zod' export type GetApiBucketsSlugMediaArgs = {read_key?: string | undefined, query?: string | undefined, props?: string | undefined, sort?: ('created_at' | '-created_at' | 'modified_at' | '-modified_at' | 'random') | undefined, limit?: number | undefined, skip?: number | undefined, useCache?: boolean | undefined, pretty?: boolean | undefined}; export type GetApiBucketsSlugMediaOptions = {baseUrl?: string | undefined, readKey?: 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 mediaListResponse = z.object({media: z.array(media), total: z.number().int().optional(), limit: 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 }; /** * List and query media * * GET /buckets/{slug}/media */ export const getApiBucketsSlugMedia = async (args: GetApiBucketsSlugMediaArgs, options?: GetApiBucketsSlugMediaOptions) => { const {baseUrl = "https://api.cosmicjs.com/v3", fetch: fetchFn = fetch, readKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.read_key != null) query.append("read_key", String(args.read_key)) if (args.query != null) query.append("query", String(args.query)) if (args.props != null) query.append("props", String(args.props)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.skip != null) query.append("skip", String(args.skip)) if (args.useCache != null) query.append("useCache", String(args.useCache)) if (args.pretty != null) query.append("pretty", String(args.pretty)) if (readKey !== undefined) query.set("read_key", readKey) const url = `${baseUrl.replace(/\/+$/, '')}/buckets/${encodeURIComponent(String(args.slug))}/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(mediaListResponse, await res.json()) };