import {z} from 'zod' export type IamV1RoleDataScope = {id?: string | undefined, roleId?: string | undefined, scopeType?: string | undefined, scopeValue?: string | undefined}; export type IamV1SetRoleDataScopesRequest = {id?: string | undefined, scopes?: Array | undefined}; export type UpdateApiV1IamRolesIdDataScopesArgs = {id: string, body: IamV1SetRoleDataScopesRequest}; export type UpdateApiV1IamRolesIdDataScopesOptions = {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 iamV1SetRoleDataScopesReply = 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 }; /** * SetRoleDataScopes 覆盖设置角色自定义数据范围明细。仅 dataScope=4 的角色允许设置。 * * PUT /v1/iam/roles/{id}/data-scopes */ export const updateApiV1IamRolesIdDataScopes = async (args: UpdateApiV1IamRolesIdDataScopesArgs, options: UpdateApiV1IamRolesIdDataScopesOptions) => { 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(/\/+$/, '')}/v1/iam/roles/${encodeURIComponent(String(args.id))}/data-scopes${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(iamV1SetRoleDataScopesReply, await res.json()) };