import {z} from 'zod' export type GettyImagesServicesConnectPublicControllersAffiliateAffiliateSearchStyle = 'photography' | 'vector'; export type GetApiV3AffiliatesSearchImagesArgs = {'Accept-Language'?: string | undefined, 'GI-Country-Code'?: string | undefined, phrase?: string | undefined, style?: GettyImagesServicesConnectPublicControllersAffiliateAffiliateSearchStyle | undefined}; export type GetApiV3AffiliatesSearchImagesOptions = {baseUrl: string, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const gettyImagesServicesConnectPublicControllersAffiliateAffiliateImageUrls = z.object({small: z.string().nullable().optional(), small_height: z.number().int().optional(), small_width: z.number().int().optional(), medium: z.string().nullable().optional(), medium_height: z.number().int().optional(), medium_width: z.number().int().optional(), large: z.string().nullable().optional(), large_height: z.number().int().optional(), large_width: z.number().int().optional()}); export const gettyImagesServicesConnectPublicControllersAffiliateAffiliateImage = z.object({id: z.string().nullable().optional(), title: z.string().nullable().optional(), caption: z.string().nullable().optional(), preview_urls: gettyImagesServicesConnectPublicControllersAffiliateAffiliateImageUrls.optional(), destination_url: z.string().nullable().optional()}); export const gettyImagesServicesConnectPublicModelsSearchAutoCorrections = z.object({phrase: z.string().nullable().optional()}); export const gettyImagesServicesConnectPublicControllersAffiliateAffiliateImageSearchResponse = z.object({images: z.array(gettyImagesServicesConnectPublicControllersAffiliateAffiliateImage).nullable().optional(), auto_corrections: gettyImagesServicesConnectPublicModelsSearchAutoCorrections.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 /v3/affiliates/search/images * * Also accepts auth: OAuth2 */ export const getApiV3AffiliatesSearchImages = async (args: GetApiV3AffiliatesSearchImagesArgs, options: GetApiV3AffiliatesSearchImagesOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Accept-Language'] != null) headers.set("Accept-Language", String(args['Accept-Language'])) if (args['GI-Country-Code'] != null) headers.set("GI-Country-Code", String(args['GI-Country-Code'])) if (args.phrase != null) query.append("phrase", String(args.phrase)) if (args.style != null) query.append("style", String(args.style)) if (ApiKey !== undefined) headers.set("Api-Key", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v3/affiliates/search/images${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(gettyImagesServicesConnectPublicControllersAffiliateAffiliateImageSearchResponse, await res.json()) };