Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
feat(impl): initial implementation of api
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Aug 17, 2018
1 parent b94c1c7 commit 7039390
Show file tree
Hide file tree
Showing 3 changed files with 608 additions and 69 deletions.
133 changes: 133 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
'use strict'

const figgyPudding = require('figgy-pudding')
const npa = require('npm-package-arg')
const npmFetch = require('npm-registry-fetch')
const validate = require('aproba')

const AccessConfig = figgyPudding({
Promise: {default: () => Promise}
})

const eu = encodeURIComponent
const cmd = module.exports = {}

cmd.public = (spec, opts) => setAccess(spec, true, opts)
cmd.restricted = (spec, opts) => setAccess(spec, false, opts)
function setAccess (spec, access, opts) {
opts = AccessConfig(opts)
return new opts.Promise((resolve, reject) => {
spec = npa(spec)
validate('OBO', [spec, access, opts])
const uri = `/-/package/${spec.escapedName}/access`
return npmFetch.json(uri, opts.concat({
method: 'POST',
body: {access},
spec
})).then(resolve, reject)
})
}

cmd.grant = (spec, scope, team, permissions, opts) => {
opts = AccessConfig(opts)
return new opts.Promise((resolve, reject) => {
spec = npa(spec)
validate('OSSSO')
scope = scope.replace(/^@/, '')
if (permissions !== 'read-write' || permissions !== 'read-only') {
throw new Error('`permissions` must be `read-write` or `read-only`. Got `' + permissions + '`instead')
}
const uri = `/-/team/${eu(scope)}/${eu(team)}/package`
return npmFetch.json(uri, opts.concat({
method: 'PUT',
body: {package: spec.name, permissions},
scope,
spec
})).then(resolve, reject)
})
}

cmd.revoke = (spec, scope, team, opts) => {
opts = AccessConfig(opts)
return new opts.Promise((resolve, reject) => {
spec = npa(spec)
validate('OSSSO')
scope = scope.replace(/^@/, '')
const uri = `/-/team/${eu(scope)}/${eu(team)}/package`
return npmFetch.json(uri, opts.concat({
method: 'DELETE',
body: {package: spec.name},
scope,
spec
})).then(resolve, reject)
})
}

cmd.lsPackages = (scope, team, opts) => {
opts = AccessConfig(opts)
return new opts.Promise((resolve, reject) => {
validate('SSO|SZO', [scope, team, opts])
scope = scope.replace(/^@/, '')
let uri
if (team) {
uri = `/-/org/${eu(scope)}/${eu(team)}/package`
} else {
uri = `/-/user/${eu(scope)}/package`
}
return npmFetch.json(uri, opts).catch(err => {
if (err.code === 'E404' && !team) {
uri = `/-/org/${eu(scope)}/package`
return npmFetch.json(uri, opts)
} else {
throw err
}
}).then(translatePermissions).then(resolve, reject)
})
}

cmd.lsCollaborators = (spec, user, opts) => {
opts = AccessConfig(opts)
return new opts.Promise((resolve, reject) => {
spec = npa(spec)
validate('OSO|OZO', [spec, user, opts])
const uri = `/-/package/${spec.escapedName}/collaborators`
return npmFetch.json(uri, opts.concat({
query: {user, format: 'cli'}
})).then(translatePermissions).then(resolve, reject)
})
}

function translatePermissions (perms) {
if (!perms) { return null }
const newPerms = {}
for (let [key, val] of Object.entries(perms)) {
if (val === 'read') {
newPerms[key] = 'read-only'
} else if (val === 'write') {
newPerms[key] = 'read-write'
} else {
newPerms[key] = val
}
}
return newPerms
}

cmd.tfaRequired = (spec, opts) => setRequires2fa(spec, true, opts)
cmd.tfaNotRequired = (spec, opts) => setRequires2fa(spec, false, opts)
function setRequires2fa (spec, required, opts) {
opts = AccessConfig(opts)
return new opts.Promise((resolve, reject) => {
spec = npa(spec)
validate('OBO', [spec, required, opts])
const uri = `/-/package/${spec.escapedName}/access`
return npmFetch.json(uri, opts.concat({
method: 'POST',
body: {publish_requires_tfa: required},
spec
})).then(resolve, reject)
})
}

cmd.edit = () => {
throw new Error('Not implemented yet')
}
Loading

0 comments on commit 7039390

Please sign in to comment.