Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

base-api-client

pustovitDmytro1kMIT1.5.10

Base API client backed by axios

readme

Logo

base-api-client

Base API client backed by axios.

Version Bundle size Downloads

CodeFactor SonarCloud Codacy Scrutinizer

Dependencies Security Build Status Coverage Status

Commit activity FOSSA License Made in Ukraine

🇺🇦 Help Ukraine

I woke up on my 26th birthday at 5 am from the blows of russian missiles. They attacked the city of Kyiv, where I live, as well as the cities in which my family and friends live. Now my country is a war zone.

We fight for democratic values, freedom, for our future! Once again Ukrainians have to stand against evil, terror, against genocide. The outcome of this war will determine what path human history is taking from now on.

💛💙 Help Ukraine! We need your support! There are dozen ways to help us, just do it!

Table of Contents

Requirements

Platform Status

To use library you need to have node and npm installed in your machine:

  • node >=10
  • npm >=6

Package is continuously tested on darwin, linux and win32 platforms. All active and maintenance LTS node releases are supported.

Installation

To install the library run the following command

  npm i --save base-api-client

Usage

Example of telegram client extended from BaseAPI:

import BaseAPI from 'base-api-client';

export default class TelegramAPI extends BaseAPI {
    constructor(id, token) {
        super(`https://api.telegram.org/${id}:${token}`);
    }

    message(chatId, html) {
        return this.post('sendMessage', {
            'parse_mode' : 'HTML',
            'text'       : html,
            'chat_id'    : chatId
        });
    }

    file(chatId, fileId) {
        return this.post('sendDocument', {
            'document' : fileId,
            'chat_id'  : chatId
        });
    }
}

Constructor arguments

  1. url - base URL of the API. Can consist apiPrefix. Will be cast to nodeJS URL object.
  2. options - object with next attributes:
    • timeout - timeout in ms format. Will be cast to integer (in milliseconds). Default: 1m.
    • logger - if applied, will add debug and verbose messages before and after each request. Should have next interface: logger.log(level, object).

Methods

HTTP methods:

  • get(url, params, options)
  • post(url, data, options)
  • patch(url, data, options)
  • put(url, data, options)
  • delete(url, options)

in all aforementioned methods url can be both global, or relative to base URL (defined in constructor). params are url query params, and data is JSON body. options are passed directly to axios request

Headers

Implement getHeaders() method to define select headers for API. Alternativelly, pass headers in options for each request if headers need behave differently.

Basic auth use auth setting, if you want to use basic auth for each request.

this.auth = {
    username : '',
    password : ''
};

Data processing

Next methods can be used for default data pre/post-processing:

        onError(error) {
            if (error.isAxiosError) {
                throw new API_ERROR(error);
            }
            throw error;
        },

        onResponse(res) {
            return res.data;
        }

Errors

the package exposes API_ERROR, that can be used outside:

import BaseAPI, { API_ERROR } from 'base-api-client';

class API extends BaseAPI {
    constructor() {
        super('http://wiwbif.is/fugo');
    }

    async createUser(email) {
        try {
            const user = await this.post('/users', { email });


            return user.id;
        } catch (error) {
            if (error instanceof API_ERROR) {
                console.log('raw http error:', error.payload);
                throw error;
            }
        }
    }
}

Logging and Tracing

You can pass logger while api creation, but also this can be done by calling initLogger(logger) method directly.

Alternatively, use polymorphism and implement the log(level, data) method on descendants.

Autogenerated Trace ID is atached to each log. If you need control over traceId generation, implement getTraceId(reqOptions, settings) method.

Testing

Depending on selected approach use setMock(mockFuction) or implement _axios(axiosOptions) method. Both mockFuction and _axios will receive axios options instead of axios instance, and should return expected result. Default mock function is () => ({ data: 1 }).

To check examples of api mocks and tesing, see implementation section.

Implementations

Looking for more examples? Check real implementations of famous APIs:

API Organization Reference Examples
Telegram Bot Telegram Bot API
Telegra.ph Telegram Telegraph API
AWS SNS Amazon AWS docs
Gitea | Gitea Swagger
GitHub Apps Microsoft Apps Reference
GitHub Repos Microsoft Repositories Reference
Heroku Salesforce Platform API Reference
JIRA Atlassian REST API
Confluence Atlassian REST API

Contribute

Make the changes to the code and tests. Then commit to your branch. Be sure to follow the commit message conventions. Read Contributing Guidelines for details.

changelog

1.5.10 (2023-02-07)

Chore

  • anti-terrorism disclaimer (96327fe)
  • fixes eslint-plugin-unicorn version (dd45e9a)
  • fixes npm audit (0500470)
  • fixes some npm audit vulnerabilities (b913fee)
  • fixes some npm audit vulnerabilities (#86) (eb549de), closes #86
  • Lock file maintenance (85b7c09)
  • Update dependency danger to v11 (1779a27)
  • Update dependency nanoid to 3.1.31 [SECURITY] (#83) (89d3014), closes #83
  • Update dependency node-fetch to 2.6.7 [SECURITY] (#84) (11e82bc), closes #84
  • Update devDependencies (non-major) (01bd6c0)
  • Update devDependencies (non-major) (85a0161)
  • Update devDependencies (non-major) (#65) (d60ef28), closes #65
  • Update devDependencies (non-major) (#66) (69aac8f), closes #66
  • Update devDependencies (non-major) (#92) (f492769), closes #92

Docs

1.5.9 (2022-06-30)

Chore

  • fixes some npm audit vulnerabilities (#57) (caddd80), closes #57
  • Lock file maintenance (#59) (c0e1c10), closes #59
  • Update devDependencies (non-major) (#40) (bf93baa), closes #40
  • Update devDependencies (non-major) (#41) (dd60b5d), closes #41

Docs

Fix

  • allow use URL in browsers (3c50963)

1.5.8 (2022-01-28)

Upgrade

  • Update dependency myrmidon to v1.7.2 (#55) (b82755c), closes #55

1.5.7 (2022-01-28)

Chore

Docs

1.5.6 (2022-01-26)

Chore

  • fixes some npm audit vulnerabilities (#53) (cd7f358), closes #53

Upgrade

  • Update dependency axios to v0.25.0 (749f044)

1.5.5 (2022-01-22)

Chore

  • (ci) fix gitleaks version (badd1c1)
  • adds telegra.ph release notes (aa811a5)
  • calc fossa results in cirrus (b2b48c1)
  • fixes some npm audit vulnerabilities (9330c38)
  • fixes some npm audit vulnerabilities (f7a14d5)
  • replace appveyor to actions (13aff75)
  • update deps badge (0836169)
  • upgrade semantic-release to v.19 [security] (0888454)

Docs

1.5.4 (2021-12-18)

Chore

  • adds a security policy (ed86aba)
  • adds circle-ci conditions (5a40980)
  • adds sponsorships (35ed8e0)
  • adds whitesorce bolt bot (0888eee)
  • change renovate schedule (b983eab)
  • drop semantic-release preinstalled plugins (69b1950)
  • fixes audit (14597ff)
  • fixes later schedule (a0cfb5c)
  • integrate lalaps (dd4f238)
  • Lock file maintenance (29fe760)
  • Lock file maintenance (9892cc5)
  • Lock file maintenance (98e62b3)
  • Lock file maintenance (7af4484)
  • Lock file maintenance (1ffc991)
  • test-security in separete ci job (a4bd846)
  • test-security in separete ci job (9559dbc)
  • Update devDependencies (non-major) (609151a)
  • Update devDependencies (non-major) (566dc53)
  • Update devDependencies (non-major) (3ce8d29)
  • Update devDependencies (non-major) (5b1a6c3)
  • Update devDependencies (non-major) (51200bf)
  • Update devDependencies (non-major) (#48) (b3f6184), closes #48

Docs

  • added basic documentation (05e1fac)

Fix

  • not send body for get requests (40254c1)

1.5.3 (2021-09-09)

Chore

  • adds 'typo' PR template (e20404d)
  • adds bump strategy for devDependencies (non-major) (03c9587)
  • adds CODE_OF_CONDUCT (a969a8d)
  • adds gitleaks to circle pipeline (114946e)
  • combine mine packages in renovate updates (54c97b8)
  • create auto pr for major dependencies (d2583a6)
  • enhance own updates commit messages (49a2365)
  • fixes audit [devDependencies] (859783a)
  • fixes audit [devDependencies] (d08b1fc)
  • fixes renovate config (982abaf)
  • Lock file maintenance (47bfacf)
  • Lock file maintenance (19c2389)
  • Lock file maintenance (2b00aec)
  • Lock file maintenance (dea06c9)
  • Lock file maintenance (e0043d8)
  • Lock file maintenance (5d305a8)
  • not allow to fail on node 16 (22ed683)
  • Update commitlint monorepo to v13 (c823b00)
  • Update devDependencies (non-major) (79914ef)
  • Update devDependencies (non-major) (33211c3)
  • Update devDependencies (non-major) (#32) (14ec77c), closes #32
  • Update devDependencies (non-major) to v13 (6894ad9)

Upgrade

  • Update dependency axios to v0.21.4 (9b3b7fe)

1.5.2 (2021-07-19)

Upgrade

  • Update dependency myrmidon to v1.6.1 (3c45881)

1.5.1 (2021-07-14)

Chore

Upgrade

  • Update dependency myrmidon to v1.5.8 (d0cb98b)

1.5.0 (2021-07-12)

Chore

  • adds sonarcloud config (c92973d)
  • Lock file maintenance (cdf4f11)
  • regenerate lock file (02a41ed)
  • removes unused devDependencies (ddb100a)
  • Update dependency eslint-plugin-regexp to ^0.12.0 (51ff304)
  • Update dependency eslint-plugin-regexp to ^0.12.0 (46bc24b)
  • Update dependency eslint-plugin-regexp to ^0.13.0 (4dd38ea)
  • Update dependency eslint-plugin-regexp to ^0.13.0 (#36) (3a8d3ec), closes #36
  • Update dependency eslint-plugin-sonarjs to ^0.9.0 (81d9681)
  • Update dependency eslint-plugin-sonarjs to ^0.9.0 (ca3e847)
  • Update dependency eslint-plugin-unicorn to v34 (c53ae90)
  • Update dependency eslint-plugin-unicorn to v34 (#34) (86efbac), closes #34
  • Update dependency husky to v7 (3578711)
  • Update dependency husky to v7 (9d45edf)

Docs

New

  • Error idempotency #18 (4bbdb1a), closes #18
  • export axiosError for futher testing, closes #18 (510e7e8), closes #18
  • use separate axios instance, closes #15 (2cb90d3), closes #15

1.4.5 (2021-06-13)

Chore

  • adds stabilityDays to renovate (3b262bc)
  • fix json in renovate (8ad7928)
  • fixes npm audit (c3beccd)
  • integrate fossa (b65e961)
  • integrate node-package-tester (9d53bf3)
  • Lock file maintenance (3291a21)
  • set YARGS_MIN_NODE_VERSION 10 (9b6bef9)
  • Update dependency eslint-plugin-unicorn to v33 (1c59af3)
  • Update dependency eslint-plugin-unicorn to v33 (bfa00ff)
  • Update dependency glob-parent to 5.1.2 [SECURITY] (c957512)
  • Update dependency mocha to v9 (93b7a17)

Fix

  • set babel target node version to 10 (f736568)

1.4.4 (2021-05-28)

Upgrade

  • Update dependency myrmidon to v1.5.3 (33b4ab6)

1.4.3 (2021-05-27)

Chore

  • 'Chore' semanticCommitType for updating devDependencies (4bf79e1)
  • (tests) clearCache on module load is optional (96c2ae5)
  • contributor login in danger message (76349e6)
  • dont store package-tests artifacts (50ebff1)
  • export default in tests (9d210e6)
  • fixes tests (e8a96fc)
  • integrate APPVEYOR_BUILD_ID to build tests (16cdd48)
  • update code to match new styleguide (0105cba)
  • update eslint (6b174b1)
  • update eslint-config-incredible (2f39edb)
  • update eslint-config-incredible (58c31d2)

Docs

  • adds node releases roadmap (ba16142)
  • move Contributing Guidelines to separate file. (081e4ed)

Fix

  • send PATCH on patch requests (abc4a78)

1.4.2 (2021-05-15)

Chore

  • additional quotes in glob pattern (f6d15cf)
  • adds appveyor (d560b4e)
  • fixes prevent require handler (7923f0e)
  • not fail package if no tmp exists (5693813)
  • package-tester improvements (d5d868b)
  • prevent package:test from using devdependencies (dc896f3)
  • update default renovate rules (7e83bc4)

Fix

  • move uuid to dependencies (fa7eca0)

1.4.1 (2021-05-10)

Upgrade

  • Update dependency myrmidon to v1.5.2 (c0fcc87)

1.4.0 (2021-05-08)

New

  • pass axiosOptions to traceId (b3f4d87)

1.3.0 (2021-05-08)

Chore

  • (tests) moves load to factory (4acb941)
  • fixes spellcheck in bugreport (7d754b6)
  • set myself as default assignee in pr (0dcd0ce)
  • tests/entry.js module resolving (c840f8b)
  • Update dependency @rollup/plugin-commonjs to v19 (bf29948)
  • Update dependency @rollup/plugin-node-resolve to v13 (265fd82)
  • update eslint (4aac03c)
  • update lock file (4ffb878)

New

  • log api in instance for redefine from top level (e177138)

1.2.2 (2021-05-05)

Upgrade

  • Update dependency myrmidon to v1.5.1 (9f874a5)

1.2.1 (2021-05-04)

chore

Chore

  • adds CIRCLE_SKIP_DEPLOY variable (231549d)
  • adds pr context (ce163d9)
  • adds trusted bots to danger (892f4a1)
  • deploy ci as single command (96c2800)
  • exit code 0 when skip (f6ee0bf)
  • fixes ci (0576a01)
  • inverse logical condition (65dd0ad)
  • telegram notifications on release (3a8036a)
  • Update dependency @rollup/plugin-node-resolve to v13 (e884252)
  • Update dependency fs-extra to v10 (f1b8460)
  • Update dependency fs-extra to v10 (a0adecc)
  • Update dependency fs-extra to v9 (e0ccfb4)
  • Update issue templates (8fdb1af)
  • Update pr template (1d4eb34)
  • update semantic to use commit convention (7e079b2)
  • use danger for internall pr (d838ede)
  • use incredible eslint config (b03d74a)

Upgrade

  • Update dependency myrmidon to v1.5.0 (cfa40c7)

1.2.0 (2021-04-30)

Chore

  • fixes Breaking increment in semantic-release (d1c4d53)

Docs

  • change travis badge to circle-ci (ebabb61)
  • fixes spellcheck (bc06d85)

New

  • adds context to circle-ci (a3a1f33)

1.1.0 (2021-04-30)

Breaking

1.0.0 (2021-04-30)

Chore

  • (git) Add logs to gitignore (d4d2077)
  • (refactor) remove unused argument (6f28b59)
  • (test) helper packages for tests (e195b58)
  • (test) working with tmp dir for test factory (cfeafbc)
  • adds commitlint (38ded63)
  • adds danger to circle-ci (ae48ac3)
  • adds danger to validate pr (7fb7040)
  • adds danger token to circle (82f4156)
  • adds dangerfile to npm ignore (8842c70)
  • adds dummy line to calc coverage (800de67)
  • adds empty line to pr comment (36d8a09)
  • adds jscpd to ignore (8520cea)
  • adds lock file lint (f5a4679)
  • adds target branch to semantic release (c4fb3f1)
  • adds technical dept check (e16a8e2)
  • adds test-results to circle-ci (767c5b3)
  • adds tests for packing process (9b9602d)
  • adds tests for prior node versions (4b00012)
  • change extention of test files to .test.js (d84ac03)
  • change tgz label (5e0e512)
  • corrected extglob matching (72a2201)
  • corrected pack pattern (273497a)
  • deploy in circle-ci (a62a5f2)
  • disable build for coverage check (0b6e984)
  • dont pin devDeps in renovate (5fb0e8d)
  • fill test entry with template (9177859)
  • fixes context config (70447d5)
  • Fixes danger-pr in circe-ci (adff8b3)
  • fixes debt typo in travis job (f9fd463)
  • fixes package process (364e26b)
  • fixes renovate config (05c5bc5)
  • ignoring all for npm packaging (409201a)
  • multi os tests for travis (7c8ce28)
  • run pr workflow only for pull requests (03e0b08)
  • split circle ci jobs (9f75a6b)
  • update .renovaterc to automerge after successfull checks (2e76ceb)
  • Update dependency babel-plugin-module-resolver to v4 (b8eb86f)
  • Update dependency eslint to v7 (0e79e0f)
  • Update dependency mocha to v8 (627a45b)
  • Update dependency nyc to v15 (1caf199)
  • Update dependency uuid to v8 (ebae34b)
  • update semantic release rules (a075dab)
  • Update semantic-release monorepo (b7ab2b1)
  • update travis badge (e159104)
  • updates semanticCommitType rule (1a1d119)
  • upgrade circle-ci to 2.1 (fab79a9)
  • use context in circle-ci (4d56277)
  • use coveralls token per repository (93afb78)
  • use native tarball generation (eeefda5)
  • using static test entry (21e5b7d)

Docs

Fix

New

Upgrade