import {z} from 'zod' export type GetApiServicesIdEdgeRoutersArgs = {id: string, limit?: number | undefined, offset?: number | undefined, filter?: string | undefined, 'session-token'?: string | undefined}; export type GetApiServicesIdEdgeRoutersOptions = {baseUrl?: string | undefined, ztSession?: string | undefined, fetch?: typeof fetch | undefined}; export const tags = z.record(z.string(), z.object({})); export const commonEdgeRouterProperties = z.object({appData: tags.optional(), capabilities: z.array(z.string()).optional(), cost: z.number().int().gte(0).lte(65535).nullable(), disabled: z.boolean(), hostname: z.string(), isOnline: z.boolean(), name: z.string(), noTraversal: z.boolean().nullable(), supportedProtocols: z.record(z.string(), z.string()), syncStatus: z.string(), version: z.string().optional()}); export const serviceEdgeRouters = z.object({edgeRouters: z.array(commonEdgeRouterProperties).optional()}); export const pagination = z.object({limit: z.number(), offset: z.number(), totalCount: z.number()}); export const meta = z.object({apiEnrollmentVersion: z.string().optional(), apiVersion: z.string().optional(), filterableFields: z.array(z.string()).optional(), pagination: pagination.optional()}); export const listServiceEdgeRoutersEnvelope = z.object({data: serviceEdgeRouters, meta: meta}); 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 of edge routers permitted to handle traffic for the specified service * * GET /services/{id}/edge-routers * * Also accepts auth: oauth2 */ export const getApiServicesIdEdgeRouters = async (args: GetApiServicesIdEdgeRoutersArgs, options?: GetApiServicesIdEdgeRoutersOptions) => { const {baseUrl = "https://demo.ziti.dev/edge/client/v1", fetch: fetchFn = fetch, ztSession} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.filter != null) query.append("filter", String(args.filter)) if (args['session-token'] != null) headers.set("session-token", String(args['session-token'])) if (ztSession !== undefined) headers.set("zt-session", ztSession) const url = `${baseUrl.replace(/\/+$/, '')}/services/${encodeURIComponent(String(args.id))}/edge-routers${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(listServiceEdgeRoutersEnvelope, await res.json()) };