acacode / acacode/swagger-typescript-api

ReferenceError: File is not defined

Offen
#606 0 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
TypeScript
Sterne
4.1k
Forks
436
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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.

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.