import {z} from 'zod' export type GetApiV1IamRolesIdDataScopesArgs = {id: string}; export type GetApiV1IamRolesIdDataScopesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const iamV1RoleDataScope = z.object({id: z.string().optional(), roleId: z.string().optional(), scopeType: z.string().optional(), scopeValue: z.string().optional()}); export const iamV1ListRoleDataScopesReply = z.object({scopes: z.array(iamV1RoleDataScope).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 }; /** * ListRoleDataScopes 获取角色自定义数据范围明细,仅 dataScope=4 的角色会实际使用这些明细。 * * GET /v1/iam/roles/{id}/data-scopes */ export const getApiV1IamRolesIdDataScopes = async (args: GetApiV1IamRolesIdDataScopesArgs, options: GetApiV1IamRolesIdDataScopesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/iam/roles/${encodeURIComponent(String(args.id))}/data-scopes${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(iamV1ListRoleDataScopesReply, await res.json()) };