This repository was archived by the owner on Jun 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstackbit.js
More file actions
62 lines (51 loc) · 1.73 KB
/
stackbit.js
File metadata and controls
62 lines (51 loc) · 1.73 KB
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
const https = require('https');
const url = require('url');
function pull(options) {
return new Promise((resolve, reject) => {
const { stackbitPullApiUrl, ...bodyOptions } = options;
const urlObject = url.parse(stackbitPullApiUrl);
const body = JSON.stringify(bodyOptions);
const requestOptions = {
hostname: urlObject.hostname,
path: urlObject.path,
protocol: urlObject.protocol,
port: urlObject.port || 443,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': body.length
}
};
const req = https.request(requestOptions, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
if (res.statusCode === 404) {
return reject(new Error('Project not found'));
}
let response;
try {
response = JSON.parse(data);
} catch (err) {
return reject(new Error(`Failed to serialize response json`));
}
if (res.statusCode >= 400) {
return reject(
new Error(`Failed to build project, statusCode: ${res.statusCode}, response: ${JSON.stringify(response)}`)
);
}
resolve(response);
});
});
req.on('error', (e) => {
reject(new Error(`Error fetching project build: ${e.message}`));
});
req.write(body);
req.end();
});
}
module.exports = {
pull
};