• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2
3const fetch = require('npm-registry-fetch')
4const validate = require('aproba')
5
6const eu = encodeURIComponent
7const cmd = module.exports = {}
8cmd.add = (name, endpoint, secret, opts = {}) => {
9  validate('SSSO', [name, endpoint, secret, opts])
10  let type = 'package'
11  if (name.match(/^@[^/]+$/)) {
12    type = 'scope'
13  }
14  if (name[0] === '~') {
15    type = 'owner'
16    name = name.slice(1)
17  }
18  return fetch.json('/-/npm/v1/hooks/hook', {
19    ...opts,
20    method: 'POST',
21    body: { type, name, endpoint, secret },
22  })
23}
24
25cmd.rm = (id, opts = {}) => {
26  validate('SO', [id, opts])
27  return fetch.json(`/-/npm/v1/hooks/hook/${eu(id)}`, {
28    ...opts,
29    method: 'DELETE',
30  }).catch(err => {
31    if (err.code === 'E404') {
32      return null
33    } else {
34      throw err
35    }
36  })
37}
38
39cmd.update = (id, endpoint, secret, opts = {}) => {
40  validate('SSSO', [id, endpoint, secret, opts])
41  return fetch.json(`/-/npm/v1/hooks/hook/${eu(id)}`, {
42    ...opts,
43    method: 'PUT',
44    body: { endpoint, secret },
45  })
46}
47
48cmd.find = (id, opts = {}) => {
49  validate('SO', [id, opts])
50  return fetch.json(`/-/npm/v1/hooks/hook/${eu(id)}`, opts)
51}
52
53cmd.ls = (opts = {}) => {
54  return cmd.ls.stream(opts).collect()
55}
56
57cmd.ls.stream = (opts = {}) => {
58  const { package: pkg, limit, offset } = opts
59  validate('S|Z', [pkg])
60  validate('N|Z', [limit])
61  validate('N|Z', [offset])
62  return fetch.json.stream('/-/npm/v1/hooks', 'objects.*', {
63    ...opts,
64    query: {
65      package: pkg,
66      limit,
67      offset,
68    },
69  })
70}
71