From 3eb4c76107b92b14e5d7fb5a2663b3f2c6d6da47 Mon Sep 17 00:00:00 2001 From: Drew Powers Date: Thu, 23 Nov 2023 11:12:19 -0700 Subject: [PATCH] Add SuccessResponseJSON, ErrorResponseJSON helpers --- .changeset/twelve-gifts-sort.md | 5 +++++ packages/openapi-typescript-helpers/index.d.ts | 15 +++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 .changeset/twelve-gifts-sort.md diff --git a/.changeset/twelve-gifts-sort.md b/.changeset/twelve-gifts-sort.md new file mode 100644 index 000000000..b460a2111 --- /dev/null +++ b/.changeset/twelve-gifts-sort.md @@ -0,0 +1,5 @@ +--- +"openapi-typescript-helpers": patch +--- + +Add SuccessResponseJSON, ErrorResponseJSON helpers diff --git a/packages/openapi-typescript-helpers/index.d.ts b/packages/openapi-typescript-helpers/index.d.ts index b7f851f11..cd7ad8929 100644 --- a/packages/openapi-typescript-helpers/index.d.ts +++ b/packages/openapi-typescript-helpers/index.d.ts @@ -67,11 +67,16 @@ export type OperationRequestBodyContent = FilterKeys< | undefined : FilterKeys, MediaType>; /** Return first 2XX response from a Response Object Map */ -export type SuccessResponse = FilterKeys, "content">; +export type SuccessResponse = ResponseContent>; /** Return first 5XX or 4XX response (in that order) from a Response Object Map */ -export type ErrorResponse = FilterKeys< - FilterKeys, - "content" +export type ErrorResponse = ResponseContent>; +/** Return first JSON-like 2XX response from a path + HTTP method */ +export type SuccessResponseJSON = JSONLike< + SuccessResponse> +>; +/** Return first JSON-like 5XX or 4XX response from a path + HTTP method */ +export type ErrorResponseJSON = JSONLike< + ErrorResponse> >; // Generic TS utils @@ -82,6 +87,8 @@ export type FilterKeys = { }[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 = FilterKeys; /** Filter objects that have required keys */ export type FindRequiredKeys = K extends unknown ? undefined extends T[K]