acacode / acacode/swagger-typescript-api

ReferenceError: File is not defined

Abierto
#606 0 comentarios 1 reacción 0 asignados Ver en GitHub
Lenguaje dominante
TypeScript
Estrellas
4.1k
Forks
436
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

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.

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.