import {z} from 'zod' export type GetApiV2SfxSearchArgs = {added_date?: string | undefined, added_date_start?: string | undefined, added_date_end?: string | undefined, duration?: number | undefined, duration_from?: number | undefined, duration_to?: number | undefined, page?: number | undefined, per_page?: number | undefined, query?: string | undefined, safe?: boolean | undefined, sort?: ('popular' | 'newest' | 'relevance' | 'random' | 'oldest') | undefined, view?: ('minimal' | 'full') | undefined, 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}; export type GetApiV2SfxSearchOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiV2SfxSearchResponse = z.object({data: z.array(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()})), message: z.string().optional(), page: z.number().int().optional(), per_page: z.number().int().optional(), total_count: z.number().int(), search_id: z.string()}); 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 }; /** * Search for sound effects * * GET /v2/sfx/search * * Also accepts auth: customer_accessCode */ export const getApiV2SfxSearch = async (args: GetApiV2SfxSearchArgs, options?: GetApiV2SfxSearchOptions) => { 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.added_date != null) query.append("added_date", String(args.added_date)) if (args.added_date_start != null) query.append("added_date_start", String(args.added_date_start)) if (args.added_date_end != null) query.append("added_date_end", String(args.added_date_end)) if (args.duration != null) query.append("duration", String(args.duration)) if (args.duration_from != null) query.append("duration_from", String(args.duration_from)) if (args.duration_to != null) query.append("duration_to", String(args.duration_to)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.query != null) query.append("query", String(args.query)) if (args.safe != null) query.append("safe", String(args.safe)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.view != null) query.append("view", String(args.view)) if (args.language != null) query.append("language", String(args.language)) if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/sfx/search${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(getApiV2SfxSearchResponse, await res.json()) };