acacode / acacode/swagger-typescript-api

async/promises are unable to resolve in ETA templates

Đang mở
#307 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
enhancement
Ngôn ngữ chính
TypeScript
Star
4.1k
Fork
436
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

Hi!

Love your package, very useful. We were attempting to use custom ETA templates but we noticed promises don't appear to be supported currently by `swagger-typescript-api`.

# Minimal replicable example

**templates/api.eta**

```
<%
const test = Promise.resolve('test');
%>

<%~ await test %>
```

**openapi.json**

Copied from https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v2.0/json/petstore.json (doesn't really matter what the contents of this file are in this case)

**Command to run**

`yarn swagger-typescript-api --templates ./templates -p ./openapi.json`

## The error

```
(node:35256) UnhandledPromiseRejectionWarning: Error: Swagger schema parse error!
Eta Error: Bad template syntax

await is only valid in async function
```

# Why would this be useful?

Our current use case is to allow the ability to use `import` syntax. You can technically use `import` already like so:

```
<%
import('micromatch').then(console.log);
%>
```

but it's not useful without being able to, ultimately, resolve the promise as mentioned previously. Another alternative would be to add support for `require` but I think supporting promises would be more useful as it would enable anything that returns a promise to be used (E.g. `readFile` or `fetch`), not just `import`.

# Potential solution

I believe all that needs to be done in order to support this feature is to make the functions that render the template internally within swagger-typescript-api `async`.

I'm quite happy to add this feature myself if you agree with the solution.

For instance:

Screen Shot 2021-10-19 at 11 45 37 am

Would be come:

Screen Shot 2021-10-19 at 11 46 01 am

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.