import {z} from 'zod' export type PricingVariant = 'default' | 'paid_tokens'; export type ItemActionRequest = {item_key: string}; export type CreateApiApiGamificationItemsPurchaseArgs = {pricing_variant?: PricingVariant | undefined, 'x-user-id'?: (string | null) | undefined, body: ItemActionRequest}; export type CreateApiApiGamificationItemsPurchaseOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const unlockType = z.enum(["free", "purchasable", "premium", "special"]); export const itemResponse = z.object({id: z.string(), key: z.string(), name: z.string(), description: z.string().nullable().optional(), item_type: z.string(), unlock_type: unlockType, token_price: z.number().int().nullable().optional(), available_from: z.string().nullable().optional(), available_until: z.string().nullable().optional(), sort_order: z.number().int().optional(), created_at: z.string()}); export const itemActionResponse = z.object({success: z.boolean(), message: z.string(), item: itemResponse.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 }; /** * Purchase Item * * POST /api/gamification/items/purchase */ export const createApiApiGamificationItemsPurchase = async (args: CreateApiApiGamificationItemsPurchaseArgs, options: CreateApiApiGamificationItemsPurchaseOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.pricing_variant != null) query.append("pricing_variant", String(args.pricing_variant)) if (args['x-user-id'] != null) headers.set("x-user-id", String(args['x-user-id'])) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/gamification/items/purchase${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(itemActionResponse, await res.json()) };