Skip to content

Commit

Permalink
refactor: shared types (miodec) (#4978)
Browse files Browse the repository at this point in the history
* rework shared types

* revert one small change
  • Loading branch information
Miodec authored Jan 29, 2024
1 parent ee629db commit 0751375
Show file tree
Hide file tree
Showing 16 changed files with 146 additions and 153 deletions.
2 changes: 1 addition & 1 deletion backend/src/api/controllers/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ interface XpResult {

async function calculateXp(
result,
xpConfiguration: MonkeyTypes.Configuration["users"]["xp"],
xpConfiguration: SharedTypes.Configuration["users"]["xp"],
uid: string,
currentTotalXp: number,
streak: number
Expand Down
4 changes: 2 additions & 2 deletions backend/src/constants/base-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* To add a new configuration. Simply add it to this object.
* When changing this template, please follow the principle of "Secure by default" (https://en.wikipedia.org/wiki/Secure_by_default).
*/
export const BASE_CONFIGURATION: MonkeyTypes.Configuration = {
export const BASE_CONFIGURATION: SharedTypes.Configuration = {
maintenance: false,
results: {
savingEnabled: false,
Expand Down Expand Up @@ -141,7 +141,7 @@ type Schema<T> = {
: never;
};

export const CONFIGURATION_FORM_SCHEMA: ObjectSchema<MonkeyTypes.Configuration> =
export const CONFIGURATION_FORM_SCHEMA: ObjectSchema<SharedTypes.Configuration> =
{
type: "object",
label: "Server Configuration",
Expand Down
4 changes: 2 additions & 2 deletions backend/src/dal/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ interface AddToInboxBulkEntry {

export async function addToInboxBulk(
entries: AddToInboxBulkEntry[],
inboxConfig: MonkeyTypes.Configuration["users"]["inbox"]
inboxConfig: SharedTypes.Configuration["users"]["inbox"]
): Promise<void> {
const { enabled, maxMail } = inboxConfig;

Expand Down Expand Up @@ -875,7 +875,7 @@ export async function addToInboxBulk(
export async function addToInbox(
uid: string,
mail: MonkeyTypes.MonkeyMail[],
inboxConfig: MonkeyTypes.Configuration["users"]["inbox"]
inboxConfig: SharedTypes.Configuration["users"]["inbox"]
): Promise<void> {
const { enabled, maxMail } = inboxConfig;

Expand Down
14 changes: 7 additions & 7 deletions backend/src/init/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { BASE_CONFIGURATION } from "../constants/base-configuration";
const CONFIG_UPDATE_INTERVAL = 10 * 60 * 1000; // 10 Minutes

function mergeConfigurations(
baseConfiguration: MonkeyTypes.Configuration,
liveConfiguration: Partial<MonkeyTypes.Configuration>
baseConfiguration: SharedTypes.Configuration,
liveConfiguration: Partial<SharedTypes.Configuration>
): void {
if (
!_.isPlainObject(baseConfiguration) ||
Expand Down Expand Up @@ -45,7 +45,7 @@ let serverConfigurationUpdated = false;

export async function getCachedConfiguration(
attemptCacheUpdate = false
): Promise<MonkeyTypes.Configuration> {
): Promise<SharedTypes.Configuration> {
if (
attemptCacheUpdate &&
lastFetchTime < Date.now() - CONFIG_UPDATE_INTERVAL
Expand All @@ -57,7 +57,7 @@ export async function getCachedConfiguration(
return configuration;
}

export async function getLiveConfiguration(): Promise<MonkeyTypes.Configuration> {
export async function getLiveConfiguration(): Promise<SharedTypes.Configuration> {
lastFetchTime = Date.now();

const configurationCollection = db.collection("configuration");
Expand All @@ -71,7 +71,7 @@ export async function getLiveConfiguration(): Promise<MonkeyTypes.Configuration>
const liveConfigurationWithoutId = _.omit(
liveConfiguration,
"_id"
) as MonkeyTypes.Configuration;
) as SharedTypes.Configuration;
mergeConfigurations(baseConfiguration, liveConfigurationWithoutId);

pushConfiguration(baseConfiguration);
Expand All @@ -93,7 +93,7 @@ export async function getLiveConfiguration(): Promise<MonkeyTypes.Configuration>
}

async function pushConfiguration(
configuration: MonkeyTypes.Configuration
configuration: SharedTypes.Configuration
): Promise<void> {
if (serverConfigurationUpdated) {
return;
Expand All @@ -111,7 +111,7 @@ async function pushConfiguration(
}

export async function patchConfiguration(
configurationUpdates: Partial<MonkeyTypes.Configuration>
configurationUpdates: Partial<SharedTypes.Configuration>
): Promise<boolean> {
try {
const currentConfiguration = _.cloneDeep(configuration);
Expand Down
2 changes: 1 addition & 1 deletion backend/src/middlewares/api-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const emptyMiddleware = (
* the criteria.
*/
function validateConfiguration(
options: ValidationOptions<MonkeyTypes.Configuration>
options: ValidationOptions<SharedTypes.Configuration>
): RequestHandler {
const {
criteria,
Expand Down
4 changes: 2 additions & 2 deletions backend/src/middlewares/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function authenticateRequest(authOptions = DEFAULT_OPTIONS): Handler {

async function authenticateWithAuthHeader(
authHeader: string,
configuration: MonkeyTypes.Configuration,
configuration: SharedTypes.Configuration,
options: RequestAuthenticationOptions
): Promise<MonkeyTypes.DecodedToken> {
if (authHeader === undefined || authHeader === "") {
Expand Down Expand Up @@ -215,7 +215,7 @@ async function authenticateWithBearerToken(

async function authenticateWithApeKey(
key: string,
configuration: MonkeyTypes.Configuration,
configuration: SharedTypes.Configuration,
options: RequestAuthenticationOptions
): Promise<MonkeyTypes.DecodedToken> {
if (!configuration.apeKeys.acceptKeys) {
Expand Down
4 changes: 2 additions & 2 deletions backend/src/queues/later-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface LaterTask<T extends LaterTaskType> {
export type LaterTaskContexts = {
"daily-leaderboard-results": {
yesterdayTimestamp: number;
modeRule: MonkeyTypes.ValidModeRule;
modeRule: SharedTypes.ValidModeRule;
};
"weekly-xp-leaderboard-results": {
lastWeekTimestamp: number;
Expand Down Expand Up @@ -82,7 +82,7 @@ class LaterQueue extends MonkeyQueue<LaterTask<LaterTaskType>> {
async scheduleForTomorrow(
taskName: LaterTaskType,
taskId: string,
modeRule: MonkeyTypes.ValidModeRule
modeRule: SharedTypes.ValidModeRule
): Promise<void> {
const currentDayTimestamp = getCurrentDayTimestamp();
const jobId = `${taskName}:${currentDayTimestamp}:${taskId}`;
Expand Down
8 changes: 4 additions & 4 deletions backend/src/services/weekly-xp-leaderboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class WeeklyXpLeaderboard {
}

public async addResult(
weeklyXpLeaderboardConfig: MonkeyTypes.Configuration["leaderboards"]["weeklyXp"],
weeklyXpLeaderboardConfig: SharedTypes.Configuration["leaderboards"]["weeklyXp"],
opts: AddResultOpts
): Promise<number> {
const { entry, xpGained, timeTypedSeconds } = opts;
Expand Down Expand Up @@ -116,7 +116,7 @@ export class WeeklyXpLeaderboard {
public async getResults(
minRank: number,
maxRank: number,
weeklyXpLeaderboardConfig: MonkeyTypes.Configuration["leaderboards"]["weeklyXp"]
weeklyXpLeaderboardConfig: SharedTypes.Configuration["leaderboards"]["weeklyXp"]
): Promise<WeeklyXpLeaderboardEntry[]> {
const connection = RedisClient.getConnection();
if (!connection || !weeklyXpLeaderboardConfig.enabled) {
Expand Down Expand Up @@ -161,7 +161,7 @@ export class WeeklyXpLeaderboard {

public async getRank(
uid: string,
weeklyXpLeaderboardConfig: MonkeyTypes.Configuration["leaderboards"]["weeklyXp"]
weeklyXpLeaderboardConfig: SharedTypes.Configuration["leaderboards"]["weeklyXp"]
): Promise<WeeklyXpLeaderboardEntry | null> {
const connection = RedisClient.getConnection();
if (!connection || !weeklyXpLeaderboardConfig.enabled) {
Expand Down Expand Up @@ -196,7 +196,7 @@ export class WeeklyXpLeaderboard {
}

export function get(
weeklyXpLeaderboardConfig: MonkeyTypes.Configuration["leaderboards"]["weeklyXp"],
weeklyXpLeaderboardConfig: SharedTypes.Configuration["leaderboards"]["weeklyXp"],
customTimestamp?: number
): WeeklyXpLeaderboard | null {
const { enabled } = weeklyXpLeaderboardConfig;
Expand Down
108 changes: 0 additions & 108 deletions backend/src/types/shared.ts

This file was deleted.

7 changes: 1 addition & 6 deletions backend/src/types/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
type Configuration = import("../types/shared").Configuration;

type ObjectId = import("mongodb").ObjectId;

type ExpressRequest = import("express").Request;

declare namespace MonkeyTypes {
type Configuration = import("./shared").Configuration;
type ValidModeRule = import("./shared").ValidModeRule;
type RewardBracket = import("./shared").RewardBracket;
interface DecodedToken {
type: "Bearer" | "ApeKey" | "None";
uid: string;
email: string;
}

interface Context {
configuration: Configuration;
configuration: SharedTypes.Configuration;
decodedToken: DecodedToken;
}

Expand Down
18 changes: 9 additions & 9 deletions backend/src/utils/daily-leaderboards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ export class DailyLeaderboard {
private leaderboardScoresKeyName: string;
private leaderboardModeKey: string;
private customTime: number;
private modeRule: MonkeyTypes.ValidModeRule;
private modeRule: SharedTypes.ValidModeRule;

constructor(modeRule: MonkeyTypes.ValidModeRule, customTime = -1) {
constructor(modeRule: SharedTypes.ValidModeRule, customTime = -1) {
const { language, mode, mode2 } = modeRule;

this.leaderboardModeKey = `${language}:${mode}:${mode2}`;
Expand Down Expand Up @@ -67,7 +67,7 @@ export class DailyLeaderboard {

public async addResult(
entry: DailyLeaderboardEntry,
dailyLeaderboardsConfig: MonkeyTypes.Configuration["dailyLeaderboards"]
dailyLeaderboardsConfig: SharedTypes.Configuration["dailyLeaderboards"]
): Promise<number> {
const connection = RedisClient.getConnection();
if (!connection || !dailyLeaderboardsConfig.enabled) {
Expand Down Expand Up @@ -123,7 +123,7 @@ export class DailyLeaderboard {
public async getResults(
minRank: number,
maxRank: number,
dailyLeaderboardsConfig: MonkeyTypes.Configuration["dailyLeaderboards"]
dailyLeaderboardsConfig: SharedTypes.Configuration["dailyLeaderboards"]
): Promise<LbEntryWithRank[]> {
const connection = RedisClient.getConnection();
if (!connection || !dailyLeaderboardsConfig.enabled) {
Expand Down Expand Up @@ -161,7 +161,7 @@ export class DailyLeaderboard {

public async getRank(
uid: string,
dailyLeaderboardsConfig: MonkeyTypes.Configuration["dailyLeaderboards"]
dailyLeaderboardsConfig: SharedTypes.Configuration["dailyLeaderboards"]
): Promise<GetRankResponse | null> {
const connection = RedisClient.getConnection();
if (!connection || !dailyLeaderboardsConfig.enabled) {
Expand Down Expand Up @@ -204,7 +204,7 @@ export class DailyLeaderboard {

export async function purgeUserFromDailyLeaderboards(
uid: string,
configuration: MonkeyTypes.Configuration["dailyLeaderboards"]
configuration: SharedTypes.Configuration["dailyLeaderboards"]
): Promise<void> {
const connection = RedisClient.getConnection();
if (!connection || !configuration.enabled) {
Expand All @@ -216,8 +216,8 @@ export async function purgeUserFromDailyLeaderboards(
}

function isValidModeRule(
modeRule: MonkeyTypes.ValidModeRule,
modeRules: MonkeyTypes.ValidModeRule[]
modeRule: SharedTypes.ValidModeRule,
modeRules: SharedTypes.ValidModeRule[]
): boolean {
const { language, mode, mode2 } = modeRule;

Expand All @@ -233,7 +233,7 @@ export function getDailyLeaderboard(
language: string,
mode: string,
mode2: string,
dailyLeaderboardsConfig: MonkeyTypes.Configuration["dailyLeaderboards"],
dailyLeaderboardsConfig: SharedTypes.Configuration["dailyLeaderboards"],
customTimestamp = -1
): DailyLeaderboard | null {
const { validModeRules, enabled } = dailyLeaderboardsConfig;
Expand Down
2 changes: 1 addition & 1 deletion backend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"ts-node": {
"files": true
},
"files": ["./src/types/types.d.ts"],
"files": ["./src/types/types.d.ts", "../shared-types/types.d.ts"],
"include": ["./src/**/*"],
"exclude": [
"node_modules",
Expand Down
Loading

0 comments on commit 0751375

Please sign in to comment.