Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
doowb committed Jun 18, 2019
1 parent c1fbc62 commit b69041a
Show file tree
Hide file tree
Showing 19 changed files with 560 additions and 877 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# npm-api [![NPM version](https://img.shields.io/npm/v/npm-api.svg?style=flat)](https://www.npmjs.com/package/npm-api) [![NPM monthly downloads](https://img.shields.io/npm/dm/npm-api.svg?style=flat)](https://npmjs.org/package/npm-api) [![NPM total downloads](https://img.shields.io/npm/dt/npm-api.svg?style=flat)](https://npmjs.org/package/npm-api) [![Linux Build Status](https://img.shields.io/travis/doowb/npm-api.svg?style=flat&label=Travis)](https://travis-ci.org/doowb/npm-api) [![Windows Build Status](https://img.shields.io/appveyor/ci/doowb/npm-api.svg?style=flat&label=AppVeyor)](https://ci.appveyor.com/project/doowb/npm-api)
# npm-api [![NPM version](https://img.shields.io/npm/v/npm-api.svg?style=flat)](https://www.npmjs.com/package/npm-api) [![NPM monthly downloads](https://img.shields.io/npm/dm/npm-api.svg?style=flat)](https://npmjs.org/package/npm-api) [![NPM total downloads](https://img.shields.io/npm/dt/npm-api.svg?style=flat)](https://npmjs.org/package/npm-api) [![Linux Build Status](https://img.shields.io/travis/doowb/npm-api.svg?style=flat&label=Travis)](https://travis-ci.org/doowb/npm-api)

> Base class for retrieving data from the npm registry.
Expand Down
28 changes: 15 additions & 13 deletions examples/get-maintainer-repos.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
/*!
* npm-api <https://github.com/doowb/npm-api>
*
* Copyright (c) 2016, Brian Woodward.
* Licensed under the MIT License.
*/

'use strict';

var npm = require('../')();
var maintainer = npm.maintainer('doowb');
maintainer.repos()
.then(function (repos) {
console.log(repos);
}, function(err) {
const NpmApi = require('../');
const npm = new NpmApi();

run()
.then(() => {
process.exit();
})
.catch(err => {
console.error(err);
process.exit(1);
});

async function run() {
let maintainer = npm.maintainer('doowb');
let repos = await maintainer.repos();
console.log(repos);
}
37 changes: 18 additions & 19 deletions examples/get-repo-downloads.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
/*!
* npm-api <https://github.com/doowb/npm-api>
*
* Copyright (c) 2016, Brian Woodward.
* Licensed under the MIT License.
*/

'use strict';

var co = require('co');
var npm = require('../')();
const NpmApi = require('../');
const npm = new NpmApi();

var repo = npm.repo('micromatch');
co(function* () {
run()
.then(() => {
process.exit();
})
.catch(err => {
console.error(err);
process.exit(1);
});

var downloads = yield repo.downloads();
async function run() {
let repo = npm.repo('micromatch');
let downloads = await repo.downloads();
console.log(downloads.length + ' days of downloads have been pulled for ' + repo.name);
console.log();

console.log('total:', yield repo.total());
console.log('last 30 days:', yield repo.last(30));
console.log('last 7 days:', yield repo.last(7));
console.log('last day:', yield repo.last(1));

process.exit();
});
console.log('total:', await repo.total());
console.log('last 30 days:', await repo.last(30));
console.log('last 7 days:', await repo.last(7));
console.log('last day:', await repo.last(1));
}
26 changes: 18 additions & 8 deletions examples/view.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
'use strict';

var co = require('co');
var npm = require('../')();
var view = npm.view('listAll');
const NpmApi = require('../');
const npm = new NpmApi();

co(function* () {
var pkg = yield view.query({
run()
.then(() => {
process.exit();
})
.catch(err => {
console.error(err);
process.exit(1);
});

async function run() {
let view = npm.view('listAll');
let pkg = await view.query({
// group_level: 4,
startkey: JSON.stringify('micromatch'),
endkey: JSON.stringify('micromatch')
});
var val = pkg[0].value;
var latest = val.versions[val['dist-tags'].latest];

let val = pkg[0].value;
let latest = val.versions[val['dist-tags'].latest];
console.log(latest);
})
}
249 changes: 123 additions & 126 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,153 +1,150 @@
/*!
* npm-api <https://github.com/doowb/npm-api>
*
* Copyright (c) 2015-2017, Brian Woodward.
* Released under the MIT License.
*/

'use strict';

var Base = require('base').namespace('cache');
var utils = require('./lib/utils');
var List = require('./lib/list');
var View = require('./lib/view');
var Repo = require('./lib/models/repo');
var Maintainer = require('./lib/models/maintainer');
var Memory = require('./lib/stores/memory');
const List = require('./lib/list');
const View = require('./lib/view');
const Repo = require('./lib/models/repo');
const Maintainer = require('./lib/models/maintainer');

const define = (obj, name, value) => Reflect.defineProperty(obj, name, { value });
let cache = null;

/**
* NpmApi constructor. Create an instance to work with maintainer and repository information.
*
* ```js
* var npm = new NpmApi();
* let npm = new NpmApi();
* ```
* @api public
*/

function NpmApi(options) {
if (!(this instanceof NpmApi)) {
return new NpmApi(options);
}
Base.call(this, null, options);
this.is('npmapi');

this.use(utils.plugin());
this.use(utils.option());

this.define('List', List);
this.define('View', View);
this.define('Repo', Repo);
this.define('Maintainer', Maintainer);

var store = typeof this.options.store === 'undefined' ? new Memory() : this.options.store;
this.define('store', store);
}

/**
* Extend `Base`
*/

Base.extend(NpmApi);

/**
* Create a new instance of `View` or get an existing instance to work
* with npm couchdb views.
*
* ```js
* var view = npm.view('byUser');
* ```
*
* @param {String} `name` Name of the couchdb view to work with.
* @return {Object} `View` instance
* @api public
*/
class NpmApi {
constructor(options = {}) {
this.options = { ...options };
this.reset();

NpmApi.prototype.view = function(name) {
if (this.has(['views', name])) {
return this.get(['views', name]);
define(this, 'List', List);
define(this, 'View', View);
define(this, 'Repo', Repo);
define(this, 'Maintainer', Maintainer);
}
var view = new View(name);
this.set(['views', name], view);
return view;
};

/**
* Create a new instance of `List` or get an existing instance to work
* with npm couchdb list.
*
* ```js
* var list = npm.list('sortCount', 'byUser');
* ```
*
* @param {String} `name` Name of the couchdb list to work with.
* @param {String|Object} `view` Name or instance of a `view` to work with.
* @return {Object} `List` instance
* @api public
*/

NpmApi.prototype.list = function(name, view) {
var viewName = view;
if (typeof view === 'object') {
viewName = view.name;
reset() {
cache = new Map();
cache.set('lists', new Map());
cache.set('views', new Map());
cache.set('repos', new Map());
cache.set('maintainers', new Map());
}

if (this.has(['lists', viewName, name])) {
return this.get(['lists', viewName, name]);
use(fn) {
fn.call(this, this, this.options);
}

if (typeof view === 'string') {
view = this.view(view);
/**
* Create a new instance of `View` or get an existing instance to work
* with npm couchdb views.
*
* ```js
* var view = npm.view('byUser');
* ```
*
* @param {String} `name` Name of the couchdb view to work with.
* @return {Object} `View` instance
* @api public
*/

view(name) {
let views = cache.get('views');
if (views.has(name)) {
return views.get(name);
}

let view = new View(name);
views.set(name, view);
return view;
}

var list = new List(name, view);
this.set(['lists', viewName, name], list);
return list;
};

/**
* Create an instance of a `repo` to work with.
*
* ```js
* var repo = npm.repo('micromatch');
* ```
*
* @param {String} `name` Name of the repo as it's published to npm.
* @return {Object} Instance of a `Repo` model to work with.
* @api public
*/

NpmApi.prototype.repo = function(name) {
var escaped = name.split('.').join('\\\\.');
if (this.has(['repos', escaped])) {
return this.get(['repos', escaped]);
/**
* Create a new instance of `List` or get an existing instance to work
* with npm couchdb list.
*
* ```js
* var list = npm.list('sortCount', 'byUser');
* ```
*
* @param {String} `name` Name of the couchdb list to work with.
* @param {String|Object} `view` Name or instance of a `view` to work with.
* @return {Object} `List` instance
* @api public
*/

list(name, view) {
let lists = cache.get('lists');
let viewName = view;
if (typeof view === 'object') {
viewName = view.name;
}

let key = `${viewName}.${name}`;
if (lists.has(key)) {
return lists.get(key);
}

if (typeof view === 'string') {
view = this.view(view);
}

let list = new List(name, view);
lists.set(key, list);
return list;
}
var repo = new Repo(name, this.store);
this.set(['repos', escaped], repo);
this.run(repo);
return repo;
};

/**
* Create an instance of a `maintainer` to work with.
*
* ```js
* var maintainer = npm.maintainer('doowb');
* ```
*
* @param {String} `name` Npm username of the maintainer.
* @return {Object} Instance of a `Maintainer` model to work with.
* @api public
*/
/**
* Create an instance of a `repo` to work with.
*
* ```js
* var repo = npm.repo('micromatch');
* ```
*
* @param {String} `name` Name of the repo as it's published to npm.
* @return {Object} Instance of a `Repo` model to work with.
* @api public
*/

repo(name) {
let repos = cache.get('repos');
if (repos.has(name)) {
return repos.get(name);
}

let repo = new Repo(name);
repos.set(name, repo);
return repo;
}

NpmApi.prototype.maintainer = function(name) {
if (this.has(['maintainers', name])) {
return this.get(['maintainers', name]);
/**
* Create an instance of a `maintainer` to work with.
*
* ```js
* var maintainer = npm.maintainer('doowb');
* ```
*
* @param {String} `name` Npm username of the maintainer.
* @return {Object} Instance of a `Maintainer` model to work with.
* @api public
*/

maintainer(name) {
let maintainers = cache.get('maintainers');
if (maintainers.has(name)) {
return maintainers.get(name);
}

let maintainer = new Maintainer(name);
maintainers.set(name, maintainer);
return maintainer;
}
var maintainer = new Maintainer(name, this.store);
this.set(['maintainers', name], maintainer);
this.run(maintainer);
return maintainer;
};
}

/**
* Exposes `NpmApi`
Expand Down
Loading

0 comments on commit b69041a

Please sign in to comment.