3.8.15 • Published 3 years ago

@kano/api-client v3.8.15

Weekly downloads
1,214
License
ISC
Repository
-
Last release
3 years ago

Kano API client

Provides a js API for browsers to communicate with our set of microservices.

Usage

  • Import the client for the service you require
  • Instanciate with options
  • Call its methods
import { AccountClient } from '@kano/api-client/index.js';

const account = new AccountClient({
    // Where to find the API
    url: 'https://my-api.stuff.me',
    // Default headers will be sent with all requests. Has default for json
    defaultHeaders: {
        'X-Special': 'Value',
    },
});

account.login('me', 'my-password').then(session => console.log(session));

You can share options across clients by giving another client to the contructor:

import { AccountClient, UserClient } from '@kano/api-client/index.js';

const account = new AccountClient({
    // Where to find the API
    url: 'https://my-api.stuff.me',
});

// Same options here
const user = new UserClient(account);

Clients

Implemented clients:

  • AccountClient:
    • login(username, password)
    • register({ username, password, email, marketing })
    • checkUsernameAvailability(username)
  • UserClient:
    • getById(id)
    • getByUsername(username)

More to come

Plugins

You can add plugin to a client. A plugin is a set of methods called during the lifecycle of a request.

Example:

account.addPlugin({
    // Will be called before fetch. The endpoint about to be called will be provided
    // You can change any of its values. Adding a response property will cancel the call
    // to fetch and return the response right away
    beforeFetch(endpoint) {
        // Hi-jack the login endpoint and return static content
        // You can use this to provide offline features
        if (endpoint.name === 'login') {
            endpoint.response = {
                data: {
                    user: {
                        id: 'Hello',
                    },
                },
            };
        }
        // Alwasy return a Promise
        return Promise.resolve(endpoint);
    },
    onError(endpoint, error) {
        if (error === 'fetch failed') {
            /* Silently spoof cached response */
            endpoint.response = ResponseCache.lookup(endponint),
            return Promise.resolve(endpoint);
        } else {
            /* Log and rethrow */
            console.log(error);
            throw error;
        }
    },
    // Will return raw data as received from the API before processing
    // Use this to cache raw responses
    afterDataReceived(endpoint, data) {
        if (name === 'login') {
            console.log(data);
        }
        return Promise.resolve(data);
    },
    // Will be called after the data was received but before it is resolved to the client user
    // Use this to reformat the data, or for logging
    afterDataProcessed(endpoint, data) {
        if (name === 'login') {
            console.log(data);
        }
        return Promise.resolve(data);
    },
});

Publishing

This is an npm package and published publicly on npm. To do this, change the version number in the package.json (eg: 2.0.3 => 2.0.4), run yarn, then push and merge to master then run the following command from the root of the repo: npm publish --access public

It will require you to have an account with npm and to be logged into npm in your terminal.

3.8.15

3 years ago

3.8.14

3 years ago

3.8.13

3 years ago

3.8.12

3 years ago

3.8.11

3 years ago

3.8.10

3 years ago

3.8.9

3 years ago

3.8.8

3 years ago

3.8.7

3 years ago

3.8.6

3 years ago

3.8.5

3 years ago

2.8.5

3 years ago

3.8.4

3 years ago

3.8.3

3 years ago

3.8.2

3 years ago

3.8.1

3 years ago

3.8.0

3 years ago

3.7.0

3 years ago

3.6.2

4 years ago

3.6.1

4 years ago

3.6.0

4 years ago

3.5.4

4 years ago

3.5.3

4 years ago

3.5.2

4 years ago

3.5.1

4 years ago

3.5.0

4 years ago

3.4.30

4 years ago

3.4.29

4 years ago

3.4.28

4 years ago

3.4.27

4 years ago

3.4.26

4 years ago

3.4.25

5 years ago

3.4.24

5 years ago

3.4.23

5 years ago

3.4.22

5 years ago

3.4.21

5 years ago

3.4.20

5 years ago

3.4.19

5 years ago

3.4.18

5 years ago

3.4.17

5 years ago

3.4.16

5 years ago

3.4.15

5 years ago

3.4.14

5 years ago

3.4.13

5 years ago

3.4.12

5 years ago

3.4.11

5 years ago

3.4.10

5 years ago

3.4.9

5 years ago

3.4.8

5 years ago

3.4.7

5 years ago

3.4.6

5 years ago

3.4.5

5 years ago

3.4.4

5 years ago

3.4.3

5 years ago

3.4.2

5 years ago

3.4.1

5 years ago

3.4.0

5 years ago

3.3.9

5 years ago

3.3.8

5 years ago

3.3.7

5 years ago

3.3.6

5 years ago

3.3.5

5 years ago

3.3.4

5 years ago

3.3.3

5 years ago

3.3.2

5 years ago

3.3.1

5 years ago

3.3.0

5 years ago

3.2.0

5 years ago

3.1.0

6 years ago

3.0.13

6 years ago

3.0.12

6 years ago

3.0.11

6 years ago

3.0.10

6 years ago

3.0.9

6 years ago

3.0.8

6 years ago

3.0.7

6 years ago

3.0.6

6 years ago

3.0.5

6 years ago

3.0.4

6 years ago

3.0.3

6 years ago

3.0.2

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.1.5

6 years ago

2.1.4

6 years ago

2.1.3

6 years ago

2.1.2

6 years ago

2.1.1

6 years ago

2.0.8

6 years ago

2.0.7

6 years ago

2.0.6

6 years ago

2.0.5

6 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago