import {z} from 'zod' export type PickGroupMemberUserUuid = {userUuid: string}; export type CreateGroup = {name: string, members?: Array | undefined}; export type CreateApiApiV1OrgGroupsArgs = {body: CreateGroup}; export type CreateApiApiV1OrgGroupsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const groupMember = z.object({lastName: z.string(), firstName: z.string(), email: z.string(), userUuid: z.string()}); export const groupWithMembers = z.object({organizationUuid: z.string(), updatedByUserUuid: z.string().nullable(), updatedAt: z.string(), createdByUserUuid: z.string().nullable(), createdAt: z.string(), name: z.string(), uuid: z.string(), memberUuids: z.array(z.string()), members: z.array(groupMember)}); export const apiCreateGroupResponse = z.object({results: groupWithMembers, status: z.literal("ok")}); 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 }; /** * Create group * * POST /api/v1/org/groups */ export const createApiApiV1OrgGroups = async (args: CreateApiApiV1OrgGroupsArgs, options: CreateApiApiV1OrgGroupsOptions) => { 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(/\/+$/, '')}/api/v1/org/groups${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(apiCreateGroupResponse, await res.json()) };