rest-api-controller
v1.4.7
Published
API Wrapper Module for a JS/TS client
Downloads
21
Readme
REST API Controller
Generic REST API wrapper for connecting any client API to a HTTP request handling library.
Installing
You can install REST API Controller using NPM to get the latest version of our library.
npm install rest-api-controller
Usage
Once installed, create your own client by using the API controller.
import ApiController from "rest-api-controller";
const API_URL = "https://my-example-api.com/v1";
const ROUTES = {
users: "/users/:id",
cats: "/users/:id/cats",
dogs: "/users/:id/dogs"
};
type RouteKey = keyof typeof ROUTES;
class ExampleClient {
private controller: ApiController<RouteKey>;
constructor() {
this.controller = new ApiController(API_URL, ROUTES);
}
public async getUsers() {
return this.controller.get<User[]>("users");
}
public async getUserCats(userId: string) {
return this.controller.get<Cats[]>("cats", { id: userId });
}
public async createUserDog(userId: string) {
const data = {
name: "Rex",
age: 3,
color: "black"
};
return this.controller.post<Dog>("dogs", { id: userId }, data);
}
}