11cb0ef41Sopenharmony_ci'use strict'
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst fetch = require('npm-registry-fetch')
41cb0ef41Sopenharmony_ciconst validate = require('aproba')
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst eu = encodeURIComponent
71cb0ef41Sopenharmony_ciconst cmd = module.exports = {}
81cb0ef41Sopenharmony_cicmd.add = (name, endpoint, secret, opts = {}) => {
91cb0ef41Sopenharmony_ci  validate('SSSO', [name, endpoint, secret, opts])
101cb0ef41Sopenharmony_ci  let type = 'package'
111cb0ef41Sopenharmony_ci  if (name.match(/^@[^/]+$/)) {
121cb0ef41Sopenharmony_ci    type = 'scope'
131cb0ef41Sopenharmony_ci  }
141cb0ef41Sopenharmony_ci  if (name[0] === '~') {
151cb0ef41Sopenharmony_ci    type = 'owner'
161cb0ef41Sopenharmony_ci    name = name.slice(1)
171cb0ef41Sopenharmony_ci  }
181cb0ef41Sopenharmony_ci  return fetch.json('/-/npm/v1/hooks/hook', {
191cb0ef41Sopenharmony_ci    ...opts,
201cb0ef41Sopenharmony_ci    method: 'POST',
211cb0ef41Sopenharmony_ci    body: { type, name, endpoint, secret },
221cb0ef41Sopenharmony_ci  })
231cb0ef41Sopenharmony_ci}
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_cicmd.rm = (id, opts = {}) => {
261cb0ef41Sopenharmony_ci  validate('SO', [id, opts])
271cb0ef41Sopenharmony_ci  return fetch.json(`/-/npm/v1/hooks/hook/${eu(id)}`, {
281cb0ef41Sopenharmony_ci    ...opts,
291cb0ef41Sopenharmony_ci    method: 'DELETE',
301cb0ef41Sopenharmony_ci  }).catch(err => {
311cb0ef41Sopenharmony_ci    if (err.code === 'E404') {
321cb0ef41Sopenharmony_ci      return null
331cb0ef41Sopenharmony_ci    } else {
341cb0ef41Sopenharmony_ci      throw err
351cb0ef41Sopenharmony_ci    }
361cb0ef41Sopenharmony_ci  })
371cb0ef41Sopenharmony_ci}
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_cicmd.update = (id, endpoint, secret, opts = {}) => {
401cb0ef41Sopenharmony_ci  validate('SSSO', [id, endpoint, secret, opts])
411cb0ef41Sopenharmony_ci  return fetch.json(`/-/npm/v1/hooks/hook/${eu(id)}`, {
421cb0ef41Sopenharmony_ci    ...opts,
431cb0ef41Sopenharmony_ci    method: 'PUT',
441cb0ef41Sopenharmony_ci    body: { endpoint, secret },
451cb0ef41Sopenharmony_ci  })
461cb0ef41Sopenharmony_ci}
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_cicmd.find = (id, opts = {}) => {
491cb0ef41Sopenharmony_ci  validate('SO', [id, opts])
501cb0ef41Sopenharmony_ci  return fetch.json(`/-/npm/v1/hooks/hook/${eu(id)}`, opts)
511cb0ef41Sopenharmony_ci}
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_cicmd.ls = (opts = {}) => {
541cb0ef41Sopenharmony_ci  return cmd.ls.stream(opts).collect()
551cb0ef41Sopenharmony_ci}
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_cicmd.ls.stream = (opts = {}) => {
581cb0ef41Sopenharmony_ci  const { package: pkg, limit, offset } = opts
591cb0ef41Sopenharmony_ci  validate('S|Z', [pkg])
601cb0ef41Sopenharmony_ci  validate('N|Z', [limit])
611cb0ef41Sopenharmony_ci  validate('N|Z', [offset])
621cb0ef41Sopenharmony_ci  return fetch.json.stream('/-/npm/v1/hooks', 'objects.*', {
631cb0ef41Sopenharmony_ci    ...opts,
641cb0ef41Sopenharmony_ci    query: {
651cb0ef41Sopenharmony_ci      package: pkg,
661cb0ef41Sopenharmony_ci      limit,
671cb0ef41Sopenharmony_ci      offset,
681cb0ef41Sopenharmony_ci    },
691cb0ef41Sopenharmony_ci  })
701cb0ef41Sopenharmony_ci}
71