import {z} from 'zod' export type CreateApiOndemandPagesOndemandIdPromotionsArgs = {ondemand_id: number, body: {access_type?: ('default' | 'vip') | undefined, code?: string | undefined, discount_type?: ('free' | 'percent') | undefined, download: boolean, end_time?: string | undefined, label?: string | undefined, percent_off?: number | undefined, product_type?: ('any' | 'buy' | 'buy_episode' | 'rent' | 'rent_episode' | 'subscribe') | undefined, start_time?: string | undefined, stream_period: '1_week' | '1_year' | '24_hour' | '30_day' | '3_month' | '48_hour' | '6_month' | '72_hour', total: number, type: 'batch' | 'single'}}; export type CreateApiOndemandPagesOndemandIdPromotionsOptions = {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 }; /** * Add a promotion to an On Demand page * * POST /ondemand/pages/{ondemand_id}/promotions */ export const createApiOndemandPagesOndemandIdPromotions = async (args: CreateApiOndemandPagesOndemandIdPromotionsArgs, options?: CreateApiOndemandPagesOndemandIdPromotionsOptions) => { const {baseUrl = "https://api.vimeo.com", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/vnd.vimeo.ondemand.promotion+json") 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${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(onDemandPromotion, await res.json()) };