import {z} from 'zod' export type PromotionLinkContentRequest = {content_id?: (string | null) | undefined, description?: (string | null) | undefined, image_url?: (string | null) | undefined, title?: (string | null) | undefined, url?: (string | null) | undefined}; export type PromotionLinkCreateRequest = {code: string, content?: PromotionLinkContentRequest | undefined, data?: {[key: string]: string} | undefined, description?: (string | null) | undefined, key: string | null, label?: string | undefined, program_url: string | null}; export type CreateApiV2PromotionLinksArgs = {body: PromotionLinkCreateRequest}; export type CreateApiV2PromotionLinksOptions = {baseUrl?: string | undefined, HEADER?: string | undefined, fetch?: typeof fetch | undefined}; export const promotionLinkContentResponse = z.object({content_id: z.string().optional(), description: z.string().optional(), image_url: z.string().optional(), title: z.string().optional(), url: z.string().optional()}); export const promotionLinkResponse = z.object({code: z.string(), content: promotionLinkContentResponse, data: z.record(z.string(), z.string()), description: z.string(), key: z.string(), label: z.string(), program_url: z.string(), promotion_url: 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 }; /** * Create a promotion link * * POST /v2/promotion-links * * Also accepts auth: QUERY, COOKIE */ export const createApiV2PromotionLinks = async (args: CreateApiV2PromotionLinksArgs, options?: CreateApiV2PromotionLinksOptions) => { const {baseUrl = "https://api.extole.io", fetch: fetchFn = fetch, HEADER} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HEADER !== undefined) headers.set("Authorization", HEADER) const url = `${baseUrl.replace(/\/+$/, '')}/v2/promotion-links${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(promotionLinkResponse, await res.json()) };