import {z} from 'zod' export type GetApiV2AudioCollectionsIdArgs = {id: string, embed?: (Array<'share_code' | 'share_url'>) | undefined, share_code?: string | undefined}; export type GetApiV2AudioCollectionsIdOptions = {baseUrl?: string | undefined, customerAccessCode?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2AudioCollectionsIdResponse = z.object({cover_item: z.object({added_time: z.string().optional(), id: z.string(), media_type: z.string().optional()}).optional(), created_time: z.string().optional(), id: z.string(), items_updated_time: z.string().optional(), name: z.string(), share_code: z.string().optional(), share_url: z.string().optional(), total_item_count: z.number().int(), updated_time: z.string().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 the details of audio collections * * GET /v2/audio/collections/{id} */ export const getApiV2AudioCollectionsId = async (args: GetApiV2AudioCollectionsIdArgs, options?: GetApiV2AudioCollectionsIdOptions) => { const {baseUrl = "https://api.shutterstock.com", fetch: fetchFn = fetch, customerAccessCode} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.embed ?? []) query.append("embed", String(item)) if (args.share_code != null) query.append("share_code", String(args.share_code)) if (customerAccessCode !== undefined) headers.set('authorization', `Bearer ${customerAccessCode}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/audio/collections/${encodeURIComponent(String(args.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(getApiV2AudioCollectionsIdResponse, await res.json()) };