Skip to content

Commit

Permalink
remove callback interface
Browse files Browse the repository at this point in the history
Closes #133
  • Loading branch information
floatdrop committed Dec 2, 2015
1 parent 7d90e91 commit 023b2f1
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 80 deletions.
27 changes: 3 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,17 +279,7 @@ function normalizeArguments(url, opts) {
return opts;
}

function got(url, opts, cb) {
if (typeof opts === 'function') {
cb = opts;
opts = {};
}

if (cb) {
asCallback(normalizeArguments(url, opts), cb);
return null;
}

function got(url, opts) {
try {
return asPromise(normalizeArguments(url, opts));
} catch (error) {
Expand All @@ -307,21 +297,10 @@ const helpers = [
];

helpers.forEach(el => {
got[el] = (url, opts, cb) => {
if (typeof opts === 'function') {
cb = opts;
opts = {};
}

return got(url, objectAssign({}, opts, {method: el.toUpperCase()}), cb);
};
got[el] = (url, opts) => got(url, objectAssign({}, opts, {method: el.toUpperCase()}));
});

got.stream = function (url, opts, cb) {
if (cb || typeof opts === 'function') {
throw new Error('callback can not be used with stream mode');
}

got.stream = function (url, opts) {
return asStream(normalizeArguments(url, opts));
};

Expand Down
30 changes: 6 additions & 24 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@ $ npm install --save got
```js
const got = require('got');

// Callback mode
got('todomvc.com', (error, body, response) => {
console.log(body);
//=> '<!doctype html> ...'
});

// Promise mode
got('todomvc.com')
.then(response => {
console.log(response.body);
Expand All @@ -46,7 +39,7 @@ got('todomvc.com')
//=> 'Internal server error ...'
});

// Stream mode
// Streams
got.stream('todomvc.com').pipe(fs.createWriteStream('index.html'));

// For POST, PUT and PATCH methods got.stream returns a WritableStream
Expand All @@ -58,7 +51,9 @@ fs.createReadStream('index.html').pipe(got.stream.post('todomvc.com'));

It's a `GET` request by default, but can be changed in `options`.

#### got(url, [options], [callback])
#### got(url, [options])

Return a Promise, that resolves to `response` object with `body` property.

##### url

Expand Down Expand Up @@ -125,23 +120,10 @@ Number of request retries when network errors happens. Delays between retries co

Option accepts `function` with `retry` argument that must return delay in milliseconds (`0` return value cancels retry).

##### callback(error, data, response)

Function to be called when error or data are received. If omitted, a promise will be returned.

###### error

`Error` object with HTTP status code as `statusCode` property.

###### data

The data you requested.

###### response

The [response object](http://nodejs.org/api/http.html#http_http_incomingmessage).
#### Streams

When in stream mode, you can listen for events:
`stream` method will return Duplex stream with additional events:

##### .on('request', request)

Expand Down
7 changes: 0 additions & 7 deletions test/arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ test('options are optional', async t => {
t.is((await got(`${s.url}/test`)).body, '/test');
});

test.cb('options are optional', t => {
got(`${s.url}/test`, (err, data) => {
t.is(data, '/test');
t.end();
});
});

test('accepts url.parse object as first argument', async t => {
t.is((await got({hostname: s.host, port: s.port, path: '/test'})).body, '/test');
});
Expand Down
7 changes: 0 additions & 7 deletions test/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ test('dns message', async t => {
});

test('options.body error message', async t => {
try {
got(s.url, {body: () => {}}, () => {});
t.fail('Exception was not thrown');
} catch (err) {
t.regexTest(/options.body must be a ReadableStream, string, Buffer or plain Object/, err.message);
}

try {
await got(s.url, {body: () => {}});
t.fail('Exception was not thrown');
Expand Down
8 changes: 0 additions & 8 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@ test.before('setup', async t => {
await s.listen(s.port);
});

test.cb('callback mode', t => {
got.get(s.url, (err, body) => {
t.ifError(err);
t.is(body, 'ok');
t.end();
});
});

test('promise mode', async t => {
t.is((await got.get(s.url)).body, 'ok');

Expand Down
10 changes: 0 additions & 10 deletions test/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,6 @@ test('option.json can not be used', t => {
}, 'got can not be used as stream when options.json is used');
});

test('callback can not be used', t => {
t.throws(() => {
got.stream(s.url, {json: true}, () => {});
}, 'callback can not be used with stream mode');

t.throws(() => {
got.stream(s.url, () => {});
}, 'callback can not be used with stream mode');
});

test.cb('returns readable stream', t => {
got.stream(s.url)
.on('data', data => {
Expand Down

0 comments on commit 023b2f1

Please sign in to comment.