import {z} from 'zod' export type GroupPutRequestObject = {displayName?: string | undefined, members?: Array> | undefined}; export type UpdateApiGroupsIdArgs = {id: string, attributes?: string | undefined, excludedAttributes?: string | undefined, body: GroupPutRequestObject}; export type UpdateApiGroupsIdOptions = {baseUrl?: string | undefined, OAuth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const groupPutResponseObject = z.object({displayName: z.string().optional(), meta: z.object({created: z.string().optional(), location: z.string().optional(), lastModified: z.string().optional(), resourceType: z.string().optional()}).optional(), schemas: z.array(z.string()).optional(), id: z.string().optional(), members: z.array(z.object({})).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 Group - PUT * * PUT /Groups/{id} */ export const updateApiGroupsId = async (args: UpdateApiGroupsIdArgs, options?: UpdateApiGroupsIdOptions) => { const {baseUrl = "https://api.asgardeo.io/t//scim2", fetch: fetchFn = fetch, OAuth2ClientCredentials} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/scim+json") headers.set('accept', "application/scim+json") if (args.attributes != null) query.append("attributes", String(args.attributes)) if (args.excludedAttributes != null) query.append("excludedAttributes", String(args.excludedAttributes)) if (OAuth2ClientCredentials !== undefined) headers.set('authorization', `Bearer ${OAuth2ClientCredentials}`) const url = `${baseUrl.replace(/\/+$/, '')}/Groups/${encodeURIComponent(String(args.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(groupPutResponseObject, await res.json()) };