From 7ecfa21b36b1520a3b8dbe97dbf3085402ecbc01 Mon Sep 17 00:00:00 2001
From: Makenna Smutz <32865577+KenzoBenzo@users.noreply.github.com>
Date: Thu, 9 May 2024 14:16:07 -0400
Subject: [PATCH] feat: show custom status code field in API docs (#833)
---
.../endpoints/EndpointContentCodeSnippets.tsx | 16 +++++-
.../utils/getSuccessMessageForStatus.ts | 51 +++++++++++++++++++
.../app/src/resolver/ApiDefinitionResolver.ts | 2 +-
.../ApiDefinitionResolver.test.ts.snap | 1 +
packages/ui/app/src/resolver/types.ts | 1 +
5 files changed, 69 insertions(+), 2 deletions(-)
create mode 100644 packages/ui/app/src/api-page/utils/getSuccessMessageForStatus.ts
diff --git a/packages/ui/app/src/api-page/endpoints/EndpointContentCodeSnippets.tsx b/packages/ui/app/src/api-page/endpoints/EndpointContentCodeSnippets.tsx
index ba1eb01ca9..80962cfe5e 100644
--- a/packages/ui/app/src/api-page/endpoints/EndpointContentCodeSnippets.tsx
+++ b/packages/ui/app/src/api-page/endpoints/EndpointContentCodeSnippets.tsx
@@ -8,6 +8,7 @@ import { CodeSnippetExample } from "../examples/CodeSnippetExample";
import { JsonPropertyPath } from "../examples/JsonPropertyPath";
import type { CodeExample, CodeExampleGroup } from "../examples/code-example";
import { lineNumberOf } from "../examples/utils";
+import { getSuccessMessageForStatus } from "../utils/getSuccessMessageForStatus";
import { CodeExampleClientDropdown } from "./CodeExampleClientDropdown";
import { EndpointUrlWithOverflow } from "./EndpointUrlWithOverflow";
import { ErrorCodeSnippetExample } from "./ErrorCodeSnippetExample";
@@ -110,11 +111,24 @@ const UnmemoizedEndpointContentCodeSnippets: React.FC
{endpoint.responseBody?.shape.type === "fileDownload" && }
+
{example.responseBody != null &&
endpoint.responseBody?.shape.type !== "fileDownload" &&
(selectedError == null ? (
+ Response -{" "}
+
+ {endpoint.responseBody?.statusCode +
+ " " +
+ getSuccessMessageForStatus(
+ endpoint.responseBody?.statusCode as number,
+ endpoint.method,
+ )}
+
+
+ }
onClick={(e) => {
e.stopPropagation();
}}
diff --git a/packages/ui/app/src/api-page/utils/getSuccessMessageForStatus.ts b/packages/ui/app/src/api-page/utils/getSuccessMessageForStatus.ts
new file mode 100644
index 0000000000..fd54b9fc53
--- /dev/null
+++ b/packages/ui/app/src/api-page/utils/getSuccessMessageForStatus.ts
@@ -0,0 +1,51 @@
+type StatusCodeMessages = {
+ [method: string]: string;
+};
+
+type SuccessMessages = {
+ [code: number]: StatusCodeMessages;
+};
+
+export const COMMON_SUCCESS_NAMES: SuccessMessages = {
+ 200: {
+ PUT: "Updated",
+ DELETE: "Deleted",
+ default: "Success",
+ },
+ 201: {
+ PUT: "Updated",
+ default: "Created",
+ },
+ 202: {
+ default: "Accepted",
+ },
+ 203: {
+ default: "Non-Authoritative Information",
+ },
+ 204: {
+ default: "No Content",
+ },
+ 205: {
+ default: "Reset Content",
+ },
+ 206: {
+ default: "Partial Content",
+ },
+ 207: {
+ default: "Multi-Status",
+ },
+ 208: {
+ default: "Already Reported",
+ },
+ 226: {
+ default: "IM Used",
+ },
+};
+
+export function getSuccessMessageForStatus(statusCode: number, method: string): string {
+ const messages = COMMON_SUCCESS_NAMES[statusCode];
+ if (!messages) {
+ return "Success";
+ }
+ return messages[method] ?? messages.default;
+}
diff --git a/packages/ui/app/src/resolver/ApiDefinitionResolver.ts b/packages/ui/app/src/resolver/ApiDefinitionResolver.ts
index 35a2d3e6f5..be034ebf15 100644
--- a/packages/ui/app/src/resolver/ApiDefinitionResolver.ts
+++ b/packages/ui/app/src/resolver/ApiDefinitionResolver.ts
@@ -414,7 +414,7 @@ export class ApiDefinitionResolver {
this.resolveResponseBodyShape(response.type),
maybeSerializeMdxContent(response.description),
]);
- return { shape, description, availability: undefined };
+ return { shape, description, statusCode: response?.statusCode ?? 200, availability: undefined };
}
// HACKHACK: this handles the case where FernIR is unable to interpret the security scheme for AsyncAPI
diff --git a/packages/ui/app/src/resolver/__test__/__snapshots__/ApiDefinitionResolver.test.ts.snap b/packages/ui/app/src/resolver/__test__/__snapshots__/ApiDefinitionResolver.test.ts.snap
index b26338d43c..9b666d2cf6 100644
--- a/packages/ui/app/src/resolver/__test__/__snapshots__/ApiDefinitionResolver.test.ts.snap
+++ b/packages/ui/app/src/resolver/__test__/__snapshots__/ApiDefinitionResolver.test.ts.snap
@@ -47,6 +47,7 @@ exports[`resolveApiDefinition > should finish resolving 1`] = `
},
"type": "list",
},
+ "statusCode": 200,
},
"slug": [
"merchants",
diff --git a/packages/ui/app/src/resolver/types.ts b/packages/ui/app/src/resolver/types.ts
index be2f95e327..8f38faa098 100644
--- a/packages/ui/app/src/resolver/types.ts
+++ b/packages/ui/app/src/resolver/types.ts
@@ -348,6 +348,7 @@ export interface ResolvedObjectProperty extends WithMetadata {
export interface ResolvedResponseBody extends WithMetadata {
shape: ResolvedHttpResponseBodyShape;
+ statusCode: number;
}
export type ResolvedEndpointPathParts = ResolvedEndpointPathParts.Literal | ResolvedEndpointPathParts.PathParameter;