import {z} from 'zod' export type RoleUpdate = {name?: string | undefined, scopes?: Array | undefined, providerId?: string | undefined, organizationIds?: Array | undefined, description?: string | undefined, organizations?: ({add?: Array | undefined, remove?: Array | undefined}) | undefined}; export type UpdateApiApiV2RolesRoleIdArgs = {'x-org-id'?: string | undefined, 'x-provider-id'?: string | undefined, body: RoleUpdate}; export type UpdateApiApiV2RolesRoleIdOptions = {baseUrl?: string | undefined, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const role = z.object({id: z.string().optional(), name: z.string(), scopes: z.array(z.string()), providerId: z.string().optional(), organizationIds: z.array(z.string()).optional(), description: z.string().optional(), createdBy: z.string().optional(), lastUpdatedBy: z.string().optional(), isCustom: z.boolean().optional(), ownerType: z.literal("provider").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 }; /** * Update a role. * * PUT /api/v2/roles/{role_id} */ export const updateApiApiV2RolesRoleId = async (args: UpdateApiApiV2RolesRoleIdArgs, options?: UpdateApiApiV2RolesRoleIdOptions) => { const {baseUrl = "https://console.jumpcloud.com", fetch: fetchFn = fetch, xApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-org-id'] != null) headers.set("x-org-id", String(args['x-org-id'])) if (args['x-provider-id'] != null) headers.set("x-provider-id", String(args['x-provider-id'])) if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/roles/${encodeURIComponent(String(args.role_id))}${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(role, await res.json()) };