import {z} from 'zod' export type Group = {id?: number | undefined, name?: string | undefined, groupType?: string | undefined, subsystemId?: number | undefined, subsystemName?: string | undefined, remarks?: string | undefined, enableFlag?: number | undefined, departmentId?: string | undefined, departmentName?: string | undefined, clusterName?: string | undefined, rmb_dcn?: string | undefined, rmb_idc?: string | undefined, service_id?: number | undefined}; export type CreateApiCcV1GroupsArgs = {body: Group}; export type CreateApiCcV1GroupsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const group = z.object({id: z.number().int().optional(), name: z.string().optional(), groupType: z.string().optional(), subsystemId: z.number().int().optional(), subsystemName: z.string().optional(), remarks: z.string().optional(), enableFlag: z.number().int().optional(), departmentId: z.string().optional(), departmentName: z.string().optional(), clusterName: z.string().optional(), rmb_dcn: z.string().optional(), rmb_idc: z.string().optional(), service_id: z.number().int().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 }; /** * Returns a list of groups. * * POST /cc/v1/groups */ export const createApiCcV1Groups = async (args: CreateApiCcV1GroupsArgs, options?: CreateApiCcV1GroupsOptions) => { const {baseUrl = "https://gateway.mlss.cc", 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(/\/+$/, '')}/cc/v1/groups${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(group, await res.json()) };