import {z} from 'zod' export type GetApiApiOrgIdFleetTokensArgs = {org_id: string, offset?: number | undefined, limit?: number | undefined}; export type GetApiApiOrgIdFleetTokensOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const collectorFleet = z.object({id: z.string(), name: z.string().nullable().optional()}); export const enrollmentToken = z.object({created_at: z.number().int(), expires_at: z.number().int().nullable().optional(), fleet: collectorFleet, id: z.string(), max_uses: z.number().int().nullable().optional(), name: z.string(), prefix: z.string(), revoked: z.boolean(), token: z.string().nullable().optional(), uses: z.number().int()}); export const tokenList = z.object({items: z.array(enrollmentToken), total: z.number().int().gte(0)}); 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 enrollment tokens; a token's value only on fleets the caller may PUT * * GET /api/{org_id}/fleet/tokens */ export const getApiApiOrgIdFleetTokens = async (args: GetApiApiOrgIdFleetTokensArgs, options: GetApiApiOrgIdFleetTokensOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/fleet/tokens${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(tokenList, await res.json()) };