forked from entropic-dev/entropic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecline.js
62 lines (51 loc) · 1.42 KB
/
decline.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'use strict';
const fetch = require('../fetch');
const figgy = require('figgy-pudding');
const parsePackageSpec = require('../canonicalize-spec');
module.exports = decline;
// usage: ds decline --package name@host/pkg --as namespace
// ds decline --namespace name@host
const declineOpts = figgy({
as: true,
namespace: true,
package: true,
registry: true,
token: true,
log: { default: require('npmlog') }
});
async function decline(opts) {
opts = declineOpts(opts);
if (!opts.as || (!opts.package && !opts.namespace)) {
console.error(
'Usage: ds decline --package <package> --as <namespace>\n' +
' ds decline --namespace <namespace>'
);
return 1;
}
let uri;
if (opts.package) {
const parsed = parsePackageSpec(
opts.package,
opts.registry.replace(/^https?:\/\//, '')
);
const invitee = opts.as;
uri = `${
opts.registry
}/v1/namespaces/namespace/${invitee}/maintainerships/${parsed.canonical}`;
} else {
let ns = opts.namespace;
if (!ns.includes('@')) {
ns += '@' + opts.registry.replace(/^https?:\/\//, '');
}
uri = `${opts.registry}/v1/users/user/memberships/invitations/${ns}`;
}
const response = await fetch(uri, {
method: 'DELETE',
headers: {
authorization: `Bearer ${opts.token}`
}
});
const body = await response.json();
console.log(body.message ? body.message : body);
return 0;
}