import {z} from 'zod' export type GetApiOndemandPagesOndemandIdPromotionsPromotionIdArgs = {ondemand_id: number, promotion_id: number}; export type GetApiOndemandPagesOndemandIdPromotionsPromotionIdOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const onDemandPromotion = z.object({access_type: z.enum(["default", "vip"]), discount_type: z.enum(["dollars", "free", "percent"]), download: z.boolean(), label: z.string().nullable(), metadata: z.object({connections: z.object({codes: z.object({options: z.array(z.string()), total: z.number(), uri: z.string()})})}), percent_off: z.number(), product_type: z.enum(["any", "buy", "buy_episode", "rent", "rent_episode", "subscribe"]), stream_period: z.enum(["1_week", "1_year", "24_hour", "30_days", "3_month", "48_hour", "6_month", "72_hour"]).nullable(), total: z.number(), type: z.enum(["batch", "batch_prefix", "single"]), uri: 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 }; /** * Get a specific promotion on an On Demand page * * GET /ondemand/pages/{ondemand_id}/promotions/{promotion_id} */ export const getApiOndemandPagesOndemandIdPromotionsPromotionId = async (args: GetApiOndemandPagesOndemandIdPromotionsPromotionIdArgs, options?: GetApiOndemandPagesOndemandIdPromotionsPromotionIdOptions) => { const {baseUrl = "https://api.vimeo.com", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.vimeo.ondemand.promotion+json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/ondemand/pages/${encodeURIComponent(String(args.ondemand_id))}/promotions/${encodeURIComponent(String(args.promotion_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(onDemandPromotion, await res.json()) };