Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use node-fetch instead of request #1422

Merged
merged 5 commits into from
May 15, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions scripts/fetch-asyncapi-example.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-var-requires */

const request = require('request');
const fetch = require('node-fetch');
const fs = require('fs');
const unzipper = require('unzipper');
const path = require('path');
Expand All @@ -24,12 +24,20 @@ const TEMP_ZIP_NAME = 'spec-examples.zip';

const fetchAsyncAPIExamplesFromExternalURL = () => {
return new Promise((resolve, reject) => {
request(SPEC_EXAMPLES_ZIP_URL)
.pipe(fs.createWriteStream(TEMP_ZIP_NAME))
.on('close', () => {
console.log('Fetched ZIP file');
resolve();
}).on('error', reject);
fetch(SPEC_EXAMPLES_ZIP_URL)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error handling can be improved by wrapping by a try/catch block (see: https://github.com/node-fetch/node-fetch?tab=readme-ov-file#handling-exceptions)

.then((res) => {
if (res.status !== 200) {
reject(new Error(`Failed to fetch examples from ${SPEC_EXAMPLES_ZIP_URL}`));
}
const file = fs.createWriteStream(TEMP_ZIP_NAME);
res.body.pipe(file);
file.on('close', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Listen also the the error event

file.on('error', (err) => {
                    reject(err);
                });

console.log('Fetched ZIP file');
file.close();
resolve();
}).on('error', reject);
}
).catch(reject);
});
};

Expand Down
Loading