|
6 | 6 |
|
7 | 7 | [`libnpmaccess`](https://github.com/npm/libnpmaccess) is a Node.js
|
8 | 8 | library that provides programmatic access to the guts of the npm CLI's `npm
|
9 |
| -access` command and its various subcommands. This includes managing account 2FA, |
10 |
| -listing packages and permissions, looking at package collaborators, and defining |
| 9 | +access` command. This includes managing account mfa settings, listing |
| 10 | +packages and permissions, looking at package collaborators, and defining |
11 | 11 | package permissions for users, orgs, and teams.
|
12 | 12 |
|
13 | 13 | ## Example
|
14 | 14 |
|
15 | 15 | ```javascript
|
16 | 16 | const access = require('libnpmaccess')
|
| 17 | +const opts = { '//registry.npmjs.org/:_authToken: 'npm_token } |
17 | 18 |
|
18 | 19 | // List all packages @zkat has access to on the npm registry.
|
19 |
| -console.log(Object.keys(await access.lsPackages('zkat'))) |
| 20 | +console.log(Object.keys(await access.getPackages('zkat', opts))) |
20 | 21 | ```
|
21 | 22 |
|
22 |
| -## Table of Contents |
23 |
| - |
24 |
| -* [Installing](#install) |
25 |
| -* [Example](#example) |
26 |
| -* [Contributing](#contributing) |
27 |
| -* [API](#api) |
28 |
| - * [access opts](#opts) |
29 |
| - * [`public()`](#public) |
30 |
| - * [`restricted()`](#restricted) |
31 |
| - * [`grant()`](#grant) |
32 |
| - * [`revoke()`](#revoke) |
33 |
| - * [`tfaRequired()`](#tfa-required) |
34 |
| - * [`tfaNotRequired()`](#tfa-not-required) |
35 |
| - * [`lsPackages()`](#ls-packages) |
36 |
| - * [`lsPackages.stream()`](#ls-packages-stream) |
37 |
| - * [`lsCollaborators()`](#ls-collaborators) |
38 |
| - * [`lsCollaborators.stream()`](#ls-collaborators-stream) |
39 |
| - |
40 |
| -### Install |
41 |
| - |
42 |
| -`$ npm install libnpmaccess` |
43 |
| - |
44 | 23 | ### API
|
45 | 24 |
|
46 |
| -#### <a name="opts"></a> `opts` for `libnpmaccess` commands |
| 25 | +#### `opts` for all `libnpmaccess` commands |
47 | 26 |
|
48 | 27 | `libnpmaccess` uses [`npm-registry-fetch`](https://npm.im/npm-registry-fetch).
|
49 |
| -All options are passed through directly to that library, so please refer to [its |
50 |
| -own `opts` |
| 28 | + |
| 29 | +All options are passed through directly to that library, so please refer |
| 30 | +to [its own `opts` |
51 | 31 | documentation](https://www.npmjs.com/package/npm-registry-fetch#fetch-options)
|
52 | 32 | for options that can be passed in.
|
53 | 33 |
|
54 |
| -A couple of options of note for those in a hurry: |
55 |
| - |
56 |
| -* `opts.token` - can be passed in and will be used as the authentication token for the registry. For other ways to pass in auth details, see the n-r-f docs. |
57 |
| -* `opts.otp` - certain operations will require an OTP token to be passed in. If a `libnpmaccess` command fails with `err.code === EOTP`, please retry the request with `{otp: <2fa token>}` |
58 |
| - |
59 |
| -#### <a name="public"></a> `> access.public(spec, [opts]) -> Promise<Boolean>` |
| 34 | +#### `spec` parameter for all `libnpmaccess` commands |
60 | 35 |
|
61 | 36 | `spec` must be an [`npm-package-arg`](https://npm.im/npm-package-arg)-compatible
|
62 | 37 | registry spec.
|
63 | 38 |
|
64 |
| -Makes package described by `spec` public. |
65 |
| - |
66 |
| -##### Example |
67 |
| - |
68 |
| -```javascript |
69 |
| -await access.public('@foo/bar', {token: 'myregistrytoken'}) |
70 |
| -// `@foo/bar` is now public |
71 |
| -``` |
72 |
| - |
73 |
| -#### <a name="restricted"></a> `> access.restricted(spec, [opts]) -> Promise<Boolean>` |
74 |
| - |
75 |
| -`spec` must be an [`npm-package-arg`](https://npm.im/npm-package-arg)-compatible |
76 |
| -registry spec. |
77 |
| - |
78 |
| -Makes package described by `spec` private/restricted. |
79 |
| - |
80 |
| -##### Example |
| 39 | +#### `access.getCollaborators(spec, opts) -> Promise<Object>` |
81 | 40 |
|
82 |
| -```javascript |
83 |
| -await access.restricted('@foo/bar', {token: 'myregistrytoken'}) |
84 |
| -// `@foo/bar` is now private |
85 |
| -``` |
| 41 | +Gets collaborators for a given package |
86 | 42 |
|
87 |
| -#### <a name="grant"></a> `> access.grant(spec, team, permissions, [opts]) -> Promise<Boolean>` |
| 43 | +#### `access.getPackages(user|scope|team, opts) -> Promise<Object>` |
88 | 44 |
|
89 |
| -`spec` must be an [`npm-package-arg`](https://npm.im/npm-package-arg)-compatible |
90 |
| -registry spec. `team` must be a fully-qualified team name, in the `scope:team` |
91 |
| -format, with or without the `@` prefix, and the team must be a valid team within |
92 |
| -that scope. `permissions` must be one of `'read-only'` or `'read-write'`. |
| 45 | +Gets all packages for a given user, scope, or team. |
93 | 46 |
|
94 |
| -Grants `read-only` or `read-write` permissions for a certain package to a team. |
| 47 | +Teams should be in the format `scope:team` or `@scope:team` |
95 | 48 |
|
96 |
| -##### Example |
| 49 | +Users and scopes can be in the format `@scope` or `scope` |
97 | 50 |
|
98 |
| -```javascript |
99 |
| -await access.grant('@foo/bar', '@foo:myteam', 'read-write', { |
100 |
| - token: 'myregistrytoken' |
101 |
| -}) |
102 |
| -// `@foo/bar` is now read/write enabled for the @foo:myteam team. |
103 |
| -``` |
| 51 | +#### `access.getVisibility(spec, opts) -> Promise<Object>` |
104 | 52 |
|
105 |
| -#### <a name="revoke"></a> `> access.revoke(spec, team, [opts]) -> Promise<Boolean>` |
| 53 | +Gets the visibility of a given package |
106 | 54 |
|
107 |
| -`spec` must be an [`npm-package-arg`](https://npm.im/npm-package-arg)-compatible |
108 |
| -registry spec. `team` must be a fully-qualified team name, in the `scope:team` |
109 |
| -format, with or without the `@` prefix, and the team must be a valid team within |
110 |
| -that scope. `permissions` must be one of `'read-only'` or `'read-write'`. |
| 55 | +#### `access.removePermissions(team, spec, opts) -> Promise<Boolean>` |
111 | 56 |
|
112 |
| -Removes access to a package from a certain team. |
| 57 | +Removes the access for a given team to a package. |
113 | 58 |
|
114 |
| -##### Example |
| 59 | +Teams should be in the format `scope:team` or `@scope:team` |
115 | 60 |
|
116 |
| -```javascript |
117 |
| -await access.revoke('@foo/bar', '@foo:myteam', { |
118 |
| - token: 'myregistrytoken' |
119 |
| -}) |
120 |
| -// @foo:myteam can no longer access `@foo/bar` |
121 |
| -``` |
| 61 | +#### `access.setAccess(package, access, opts) -> Promise<Boolean>` |
122 | 62 |
|
123 |
| -#### <a name="tfa-required"></a> `> access.tfaRequired(spec, [opts]) -> Promise<Boolean>` |
| 63 | +Sets access level for package described by `spec`. |
124 | 64 |
|
125 |
| -`spec` must be an [`npm-package-arg`](https://npm.im/npm-package-arg)-compatible |
126 |
| -registry spec. |
127 |
| - |
128 |
| -Makes it so publishing or managing a package requires using 2FA tokens to |
129 |
| -complete operations. |
| 65 | +The npm registry accepts the following `access` levels: |
130 | 66 |
|
131 |
| -##### Example |
| 67 | +`public`: package is public |
| 68 | +`private`: package is private |
132 | 69 |
|
133 |
| -```javascript |
134 |
| -await access.tfaRequires('lodash', {token: 'myregistrytoken'}) |
135 |
| -// Publishing or changing dist-tags on `lodash` now require OTP to be enabled. |
136 |
| -``` |
| 70 | +The npm registry also only allows scoped packages to have their access |
| 71 | +level set. |
137 | 72 |
|
138 |
| -#### <a name="tfa-not-required"></a> `> access.tfaNotRequired(spec, [opts]) -> Promise<Boolean>` |
| 73 | +#### access.setMfa(spec, level, opts) -> Promise<Boolean>` |
139 | 74 |
|
140 |
| -`spec` must be an [`npm-package-arg`](https://npm.im/npm-package-arg)-compatible |
141 |
| -registry spec. |
| 75 | +Sets the publishing mfa requirements for a given package. Level must be one of the |
| 76 | +following |
142 | 77 |
|
143 |
| -Disabled the package-level 2FA requirement for `spec`. Note that you will need |
144 |
| -to pass in an `otp` token in `opts` in order to complete this operation. |
| 78 | +`none`: mfa is not required to publish this package. |
| 79 | +`publish`: mfa is required to publish this package, automation tokens |
| 80 | +cannot be used to publish. |
| 81 | +`automation`: mfa is required to publish this package, automation tokens |
| 82 | +may also be used for publishing from continuous integration workflows. |
145 | 83 |
|
146 |
| -##### Example |
| 84 | +#### access.setPermissions(team, spec, permssions, opts) -> Promise<Boolean>` |
147 | 85 |
|
148 |
| -```javascript |
149 |
| -await access.tfaNotRequired('lodash', {otp: '123654', token: 'myregistrytoken'}) |
150 |
| -// Publishing or editing dist-tags on `lodash` no longer requires OTP to be |
151 |
| -// enabled. |
152 |
| -``` |
| 86 | +Sets permissions levels for a given team to a package. |
153 | 87 |
|
154 |
| -#### <a name="ls-packages"></a> `> access.lsPackages(entity, [opts]) -> Promise<Object | null>` |
| 88 | +Teams should be in the format `scope:team` or `@scope:team` |
155 | 89 |
|
156 |
| -`entity` must be either a valid org or user name, or a fully-qualified team name |
157 |
| -in the `scope:team` format, with or without the `@` prefix. |
| 90 | +The npm registry accepts the following `permissions`: |
158 | 91 |
|
159 |
| -Lists out packages a user, org, or team has access to, with corresponding |
160 |
| -permissions. Packages that the access token does not have access to won't be |
161 |
| -listed. |
162 |
| - |
163 |
| -In order to disambiguate between users and orgs, two requests may end up being |
164 |
| -made when listing orgs or users. |
165 |
| - |
166 |
| -For a streamed version of these results, see |
167 |
| -[`access.lsPackages.stream()`](#ls-package-stream). |
168 |
| - |
169 |
| -##### Example |
170 |
| - |
171 |
| -```javascript |
172 |
| -await access.lsPackages('zkat', { |
173 |
| - token: 'myregistrytoken' |
174 |
| -}) |
175 |
| -// Lists all packages `@zkat` has access to on the registry, and the |
176 |
| -// corresponding permissions. |
177 |
| -``` |
178 |
| - |
179 |
| -#### <a name="ls-packages-stream"></a> `> access.lsPackages.stream(scope, [team], [opts]) -> Stream` |
180 |
| - |
181 |
| -`entity` must be either a valid org or user name, or a fully-qualified team name |
182 |
| -in the `scope:team` format, with or without the `@` prefix. |
183 |
| - |
184 |
| -Streams out packages a user, org, or team has access to, with corresponding |
185 |
| -permissions, with each stream entry being formatted like `[packageName, |
186 |
| -permissions]`. Packages that the access token does not have access to won't be |
187 |
| -listed. |
188 |
| - |
189 |
| -In order to disambiguate between users and orgs, two requests may end up being |
190 |
| -made when listing orgs or users. |
191 |
| - |
192 |
| -The returned stream is a valid `asyncIterator`. |
193 |
| - |
194 |
| -##### Example |
195 |
| - |
196 |
| -```javascript |
197 |
| -for await (let [pkg, perm] of access.lsPackages.stream('zkat')) { |
198 |
| - console.log('zkat has', perm, 'access to', pkg) |
199 |
| -} |
200 |
| -// zkat has read-write access to eggplant |
201 |
| -// zkat has read-only access to @npmcorp/secret |
202 |
| -``` |
203 |
| - |
204 |
| -#### <a name="ls-collaborators"></a> `> access.lsCollaborators(spec, [user], [opts]) -> Promise<Object | null>` |
205 |
| - |
206 |
| -`spec` must be an [`npm-package-arg`](https://npm.im/npm-package-arg)-compatible |
207 |
| -registry spec. `user` must be a valid user name, with or without the `@` |
208 |
| -prefix. |
209 |
| - |
210 |
| -Lists out access privileges for a certain package. Will only show permissions |
211 |
| -for packages to which you have at least read access. If `user` is passed in, the |
212 |
| -list is filtered only to teams _that_ user happens to belong to. |
213 |
| - |
214 |
| -For a streamed version of these results, see [`access.lsCollaborators.stream()`](#ls-collaborators-stream). |
215 |
| - |
216 |
| -##### Example |
217 |
| - |
218 |
| -```javascript |
219 |
| -await access.lsCollaborators('@npm/foo', 'zkat', { |
220 |
| - token: 'myregistrytoken' |
221 |
| -}) |
222 |
| -// Lists all teams with access to @npm/foo that @zkat belongs to. |
223 |
| -``` |
224 |
| - |
225 |
| -#### <a name="ls-collaborators-stream"></a> `> access.lsCollaborators.stream(spec, [user], [opts]) -> Stream` |
226 |
| - |
227 |
| -`spec` must be an [`npm-package-arg`](https://npm.im/npm-package-arg)-compatible |
228 |
| -registry spec. `user` must be a valid user name, with or without the `@` |
229 |
| -prefix. |
230 |
| - |
231 |
| -Stream out access privileges for a certain package, with each entry in `[user, |
232 |
| -permissions]` format. Will only show permissions for packages to which you have |
233 |
| -at least read access. If `user` is passed in, the list is filtered only to teams |
234 |
| -_that_ user happens to belong to. |
235 |
| - |
236 |
| -The returned stream is a valid `asyncIterator`. |
237 |
| - |
238 |
| -##### Example |
239 |
| - |
240 |
| -```javascript |
241 |
| -for await (let [usr, perm] of access.lsCollaborators.stream('npm')) { |
242 |
| - console.log(usr, 'has', perm, 'access to npm') |
243 |
| -} |
244 |
| -// zkat has read-write access to npm |
245 |
| -// iarna has read-write access to npm |
246 |
| -``` |
| 92 | +`read-only`: Read only permissions |
| 93 | +`read-write`: Read and write (aka publish) permissions |
0 commit comments