1.4.7 • Published 3 years ago

rest-api-controller v1.4.7

Weekly downloads
3
License
MIT
Repository
bitbucket
Last release
3 years ago

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

1.4.7

3 years ago

1.4.6

3 years ago

1.4.5

3 years ago

1.4.4

3 years ago

1.4.3

3 years ago

1.4.2

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.3.0

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago