import { createTypeLevelClient, initUntypeable } from "../../src/index"; import { Person, Paginated, Planet, Film, Vehicle } from "./types"; const u = initUntypeable(); /** * Create the router */ const router = u.router({ "/people/:id": u.input<{ id: string }>().output(), "/people": u.output>(), "/planets/:id": u.input<{ id: string }>().output(), "/planets": u.output>(), "/films/:id": u.input<{ id: string }>().output(), "/films": u.output>(), "/vehicles/:id": u.input<{ id: string }>().output(), "/vehicles": u.output>(), }); /** * Create the client, using the zero-bundle method */ export const fetchFromSwapi = createTypeLevelClient( (path, input = {}) => { // Replace dynamic path params in url const pathWithParams = path.replace( /:([a-zA-Z0-9_]+)/g, (_, key) => input[key], ); return fetch(`https://swapi.dev/api${pathWithParams}`, { method: "GET", headers: { "Content-Type": "application/json", }, }).then((res) => { if (!res.ok) { throw new Error(`HTTP error! status: ${res.status}`); } return res.json(); }); }, );