import {z} from 'zod' export type GetApiV2OrganizationsOrganizationServicesArgs = {organization: string}; export type GetApiV2OrganizationsOrganizationServicesOptions = {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 v2ListServicesResponse = 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 }; /** * List the services that have been registered in an organization * * GET /v2/organizations/{organization}/services */ export const getApiV2OrganizationsOrganizationServices = async (args: GetApiV2OrganizationsOrganizationServicesArgs, options: GetApiV2OrganizationsOrganizationServicesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v2/organizations/${encodeURIComponent(String(args.organization))}/services${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(v2ListServicesResponse, await res.json()) };