Skip to content

Commit

Permalink
Add SuccessResponseJSON, ErrorResponseJSON helpers (#1456)
Browse files Browse the repository at this point in the history
  • Loading branch information
drwpow committed Nov 23, 2023
1 parent 48fa530 commit 5be2082
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-gifts-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-typescript-helpers": patch
---

Add SuccessResponseJSON, ErrorResponseJSON helpers
15 changes: 11 additions & 4 deletions packages/openapi-typescript-helpers/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,16 @@ export type OperationRequestBodyContent<T> = FilterKeys<
| undefined
: FilterKeys<OperationRequestBodyMediaContent<T>, MediaType>;
/** Return first 2XX response from a Response Object Map */
export type SuccessResponse<T> = FilterKeys<FilterKeys<T, OkStatus>, "content">;
export type SuccessResponse<T> = ResponseContent<FilterKeys<T, OkStatus>>;
/** Return first 5XX or 4XX response (in that order) from a Response Object Map */
export type ErrorResponse<T> = FilterKeys<
FilterKeys<T, ErrorStatus>,
"content"
export type ErrorResponse<T> = ResponseContent<FilterKeys<T, ErrorStatus>>;
/** Return first JSON-like 2XX response from a path + HTTP method */
export type SuccessResponseJSON<PathMethod> = JSONLike<
SuccessResponse<ResponseObjectMap<PathMethod>>
>;
/** Return first JSON-like 5XX or 4XX response from a path + HTTP method */
export type ErrorResponseJSON<PathMethod> = JSONLike<
ErrorResponse<ResponseObjectMap<PathMethod>>
>;

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

0 comments on commit 5be2082

Please sign in to comment.