Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

update error schemas for OpenAPI responses

Kiran Kbca69c32bf21
diff --git a/apps/web/lib/api/errors.ts b/apps/web/lib/api/errors.ts
index ef2e78cbed..2db72c4b17 100644
--- a/apps/web/lib/api/errors.ts
+++ b/apps/web/lib/api/errors.ts
@@ -3,6 +3,7 @@ import { capitalize } from "@dub/utils";
import { NextResponse } from "next/server";
import { ZodError } from "zod";
import { generateErrorMessage } from "zod-error";
+import { ZodOpenApiResponseObject } from "zod-openapi";
import { PlanProps } from "../types";
export const ErrorCode = z.enum([
@@ -156,36 +157,48 @@ export function handleAndReturnErrorResponse(
return NextResponse.json<ErrorResponse>({ error }, { headers, status });
}
-export const errorSchemaFactory = (code: z.infer<typeof ErrorCode>) => {
+export const errorSchemaFactory = (
+ code: z.infer<typeof ErrorCode>,
+ description: string,
+): ZodOpenApiResponseObject => {
return {
- "x-speakeasy-name-override": speakeasyErrorOverrides[code],
- type: "object",
- properties: {
- error: {
- type: "object",
- properties: {
- code: {
- type: "string",
- enum: [code],
- description: "A short code indicating the error code returned.",
- example: code,
- },
- message: {
- type: "string",
- description: "A human readable explanation of what went wrong.",
- example: "The requested resource was not found.",
- },
- doc_url: {
- type: "string",
- description:
- "A link to our documentation with more details about this error code",
- example: `${docErrorUrl}#${code}`,
+ description,
+ content: {
+ "application/json": {
+ schema: {
+ "x-speakeasy-name-override": speakeasyErrorOverrides[code],
+ type: "object",
+ properties: {
+ error: {
+ type: "object",
+ properties: {
+ code: {
+ type: "string",
+ enum: [code],
+ description:
+ "A short code indicating the error code returned.",
+ example: code,
+ },
+ message: {
+ type: "string",
+ description:
+ "A human readable explanation of what went wrong.",
+ example: "The requested resource was not found.",
+ },
+ doc_url: {
+ type: "string",
+ description:
+ "A link to our documentation with more details about this error code",
+ example: `${docErrorUrl}#${code}`,
+ },
+ },
+ required: ["code", "message"],
+ },
},
+ required: ["error"],
},
- required: ["code", "message"],
},
},
- required: ["error"],
};
};
diff --git a/apps/web/lib/openapi/index.ts b/apps/web/lib/openapi/index.ts
index 9c115796d1..5bc2b90ec5 100644
--- a/apps/web/lib/openapi/index.ts
+++ b/apps/web/lib/openapi/index.ts
@@ -53,8 +53,6 @@ export const openApiObject: ZodOpenApiObject = {
scheme: "bearer",
},
},
- // @ts-ignore
- // TODO: Fix this typing issue
responses: {
...openApiErrorResponses,
},
diff --git a/apps/web/lib/openapi/responses.ts b/apps/web/lib/openapi/responses.ts
index 2e77b33dea..d322a8dc7a 100644
--- a/apps/web/lib/openapi/responses.ts
+++ b/apps/web/lib/openapi/responses.ts
@@ -1,90 +1,49 @@
import { errorSchemaFactory } from "@/lib/api/errors";
-
-export const openApiErrorResponses = {
- 400: {
- description:
- "The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).",
- content: {
- "application/json": {
- schema: errorSchemaFactory("bad_request"),
- },
- },
- },
-
- 401: {
- description: `Although the HTTP standard specifies "unauthorized", semantically this response means "unauthenticated". That is, the client must authenticate itself to get the requested response.`,
- content: {
- "application/json": {
- schema: errorSchemaFactory("unauthorized"),
- },
- },
- },
-
- 403: {
- description:
- "The client does not have access rights to the content; that is, it is unauthorized, so the server is refusing to give the requested resource. Unlike 401 Unauthorized, the client's identity is known to the server.",
- content: {
- "application/json": {
- schema: errorSchemaFactory("forbidden"),
- },
- },
- },
-
- 404: {
- description: "The server cannot find the requested resource.",
- content: {
- "application/json": {
- schema: errorSchemaFactory("not_found"),
- },
- },
- },
-
- 409: {
- description:
- "This response is sent when a request conflicts with the current state of the server.",
- content: {
- "application/json": {
- schema: errorSchemaFactory("conflict"),
- },
- },
- },
-
- 410: {
- description:
- "This response is sent when the requested content has been permanently deleted from server, with no forwarding address.",
- content: {
- "application/json": {
- schema: errorSchemaFactory("invite_expired"),
- },
- },
- },
-
- 422: {
- description:
- "The request was well-formed but was unable to be followed due to semantic errors.",
- content: {
- "application/json": {
- schema: errorSchemaFactory("unprocessable_entity"),
- },
- },
- },
-
- 429: {
- description: `The user has sent too many requests in a given amount of time ("rate limiting")`,
- content: {
- "application/json": {
- schema: errorSchemaFactory("rate_limit_exceeded"),
- },
- },
- },
-
- 500: {
- description:
- "The server has encountered a situation it does not know how to handle.",
- content: {
- "application/json": {
- schema: errorSchemaFactory("internal_server_error"),
- },
- },
- },
+import { ZodOpenApiComponentsObject } from "zod-openapi";
+
+export const openApiErrorResponses: ZodOpenApiComponentsObject["responses"] = {
+ "400": errorSchemaFactory(
+ "bad_request",
+ "The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).",
+ ),
+
+ "401": errorSchemaFactory(
+ "unauthorized",
+ `Although the HTTP standard specifies "unauthorized", semantically this response means "unauthenticated". That is, the client must authenticate itself to get the requested response.`,
+ ),
+
+ "403": errorSchemaFactory(
+ "forbidden",
+ "The client does not have access rights to the content; that is, it is unauthorized, so the server is refusing to give the requested resource. Unlike 401 Unauthorized, the client's identity is known to the server.",
+ ),
+
+ "404": errorSchemaFactory(
+ "not_found",
+ "The server cannot find the requested resource.",
+ ),
+
+ "409": errorSchemaFactory(
+ "conflict",
+ "This response is sent when a request conflicts with the current state of the server.",
+ ),
+
+ "410": errorSchemaFactory(
+ "invite_expired",
+ "This response is sent when the requested content has been permanently deleted from server, with no forwarding address.",
+ ),
+
+ "422": errorSchemaFactory(
+ "unprocessable_entity",
+ "The request was well-formed but was unable to be followed due to semantic errors.",
+ ),
+
+ "429": errorSchemaFactory(
+ "rate_limit_exceeded",
+ `The user has sent too many requests in a given amount of time ("rate limiting")`,
+ ),
+
+ "500": errorSchemaFactory(
+ "internal_server_error",
+ "The server has encountered a situation it does not know how to handle.",
+ ),
};