Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ import {
badAuthRateLimiterHandler,
rootRateLimiter,
} from "./middlewares/rate-limit";
import { compatibilityCheckMiddleware } from "./middlewares/compatibilityCheck";
import { COMPATIBILITY_CHECK_HEADER } from "@monkeytype/contracts";

function buildApp(): express.Application {
const app = express();

app.use(urlencoded({ extended: true }));
app.use(json());
app.use(cors());
app.use(cors({ exposedHeaders: [COMPATIBILITY_CHECK_HEADER] }));
app.use(helmet());

app.set("trust proxy", 1);

app.use(compatibilityCheckMiddleware);
app.use(contextMiddleware);

app.use(badAuthRateLimiterHandler);
Expand Down
20 changes: 20 additions & 0 deletions backend/src/middlewares/compatibilityCheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {
COMPATIBILITY_CHECK,
COMPATIBILITY_CHECK_HEADER,
} from "@monkeytype/contracts";
import type { Response, NextFunction, Request } from "express";

/**
* Add the COMPATIBILITY_CHECK_HEADER to each response
* @param _req
* @param res
* @param next
*/
export async function compatibilityCheckMiddleware(
_req: Request,
res: Response,
next: NextFunction
): Promise<void> {
res.setHeader(COMPATIBILITY_CHECK_HEADER, COMPATIBILITY_CHECK);
next();
}
30 changes: 29 additions & 1 deletion frontend/src/ts/ape/adapters/ts-rest-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ import {
import { getIdToken } from "firebase/auth";
import { envConfig } from "../../constants/env-config";
import { getAuthenticatedUser, isAuthenticated } from "../../firebase";
import {
COMPATIBILITY_CHECK,
COMPATIBILITY_CHECK_HEADER,
} from "@monkeytype/contracts";
import * as Notifications from "../../elements/notifications";

let bannerActive = false;

function timeoutSignal(ms: number): AbortSignal {
const ctrl = new AbortController();
Expand Down Expand Up @@ -35,14 +42,35 @@ function buildApi(timeout: number): (args: ApiFetcherArgs) => Promise<{
: AbortSignal.timeout(timeout),
};
const response = await tsRestFetchApi(request);

if (response.status >= 400) {
console.error(`${request.method} ${request.path} failed`, {
status: response.status,
...(response.body as object),
});
}

const compatibilityCheck = response.headers.get(
COMPATIBILITY_CHECK_HEADER
);
if (compatibilityCheck !== null && !bannerActive) {
const backendCheck = parseInt(compatibilityCheck);
if (backendCheck !== COMPATIBILITY_CHECK) {
const message =
backendCheck > COMPATIBILITY_CHECK
? `Looks like the client and server versions are mismatched (backend is newer). Please <a onClick="location.reload(true)">refresh</a> the page.`
: `Looks like our monkeys didn't deploy the new server version correctly. If this message persists contact support.`;
Notifications.addBanner(
message,
1,
undefined,
false,
() => (bannerActive = false),
true
);
bannerActive = true;
}
}

return response;
} catch (e: Error | unknown) {
let message = "Unknown error";
Expand Down
7 changes: 7 additions & 0 deletions packages/contracts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,10 @@ export const contract = c.router({
quotes: quotesContract,
webhooks: webhooksContract,
});

/**
* Whenever there is a breaking change with old frontend clients increase this number.
* This will inform the frontend to refresh.
*/
export const COMPATIBILITY_CHECK = 0;
export const COMPATIBILITY_CHECK_HEADER = "X-Compatibility-Check";