import {z} from 'zod' export type GetApiApiV1SystemsSystemIdPoliciesSubjectsArgs = {system_id: string, ids: string, type?: ('abac' | 'rbac') | undefined}; export type GetApiApiV1SystemsSystemIdPoliciesSubjectsOptions = {baseUrl: string, AppCode?: string | undefined, fetch?: typeof fetch | undefined}; export const iamPkgApiOpenHandlerPolicyResponseSubject = z.object({id: z.string().optional(), name: z.string().optional(), type: z.string().optional()}); export const handlerPolicyIDSubject = z.object({id: z.number().int().optional(), subject: iamPkgApiOpenHandlerPolicyResponseSubject.optional()}); export const getApiApiV1SystemsSystemIdPoliciesSubjectsResponse = z.object({code: z.number().int().optional(), data: z.array(handlerPolicyIDSubject).optional(), message: z.string().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 }; /** * query subjects/获取有权限的用户列表 * * GET /api/v1/systems/{system_id}/policies/-/subjects * * Also accepts auth: AppSecret */ export const getApiApiV1SystemsSystemIdPoliciesSubjects = async (args: GetApiApiV1SystemsSystemIdPoliciesSubjectsArgs, options: GetApiApiV1SystemsSystemIdPoliciesSubjectsOptions) => { const {baseUrl, fetch: fetchFn = fetch, AppCode} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.ids != null) query.append("ids", String(args.ids)) if (args.type != null) query.append("type", String(args.type)) if (AppCode !== undefined) headers.set("X-Bk-App-Code", AppCode) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/systems/${encodeURIComponent(String(args.system_id))}/policies/-/subjects${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(getApiApiV1SystemsSystemIdPoliciesSubjectsResponse, await res.json()) };