rest-client

Client for foxify-restify-odin package

Npm Version TypeScript Version Package Quality Npm Total Downloads Npm Monthly Downloads Open Issues Closed Issues Known Vulnerabilities Dependencies Status Pull Requests License Build Status Github Stars Github Forks

Table on Contents

Getting Started

Prerequisites

npm i axios prototyped.js qs

if you’re using typescript:

npm i -D @types/axios @types/qs

Installation

npm i @foxify/rest-client

Usage

const Client = require("@foxify/rest-client");

Client.setProxy()

Sets global proxy

Client.proxy({
  host: "host",
  port: 8080,
  auth: { // not required
    username: "username",
    password: "password",
  }
});

Client.setAPI()

Sets global API address

Client.setAPI("https://your_api_address");

Client.setToken()

Sets global API token

Client.setToken("your bearer token");

Client.setQSOptions()

Sets global QS.stringify options

Client.setQSOptions({ ... });

client

Create a new instance

const client = new Client();

client.setProxy()

Sets instance’s proxy

Client.proxy({
  host: "host",
  port: 8080,
  auth: { // not required
    username: "username",
    password: "password",
  }
});

client.setAPI()

Sets instance’s API address

Client.setAPI("https://your_api_address");

client.setToken()

Sets instance’s API token

Client.setToken("your bearer token");

client.setQSOptions()

Sets instance’s QS.stringify options

Client.setQSOptions({ ... });

client.count()

class Client<T extends object = any> {
  public count(resource: string): Count<T>;
  public count(prefix: string, resource: string): Count<T>;
}

Requests [GET] /{resource}/count or /{prefix}/{resource}/count

Returns an instance of Count

client.index()

class Client<T extends object = any> {
  public index<R extends string>(resource: R): Index<T, R>;
  public index<R extends string>(prefix: string, resource: R): Index<T, R>;
}

Requests [GET] /{resource} or /{prefix}/{resource}

Returns an instance of Index

client.show()

class Client<T extends object = any> {
  public show(resource: string): Show<T>;
  public show(prefix: string, resource: string): Show<T>;
}

Requests [GET] /{resource}/{id} or /{prefix}/{resource}/{id}

Returns an instance of Show

client.store()

class Client<T extends object = any> {
  public store(resource: string, data: T): Promise<T>;
  public store(prefix: string, resource: string, data: T): Promise<T>;
  public store(resource: string, data: T, callback: Client.Callback<T>): void;
  public store(prefix: string, resource: string, data: T, callback: Client.Callback<T>): void;
}

Requests [POST] /{resource} or /{prefix}/{resource} by the given resource data

Results an object which is the stored resource

client.update()

class Client<T extends object = any> {
  public update(resource: string, id: string, data: Partial<T>): Promise<T>;
  public update(prefix: string, resource: string, id: string, data: Partial<T>): Promise<T>;
  public update(resource: string, id: string, data: Partial<T>, callback: Client.Callback<T>): void;
  public update(prefix: string, resource: string, id: string, data: Partial<T>, callback: Client.Callback<T>): void;
}

Requests [PATCH] /{resource}/{id} or /{prefix}/{resource}/{id} by the given resource data

Results an object which is the updated resource

client.delete()

class Client<T extends object = any> {
  public delete(path: string, id: string): Promise<string>;
  public delete(path: string, id: string, callback: Client.Callback<string>): void;
}

Requests [DELETE] /{resource}/{id} or /{prefix}/{resource}/{id}

Results a message

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE file for details

Support

If my work helps you, please consider

Become A Patron

Buy Me A Coffee