Welcome to mirror list, hosted at ThFree Co, Russian Federation.

star.js « lib « npm-registry-client « node_modules « npm « deps - github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5c9224eaa21a72a3177e373bc1309c588b6e0926 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
module.exports = star

var assert = require('assert')

function star (uri, params, cb) {
  assert(typeof uri === 'string', 'must pass registry URI to star')
  assert(params && typeof params === 'object', 'must pass params to star')
  assert(typeof cb === 'function', 'must pass callback to star')

  var starred = !!params.starred

  var auth = params.auth
  assert(auth && typeof auth === 'object', 'must pass auth to star')
  if (!(auth.token || (auth.password && auth.username && auth.email))) {
    var er = new Error('Must be logged in to star/unstar packages')
    er.code = 'ENEEDAUTH'
    return cb(er)
  }

  var client = this
  this.request(uri + '?write=true', { auth: auth }, function (er, fullData) {
    if (er) return cb(er)

    client.whoami(uri, params, function (er, username) {
      if (er) return cb(er)

      var data = {
        _id: fullData._id,
        _rev: fullData._rev,
        users: fullData.users || {}
      }

      if (starred) {
        client.log.info('starring', data._id)
        data.users[username] = true
        client.log.verbose('starring', data)
      } else {
        delete data.users[username]
        client.log.info('unstarring', data._id)
        client.log.verbose('unstarring', data)
      }

      var options = {
        method: 'PUT',
        body: data,
        auth: auth
      }
      return client.request(uri, options, cb)
    })
  })
}