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

Runtime aspect of HttpStatusCode missing from JavaScript files #5126

Closed
tjcrowder opened this issue Oct 14, 2022 · 5 comments · Fixed by #5345
Closed

Runtime aspect of HttpStatusCode missing from JavaScript files #5126

tjcrowder opened this issue Oct 14, 2022 · 5 comments · Fixed by #5345

Comments

@tjcrowder
Copy link

tjcrowder commented Oct 14, 2022

Describe the bug

Apologies in advance if I've got the wrong end of the stick, but I think the recent PR adding HttpStatusCode missed out the runtime aspect of the new enum. (TypeScript enums aren't purely type information, they also include a runtime aspect.) For instance, for HttpStatusCode it would look like (depending on TypeScript settings):

export var HttpStatusCode;
(function (HttpStatusCode) {
    HttpStatusCode[HttpStatusCode["Continue"] = 100] = "Continue";
    HttpStatusCode[HttpStatusCode["SwitchingProtocols"] = 101] = "SwitchingProtocols";
    // ...
})(HttpStatusCode || (HttpStatusCode = {}));

But HttpStatusCode doesn't appear anywhere in the current axios other than index.d.ts.

Without the runtime part, you get runtime errors trying to use HttpStatusCode, see this Stack Overflow question.

To Reproduce

Here's a repo with a minimal project to replicate it: https://github.com/tjcrowder/typescript-axios-issue. If you clone it, then npm install && npm run build && node test.js, it'll demonstrate the problem.

Alternatively, here are steps for creating it from scratch:

  1. Create a new blank project
  2. npm install axios
  3. npm install --save-dev typescript
  4. Add test.ts:
    import * as axios from "axios";
    import { HttpStatusCode } from "axios";
    console.log(typeof axios); // Works: "function"
    console.log(HttpStatusCode.Ok);
    
  5. Edit "scripts" to have "build": "tsc test.ts"
  6. npm run build
  7. node test.js

Result:

TypeError: Cannot read properties of undefined (reading 'Ok')

Code snippet

No response

Expected behavior

It should show 200 for HttpStatusCode.

Axios Version

1.1.2

Adapter Version

n/a

Browser

n/a

Browser Version

n/a

Node.js Version

16.13.2

OS

Linux Mint v20

Additional Library Versions

No response

Additional context/Screenshots

(What does "Feel free to these instructions before submitting the issue" in the bug report template mean? :-) )

@mcmxcdev
Copy link

I can confirm this, happens to us too and keeps us from being able to drop our dependency on https://github.com/jrylan/status-code-enum

@goonerify
Copy link

Same here. I ended up just copying over the enum values from axios

@GalZen
Copy link

GalZen commented Oct 31, 2022

same here

@ninoman
Copy link

ninoman commented Nov 16, 2022

same for me

@Strato
Copy link

Strato commented Dec 2, 2022

same here, but better

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 a pull request may close this issue.

6 participants