rest-api-controller
TypeScript icon, indicating that this package has built-in type declarations

1.4.7 • Public • Published

REST API Controller

npm npm bundle size semantic-release

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);
    }
}

License

MIT

Dependencies (2)

Dev Dependencies (8)

Package Sidebar

Install

npm i rest-api-controller

Weekly Downloads

3

Version

1.4.7

License

MIT

Unpacked Size

21.5 kB

Total Files

16

Last publish

Collaborators

  • digitalmasterpieces-admin