Skip to content

Commit d94eea9

Browse files
authored
fix(runtime): Enhance status response type to handle 5XX errors (#76)
1 parent 913a274 commit d94eea9

File tree

3 files changed

+49
-50
lines changed

3 files changed

+49
-50
lines changed

.yarn/versions/c24b218d.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
releases:
2+
"@typoas/runtime": minor
3+
4+
declined:
5+
- "@typoas/react-query"

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
## 4.2.0 - 2025-02-10
88

9-
> All packages were updated to 4.2.0 except runtime (still at 4.1.0)
9+
> All packages were updated to 4.2.0
1010
11+
- fix(runtime): Enhance status response type to handle 5XX errors [#76](https://github.com/Embraser01/typoas/pull/76)
1112
- feat(react-query): Export getQueryFunctionKey helper [#75](https://github.com/Embraser01/typoas/pull/75)
1213
- fix(generator): Simplify sub schemas with type object [#74](https://github.com/Embraser01/typoas/pull/74)
1314
- feat(generator): Allow overrides during generation (fix [#71](https://github.com/Embraser01/typoas/issues/71)) [#73](https://github.com/Embraser01/typoas/pull/73)

packages/typoas-runtime/src/fetcher/types.ts

Lines changed: 42 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,9 @@ export interface ResponseBody {
4747
json(): Promise<unknown>;
4848
}
4949

50-
/**
51-
* Represents an HTTP status code including the default
52-
* status codes and the 1XX, 4XX, 5XX ranges.
53-
*/
54-
export type EnhancedHTTPStatus =
55-
| 'default'
56-
| '1XX'
57-
| '2XX'
58-
| '3XX'
59-
| '4XX'
60-
| '5XX'
61-
| 100
62-
| 101
63-
| 102
64-
| 103
50+
export type HTTPStatus1XX = 100 | 101 | 102 | 103;
51+
52+
export type HTTPStatus2XX =
6553
| 200
6654
| 201
6755
| 202
@@ -71,16 +59,11 @@ export type EnhancedHTTPStatus =
7159
| 206
7260
| 207
7361
| 208
74-
| 226
75-
| 300
76-
| 301
77-
| 302
78-
| 303
79-
| 304
80-
| 305
81-
| 306
82-
| 307
83-
| 308
62+
| 226;
63+
64+
export type HTTPStatus3XX = 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308;
65+
66+
export type HTTPStatus4XX =
8467
| 400
8568
| 401
8669
| 402
@@ -111,7 +94,9 @@ export type EnhancedHTTPStatus =
11194
| 429
11295
| 430
11396
| 431
114-
| 451
97+
| 451;
98+
99+
export type HTTPStatus5XX =
115100
| 500
116101
| 501
117102
| 502
@@ -126,32 +111,28 @@ export type EnhancedHTTPStatus =
126111
| 511;
127112

128113
/**
129-
* Extracts only the successful status codes of EnhancedHTTPStatus.
114+
* Represents an HTTP status code including the default
115+
* status codes and the 1XX, 4XX, 5XX ranges.
130116
*/
131-
export type SuccessfulStatus = Extract<
132-
EnhancedHTTPStatus,
117+
export type EnhancedHTTPStatus =
133118
| 'default'
119+
| '1XX'
120+
| HTTPStatus1XX
134121
| '2XX'
135-
| 200
136-
| 201
137-
| 202
138-
| 203
139-
| 204
140-
| 205
141-
| 206
142-
| 207
143-
| 208
144-
| 226
122+
| HTTPStatus2XX
145123
| '3XX'
146-
| 300
147-
| 301
148-
| 302
149-
| 303
150-
| 304
151-
| 305
152-
| 306
153-
| 307
154-
| 308
124+
| HTTPStatus3XX
125+
| '4XX'
126+
| HTTPStatus4XX
127+
| '5XX'
128+
| HTTPStatus5XX;
129+
130+
/**
131+
* Extracts only the successful status codes of EnhancedHTTPStatus.
132+
*/
133+
export type SuccessfulStatus = Extract<
134+
EnhancedHTTPStatus,
135+
'default' | '2XX' | HTTPStatus2XX | '3XX' | HTTPStatus3XX
155136
>;
156137

157138
/**
@@ -166,7 +147,19 @@ export type StatusResponse<
166147
headers: Record<string, string>;
167148
ok: S extends SuccessfulStatus ? true : S extends 'default' ? boolean : false;
168149
// When using ranges or default, we can't now the specific status code.
169-
status: S extends string ? number : S;
150+
status: S extends '1XX'
151+
? HTTPStatus1XX
152+
: S extends '2XX'
153+
? HTTPStatus2XX
154+
: S extends '3XX'
155+
? HTTPStatus3XX
156+
: S extends '4XX'
157+
? HTTPStatus4XX
158+
: S extends '5XX'
159+
? HTTPStatus5XX
160+
: S extends 'default'
161+
? number
162+
: S;
170163
};
171164

172165
export type BaseFetcherData = {

0 commit comments

Comments
 (0)