import {z} from 'zod' export type GetApiV2BlocksGlobalArgs = {search_query?: string | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiV2BlocksGlobalOptions = {baseUrl?: string | undefined, HEADER?: string | undefined, fetch?: typeof fetch | undefined}; export const zonedDateTime = z.string(); export const blockResponse = z.object({block_id: z.string(), client_id: z.string().nullable(), created_date: zonedDateTime, filter_type: z.enum(["BLACK", "WHITE"]), list_type: z.enum(["CIDR", "EMAIL_DOMAIN", "NORMALIZED_EMAIL", "USER_AGENT"]), source: z.string(), user_id: z.string().nullable(), value: z.string()}); export const getApiV2BlocksGlobalResponse = z.array(blockResponse); 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 global blocks * * GET /v2/blocks/global * * Also accepts auth: QUERY, COOKIE */ export const getApiV2BlocksGlobal = async (args: GetApiV2BlocksGlobalArgs, options?: GetApiV2BlocksGlobalOptions) => { const {baseUrl = "https://api.extole.io", fetch: fetchFn = fetch, HEADER} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.search_query != null) query.append("search_query", String(args.search_query)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (HEADER !== undefined) headers.set("Authorization", HEADER) const url = `${baseUrl.replace(/\/+$/, '')}/v2/blocks/global${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(getApiV2BlocksGlobalResponse, await res.json()) };