acacode / acacode/swagger-typescript-api

ReferenceError: File is not defined

Open
#606 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
4.1k
Forks
436
PR merge metrics
No merged PRs in 30d

Description

A generated client with Axios won't work as expected when executed from Node as you'll get the following error: `ReferenceError: File is not defined`.

The reason is that the following checks were added in https://github.com/acacode/swagger-typescript-api/pull/350 in response to https://github.com/acacode/swagger-typescript-api/issues/293:

https://github.com/acacode/swagger-typescript-api/blob/2b6db2346028fe6fb1f311bf5b1221a89837032e/templates/base/http-clients/axios-http-client.ejs#L87

[`File`](https://developer.mozilla.org/docs/Web/API/File) isn't supported on Node, and based on the description of this project, it seems like generated clients should be working from both Node and browsers, so we shouldn't reference `File` in here.

### Expected Behavior

Uploading a file / using [`multipart/form-data`](https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects) / using [`FormData`](https://developer.mozilla.org/docs/Web/API/FormData) should work as expected from browsers and Node environments.

### Actual Behavior

It works as expected within browsers but crashes with the following error on Node: `ReferenceError: File is not defined`.

### Potential Solution

[`File`](https://developer.mozilla.org/docs/Web/API/File) inherits from [`Blob`](https://developer.mozilla.org/docs/Web/API/Blob), meaning that an instance of `File` will always be an instance of `Blob` as `Blob` constructor's `prototype` property appears in the prototype chain of `File`'s instance:

```typescript
new File(['foo'], 'foo.txt') instanceof Blob // true
```

It means that we should be able to strip the usage of `File` completely and only rely on `Blob`, which is supported by all environments.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.