11const { log, output, META } = require ( 'proc-log' )
2- const { listTokens , createGatToken , removeToken } = require ( 'npm-profile ' )
2+ const fetch = require ( 'npm-registry-fetch ' )
33const { otplease } = require ( '../utils/auth.js' )
44const readUserInfo = require ( '../utils/read-user-info.js' )
55const BaseCommand = require ( '../base-cmd.js' )
66
7+ async function paginate ( href , opts , items = [ ] ) {
8+ while ( href ) {
9+ const result = await fetch . json ( href , opts )
10+ items = items . concat ( result . objects )
11+ href = result . urls . next
12+ }
13+ return items
14+ }
15+
716class Token extends BaseCommand {
817 static description = 'Manage your authentication tokens'
918 static name = 'token'
@@ -63,7 +72,7 @@ class Token extends BaseCommand {
6372 const json = this . npm . config . get ( 'json' )
6473 const parseable = this . npm . config . get ( 'parseable' )
6574 log . info ( 'token' , 'getting list' )
66- const tokens = await listTokens ( this . npm . flatOptions )
75+ const tokens = await paginate ( '/-/npm/v1/tokens' , this . npm . flatOptions )
6776 if ( json ) {
6877 output . buffer ( tokens )
6978 return
@@ -104,10 +113,9 @@ class Token extends BaseCommand {
104113 const json = this . npm . config . get ( 'json' )
105114 const parseable = this . npm . config . get ( 'parseable' )
106115 const toRemove = [ ]
107- const opts = { ...this . npm . flatOptions }
108116 log . info ( 'token' , `removing ${ toRemove . length } tokens` )
109- const tokens = await listTokens ( opts )
110- args . forEach ( id => {
117+ const tokens = await paginate ( '/-/npm/v1/tokens' , this . npm . flatOptions )
118+ for ( const id of args ) {
111119 const matches = tokens . filter ( token => token . key . indexOf ( id ) === 0 )
112120 if ( matches . length === 1 ) {
113121 toRemove . push ( matches [ 0 ] . key )
@@ -123,12 +131,16 @@ class Token extends BaseCommand {
123131
124132 toRemove . push ( id )
125133 }
126- } )
127- await Promise . all (
128- toRemove . map ( key => {
129- return otplease ( this . npm , opts , c => removeToken ( key , c ) )
130- } )
131- )
134+ }
135+ for ( const tokenKey of toRemove ) {
136+ await otplease ( this . npm , this . npm . flatOptions , opts =>
137+ fetch ( `/-/npm/v1/tokens/token/${ tokenKey } ` , {
138+ ...opts ,
139+ method : 'DELETE' ,
140+ ignoreBody : true ,
141+ } )
142+ )
143+ }
132144 if ( json ) {
133145 output . buffer ( toRemove )
134146 } else if ( parseable ) {
@@ -204,10 +216,12 @@ class Token extends BaseCommand {
204216 }
205217
206218 log . info ( 'token' , 'creating' )
207- const result = await otplease (
208- this . npm ,
209- { ...this . npm . flatOptions } ,
210- c => createGatToken ( tokenData , c )
219+ const result = await otplease ( this . npm , this . npm . flatOptions , opts =>
220+ fetch . json ( '/-/npm/v1/tokens' , {
221+ ...opts ,
222+ method : 'POST' ,
223+ body : tokenData ,
224+ } )
211225 )
212226 delete result . key
213227 delete result . updated
0 commit comments