summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/npm-registry-client/lib/org.js
blob: 7c73caf7daf24f51c182e92c2b0ecbd7178fda9d (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
52
53
54
55
56
57
58
59
60
61
62
'use strict'

module.exports = org

var assert = require('assert')
var url = require('url')

var subcommands = {}

function org (subcommand, uri, params, cb) {
  orgAssertions(subcommand, uri, params, cb)
  return subcommands[subcommand].call(this, uri, params, cb)
}

subcommands.set = subcommands.add = function (uri, params, cb) {
  return this.request(apiUri(uri, 'org', params.org, 'user'), {
    method: 'PUT',
    auth: params.auth,
    body: JSON.stringify({
      user: params.user,
      role: params.role
    })
  }, cb)
}

subcommands.rm = function (uri, params, cb) {
  return this.request(apiUri(uri, 'org', params.org, 'user'), {
    method: 'DELETE',
    auth: params.auth,
    body: JSON.stringify({
      user: params.user
    })
  }, cb)
}

subcommands.ls = function (uri, params, cb) {
  return this.request(apiUri(uri, 'org', params.org, 'user'), {
    method: 'GET',
    auth: params.auth
  }, cb)
}

function apiUri (registryUri) {
  var path = Array.prototype.slice.call(arguments, 1)
    .map(encodeURIComponent)
    .join('/')
  return url.resolve(registryUri, '-/' + path)
}

function orgAssertions (subcommand, uri, params, cb) {
  assert(subcommand, 'subcommand is required')
  assert(subcommands.hasOwnProperty(subcommand),
         'org subcommand must be one of ' + Object.keys(subcommands))
  assert(typeof uri === 'string', 'registry URI is required')
  assert(typeof params === 'object', 'params are required')
  assert(typeof params.auth === 'object', 'auth is required')
  assert(!cb || typeof cb === 'function', 'callback must be a function')
  assert(typeof params.org === 'string', 'org name is required')
  if (subcommand === 'rm' || subcommand === 'add' || subcommand === 'set') {
    assert(typeof params.user === 'string', 'user is required')
  }
}