import {z} from 'zod' export type GettyImagesServicesConnectPublicControllersAssetLicensingExtendedLicenses = 'multiseat' | 'unlimited' | 'resale' | 'indemnification'; export type GettyImagesServicesConnectPublicControllersAssetLicensingAcquireAssetLicensesRequest = {extended_licenses: Array, use_team_credits?: boolean | undefined}; export type CreateApiV3AssetLicensingAssetIdArgs = {'Accept-Language'?: string | undefined, assetId: string, body: GettyImagesServicesConnectPublicControllersAssetLicensingAcquireAssetLicensesRequest}; export type CreateApiV3AssetLicensingAssetIdOptions = {baseUrl: string, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const gettyImagesServicesConnectPublicControllersAssetLicensingExtendedLicenses = z.enum(["multiseat", "unlimited", "resale", "indemnification"]); export const gettyImagesServicesConnectPublicControllersAssetLicensingAssetLicensingResponse = z.object({credits_used: z.number().int().optional(), acquired_licenses: z.array(gettyImagesServicesConnectPublicControllersAssetLicensingExtendedLicenses).nullable().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 }; /** * Endpoint for acquiring extended licenses with iStock credits for an asset. * * POST /v3/asset-licensing/{assetId} * * Also accepts auth: OAuth2 */ export const createApiV3AssetLicensingAssetId = async (args: CreateApiV3AssetLicensingAssetIdArgs, options: CreateApiV3AssetLicensingAssetIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Accept-Language'] != null) headers.set("Accept-Language", String(args['Accept-Language'])) if (ApiKey !== undefined) headers.set("Api-Key", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v3/asset-licensing/${encodeURIComponent(String(args.assetId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(gettyImagesServicesConnectPublicControllersAssetLicensingAssetLicensingResponse, await res.json()) };