acacode / acacode/swagger-typescript-api

[SUGGESTION] New config option similar to `unwrapResponseData` that preserves error types

Đang mở
#1,237 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ả

# Suggestion:

Would it be desirable to have another option `unwrapResponseDataWithErrors` or perhaps to turn `unwrapResponseData` into a union `'disabled' | 'responseOnly' | 'responseAndError'`?

At the moment, it seems that `unwrapResponseData` is not including the error and default response types.

Here:
https://github.com/acacode/swagger-typescript-api/blob/0e251bb042bdec35fd6efa714868486ff882c7e4/templates/base/http-clients/fetch-http-client.ejs#L170-L175

This could be something like this instead:
```ejs
<% if (config.unwrapResponseData) { %>
}: FullRequestParams): Promise => {
<% } else if (config.unwrapResponseDataWithErrors) { %>
}: FullRequestParams): Promise => {
<% } else { %>
}: FullRequestParams): Promise> => {
<% } %>
```
Maybe I am misunderstanding the intent behind `unwrapResponseData` and this is not a good idea for reasons. But if this is something that could be useful, I am happy to contribute with a PR.

# Context

I have the following (shortened) OpenAPI spec:

[openapi-spec.json](https://github.com/user-attachments/files/20264378/openapi-spec.json)

The takeaway from this specification is that I expect the return type to be something like this:

```ts
export type ApiResponse =
| {
success: 'true';
data: object;
}
| {
success: 'false';
error:
| 'bad-request'
| 'github-api-error'
| 'parse-or-unknown'
| 'missing-envs'
| 'blob-storage-error'
| 'file-too-large';
description: string;
};
```
I generate a client from the specification above using `swagger-typescript-api` version `13.1.3`:

```ts
import { generateApi } from 'swagger-typescript-api';
import specification from '...';

(async () => {
await generateApi({ spec: specification, output: process.cwd(), unwrapResponseData: true });
})();
```

## The problem
The client works, but `unwrapResponseData` only includes the response type for status `200`, so the return type that I'm actually getting is this:

```ts
export type ApiResponse = {
success: 'true';
data: object;
};
```
![image](https://github.com/user-attachments/assets/11bdc13c-76eb-4c4d-ade9-4da426134fc2)

## The solution
If I want the return type to include the error; in the generated client, all I need to do is modify this line:
https://github.com/acacode/swagger-typescript-api/blob/0e251bb042bdec35fd6efa714868486ff882c7e4/templates/base/http-clients/fetch-http-client.ejs#L172
To be
```ts
public request = async ({
body,
secure,
path,
type,
query,
format,
baseUrl,
cancelToken,
...params
}: FullRequestParams): Promise => { // Added | E here
```
Instead of
```ts
public request = async ({
body,
secure,
path,
type,
query,
format,
baseUrl,
cancelToken,
...params
}: FullRequestParams): Promise => {
```
And then my types are assigned as expected:
![image](https://github.com/user-attachments/assets/e0ea36d6-e800-4679-aed7-27a6d3e6ce10)

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.