import {z} from 'zod' export type PromotionsStacksCreateRequestBody = {name: string, tiers: {ids: Array, hierarchy_mode?: 'MANUAL' | undefined}, category_id?: string | undefined}; export type CreateApiV1PromotionsCampaignIdStacksArgs = {body: PromotionsStacksCreateRequestBody}; export type CreateApiV1PromotionsCampaignIdStacksOptions = {baseUrl?: string | undefined, XAppId?: string | undefined, XAppToken?: string | undefined, fetch?: typeof fetch | undefined}; export const promotionStackBase = z.object({name: z.string(), tiers: z.object({ids: z.array(z.string()), hierarchy_mode: z.literal("MANUAL").optional()})}); export const promotionsStacksCreateResponseBody = z.object({name: z.string(), tiers: z.object({ids: z.array(z.string()), hierarchy_mode: z.literal("MANUAL").optional()}), id: z.string(), created_at: z.string(), campaign_id: z.string(), object: z.literal("promotion_stack"), category_id: z.string().nullable(), categories: z.array(promotionStackBase)}); 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 Promotion Stack * * POST /v1/promotions/{campaignId}/stacks * * Also accepts auth: X-Voucherify-OAuth */ export const createApiV1PromotionsCampaignIdStacks = async (args: CreateApiV1PromotionsCampaignIdStacksArgs, options?: CreateApiV1PromotionsCampaignIdStacksOptions) => { const {baseUrl = "https://api.voucherify.io", fetch: fetchFn = fetch, XAppId, XAppToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (XAppId !== undefined) headers.set("X-App-Id", XAppId) if (XAppToken !== undefined) headers.set("X-App-Token", XAppToken) const url = `${baseUrl.replace(/\/+$/, '')}/v1/promotions/${encodeURIComponent(String(args.campaignId))}/stacks${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(promotionsStacksCreateResponseBody, await res.json()) };