import {z} from 'zod' export type V1ClusterTarget = {clusterName: string, namespace?: string | undefined}; export type V1CreateTargetRequest = {alias?: string | undefined, cluster?: V1ClusterTarget | undefined, description?: string | undefined, name: string, project: string, variable?: Record | undefined}; export type CreateApiApiV1TargetsArgs = {body: V1CreateTargetRequest}; export type CreateApiApiV1TargetsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const v1ClusterTarget = z.object({clusterName: z.string(), namespace: z.string().optional()}); export const v1NameAlias = z.object({alias: z.string(), name: z.string()}); export const v1DetailTargetResponse = z.object({alias: z.string().optional(), appNum: z.number().int().optional(), cluster: v1ClusterTarget.optional(), clusterAlias: z.string().optional(), createTime: z.string(), description: z.string().optional(), name: z.string(), project: v1NameAlias, updateTime: z.string(), variable: z.object({}).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 }; /** * create Target * * POST /api/v1/targets */ export const createApiApiV1Targets = async (args: CreateApiApiV1TargetsArgs, options: CreateApiApiV1TargetsOptions) => { 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(/\/+$/, '')}/api/v1/targets${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(v1DetailTargetResponse, await res.json()) };