import {z} from 'zod' export type GetApiApiV2DistributionListsArgs = {page?: number | undefined, limit?: number | undefined, query?: string | undefined, orderBy?: ('email' | 'name' | 'membersCount') | undefined, direction?: ('asc' | 'desc') | undefined}; export type GetApiApiV2DistributionListsOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const distributionListListItem = z.object({id: z.number().int(), name: z.string(), email: z.string(), membersCount: z.number().int()}); export const getDistributionListsPaginatedResponse = z.object({page: z.number().int(), pageCount: z.number().int(), limit: z.number().int(), resources: z.array(distributionListListItem)}); 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 all distribution lists * * GET /api/v2/distribution-lists */ export const getApiApiV2DistributionLists = async (args: GetApiApiV2DistributionListsArgs, options?: GetApiApiV2DistributionListsOptions) => { const {baseUrl = "https://rest.textmagic.com", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.query != null) query.append("query", String(args.query)) if (args.orderBy != null) query.append("orderBy", String(args.orderBy)) if (args.direction != null) query.append("direction", String(args.direction)) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/distribution-lists${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(getDistributionListsPaginatedResponse, await res.json()) };