This package has been deprecated

Author message:

Use @ackee/antonio instead

ackee-api-agent

1.3.6 • Public • Published

Renamed (and reimplemented) to @ackee/antonio

ApiAgent · GitHub license npm version CI Status PRs Welcome

Quick start

import ApiAgent from 'ackee-api-agent';

const api = new ApiAgent('http://my-custom-api.ack.ee/api/v2');
const driversFilter = {
    filter: {
        offset: 3,
        limit: 10,
    },
}
const params = {
    resolveWithFullResponse: true,
};


// get data
api.get('/metadata')
    .then(metadata => console.log(metadata));

// set data
api.post('/drivers', driversFilter, params);
    .then(drivers => console.log(drivers))

api.post('/groups', { name: 'new group', description: 'New testing group' })
    .catch(err => console.log('Group was not created'));

Api

ApiAgent

Class that has following instance methods.

  • constructor(basePath: string)

  • request(path: string, params: IParams): Promise<IResponse|IResponseBody>

  • get(path: string, params: IParams): Promise<IResponse|IResponseBody>

  • post(path: string, data: IData, options: IOptions): Promise<IResponse|IResponseBody>

  • put(path: string, data: IData, options: IOptions): Promise<IResponse|IResponseBody>

  • patch(path: string, data: IData, options: IOptions): Promise<IResponse|IResponseBody>

  • delete(path: string, options: IOptions): Promise<IResponse|IResponseBody>

IData: Object

Key-value object containing any data you want to send to server in request body.

IOptions: Object

Object optionally containing following properties.

  • uriParams: Object Key-value object containing request uri params. Params that are found in url are replaced, rest is appended as a query parameters.
const config = {
    api: {
        base: "http://api-domain",
        user: "/users/:user",
    },
};

const api = new ApiAgent(config.api.base);
const uriParams = {
    user: 13,
    include: "address",
};

api.get(config.api.user, { uriParams });
// requested url is 'http://api-domain/users/13?include=address
  • qs: Object Ke-value object containing request query string params. Add query params to url
const config = {
    api: {
        base: "http://api-domain",
        users: "/users",
    },
};

const api = new ApiAgent(config.api.base);
const qs = {
    page: 3,
    offset: 20,
};

api.get(config.api.users, { qs });
// requested url is 'http://api-domain/users?page=3&offset=20
  • json: boolean Determine when optionally provided IData is in JSON format and should be serialized to string. Alos set request header ContentType=application/json. Default is true.

  • headers: Object Request HTTP headers.

  • resolveWithFullResponse: boolean Determine if request should resolve with full response or just a response body. Default is false.

  • blob: boolean Determine if response should be treated as a binary data. Default is false.

const api = new ApiAgent(config.api.base);

api.get("/download/pdf", { blob: true }).then((pdf) => {
    // pdf === Blob(205990) {size: 205990, type: "application/octet-stream"}

    const uri = URL.createObjectURL(pdf);
    // uri === 'blob:http://my-app-url/fb0b4600-8377-4357-a240-8346e94a0384'
});

IParams: Object

All the IOptions contains and following properties.

  • method: string Http method name.

  • body: IData Optional.

IResponse|IResponseBody

Type of response is determined by resolveWithFullResponse request property. It's either just response data in JSON format, or full response containing following properties.

const api = new ApiAgent(config.api.base);

api.get("/users").then((users) => {
    console.log(JSON.stringify(users));
    // [{"id":1,"name":"Me"},{"id":2,"name":"You"},{"id":3,"name":"He"}]
});

api.get("/users", { resolveWithFullResponse: true }).then((response) => {
    console.log(JSON.stringify(response.body));
    // [{"id":1,"name":"Me"},{"id":2,"name":"You"},{"id":3,"name":"He"}]

    response.status === 200; // true
    result.headers.get("x-total-count") === "3"; // true
});
  • body: IResponseBody

  • status: number

  • statusText: string

  • url: string

  • headers: Object Key-value object containing response headers.

  • ok: boolean

Readme

Keywords

none

Package Sidebar

Install

npm i ackee-api-agent

Weekly Downloads

1

Version

1.3.6

License

MIT

Unpacked Size

23.7 kB

Total Files

15

Last publish

Collaborators

  • pivl
  • hofp37
  • grissius
  • ackeecz
  • horaklukas
  • borecekbaji
  • davidbilik