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