import {z} from 'zod' export type GetApiPlansOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const publicPlan = z.object({tier: z.enum(["free", "silver", "gold", "platinum"]), name: z.string(), monthly_price_usd: z.number().int().gte(0), annual_price_usd: z.number().int().gte(0), monthly_credits: z.number().int().gte(0), api_access: z.enum(["full", "search_and_depth"]), max_api_keys: z.number().int().gte(1), rate_limit_per_minute: z.number().int().gte(1), daily_search_quota: z.number().int().gte(0), tech_stack_search: z.boolean(), checkout_url: z.string()}); export const publicPlans = z.object({currency: z.literal("USD"), plans: z.array(publicPlan), enterprise: z.object({contact_url: z.string(), note: z.string()}), how_to_get_a_key: z.object({signup_url: z.string(), keys_url: z.string(), note: z.string()}), checkout: z.record(z.string(), z.string()), docs: z.object({api_docs_url: z.string(), openapi_url: z.string()})}); export const getApiPlansResponse = z.object({data: publicPlans}); 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 plans, included credits and how to get a key * * GET /plans */ export const getApiPlans = async (options?: GetApiPlansOptions) => { const {baseUrl = "https://indexed.vc/api/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") 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()) };