UNPKG

infinibrowser

Version:

A TypeScript API wrapper for the Infinibrowser Wiki

85 lines (84 loc) 2.27 kB
// src/types/items.ts import { z } from "zod"; var ElementSchema = z.object({ id: z.string(), emoji: z.string() }); var ItemDataSchema = z.object({ id: z.number(), text: z.string(), emoji: z.string(), use_count: z.number(), recipe_count: z.number(), depth: z.number() }); var RecipeSchema = z.tuple([ElementSchema, ElementSchema]); var RecipesDataSchema = z.object({ total: z.number(), recipes: z.array(RecipeSchema) }); var UseSchema = z.object({ pair: ElementSchema, result: ElementSchema }); var UsesDataSchema = z.object({ total: z.number(), uses: z.array(UseSchema) }); var StepSchema = z.object({ a: ElementSchema, b: ElementSchema, result: ElementSchema }); var LineageSchema = z.array(StepSchema); var LineageDataSchema = z.object({ steps: LineageSchema, missing: z.record(z.union([z.string(), z.literal("loop")])) }); // src/client.ts class Infinibrowser { static API_URL = "https://infinibrowser.wiki/"; async _get(options) { const url = new URL(options.path, Infinibrowser.API_URL); if (options.params) { for (const [key, value] of Object.entries(options.params)) { if (value !== null && value !== undefined) { url.searchParams.append(key, String(value)); } } } const res = await fetch(url.toString()); if (!res.ok) { throw new Error(`HTTP ${res.status} - ${res.statusText}`); } const json = await res.json(); return options.schema.parse(json); } async getItem(id) { const path = "/api/item"; const params = { id }; const data = await this._get({ path, params, schema: ItemDataSchema }); return data; } async getRecipes(id, offset = 0) { const path = "/api/recipes"; const params = { id, offset }; const data = await this._get({ path, params, schema: RecipesDataSchema }); return data; } async getUses(id, offset = 0) { const path = "/api/uses"; const params = { id, offset }; const data = await this._get({ path, params, schema: UsesDataSchema }); return data; } async getLineage(id) { const path = "/api/recipe"; const params = { id }; const data = await this._get({ path, params, schema: LineageDataSchema }); return data; } } export { Infinibrowser };