import {z} from 'zod' export type V2NamespaceSelector = {names: Array}; export type V2ServiceLookupRequest = {selector: V2NamespaceSelector, parent: string}; export type CreateApiV2LookupServicesArgs = {body: V2ServiceLookupRequest}; export type CreateApiV2LookupServicesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const v2Port = z.object({number: z.number().int(), name: z.string().optional(), serviceDeployments: z.array(z.string()).optional()}); export const v2ServiceType = z.enum(["INVALID_TYPE", "INTERNAL", "LOADBALANCER", "MESH_EXTERNAL"]); export const v2State = z.enum(["INVALID_STATE", "EXTERNAL", "OBSERVED", "CONTROLLED"]); export const metricConfigMetricType = z.enum(["INVALID", "SINGLE_INSTANCE", "SUBSET", "GLOBAL", "ENDPOINT", "ENDPOINT_INSTANCE"]); export const serviceMetricConfig = z.object({name: z.string().optional(), description: z.string().optional(), aggregationKey: z.string().optional(), type: metricConfigMetricType.optional(), serviceDeployment: z.string().optional(), parentMetric: z.string().optional()}); export const serviceServiceDeployment = z.object({fqn: z.string().optional(), source: z.string().optional()}); export const v2Subset = z.object({name: z.string().optional(), serviceDeployments: z.array(z.string()).optional()}); export const v2Service = z.object({fqn: z.string().optional(), displayName: z.string().optional(), etag: z.string().optional(), description: z.string().optional(), shortName: z.string(), hostnames: z.array(z.string()).optional(), ports: z.array(v2Port).optional(), subsets: z.array(z.string()).optional(), serviceType: v2ServiceType, externalAddresses: z.array(z.string()).optional(), state: v2State, metrics: z.array(serviceMetricConfig).optional(), serviceDeployments: z.array(serviceServiceDeployment).optional(), subsetDeployments: z.array(v2Subset).optional(), canonicalName: z.string().optional(), spiffeIds: z.array(z.string()).optional()}); export const v2ServiceLookupResponse = z.object({services: z.array(v2Service).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 all the services in the registry that are part of the given selector. * This method can be used to resolve the registered services that are part of a workspace * or group. * This method can be also used to figure out how applying a selector could affect * the platform and have an understanding of which of the existing services would be * included in the selection. * * POST /v2/lookup/services */ export const createApiV2LookupServices = async (args: CreateApiV2LookupServicesArgs, options: CreateApiV2LookupServicesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v2/lookup/services${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(v2ServiceLookupResponse, await res.json()) };