-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcreate-new-spec.js
More file actions
40 lines (39 loc) · 1.26 KB
/
Copy pathcreate-new-spec.js
File metadata and controls
40 lines (39 loc) · 1.26 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
const getSpecVersion = require('./get-spec-version');
const updateExistingSpec = require('./update-existing-spec');
const sdk = require('api')('@developers/v2.0#kpjtacsldjbscf9');
const fs = require('fs');
module.exports = async function createNewSpec(apiKey, versionNumber, filePath) {
const specFile = fs.createReadStream(filePath);
try {
await sdk.auth(apiKey);
let createdSpec = await sdk.uploadAPISpecification(
{
spec: specFile,
},
{
accept: 'application/json',
'x-readme-version': versionNumber,
},
);
specFile.destroy();
console.log('[INFO] Created API spec', createdSpec.id);
return createdSpec;
} catch (err) {
specFile.destroy();
console.error('[ERROR] Error creating API spec', err);
if (err.status == 503) {
console.log('[INFO] 503 error, trying again');
try {
specId = await getSpecVersion(versionNumber, apiKey);
console.log(`[INFO] Updating API spec ${specId} on version ${versionNumber}`);
return await updateExistingSpec(specId, apiKey, filePath);
} catch (err) {
throw err;
}
} else {
var message = await err.json();
console.error('[ERROR] Error creating API spec', message);
throw err;
}
}
};