Skip to content

Write APIs in TypeScript which compile to less than 400 bytes

License

Notifications You must be signed in to change notification settings

AlfieJones/api-typify

Repository files navigation


NPM version

api-typify

Write APIs in TypeScript which compile to less than 400 bytes.

With this package, developers can effortlessly define accurate and robust type definitions for their API responses, request payloads, and endpoints, reducing the risk of runtime errors and enhancing code maintainability.

Examples

// api.ts
const routes = {
  GET: {
    "/users/{id}": {
      req: undefined, // Requests body type
      res: User, // Response type
      queries?: {
        full?: boolean; // Include all user data
      }
    },
  },
};

const api = getAPI<typeof routes>(
  "https://api.example.com",
  fetch,
);

// Get's the user object
const user = await api.get("/users/{id}", {
  params: {
    id: "123",
  },
});

// Get's the full user object. Queries adds the request as a search parameter const user = await api.get("/users/{id}", { params: { id: "123", }, queries: { full: true, } });