Skip to content

Commit

Permalink
fix: only refetch version every 60min
Browse files Browse the repository at this point in the history
  • Loading branch information
casperiv0 committed Sep 20, 2023
1 parent bba487e commit 2d8458b
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion apps/api/src/middlewares/auth/is-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import { CadFeatureOptions } from "@snailycad/types";

@Middleware()
export class IsAuth implements MiddlewareMethods {
private lastVersionCheck: number | null = null;
private currentVersion: Awaited<ReturnType<typeof getCADVersion>> | null = null;
private VERSION_CHECK_INTERVAL = 1000 * 60 * 60;

async use(@Req() req: Req, @Res() res: Res, @Context() ctx: Context) {
const globalCADApiToken = req.headers[API_TOKEN_HEADER];

Expand Down Expand Up @@ -61,7 +65,14 @@ export class IsAuth implements MiddlewareMethods {
});
}

ctx.set("cad", { ...setCADFeatures(cad), version: await getCADVersion() });
const now = Date.now();

if (this.lastVersionCheck && now - this.lastVersionCheck > this.VERSION_CHECK_INTERVAL) {
this.lastVersionCheck = now;
this.currentVersion = await getCADVersion();
}

ctx.set("cad", { ...setCADFeatures(cad), version: this.currentVersion });
}

// localized error messages
Expand Down

0 comments on commit 2d8458b

Please sign in to comment.