api-def API Reference
    Preparing search index...

    api-def API Reference

    api-def · GitHub license npm version build status

    Typed APIs with middleware support

    API def provides a unified way to type your endpoints allowing for compile time checking of query, body, response and even url parameters

    npm i api-def
    

    Requires Node.js 22 or newer. Browser builds target ES2020 and support both ESM and CommonJS consumers.

    import { Api } from "api-def";

    const api = new Api({
    baseUrl: "https://my-api/",
    name: "My API",
    });

    const fetchData = api
    .endpoint()
    .queryOf<{ includeAwesome: boolean }>()
    .responseOf<{ data: { awesome: boolean } }>()
    .build({
    id: "fetch_data",
    method: "get",
    path: "/data",
    });

    // calls GET https://my-api/data?includeAwesome=true
    const res = await fetchData.submit({
    query: { includeAwesome: true },
    });

    console.log(res.data); // { data: { awesome: true } }