import {z} from 'zod' export type GetApiListsSuppressionsArgs = {cursor?: string | undefined}; export type GetApiListsSuppressionsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const suppressionList = z.object({id: z.string(), name: z.string(), status: z.enum(["Pending", "Validating", "Ready", "Invalid", "Retired", "Deleted"]), createdAt: z.number().int(), domainCount: z.number().int().lte(250000).optional(), sizeBytes: z.number().int().optional(), error: z.string().optional(), retiredAt: z.number().int().optional()}); export const suppressionListPage = z.object({lists: z.array(suppressionList), nextCursor: z.string().nullable(), allocatedBytes: z.number().int(), limits: z.object({fileBytes: z.unknown(), domains: z.unknown(), accountBytes: z.unknown(), uploadsPerDay: z.unknown(), pendingUploads: z.unknown(), uploadSeconds: z.unknown(), activeUploadSeconds: z.unknown(), abandonedSeconds: z.unknown()})}); 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 account suppression IDs, statuses and storage allocation * * GET /lists/suppressions */ export const getApiListsSuppressions = async (args: GetApiListsSuppressionsArgs, options?: GetApiListsSuppressionsOptions) => { const {baseUrl = "https://api.wappalyzer.com/v2", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cursor != null) query.append("cursor", String(args.cursor)) if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/lists/suppressions${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(suppressionListPage, await res.json()) };