Skip to content

Commit 3982b7e

Browse files
authored
chore: Alias /openapi.json to /json (#716)
1 parent 69b9eb8 commit 3982b7e

File tree

6 files changed

+24
-14
lines changed

6 files changed

+24
-14
lines changed

src/server/middleware/auth.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { logger } from "../../utils/logger";
2525
import { sendWebhookRequest } from "../../utils/webhook";
2626
import { Permission } from "../schemas/auth";
2727
import { ADMIN_QUEUES_BASEPATH } from "./adminRoutes";
28+
import { OPENAPI_ROUTES } from "./open-api";
2829

2930
export type TAuthData = never;
3031
export type TAuthSession = { permissions: string };
@@ -211,7 +212,7 @@ const handlePublicEndpoints = (req: FastifyRequest): AuthResponse => {
211212
req.url === "/favicon.ico" ||
212213
req.url === "/" ||
213214
req.url === "/system/health" ||
214-
req.url === "/json" ||
215+
OPENAPI_ROUTES.includes(req.url) ||
215216
req.url.startsWith("/auth/user") ||
216217
req.url.startsWith("/transaction/status")
217218
) {

src/server/middleware/logs.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import type { FastifyInstance } from "fastify";
22
import { stringify } from "thirdweb/utils";
33
import { logger } from "../../utils/logger";
44
import { ADMIN_QUEUES_BASEPATH } from "./adminRoutes";
5+
import { OPENAPI_ROUTES } from "./open-api";
56

67
const SKIP_LOG_PATHS = new Set([
78
"",
89
"/",
910
"/favicon.ico",
1011
"/system/health",
11-
"/json",
1212
"/static",
13+
...OPENAPI_ROUTES,
1314
// Skip these routes case of importing sensitive details.
1415
"/backend-wallet/import",
1516
"/configuration/wallets",

src/server/middleware/open-api.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import swagger from "@fastify/swagger";
22
import type { FastifyInstance } from "fastify";
33

4+
export const OPENAPI_ROUTES = ["/json", "/openapi.json"];
5+
46
export const withOpenApi = async (server: FastifyInstance) => {
57
await server.register(swagger, {
68
openapi: {
79
info: {
810
title: "thirdweb Engine",
9-
description: "The open-source server for scalable web3 apps.",
10-
version: "0.0.2",
11+
description:
12+
"Engine is an open-source, backend server that reads, writes, and deploys contracts at production scale.",
13+
version: "1.0.0",
1114
license: {
1215
name: "Apache 2.0",
1316
url: "http://www.apache.org/licenses/LICENSE-2.0.html",
@@ -31,8 +34,9 @@ export const withOpenApi = async (server: FastifyInstance) => {
3134
},
3235
});
3336

34-
// Exports the /json endpoint without the Swagger UI.
35-
server.get("/json", {}, async (_, res) => {
36-
res.send(server.swagger());
37-
});
37+
for (const path of OPENAPI_ROUTES) {
38+
server.get(path, {}, async (_, res) => {
39+
res.send(server.swagger());
40+
});
41+
}
3842
};

src/server/middleware/rateLimit.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import { FastifyInstance } from "fastify";
1+
import type { FastifyInstance } from "fastify";
22
import { StatusCodes } from "http-status-codes";
33
import { env } from "../../utils/env";
44
import { redis } from "../../utils/redis/redis";
55
import { createCustomError } from "./error";
6+
import { OPENAPI_ROUTES } from "./open-api";
7+
8+
const SKIP_RATELIMIT_PATHS = ["/", ...OPENAPI_ROUTES];
69

710
export const withRateLimit = async (server: FastifyInstance) => {
811
server.addHook("onRequest", async (request, reply) => {
9-
if (request.url === "/" || request.url === "/json") {
12+
if (SKIP_RATELIMIT_PATHS.includes(request.url)) {
1013
return;
1114
}
1215

src/server/routes/home.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Static, Type } from "@sinclair/typebox";
2-
import { FastifyInstance } from "fastify";
1+
import { Type, type Static } from "@sinclair/typebox";
2+
import type { FastifyInstance } from "fastify";
33
import { StatusCodes } from "http-status-codes";
44

55
const responseBodySchema = Type.Object({
@@ -22,7 +22,7 @@ export async function home(fastify: FastifyInstance) {
2222
[StatusCodes.OK]: responseBodySchema,
2323
},
2424
},
25-
handler: async (req, res) => {
25+
handler: async (_, res) => {
2626
return res.status(StatusCodes.OK).send({
2727
message:
2828
"Engine is set up successfully. Manage your Engine from https://thirdweb.com/dashboard/engine.",

src/utils/usage.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { UsageEvent } from "@thirdweb-dev/service-utils/cf-worker";
33
import { FastifyInstance } from "fastify";
44
import { Address, Hex } from "thirdweb";
55
import { ADMIN_QUEUES_BASEPATH } from "../server/middleware/adminRoutes";
6+
import { OPENAPI_ROUTES } from "../server/middleware/open-api";
67
import { contractParamSchema } from "../server/schemas/sharedApiSchemas";
78
import { walletWithAddressParamSchema } from "../server/schemas/wallet";
89
import { getChainIdFromChain } from "../server/utils/chain";
@@ -49,8 +50,8 @@ const SKIP_USAGE_PATHS = new Set([
4950
"/",
5051
"/favicon.ico",
5152
"/system/health",
52-
"/json",
5353
"/static",
54+
...OPENAPI_ROUTES,
5455
]);
5556

5657
export const withServerUsageReporting = (server: FastifyInstance) => {

0 commit comments

Comments
 (0)