import {z} from 'zod' export type GetApiOperationsAdminArgs = {q?: (string | null) | undefined, permissions?: (Array<'MANAGE_USERS' | 'MANAGE_BILLING_SUBSCRIPTIONS' | 'MANAGE_CAMPAIGNS' | 'MANAGE_ADMINS'> | null) | undefined, perPage?: number | undefined, cursor?: string | undefined}; export type GetApiOperationsAdminOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const operationsAdminSchema = z.object({id: z.number().int(), email: z.string(), name: z.string(), permissions: z.array(z.enum(["MANAGE_USERS", "MANAGE_BILLING_SUBSCRIPTIONS", "MANAGE_CAMPAIGNS", "MANAGE_ADMINS"]))}); export const getApiOperationsAdminResponse = z.object({items: z.array(operationsAdminSchema), perPage: z.number().int(), previous: z.string().nullable(), next: z.string().nullable()}); 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 }; /** * GET /operations/admin */ export const getApiOperationsAdmin = async (args: GetApiOperationsAdminArgs, options: GetApiOperationsAdminOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.q != null) query.append("q", String(args.q)) for (const item of args.permissions ?? []) query.append("permissions", String(item)) if (args.perPage != null) query.append("perPage", String(args.perPage)) if (args.cursor != null) query.append("cursor", String(args.cursor)) const url = `${baseUrl.replace(/\/+$/, '')}/operations/admin${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(getApiOperationsAdminResponse, await res.json()) };