From 6bd364119a084940315403ef69ddb6b1e466acc1 Mon Sep 17 00:00:00 2001 From: Ankit Dash Date: Tue, 14 May 2024 12:05:15 +0530 Subject: [PATCH 1/2] change request to node-fetch --- scripts/fetch-asyncapi-example.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/scripts/fetch-asyncapi-example.js b/scripts/fetch-asyncapi-example.js index 0f60108ff5e..13cc86c0393 100644 --- a/scripts/fetch-asyncapi-example.js +++ b/scripts/fetch-asyncapi-example.js @@ -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'); @@ -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) + .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', () => { + console.log('Fetched ZIP file'); + file.close(); + resolve(); + }).on('error', reject); + } + ).catch(reject); }); }; From b4322094c6168784fcb30924b7c6ad4336bf08d5 Mon Sep 17 00:00:00 2001 From: Ankit Dash Date: Tue, 14 May 2024 16:51:12 +0530 Subject: [PATCH 2/2] improve error handling --- scripts/fetch-asyncapi-example.js | 38 ++++++++++++++++++------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/scripts/fetch-asyncapi-example.js b/scripts/fetch-asyncapi-example.js index 13cc86c0393..636178cc880 100644 --- a/scripts/fetch-asyncapi-example.js +++ b/scripts/fetch-asyncapi-example.js @@ -23,22 +23,28 @@ const EXAMPLE_DIRECTORY = path.join(__dirname, '../assets/examples'); const TEMP_ZIP_NAME = 'spec-examples.zip'; const fetchAsyncAPIExamplesFromExternalURL = () => { - return new Promise((resolve, reject) => { - fetch(SPEC_EXAMPLES_ZIP_URL) - .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', () => { - console.log('Fetched ZIP file'); - file.close(); - resolve(); - }).on('error', reject); - } - ).catch(reject); - }); + try { + return new Promise((resolve, reject) => { + fetch(SPEC_EXAMPLES_ZIP_URL) + .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', () => { + console.log('Fetched ZIP file'); + file.close(); + resolve(); + }).on('error', (err) => { + reject(err); + }); + }) + .catch(reject); + }); + } catch (error) { + console.error(error); + } }; const unzipAsyncAPIExamples = async () => {