import {z} from 'zod' export type GetApiPlansArgs = {api?: string | undefined, query?: string | undefined, sort?: ('score' | 'provider' | 'price') | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiPlansOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const plan = z.object({provider: z.unknown(), api: z.unknown(), website: z.unknown(), pricingPage: z.unknown(), pitch: z.unknown(), strengths: z.unknown(), aggregators: z.unknown(), waterfall: z.unknown(), freshness: z.unknown(), fieldFreshness: z.unknown(), history: z.unknown(), countries: z.unknown(), fullFamilyTree: z.unknown(), checks: z.unknown(), searchesIn: z.unknown(), keep: z.unknown(), use: z.unknown(), plan: z.unknown(), who: z.unknown(), auth: z.unknown(), billing: z.unknown(), charged: z.unknown(), topUp: z.unknown(), pay: z.unknown(), currency: z.unknown(), exchangeRate: z.unknown(), termMonths: z.unknown(), score: z.unknown(), from: z.unknown(), graduated: z.unknown(), units: z.unknown(), unit: z.unknown(), renews: z.unknown(), expires: z.unknown(), ends: z.unknown(), calls: z.unknown(), monthlyCap: z.unknown(), perThousand: z.unknown(), listPerThousand: z.unknown(), overagePerThousand: z.unknown(), unitsCap: z.unknown(), addons: z.unknown(), notFoundPerThousand: z.unknown(), catchAllPerThousand: z.unknown(), bounceRefund: z.unknown(), repeatFree: z.unknown(), alsoPaysFor: z.unknown(), sharesBudgetWith: z.unknown(), startPerCall: z.unknown(), startPerCallMax: z.unknown(), minimumPerCall: z.unknown(), perThousandFrom: z.unknown(), monthlyFee: z.unknown(), monthlyFeePer: z.unknown(), extras: z.unknown(), costUnits: z.unknown(), per: z.unknown(), resultsPerPage: z.unknown(), resultsCap: z.unknown(), perSecond: z.unknown(), perDay: z.unknown(), limitsCountedIn: z.unknown(), concurrent: z.unknown(), batch: z.unknown(), minBatch: z.unknown(), answer: z.unknown(), call: z.unknown(), method: z.unknown(), path: z.unknown(), send: z.unknown(), fields: z.unknown(), docsBehind: z.unknown(), latencyMs: z.unknown(), docs: z.unknown(), readFrom: z.unknown(), checked: z.unknown(), page: z.string().optional(), rank: z.number().int().gte(1).nullable().optional()}); export const getApiPlansResponse = z.object({total: z.number().int().optional(), offset: z.number().int().optional(), sheetPage: z.string().nullable().optional(), bestPage: z.string().nullable().optional(), plans: z.array(plan).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 the plans * * GET /plans */ export const getApiPlans = async (args: GetApiPlansArgs, options?: GetApiPlansOptions) => { const {baseUrl = "https://api.datacircle.dev/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.api != null) query.append("api", String(args.api)) if (args.query != null) query.append("query", String(args.query)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) const url = `${baseUrl.replace(/\/+$/, '')}/plans${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(getApiPlansResponse, await res.json()) };