import {z} from 'zod' export type GetApiGroupsArgs = {searchQuery?: string | undefined, orderBy?: ('NAME' | 'DATE_CREATED') | undefined, format?: ('ASCENDING' | 'DESCENDING' | 'ascending' | 'descending') | undefined, afterGroupId?: string | undefined, afterGroupName?: string | undefined, afterDateCreated?: string | undefined}; export type GetApiGroupsOptions = {baseUrl?: string | undefined, basic?: string | undefined, fetch?: typeof fetch | undefined}; export const getGroupResponse = z.object({groupId: z.string().optional(), groupChatId: z.string().optional(), groupChatName: z.string().optional(), memberCount: z.string().optional(), canManageGroupAccess: z.boolean().optional(), groupAccessLevel: z.enum(["FULL_ACCESS", "SEND_ONLY_ACCESS", "NO_ACCESS"]).optional()}); export const getGroupsPaginationParams = z.object({afterGroupId: z.string().optional(), afterGroupName: z.string().optional(), afterDateCreated: z.string().optional()}); export const getPaginatedGroupsResponseDto = z.object({groups: z.array(getGroupResponse).optional(), paginationParams: getGroupsPaginationParams.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 }; /** * Get paginated groups * * GET /groups */ export const getApiGroups = async (args: GetApiGroupsArgs, options?: GetApiGroupsOptions) => { const {baseUrl = "https://public.doubletick.io", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.searchQuery != null) query.append("searchQuery", String(args.searchQuery)) if (args.orderBy != null) query.append("orderBy", String(args.orderBy)) if (args.format != null) query.append("format", String(args.format)) if (args.afterGroupId != null) query.append("afterGroupId", String(args.afterGroupId)) if (args.afterGroupName != null) query.append("afterGroupName", String(args.afterGroupName)) if (args.afterDateCreated != null) query.append("afterDateCreated", String(args.afterDateCreated)) if (basic !== undefined) headers.set("Authorization", basic) 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(getPaginatedGroupsResponseDto, await res.json()) };