import {z} from 'zod' export type DncValueType = 'all' | 'email' | 'domain'; export type GetApiV1DncItemSearchArgs = {search?: string | undefined, pageSize?: number | undefined, page?: number | undefined, type: DncValueType}; export type GetApiV1DncItemSearchOptions = {baseUrl: string, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const dncItems = z.object({id: z.string().optional(), value: z.string().optional(), type: z.string().optional(), createdAt: z.string().optional()}); export const dncList = z.object({id: z.string().optional(), name: z.string().optional(), numberOfItems: z.number().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), addedBy: z.string().optional(), ownerId: z.string().optional(), dncItems: z.array(dncItems).optional()}); export const meta = z.object({totalItems: z.number().optional(), currentPage: z.number().optional(), itemsPerPage: z.number().optional(), totalPages: z.number().optional(), itemCount: z.number().optional()}); export const fetchDncListResponse = z.object({dncList: z.array(dncList).optional(), meta: meta.optional()}); export const fetchDncListResponseDto = z.object({message: z.string().optional(), payload: fetchDncListResponse.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 }; /** * Search DNC item across all lists * * GET /v1/dnc/item/search */ export const getApiV1DncItemSearch = async (args: GetApiV1DncItemSearchArgs, options: GetApiV1DncItemSearchOptions) => { const {baseUrl, fetch: fetchFn = fetch, xApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.search != null) query.append("search", String(args.search)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.page != null) query.append("page", String(args.page)) if (args.type != null) query.append("type", String(args.type)) if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/dnc/item/search${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(fetchDncListResponseDto, await res.json()) };