-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
151 lines (130 loc) · 3.68 KB
/
index.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
'use strict';
var request = require('hyperquest'),
url = require('url');
//
// Methods that require an `application/json` header.
//
var methods = ['POST', 'PUT'];
/**
* Carpenter API client.
*
* @constructor
* @param {Object|String} opts Options for root URL of carpenter service
* @param {String} opts.url The root URL of the carpenter service
* @param {String} opts.uri The root URL of the carpenter service
* @param {String} opts.href The href for root URL of the carpenter service
* @param {String} opts.protocol Protocol for root URL of the carpenter service
* @param {String} opts.version The carpenter build API version to use (defaults to v2)
* @public
*/
function Carpenter(opts) {
// eslint-disable-next-line no-new
if (!this) new Carpenter(opts);
if (typeof opts === 'string') {
this.base = opts;
} else if (opts.protocol && opts.href) {
this.base = url.format(opts);
} else if (opts.url || opts.uri) {
this.base = opts.url || opts.uri;
} else {
throw new Error('Carpenter URL required');
}
this.version = (opts.version === '1' || opts.version === 'v1' || opts.version === 1) ? '' : 'v2';
//
// Handle all possible cases
//
this.base = typeof this.base === 'object'
? url.format(this.base)
: this.base;
this.timeout = opts.timeout || 0;
this.agent = opts.agent;
}
/**
* Trigger a new build.
*
* @param {Object} options Configuration.
* @param {Function} next Completion callback.
* @returns {Stream} the request
* @private
*/
Carpenter.prototype.build = function build(options, next) {
options = options || {};
options.method = options.method || 'POST';
return this.send([
this.version,
'build'
].filter(Boolean).join('/'), options, next);
};
/**
* Cancel a build. The parameters are provided by options and filtered to allow
* optional parameters.
*
* @param {Object} options Configuration.
* @param {Function} next Completion callback.
* @returns {Stream} the request
* @private
*/
Carpenter.prototype.cancel = function cancel(options, next) {
options = options || {};
options.method = 'GET';
return this.send([
'cancel',
options.pkg,
options.version,
options.env
].filter(Boolean).join('/'), options, next);
};
/**
* Internal API for sending data.
*
* @param {String} pathname Pathname we need to hit.
* @param {Object} options Hyperquest options
* @param {Function} next Completion callback.
* @returns {Stream} the request
* @api private
*/
Carpenter.prototype.send = function send(pathname, options, next) {
var base = url.parse(this.base),
data = false,
req;
if (typeof pathname === 'object') {
options = pathname;
pathname = null;
}
if (typeof options === 'function') {
next = options;
options = {};
}
options.agent = this.agent;
options.headers = options.headers || {};
options.timeout = options.timeout || this.timeout;
base.pathname = pathname || options.pathname || '/';
//
// Setup options from method and optional data.
//
data = options.data;
if (typeof data === 'object' || ~methods.indexOf(options.method)) {
options.method = options.method || 'POST';
options.headers['Content-Type'] = 'application/json';
}
//
// Setup hyperquest to formatted URL.
//
req = request(url.format(base), options, next);
//
// Write JSON data to the request if provided. Yhis is only required for
// POST and PUT as hyperquest by default ends GET, DELETE and HEAD requests.
//
try {
if (~methods.indexOf(options.method)) {
req.end(typeof data === 'object' ? JSON.stringify(data) : data);
}
} catch (error) {
return next(error);
}
return req;
};
//
// Expose the interface.
//
module.exports = Carpenter;