import {z} from 'zod' export type GetApiAccountsArgs = {id?: string | undefined, status?: string | undefined, adminRoleArn?: string | undefined, principalRoleArn?: string | undefined, principalPolicyHash?: string | undefined, nextId?: string | undefined, limit?: number | undefined}; export type GetApiAccountsOptions = {baseUrl: string, sigv4?: string | undefined, fetch?: typeof fetch | undefined}; export const accountStatus = z.enum(["Ready", "NotReady", "Leased", "Orphaned"]); export const account = z.object({id: z.string().optional(), accountStatus: accountStatus.optional(), adminRoleArn: z.string().optional(), principalRoleArn: z.string().optional(), principalPolicyHash: z.string().optional(), lastModifiedOn: z.number().int().optional(), createdOn: z.number().int().optional(), metadata: z.object({}).optional()}); export const getApiAccountsResponse = z.array(account); 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 accounts * * GET /accounts */ export const getApiAccounts = async (args: GetApiAccountsArgs, options: GetApiAccountsOptions) => { const {baseUrl, fetch: fetchFn = fetch, sigv4} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.id != null) query.append("id", String(args.id)) if (args.status != null) query.append("status", String(args.status)) if (args.adminRoleArn != null) query.append("adminRoleArn", String(args.adminRoleArn)) if (args.principalRoleArn != null) query.append("principalRoleArn", String(args.principalRoleArn)) if (args.principalPolicyHash != null) query.append("principalPolicyHash", String(args.principalPolicyHash)) if (args.nextId != null) query.append("nextId", String(args.nextId)) if (args.limit != null) query.append("limit", String(args.limit)) if (sigv4 !== undefined) headers.set("Authorization", sigv4) const url = `${baseUrl.replace(/\/+$/, '')}/accounts${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(getApiAccountsResponse, await res.json()) };