Skip to content

Commit

Permalink
chore: Alias /openapi.json to /json (#716)
Browse files Browse the repository at this point in the history
  • Loading branch information
arcoraven authored Oct 9, 2024
1 parent 69b9eb8 commit 3982b7e
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/server/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { logger } from "../../utils/logger";
import { sendWebhookRequest } from "../../utils/webhook";
import { Permission } from "../schemas/auth";
import { ADMIN_QUEUES_BASEPATH } from "./adminRoutes";
import { OPENAPI_ROUTES } from "./open-api";

export type TAuthData = never;
export type TAuthSession = { permissions: string };
Expand Down Expand Up @@ -211,7 +212,7 @@ const handlePublicEndpoints = (req: FastifyRequest): AuthResponse => {
req.url === "/favicon.ico" ||
req.url === "/" ||
req.url === "/system/health" ||
req.url === "/json" ||
OPENAPI_ROUTES.includes(req.url) ||
req.url.startsWith("/auth/user") ||
req.url.startsWith("/transaction/status")
) {
Expand Down
3 changes: 2 additions & 1 deletion src/server/middleware/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import type { FastifyInstance } from "fastify";
import { stringify } from "thirdweb/utils";
import { logger } from "../../utils/logger";
import { ADMIN_QUEUES_BASEPATH } from "./adminRoutes";
import { OPENAPI_ROUTES } from "./open-api";

const SKIP_LOG_PATHS = new Set([
"",
"/",
"/favicon.ico",
"/system/health",
"/json",
"/static",
...OPENAPI_ROUTES,
// Skip these routes case of importing sensitive details.
"/backend-wallet/import",
"/configuration/wallets",
Expand Down
16 changes: 10 additions & 6 deletions src/server/middleware/open-api.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import swagger from "@fastify/swagger";
import type { FastifyInstance } from "fastify";

export const OPENAPI_ROUTES = ["/json", "/openapi.json"];

export const withOpenApi = async (server: FastifyInstance) => {
await server.register(swagger, {
openapi: {
info: {
title: "thirdweb Engine",
description: "The open-source server for scalable web3 apps.",
version: "0.0.2",
description:
"Engine is an open-source, backend server that reads, writes, and deploys contracts at production scale.",
version: "1.0.0",
license: {
name: "Apache 2.0",
url: "http://www.apache.org/licenses/LICENSE-2.0.html",
Expand All @@ -31,8 +34,9 @@ export const withOpenApi = async (server: FastifyInstance) => {
},
});

// Exports the /json endpoint without the Swagger UI.
server.get("/json", {}, async (_, res) => {
res.send(server.swagger());
});
for (const path of OPENAPI_ROUTES) {
server.get(path, {}, async (_, res) => {
res.send(server.swagger());
});
}
};
7 changes: 5 additions & 2 deletions src/server/middleware/rateLimit.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { FastifyInstance } from "fastify";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { env } from "../../utils/env";
import { redis } from "../../utils/redis/redis";
import { createCustomError } from "./error";
import { OPENAPI_ROUTES } from "./open-api";

const SKIP_RATELIMIT_PATHS = ["/", ...OPENAPI_ROUTES];

export const withRateLimit = async (server: FastifyInstance) => {
server.addHook("onRequest", async (request, reply) => {
if (request.url === "/" || request.url === "/json") {
if (SKIP_RATELIMIT_PATHS.includes(request.url)) {
return;
}

Expand Down
6 changes: 3 additions & 3 deletions src/server/routes/home.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Static, Type } from "@sinclair/typebox";
import { FastifyInstance } from "fastify";
import { Type, type Static } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";

const responseBodySchema = Type.Object({
Expand All @@ -22,7 +22,7 @@ export async function home(fastify: FastifyInstance) {
[StatusCodes.OK]: responseBodySchema,
},
},
handler: async (req, res) => {
handler: async (_, res) => {
return res.status(StatusCodes.OK).send({
message:
"Engine is set up successfully. Manage your Engine from https://thirdweb.com/dashboard/engine.",
Expand Down
3 changes: 2 additions & 1 deletion src/utils/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { UsageEvent } from "@thirdweb-dev/service-utils/cf-worker";
import { FastifyInstance } from "fastify";
import { Address, Hex } from "thirdweb";
import { ADMIN_QUEUES_BASEPATH } from "../server/middleware/adminRoutes";
import { OPENAPI_ROUTES } from "../server/middleware/open-api";
import { contractParamSchema } from "../server/schemas/sharedApiSchemas";
import { walletWithAddressParamSchema } from "../server/schemas/wallet";
import { getChainIdFromChain } from "../server/utils/chain";
Expand Down Expand Up @@ -49,8 +50,8 @@ const SKIP_USAGE_PATHS = new Set([
"/",
"/favicon.ico",
"/system/health",
"/json",
"/static",
...OPENAPI_ROUTES,
]);

export const withServerUsageReporting = (server: FastifyInstance) => {
Expand Down

0 comments on commit 3982b7e

Please sign in to comment.