import {z} from 'zod' export type GetApiV1AdminApikeysArgs = {userId?: string | undefined, status?: number | undefined, page?: number | undefined, pageSize?: number | undefined}; export type GetApiV1AdminApikeysOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const apikeyV1APIKeyInfo = z.object({billingMode: z.string().optional(), channelSourceCode: z.string().optional(), channelSourceId: z.string().optional(), createdAt: z.string().optional(), creatorId: z.string().optional(), creatorName: z.string().optional(), cumulativeUsedMicros: z.string().optional(), expiresAt: z.string().optional(), id: z.string().optional(), keyPrefix: z.string().optional(), lastUsedAt: z.string().optional(), lastUsedIp: z.string().optional(), name: z.string().optional(), scopes: z.array(z.string()).optional(), status: z.number().int().optional(), subscriptionId: z.string().optional(), teamId: z.string().optional(), teamName: z.string().optional(), updatedAt: z.string().optional(), userId: z.string().optional()}); export const apikeyV1ListAllAPIKeysReply = z.object({apikeys: z.array(apikeyV1APIKeyInfo).optional(), total: z.string().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 }; /** * GET /v1/admin/apikeys */ export const getApiV1AdminApikeys = async (args: GetApiV1AdminApikeysArgs, options: GetApiV1AdminApikeysOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.userId != null) query.append("userId", String(args.userId)) if (args.status != null) query.append("status", String(args.status)) if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/admin/apikeys${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(apikeyV1ListAllAPIKeysReply, await res.json()) };