import {z} from 'zod' export type GetApiV2SfxIdArgs = {id: unknown, language?: ('ar' | 'bg' | 'bn' | 'cs' | 'da' | 'de' | 'el' | 'en' | 'es' | 'fi' | 'fr' | 'gu' | 'he' | 'hi' | 'hr' | 'hu' | 'id' | 'it' | 'ja' | 'kn' | 'ko' | 'ml' | 'mr' | 'nb' | 'nl' | 'or' | 'pl' | 'pt' | 'ro' | 'ru' | 'sk' | 'sl' | 'sv' | 'ta' | 'te' | 'th' | 'tr' | 'uk' | 'ur' | 'vi' | 'zh' | 'zh-Hant') | undefined, view?: ('minimal' | 'full') | undefined, library?: ('shutterstock' | 'premier' | 'premiumbeat') | undefined, search_id?: string | undefined}; export type GetApiV2SfxIdOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiV2SfxIdResponse = z.object({added_date: z.string().optional(), affiliate_url: z.string().optional(), artist: z.string().optional(), assets: z.object({preview_mp3: z.object({file_size: z.number().int().optional(), url: z.string().optional()}).optional(), waveform: z.object({file_size: z.number().int().optional(), url: z.string().optional()}).optional()}).optional(), contributor: z.object({id: z.string(), name: z.string().optional()}), description: z.string().optional(), duration: z.number().optional(), id: z.string(), keywords: z.array(z.string()).optional(), media_type: z.string(), releases: z.array(z.string()).optional(), title: z.string().optional(), updated_time: z.string().optional(), url: 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 details about sound effects * * GET /v2/sfx/{id} * * Also accepts auth: customer_accessCode */ export const getApiV2SfxId = async (args: GetApiV2SfxIdArgs, options?: GetApiV2SfxIdOptions) => { const {baseUrl = "https://api.shutterstock.com", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.language != null) query.append("language", String(args.language)) if (args.view != null) query.append("view", String(args.view)) if (args.library != null) query.append("library", String(args.library)) if (args.search_id != null) query.append("search_id", String(args.search_id)) if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/sfx/${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(getApiV2SfxIdResponse, await res.json()) };