import {z} from 'zod' export type GetApiServicesIdOptions = {baseUrl?: string | undefined, ztSession?: string | undefined, fetch?: typeof fetch | undefined}; export const pagination = z.object({limit: z.number(), offset: z.number(), totalCount: z.number()}); export const meta = z.object({filterableFields: z.array(z.string()).optional(), pagination: pagination.optional(), apiEnrollmentVersion: z.string().optional(), apiVersion: z.string().optional()}); export const link = z.object({href: z.string(), method: z.string().optional(), comment: z.string().optional()}); export const links = z.record(z.string(), link); export const tags = z.record(z.string(), z.object({})); export const serviceDetail = z.object({id: z.string(), createdAt: z.string(), updatedAt: z.string(), _links: links, tags: tags, name: z.string(), terminatorStrategy: z.string(), maxIdleTimeMillis: z.number().int()}); export const detailServiceEnvelope = z.object({meta: meta, data: serviceDetail}); 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 }; /** * Retrieves a single service * * GET /services/{id} * * Also accepts auth: oauth2 */ export const getApiServicesId = async (options?: GetApiServicesIdOptions) => { const {baseUrl = "https://demo.ziti.dev/fabric/v1", fetch: fetchFn = fetch, ztSession} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ztSession !== undefined) headers.set("zt-session", ztSession) const url = `${baseUrl.replace(/\/+$/, '')}/services/${encodeURIComponent(String(args.id))}${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(detailServiceEnvelope, await res.json()) };