Skip to content

fix: switch to cross-fetch from request - #7

Merged
daniel-white merged 5 commits into
stoplightio:masterfrom
strmer15:use_node_fetch
Jul 21, 2022
Merged

fix: switch to cross-fetch from request#7
daniel-white merged 5 commits into
stoplightio:masterfrom
strmer15:use_node_fetch

Conversation

@strmer15

@strmer15 strmer15 commented Apr 18, 2022

Copy link
Copy Markdown

Motivation and Context

This library currently uses the request library for its CLI usage, but request has been deprecated for over 2 years, which means that all projects that depend on it also install request, which includes all users of @stoplight/prism-cli. cross-fetch is an isomorphic implementation of the fetch API that is not deprecated and provides very similar functionality.

Description

I removed the request dependency, added in the cross-fetch dependency, removed yarn.lock and then ran yarn install. I replaced the require('request') with require('node-fetch'), switched the request(...) invocation to fetch(...).then(...).catch(...), and pulled out the common error handling code into a separate function. I uncommented all the CLI tests, fixed them, then ran yarn test to verify they all passed.

How Has This Been Tested?

Ran yarn test to verify everything passed locally, and uncommented the CLI tests to verify they worked.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

@strmer15

Copy link
Copy Markdown
Author

@chohmann Not sure if you can review or someone else can, this would help resolve a lot of dependency pollution in apps like ours that don't use the request library but have it pulled in by @stoplight/prism-cli.

@strmer15

Copy link
Copy Markdown
Author

@marbemac Can you review this or do you know who can? Thanks!

@CGiustiniani

Copy link
Copy Markdown

@marbemac, @wmhilton, @daniel-white, @lottamus, @mmiask Can anybody please review/check this PR as the request package is deprecated for over 2 years now

@daniel-white

Copy link
Copy Markdown

Should we make this use an isomorphic version to run in the browser?

Comment thread lib/cli/in.js Outdated
fetch(url)
.then(function (response) {
if (response.status === 200) {
callback(body);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

where is body coming from now? we'll need to use the response.text() or equivalent here

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good call - I'll fix that up. I guess I'm too used to Typescript where I would have gotten a type error 😁

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Should be fixed now! I also spent a lot of time making sure the unit tests actually worked - they would pass previously but they weren't actually working due to the way stdout and stderr were being passed back in the test helper function. I added in a test express server that would allow us to verify that fetch is bringing back the correct data.

Comment thread package.json Outdated
Comment thread package.json
@strmer15

Copy link
Copy Markdown
Author

Should we make this use an isomorphic version to run in the browser?

@daniel-white Thanks for reviewing! This is a CLI as far as I can tell, so I don't think it will ever run in a browser. That said, I don't mind using cross-fetch if you think it makes sense.

@daniel-white

Copy link
Copy Markdown

@strmer15 cross-fetch would be appreciated - internally we do use this in a browser, so the flexibility there would be be appreciated

…eading the request body; use cross-fetch instead of node-fetch; fix the unit tests and add in a test server; remove old stubby config that wasn't used; fix reading from stdin
@strmer15 strmer15 changed the title switch to node-fetch from request, update dependencies switch to cross-fetch from request Jul 20, 2022
@strmer15

Copy link
Copy Markdown
Author

@strmer15 cross-fetch would be appreciated - internally we do use this in a browser, so the flexibility there would be be appreciated

@daniel-white done! Let me know if you have any other concerns with this PR, thanks for reviewing!

@daniel-white daniel-white changed the title switch to cross-fetch from request fix: switch to cross-fetch from request Jul 21, 2022
@daniel-white
daniel-white merged commit 0a85c0e into stoplightio:master Jul 21, 2022
@strmer15
strmer15 deleted the use_node_fetch branch July 21, 2022 13:17
@stoplight-bot

Copy link
Copy Markdown
Collaborator

🎉 This PR is included in version 1.0.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants