Skip to content

Commit 5be2082

Browse files
authored
Add SuccessResponseJSON, ErrorResponseJSON helpers (#1456)
1 parent 48fa530 commit 5be2082

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

.changeset/twelve-gifts-sort.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-typescript-helpers": patch
3+
---
4+
5+
Add SuccessResponseJSON, ErrorResponseJSON helpers

packages/openapi-typescript-helpers/index.d.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,16 @@ export type OperationRequestBodyContent<T> = FilterKeys<
6767
| undefined
6868
: FilterKeys<OperationRequestBodyMediaContent<T>, MediaType>;
6969
/** Return first 2XX response from a Response Object Map */
70-
export type SuccessResponse<T> = FilterKeys<FilterKeys<T, OkStatus>, "content">;
70+
export type SuccessResponse<T> = ResponseContent<FilterKeys<T, OkStatus>>;
7171
/** Return first 5XX or 4XX response (in that order) from a Response Object Map */
72-
export type ErrorResponse<T> = FilterKeys<
73-
FilterKeys<T, ErrorStatus>,
74-
"content"
72+
export type ErrorResponse<T> = ResponseContent<FilterKeys<T, ErrorStatus>>;
73+
/** Return first JSON-like 2XX response from a path + HTTP method */
74+
export type SuccessResponseJSON<PathMethod> = JSONLike<
75+
SuccessResponse<ResponseObjectMap<PathMethod>>
76+
>;
77+
/** Return first JSON-like 5XX or 4XX response from a path + HTTP method */
78+
export type ErrorResponseJSON<PathMethod> = JSONLike<
79+
ErrorResponse<ResponseObjectMap<PathMethod>>
7580
>;
7681

7782
// Generic TS utils
@@ -82,6 +87,8 @@ export type FilterKeys<Obj, Matchers> = {
8287
}[keyof Obj];
8388
/** Return any `[string]/[string]` media type (important because openapi-fetch allows any content response, not just JSON-like) */
8489
export type MediaType = `${string}/${string}`;
90+
/** Return any media type containing "json" (works for "application/json", "application/vnd.api+json", "application/vnd.oai.openapi+json") */
91+
export type JSONLike<T> = FilterKeys<T, `${string}/json`>;
8592
/** Filter objects that have required keys */
8693
export type FindRequiredKeys<T, K extends keyof T> = K extends unknown
8794
? undefined extends T[K]

0 commit comments

Comments
 (0)