Skip to content

Feature add custom http headers #764

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

Merged
merged 35 commits into from
Sep 30, 2021
Merged

Conversation

drwpow
Copy link
Contributor

@drwpow drwpow commented Sep 30, 2021

Supercedes #713.

Changes

Other than the commented code below, the following changes were made from #713

  • The load unit test was removed. Reason being that didn’t give much assurance that the entire CLI was behaving as desired (in my limited testing, it wasn’t)
  • The util.test.ts unit test was also removed. This was a little too implementation detail-y, and it only retested what existed in other tests.

let httpHeaders = {};
// prefer --headersObject if specified
if (cli.flags.headersObject) {
httpHeaders = JSON.parse(cli.flags.headersObject); // note: this will generate a recognizable error for the user to act on
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Minor change here: we had a getCLISchemaHeadersJSON() function that did a lot of setup around JSON.parse(), and in the end obscured the error from the user. Instead, I’d like to expose the error to the user so it’s more clear what’s not parsing correctly.

// otherwise, parse --header
else if (Array.isArray(cli.flags.header)) {
cli.flags.header.forEach((header) => {
const firstColon = header.indexOf(":");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was using a RegEx instead (/(\w+)(\:)\s((\w+)[\-|\:]?.+)/gi;), but in testing I found it to be brittle and a little awkward. Instead I opted for more of a curl-like approach where the first : designates the key–value relationship. It’s then up to the user to format their header correctly.

@codecov
Copy link

codecov bot commented Sep 30, 2021

Codecov Report

Merging #764 (4d9d18c) into main (56cc872) will decrease coverage by 2.21%.
The diff coverage is 42.85%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #764      +/-   ##
==========================================
- Coverage   96.34%   94.12%   -2.22%     
==========================================
  Files          13       13              
  Lines         547      562      +15     
  Branches      194      195       +1     
==========================================
+ Hits          527      529       +2     
- Misses         19       32      +13     
  Partials        1        1              
Impacted Files Coverage Δ
src/load.ts 82.50% <33.33%> (-8.93%) ⬇️
src/index.ts 96.66% <100.00%> (ø)
src/utils.ts 94.82% <0.00%> (-1.73%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 56cc872...4d9d18c. Read the comment docs.

@drwpow drwpow force-pushed the feature-add-custom-http-headers branch from 4456647 to 4d9d18c Compare September 30, 2021 20:13
* or Accept: text/yaml to be sent in order to figure out how to properly fetch the OpenAPI/Swagger document as code.
* These headers will only be sent in the case that the schema URL protocol is of type http or https.
*/
httpHeaders?: Headers;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is an opinionated change, but in the previous PR we allowed httpHeaders?: to be a polymorphic type, from null to a JS object to a Map. We then had to implement quite a bit of runtime logic to check to make sure everything lined up correctly.

Instead, I opted to go with just a plain JS object for headers because that’s what gets passed to fetch() anyway. Servers are lax in what they accept. And while I completely get the author’s original desire to strongly type their request headers, I believe that’s outside the scope of this library, and could only introduce possible bugs/unnecessary maintenance.

*
* @default {string} GET
*/
httpMethod?: string;
Copy link
Contributor Author

@drwpow drwpow Sep 30, 2021

Choose a reason for hiding this comment

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

Similar approach: rather than maintain a discriminated union of all valid HTTP methods, I opted instead to allow a simple string like most every other library allows. Perhaps your server allows more!

Again, I know this was not the author’s original intent, and I respect their level of quality! But I feel this was one of many details which just added on a lot of maintenance and potential typing bugs without much benefit to the average user.

@drwpow drwpow merged commit 3923cb9 into main Sep 30, 2021
@drwpow drwpow deleted the feature-add-custom-http-headers branch September 30, 2021 20:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants