import {z} from 'zod' export type GetApiQstashUsersOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const qStashUser = z.object({customer_id: z.string().optional(), id: z.string().optional(), token: z.string().optional(), read_only_token: z.string().optional(), active: z.boolean().optional(), state: z.string().optional(), modifying_state: z.string().optional(), last_plan_upgrade_time: z.number().int().optional(), max_message_size: z.number().int().optional(), max_requests_per_day: z.number().int().optional(), max_requests_per_day_hard: z.number().int().optional(), max_endpoints_per_topic: z.number().int().optional(), max_requests_per_second: z.number().int().optional(), max_dlq_size: z.number().int().optional(), max_retries: z.number().int().optional(), max_topics: z.number().int().optional(), max_schedules: z.number().int().optional(), max_events_size: z.number().int().optional(), max_dlq_retention_time_milis: z.number().int().optional(), max_delay: z.number().int().optional(), max_parallelism: z.number().int().optional(), max_global_parallelism: z.number().int().optional(), max_queues: z.number().int().optional(), max_bandwidth_per_second: z.number().int().optional(), prod_pack_enabled: z.boolean().optional(), prometheus_enabled: z.enum(["true", "false"]).optional(), timeout: z.number().int().optional(), type: z.enum(["free", "paid"]).optional(), region: z.enum(["eu-central-1", "us-east-1"]).optional(), reserved_type: z.string().optional(), reserved_price: z.number().int().optional(), created_by: z.string().optional(), creation_time: z.number().int().optional(), deletion_time: z.number().int().optional(), budget: z.number().int().optional(), enterprise: z.object({enabled: z.boolean().optional()}).optional()}); export const getApiQstashUsersResponse = z.array(qStashUser); 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 }; /** * List QStash Users * * GET /qstash/users */ export const getApiQstashUsers = async (options?: GetApiQstashUsersOptions) => { const {baseUrl = "https://api.upstash.com/v2", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/qstash/users${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiQstashUsersResponse, await res.json()) };