import {z} from 'zod' export type ProtectGroupSortFieldOptions = string; export type ProtectGroupSortFieldOrder = string; export type GetApiGroupsArgs = {name?: string | undefined, mapping?: string | undefined, createdOn?: string | undefined, lastModifiedOn?: string | undefined, sortField?: ProtectGroupSortFieldOptions | undefined, sortOrder?: ProtectGroupSortFieldOrder | undefined, limit?: number | undefined, after?: (string | null) | undefined}; export type GetApiGroupsOptions = {baseUrl: string, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const cursors = z.object({after: z.string().optional()}); export const provider = z.string(); export const groupMapping = z.object({provider: provider, identity: z.string()}); export const group = z.object({name: z.string(), mappings: z.array(groupMapping)}); export const pagedGroups = z.object({cursors: cursors, results: z.array(group)}); 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 }; /** * List groups * * GET /groups * * Also accepts auth: jwtAuth, oauth2, oauth2 */ export const getApiGroups = async (args: GetApiGroupsArgs, options: GetApiGroupsOptions) => { const {baseUrl, fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.name != null) query.append("name", String(args.name)) if (args.mapping != null) query.append("mapping", String(args.mapping)) if (args.createdOn != null) query.append("createdOn", String(args.createdOn)) if (args.lastModifiedOn != null) query.append("lastModifiedOn", String(args.lastModifiedOn)) if (args.sortField != null) query.append("sortField", String(args.sortField)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.after != null) query.append("after", String(args.after)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/groups${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(pagedGroups, await res.json()) };