1'use strict' 2 3const eu = encodeURIComponent 4const fetch = require('npm-registry-fetch') 5const figgyPudding = require('figgy-pudding') 6const getStream = require('get-stream') 7const validate = require('aproba') 8 9const OrgConfig = figgyPudding({ 10 Promise: { default: () => Promise } 11}) 12 13// From https://github.com/npm/registry/blob/master/docs/orgs/memberships.md 14const cmd = module.exports = {} 15 16class MembershipDetail {} 17cmd.set = (org, user, role, opts) => { 18 if (typeof role === 'object' && !opts) { 19 opts = role 20 role = undefined 21 } 22 opts = OrgConfig(opts) 23 return new opts.Promise((resolve, reject) => { 24 validate('SSSO|SSZO', [org, user, role, opts]) 25 user = user.replace(/^@?/, '') 26 org = org.replace(/^@?/, '') 27 fetch.json(`/-/org/${eu(org)}/user`, opts.concat({ 28 method: 'PUT', 29 body: { user, role } 30 })).then(resolve, reject) 31 }).then(ret => Object.assign(new MembershipDetail(), ret)) 32} 33 34cmd.rm = (org, user, opts) => { 35 opts = OrgConfig(opts) 36 return new opts.Promise((resolve, reject) => { 37 validate('SSO', [org, user, opts]) 38 user = user.replace(/^@?/, '') 39 org = org.replace(/^@?/, '') 40 fetch(`/-/org/${eu(org)}/user`, opts.concat({ 41 method: 'DELETE', 42 body: { user }, 43 ignoreBody: true 44 })).then(resolve, reject) 45 }).then(() => null) 46} 47 48class Roster {} 49cmd.ls = (org, opts) => { 50 opts = OrgConfig(opts) 51 return new opts.Promise((resolve, reject) => { 52 getStream.array(cmd.ls.stream(org, opts)).then(entries => { 53 const obj = {} 54 for (let [key, val] of entries) { 55 obj[key] = val 56 } 57 return obj 58 }).then(resolve, reject) 59 }).then(ret => Object.assign(new Roster(), ret)) 60} 61 62cmd.ls.stream = (org, opts) => { 63 opts = OrgConfig(opts) 64 validate('SO', [org, opts]) 65 org = org.replace(/^@?/, '') 66 return fetch.json.stream(`/-/org/${eu(org)}/user`, '*', opts.concat({ 67 mapJson (value, [key]) { 68 return [key, value] 69 } 70 })) 71} 72