1.3.4 • Published 5 years ago

cuddle v1.3.4

Weekly downloads
7
License
MIT
Repository
github
Last release
5 years ago

Cuddle

Build Status NPM version

Cuddle is a minimal, chainable, retryable and "readability first" node http client. It's built to use for calling third-party APIs. Just what you need.

npm i cuddle@latest -S

Use Cases

Important notes

  • Status code < 200 or >= 300 will be classifed as an error.
  • Request will not fire unless .then or .end is called

Simple

const cudl = require('cuddle');

cudl.post
    .to('http://localhost:8082/api/user/1')
    .set_header('Authorization', 'Token sampletoken')
    .send({
        username: 'rvnjl',
        sex: 'male'
    })
    .then((err, result) => {

        if (err) {
            //handle error
        }

        console.log(result);
    });

Promise:

const cudl = require('cuddle');

cudl.post
    .to('http://localhost:8082/api/user/1')
    .set_header('Authorization', 'Token sampletoken')
    .send({
        username: 'rvnjl',
        sex: 'male'
    })
    .promise()
    .then(success)
    .catch(fail);

Using with generators:

const cudl = require('cuddle');
const co = require('co');

function* foo () {
    let user = yield cudl.get
        .to('http://localhost:8082/api/user/1')
        .set_header('Authorization', 'Token sampletoken')
        .promise();

    console.log(user);
}

co(foo);

Throttling requests

// will only let 50 concurrent requests
cudl.throttle(50);

Easy scoping through args:

const cudl = require('cuddle');

function foo () {
    const users = [
        {id: 1, name: 'jeff'},
        {id: 2, name: 'jenny'},
        {id: 3, name: 'julius'}
    ];

    users.forEach(user => {
        cudl.get
            .to('http://localhost:8082/api/user/' + user.id)
            .args(user)
            //.max_retry(10)    // to set the number of retries
            //.debug()          // to log all
            //.logger(winston)  // to replace the logger (default is console)
            .then(bar);
    });
}

function bar (err, result, request, [user]) {

    if (err) {
        // cuddle will return a different error after reaching maximum retries
        if (err.code >= 500) {
            return request.retry();
        }

        console.error('Error with user ' + user.id + request);
        return;
    }

    user.more_info = result;

    // ...
}

foo();
1.3.4

5 years ago

1.3.3

6 years ago

1.3.2

6 years ago

1.3.1

6 years ago

1.3.0

7 years ago

1.2.0

7 years ago

1.1.12

7 years ago

1.1.11

7 years ago

1.1.10

7 years ago

1.1.9

7 years ago

1.1.8

8 years ago

1.1.7

8 years ago

1.1.6

8 years ago

1.1.5

8 years ago

1.1.4

8 years ago

1.1.3

8 years ago

1.1.2

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.0.56

8 years ago

0.0.55

8 years ago

0.0.54

8 years ago

0.0.53

8 years ago

0.0.52

8 years ago

0.0.51

8 years ago

0.0.50

9 years ago

0.0.49

9 years ago

0.0.48

9 years ago

0.0.47

9 years ago

0.0.46

9 years ago

0.0.45

9 years ago

0.0.44

9 years ago

0.0.43

9 years ago

0.0.42

9 years ago

0.0.41

9 years ago

0.0.40

9 years ago

0.0.39

9 years ago

0.0.38

9 years ago

0.0.37

9 years ago

0.0.36

9 years ago

0.0.35

9 years ago

0.0.34

9 years ago

0.0.33

9 years ago

0.0.32

9 years ago

0.0.31

9 years ago

0.0.30

9 years ago

0.0.29

9 years ago

0.0.28

9 years ago

0.0.27

9 years ago

0.0.26

9 years ago

0.0.25

9 years ago

0.0.24

9 years ago

0.0.23

9 years ago

0.0.22

9 years ago

0.0.21

9 years ago

0.0.20

9 years ago

0.0.19

9 years ago

0.0.18

10 years ago

0.0.17

10 years ago

0.0.16

10 years ago

0.0.15

10 years ago

0.0.14

10 years ago

0.0.13

10 years ago

0.0.12

10 years ago

0.0.11

10 years ago

0.0.10

10 years ago

0.0.9

10 years ago

0.0.8

10 years ago

0.0.7

10 years ago

0.0.6

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago

0.0.0

10 years ago