Skip to content

Commit

Permalink
Load env with a validation schema to safeguard and type
Browse files Browse the repository at this point in the history
  • Loading branch information
ygrishajev committed Apr 1, 2024
1 parent 15fd8ba commit bea8819
Show file tree
Hide file tree
Showing 14 changed files with 280 additions and 102 deletions.
35 changes: 18 additions & 17 deletions api/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
const MAP_ALIASES = {
'^@src(.*)$': "<rootDir>/src$1",
'^@shared/(.*)$': '<rootDir>/../shared/$1',
"^@src(.*)$": "<rootDir>/src/$1",
"^@test/(.*)$": "<rootDir>/test/$1",
"^@shared/(.*)$": "<rootDir>/../shared/$1"
};
const MAP_TO_SAME_SEQUELIZE_PACKAGE = {
'^sequelize(.*)$': '<rootDir>/node_modules/sequelize$1',
}
"^sequelize(.*)$": "<rootDir>/node_modules/sequelize$1"
};

const common = {
transform: {
'^.+\\.(t|j)s$': ['ts-jest', { tsconfig: './tsconfig.json' }],
"^.+\\.(t|j)s$": ["ts-jest", { tsconfig: "./tsconfig.json" }]
},
rootDir: '.',
rootDir: ".",
moduleNameMapper: {
...MAP_ALIASES,
...MAP_TO_SAME_SEQUELIZE_PACKAGE,
...MAP_TO_SAME_SEQUELIZE_PACKAGE
},
setupFiles: ['./test/setup.ts'],
setupFiles: ["./test/setup.ts"]
};

module.exports = {
collectCoverageFrom: ['./src/**/*.{js,ts}'],
collectCoverageFrom: ["./src/**/*.{js,ts}"],
projects: [
{
displayName: 'unit',
displayName: "unit",
...common,
testMatch: ['<rootDir>/src/**/*.spec.ts'],
setupFilesAfterEnv: ['./test/setup-unit-tests.ts'],
testMatch: ["<rootDir>/src/**/*.spec.ts"],
setupFilesAfterEnv: ["./test/setup-unit-tests.ts"]
},
{
displayName: 'functional',
displayName: "functional",
...common,
testMatch: ['<rootDir>/test/functional/**/*.spec.ts'],
setupFilesAfterEnv: ['./test/setup-functional-tests.ts'],
},
],
testMatch: ["<rootDir>/test/functional/**/*.spec.ts"],
setupFilesAfterEnv: ["./test/setup-functional-tests.ts"]
}
]
};
130 changes: 130 additions & 0 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"hono": "3.12.0",
"human-interval": "^2.0.1",
"js-sha256": "^0.9.0",
"lodash": "^4.17.21",
"markdown-to-txt": "^2.0.1",
"memory-cache": "^0.2.0",
"node-fetch": "^2.6.1",
Expand All @@ -59,7 +60,9 @@
"zod": "^3.22.4"
},
"devDependencies": {
"@faker-js/faker": "^8.4.1",
"@types/jest": "^29.5.12",
"@types/lodash": "^4.17.0",
"@types/memory-cache": "^0.2.2",
"@types/node": "^16.6.0",
"@types/node-fetch": "^2.6.2",
Expand All @@ -71,6 +74,7 @@
"alias-hq": "^5.1.6",
"eslint": "^8.56.0",
"jest": "^29.7.0",
"nock": "^13.5.4",
"nodemon": "^2.0.7",
"nodemon-webpack-plugin": "^4.5.2",
"prettier": "^3.2.5",
Expand Down
17 changes: 5 additions & 12 deletions api/src/caching/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,16 @@ interface MemoizeOptions {
export const Memoize = (options?: MemoizeOptions) => (target: object, propertyName: string, descriptor: PropertyDescriptor) => {
const originalMethod = descriptor.value;

const cacheKeySymbol = options?.key || `${target.constructor.name}#${propertyName}`;
const cacheKey = options?.key || `${target.constructor.name}#${propertyName}`;

descriptor.value = async function memoizedFunction(...args: unknown[]) {
return await cacheResponse(
options?.ttlInSeconds || 60 * 2,
cacheKeySymbol,
originalMethod.bind(this, ...args),
options?.keepData
);
}
}

return await cacheResponse(options?.ttlInSeconds || 60 * 2, cacheKey, originalMethod.bind(this, ...args), options?.keepData);
};
};

export async function cacheResponse<T>(seconds: number, key: string, refreshRequest: () => Promise<T>, keepData?: boolean): Promise<T> {
const duration = seconds * 1000;
const cachedObject = cacheEngine.getFromCache(key) as CachedObject<T> | undefined;

console.log(`Cache key: ${key}`);

// If first time or expired, must refresh data if not already refreshing
Expand Down Expand Up @@ -83,5 +76,5 @@ export const cacheKeys = {
getMainnetVersion: "getMainnetVersion",
getTestnetVersion: "getTestnetVersion",
getSandboxVersion: "getSandboxVersion",
getGpuModels: "getGpuModels",
getGpuModels: "getGpuModels"
};
2 changes: 1 addition & 1 deletion api/src/routes/v1/nodes/mainnet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
import { nodeClient } from '@src/routes/v1/nodes/node-client';
import { nodeClient } from '@src/routes/v1/nodes/nodeClient';

const route = createRoute({
method: "get",
Expand Down
Loading

0 comments on commit bea8819

Please sign in to comment.