Skip to content

Commit

Permalink
refactor(secrets): replace request with got
Browse files Browse the repository at this point in the history
  • Loading branch information
masontikhonov committed Jul 3, 2024
1 parent a1bdf30 commit 9fbeeee
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 56 deletions.
50 changes: 26 additions & 24 deletions lib/addNewMask.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
const rp = require('request-promise');
const { secretsServerAddress } = require('./logger');

function updateMasks(secret) {
secretsServerAddress
.then((address) => {
const opts = {
uri: `${address}/secrets`,
method: 'POST',
json: true,
body: secret,
resolveWithFullResponse: true,
};
return rp(opts);
})
.then((res) => {
if (res.statusCode >= 400) {
console.log(`could not create mask for secret: ${secret.key}, because server responded with: ${res.statusCode}\n\n${res.body}`);
process.exit(1);
}
console.log(`successfully updated masks with secret: ${secret.key}`);
process.exit(0);
})
.catch((err) => {
console.log(`could not create mask for secret: ${secret.key}, due to error: ${err}`);
process.exit(1);
async function updateMasks(secret) {
try {
const serverAddress = await secretsServerAddress;
console.log(`server address: ${serverAddress}`);

const { default: got } = await import('got');
/** @type {import('got').Got} */
const httpClient = got.extend({
prefixUrl: serverAddress,
responseType: 'json',
throwHttpErrors: false,
});
const response = await httpClient.post(
'secrets',
{ json: secret },
);

if (!response.ok) {
console.error(`could not create mask for secret: ${secret.key}, because server responded with: ${response.statusCode}\n\n${JSON.stringify(response.body)}`);
process.exit(1);
}
console.log(`successfully updated masks with secret: ${secret.key}`);
process.exit(0);
} catch (error) {
console.log(`could not create mask for secret: ${secret.key}, due to error: ${error}`);
process.exit(1);
}
}

if (require.main === module) {
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
"docker-events": "0.0.2",
"dockerode": "^2.5.8",
"fastify": "^4.28.1",
"got": "^14.4.1",
"lodash": "^4.17.21",
"promise-retry": "^2.0.1",
"q": "^1.5.1",
"request": "^2.88.2",
"request-promise": "^4.2.6"
"q": "^1.5.1"
},
"devDependencies": {
"chai": "^4.3.6",
Expand Down
Loading

0 comments on commit 9fbeeee

Please sign in to comment.