restmix
TypeScript icon, indicating that this package has built-in type declarations

0.5.0 • Public • Published

Restmix

pub package

A lightweight Typescript friendly requests manager for rest apis

📚 Documentation

import { useApi, ApiResponse } from 'restmix';

const api = useApi();

interface TodoItemContract {
  userId: number;
  id: number;
  title: string;
  completed: boolean;
}

const res: ApiResponse<TodoItemContract> = await api.get<TodoItemContract>(
  "https://jsonplaceholder.typicode.com/todos/1",
);
if (res.ok) {
  // status code is in the 200/299 range
  const data: TodoItemContract = res.data;
} else {
  // status code is > 299
  const responseStatus: number = res.status;
  throw new Error(res.statusText)
}

It is the same as fetch except that it takes care of the response body parsing and delivers typed data

interface ApiResponse<T = Record<string, any> | Array<any>> {
  ok: boolean;
  url: string;
  headers: Record<string, string>;
  status: number;
  statusText: string;
  data: T;
  text: string;
}

Readme

Keywords

none

Package Sidebar

Install

npm i restmix

Weekly Downloads

9

Version

0.5.0

License

MIT

Unpacked Size

24.2 kB

Total Files

8

Last publish

Collaborators

  • synw