acacode / acacode/swagger-typescript-api

ReferenceError: File is not defined

未關閉
#606 0 則留言 1 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
TypeScript
星號
4.1k
分支
436
PR 合併指標
30 天內沒有已合併 PR

描述

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.

貢獻指南

這個儲存庫沒有索引到貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。