import {z} from 'zod' export type HotspotVoucherCreationRequest = {count?: number | undefined, name: string, authorizedGuestLimit?: number | undefined, timeLimitMinutes: number, dataUsageLimitMBytes?: number | undefined, rxRateLimitKbps?: number | undefined, txRateLimitKbps?: number | undefined}; export type CreateApiV1SitesSiteIdHotspotVouchersArgs = {siteId: string, body: HotspotVoucherCreationRequest}; export type CreateApiV1SitesSiteIdHotspotVouchersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const hotspotVoucherDetails = z.object({id: z.string(), createdAt: z.string(), name: z.string(), code: z.string(), authorizedGuestLimit: z.number().int().optional(), authorizedGuestCount: z.number().int(), activatedAt: z.string().optional(), expiresAt: z.string().optional(), expired: z.boolean(), timeLimitMinutes: z.number().int(), dataUsageLimitMBytes: z.number().int().optional(), rxRateLimitKbps: z.number().int().optional(), txRateLimitKbps: z.number().int().optional()}); export const integrationVoucherCreationResultDto = z.object({vouchers: z.array(hotspotVoucherDetails).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 }; /** * Generate Vouchers * * POST /v1/sites/{siteId}/hotspot/vouchers */ export const createApiV1SitesSiteIdHotspotVouchers = async (args: CreateApiV1SitesSiteIdHotspotVouchersArgs, options?: CreateApiV1SitesSiteIdHotspotVouchersOptions) => { const {baseUrl = "https://api.ui.com/v1/connector/consoles/942A6FF664C0000000000964970E0000000009E710560000000068A9873F:9999999999/proxy/network/integration", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/sites/${encodeURIComponent(String(args.siteId))}/hotspot/vouchers${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(integrationVoucherCreationResultDto, await res.json()) };