From 01f068a62de34d6deeb41d7f00b1ec2fd4b104d8 Mon Sep 17 00:00:00 2001 From: Ankit Dash <100564488+helios2003@users.noreply.github.com> Date: Wed, 15 May 2024 21:17:02 +0530 Subject: [PATCH] chore: use ``node-fetch`` instead of ``request`` (#1422) Co-authored-by: asyncapi-bot %0ACo-authored-by: samz --- scripts/fetch-asyncapi-example.js | 32 ++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/scripts/fetch-asyncapi-example.js b/scripts/fetch-asyncapi-example.js index 0f60108ff5e..636178cc880 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'); @@ -23,14 +23,28 @@ const EXAMPLE_DIRECTORY = path.join(__dirname, '../assets/examples'); 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); - }); + 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 () => {