Skip to content

Commit

Permalink
move opts.url into a separate function argument
Browse files Browse the repository at this point in the history
nicer API
  • Loading branch information
sindresorhus committed Jan 11, 2015
1 parent 45e89b3 commit d97ef45
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 35 deletions.
26 changes: 10 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,33 @@ $ npm install --save psi
var psi = require('psi');

// get the PageSpeed Insights report
psi({url: 'html5rocks.com'}, function (err, data) {
psi('html5rocks.com', function (err, data) {
console.log(data.score);
console.log(data.pageStats);
});

// output a formatted report to the terminal
psi.output({url: 'html5rocks.com'}, function (err) {
psi.output('html5rocks.com', function (err) {
console.log('done');
});
```


## API

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

#### options

*Required*
Type: `object`

##### url
#### url

*Required*
Type: `string`

URL of the page for which the PageSpeed Insights API should generate results.

#### options

Type: `object`

##### key

Type: `string`
Expand Down Expand Up @@ -88,16 +87,11 @@ Type: `object`

The response from Google PageSpeed Insights.

### psi.output(options, [callback])
### psi.output(url, [options], [callback])

Output the formatted report to the terminal.

#### options

*Required*
Type: `object`

Same as for `psi()`.
`url` and `options` is the same as `psi()`.

#### callback(error)

Expand Down
5 changes: 1 addition & 4 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
'use strict';
var meow = require('meow');
var updateNotifier = require('update-notifier');
var prependHttp = require('prepend-http');
var psi = require('./');

var cli = meow({
Expand Down Expand Up @@ -33,9 +32,7 @@ if (!cli.input[0]) {
process.exit(1);
}

cli.flags.url = cli.input[0];

psi.output(cli.flags, function (err, res) {
psi.output(cli.input[0], cli.flags, function (err, res) {
if (err) {
if (err.noStack) {
console.error(err.message);
Expand Down
37 changes: 23 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,28 @@ var objectAssign = require('object-assign');
var pagespeed = googleapis.pagespeedonline('v1').pagespeedapi.runpagespeed;
var output = require('./lib/output');

function handleOpts(opts) {
opts = objectAssign({
strategy: 'mobile'
}, opts);

function handleOpts(url, opts) {
opts = objectAssign({strategy: 'mobile'}, opts);
opts.nokey = opts.key === undefined;
opts.url = prependHttp(opts.url);

opts.url = prependHttp(url);
return opts;
}

var psi = module.exports = function (opts, cb) {
if (!opts.url) {
var psi = module.exports = function (url, opts, cb) {
if (typeof opts !== 'object') {
cb = opts;
opts = {};
}

if (!url) {
throw new Error('URL required');
}

pagespeed(handleOpts(opts), function (err, response) {
if (typeof cb !== 'function') {
throw new Error('Callback required');
}

pagespeed(handleOpts(url, opts), function (err, response) {
if (err) {
cb(err);
return;
Expand All @@ -31,17 +36,21 @@ var psi = module.exports = function (opts, cb) {
});
};

module.exports.output = function (opts, cb) {
module.exports.output = function (url, opts, cb) {
if (typeof opts !== 'object') {
cb = opts;
opts = {};
}

cb = cb || function () {};
opts = handleOpts(opts);

psi(opts, function (err, data) {
psi(url, opts, function (err, data) {
if (err) {
cb(err);
return;
}

output(opts, data);
output(handleOpts(url, opts), data);
cb();
});
};
10 changes: 9 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,18 @@ describe('Formatting', function () {

describe('API', function () {
it('should get data from PageSpeed Insights', function (cb) {
psi({url: 'google.com'}, function (err, data) {
psi('google.com', function (err, data) {
assert(!err, err);
assert.strictEqual(data.title, 'Google');
cb();
});
});

it('should support options', function (cb) {
psi('google.com', {locale: 'no'}, function (err, data) {
assert(!err, err);
assert.strictEqual(data.formattedResults.locale, 'no');
cb();
});
});
});

0 comments on commit d97ef45

Please sign in to comment.