Socket
Book a DemoInstallSign in
Socket

rest-api-controller

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rest-api-controller

API Wrapper Module for a JS/TS client

latest
Source
npmnpm
Version
1.4.7
Version published
Maintainers
1
Created
Source

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

FAQs

Package last updated on 16 Dec 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.