Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/ts #225

Closed
wants to merge 6 commits into from
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
70 changes: 70 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"env": {
"node": true,
"es2021": true
},
"extends": ["eslint:recommended"],
"plugins": ["simple-import-sort"],
"ignorePatterns": [
"node_modules",
"dist",
"build",
"public"
],
"rules": {
"@typescript-eslint/no-unused-vars": ["error", { "ignoreRestSiblings": true }],
"simple-import-sort/imports": [
"error",
{
"groups": [
["^react", "^(?!@src)@?\\w"],
[
"^@src",
"^\\.\\.(?!/?$)",
"^\\.\\./?$",
"^\\./(?=.*/)(?!/?$)",
"^\\.(?!/?$)",
"^\\./?$"
],
["^\\u0000"]
]
}
]
},
"settings": {
"next": {
"rootDir": "apps/*-web"
}
},
"overrides": [
{
"files": ["apps/*-web/**/*.{ts,tsx}"],
"extends": "next/core-web-vitals",
"rules": {
"react/no-unescaped-entities": "warn",
"react-hooks/exhaustive-deps": "warn",
"import/no-anonymous-default-export": "warn",
"react/display-name": "warn"
}
},
{
"files": ["**/*.{ts,tsx}"],
"extends": ["plugin:@typescript-eslint/recommended"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": ["@typescript-eslint"],
"rules": {
"@typescript-eslint/no-explicit-any": ["warn"]
}
},
{
"files": ["**/sentry.*.config.js"],
"parserOptions": {
"sourceType": "module"
}
}
]
}
4 changes: 2 additions & 2 deletions .github/workflows/validate-n-build-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ jobs:

- name: Install dependencies
if: steps.filter.outputs.api == 'true'
run: npm ci --workspace=apps/api
run: npm ci

- name: Run static code analysis
if: steps.filter.outputs.api == 'true'
run: npm run lint --workspace=apps/api
run: npm run lint:api

- name: Run unit tests
if: steps.filter.outputs.api == 'true'
Expand Down
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
build
dist
2 changes: 1 addition & 1 deletion apps/stats-web/.prettierrc → .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"semi": true,
"quoteProps": "as-needed",
"jsxSingleQuote": false,
"jsxBracketSameLine": false,
"bracketSameLine": false,
"trailingComma": "none",
"requirePragma": false,
"insertPragma": false,
Expand Down
2 changes: 2 additions & 0 deletions Dockerfile.api
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ WORKDIR /app

COPY /apps/api /app/apps/api
COPY /packages/shared /app/packages/shared
COPY tsconfig.base-node.json /app
COPY tsconfig.base.json /app
COPY package.json /app
COPY package-lock.json /app

Expand Down
16 changes: 0 additions & 16 deletions apps/api/.prettierrc

This file was deleted.

8 changes: 2 additions & 6 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"scripts": {
"start": "webpack --config webpack.dev.js --watch",
"build": "webpack --config webpack.prod.js",
"lint": "eslint .",
"lint": "eslint --config=../../.eslintrc.json .",
"test": "jest --selectProjects unit functional",
"test:watch": "jest --selectProjects unit functional --watch",
"test:cov": "jest --selectProjects unit functional --coverage",
Expand Down Expand Up @@ -72,19 +72,15 @@
"@types/pg": "^8.6.5",
"@types/semver": "^7.5.2",
"@types/uuid": "^8.3.1",
"@typescript-eslint/eslint-plugin": "^6.19.0",
"@typescript-eslint/parser": "^6.19.0",
"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",
"supertest": "^6.1.5",
"ts-jest": "^29.1.2",
"ts-loader": "^9.2.5",
"typescript": "^5.3.3",
"typescript": "^5.4.5",
"webpack": "^5.33.2",
"webpack-cli": "4.7.2",
"webpack-node-externals": "^3.0.0"
Expand Down
33 changes: 17 additions & 16 deletions apps/api/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import packageJson from "../package.json";
import { isProd } from "./utils/constants";
import * as Sentry from "@sentry/node";
import { Scheduler } from "./scheduler";
import { apiRouter } from "./routers/apiRouter";
import { userRouter } from "./routers/userRouter";
import { web3IndexRouter } from "./routers/web3indexRouter";
import { bytesToHumanReadableSize } from "./utils/files";
import { env } from "./utils/env";
import { chainDb, syncUserSchema, userDb } from "./db/dbConnection";
import { dashboardRouter } from "./routers/dashboardRouter";
import { Hono } from "hono";
import { cors } from "hono/cors";
import { serve } from "@hono/node-server";
import { legacyRouter } from "./routers/legacyRouter";
// TODO: find out how to properly import this
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
import { sentry } from "@hono/sentry";
import * as Sentry from "@sentry/node";
import { Hono } from "hono";
import { cors } from "hono/cors";

import packageJson from "../package.json";
import { chainDb, syncUserSchema, userDb } from "./db/dbConnection";
import { apiRouter } from "./routers/apiRouter";
import { dashboardRouter } from "./routers/dashboardRouter";
import { internalRouter } from "./routers/internalRouter";
import { legacyRouter } from "./routers/legacyRouter";
import { userRouter } from "./routers/userRouter";
import { web3IndexRouter } from "./routers/web3indexRouter";
import { isProd } from "./utils/constants";
import { env } from "./utils/env";
import { bytesToHumanReadableSize } from "./utils/files";
import { Scheduler } from "./scheduler";

const appHono = new Hono();
appHono.use(
Expand Down Expand Up @@ -59,7 +60,7 @@ appHono.use(
sentry({
dsn: env.SentryDSN,
environment: env.NODE_ENV,
beforeSend: (event) => {
beforeSend: event => {
event.server_name = env.SentryServerName;
return event;
},
Expand All @@ -76,7 +77,7 @@ appHono.route("/web3-index", web3IndexRouter);
appHono.route("/dashboard", dashboardRouter);
appHono.route("/internal", internalRouter);

appHono.get("/status", (c) => {
appHono.get("/status", c => {
const version = packageJson.version;
const tasksStatus = scheduler.getTasksStatus();
const memoryInBytes = process.memoryUsage();
Expand Down
7 changes: 4 additions & 3 deletions apps/api/src/caching/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as Sentry from "@sentry/node";
import { differenceInSeconds } from "date-fns";

import MemoryCacheEngine from "./memoryCacheEngine";
import * as Sentry from "@sentry/node";

export const cacheEngine = new MemoryCacheEngine();
const pendingRequests: { [key: string]: Promise<unknown> } = {};
Expand Down Expand Up @@ -36,11 +37,11 @@ export async function cacheResponse<T>(seconds: number, key: string, refreshRequ
if ((!cachedObject || cacheExpired) && !(key in pendingRequests)) {
// console.log(`Making request: ${key}`);
pendingRequests[key] = refreshRequest()
.then((data) => {
.then(data => {
cacheEngine.storeInCache(key, { date: new Date(), data: data }, keepData ? undefined : duration);
return data;
})
.catch((err) => {
.catch(err => {
// console.log(`Error making cache request ${err}`);
Sentry.captureException(err);
})
Expand Down
13 changes: 7 additions & 6 deletions apps/api/src/db/dbConnection.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { chainDefinitions } from "@akashnetwork/cloudmos-shared/chainDefinitions";
import { chainModels, getChainModels, userModels } from "@akashnetwork/cloudmos-shared/dbSchemas";
import { Template, TemplateFavorite, UserAddressName, UserSetting } from "@akashnetwork/cloudmos-shared/dbSchemas/user";
import pg from "pg";
import { env } from "@src/utils/env";
import { Transaction as DbTransaction } from "sequelize";
import { Sequelize } from "sequelize-typescript";
import { chainModels, getChainModels, userModels } from "@akashnetwork/cloudmos-shared/dbSchemas";
import { Template, TemplateFavorite, UserAddressName, UserSetting } from "@akashnetwork/cloudmos-shared/dbSchemas/user";
import { chainDefinitions } from "@akashnetwork/cloudmos-shared/chainDefinitions";

import { env } from "@src/utils/env";

function isValidNetwork(network: string): network is keyof typeof csMap {
return network in csMap;
Expand Down Expand Up @@ -37,7 +38,7 @@ export const chainDb = new Sequelize(csMap[env.Network], {
});

export const chainDbs: { [key: string]: Sequelize } = Object.keys(chainDefinitions)
.filter((x) => chainDefinitions[x].connectionString)
.filter(x => chainDefinitions[x].connectionString)
.reduce(
(obj, chain) => ({
...obj,
Expand Down Expand Up @@ -74,4 +75,4 @@ export async function syncUserSchema() {
await TemplateFavorite.sync();
}

export const closeConnections = async () => await Promise.all([chainDb.close(), userDb.close(), ...Object.values(chainDbs).map((db) => db.close())]);
export const closeConnections = async () => await Promise.all([chainDb.close(), userDb.close(), ...Object.values(chainDbs).map(db => db.close())]);
3 changes: 2 additions & 1 deletion apps/api/src/middlewares/privateMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { env } from "@src/utils/env";
import { Context, Next } from "hono";

import { env } from "@src/utils/env";

export async function privateMiddleware(c: Context, next: Next) {
if (!env.SecretToken) {
await next();
Expand Down
5 changes: 3 additions & 2 deletions apps/api/src/middlewares/userMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { env } from "@src/utils/env";
import { getPayloadFromContext, verifyRsaJwt } from "../verify-rsa-jwt-cloudflare-worker-main";
import { Context } from "hono";

import { cacheEngine } from "@src/caching/helpers";
import { env } from "@src/utils/env";
import { getPayloadFromContext, verifyRsaJwt } from "../verify-rsa-jwt-cloudflare-worker-main";

export const kvStore = {
async get(key: string, format: string) {
Expand Down
9 changes: 5 additions & 4 deletions apps/api/src/routers/apiRouter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { OpenAPIHono } from "@hono/zod-openapi";
import { swaggerUI } from "@hono/swagger-ui";
import routesV1 from "../routes/v1";
import { OpenAPIHono } from "@hono/zod-openapi";

import { env } from "@src/utils/env";
import routesV1 from "../routes/v1";

export const apiRouter = new OpenAPIHono();

Expand All @@ -20,9 +21,9 @@ function registerApiVersion(version: string, baseRouter: OpenAPIHono, versionRou
const swaggerInstance = swaggerUI({ url: `/${version}/doc` });

versionRouter.get(`/swagger`, swaggerInstance);
versionRouter.get(`/swagger/`, (c) => c.redirect(`/${version}/swagger`));
versionRouter.get(`/swagger/`, c => c.redirect(`/${version}/swagger`));

versionRoutes.forEach((route) => versionRouter.route(`/`, route));
versionRoutes.forEach(route => versionRouter.route(`/`, route));
baseRouter.route(`/${version}`, versionRouter);
}

Expand Down
5 changes: 3 additions & 2 deletions apps/api/src/routers/dashboardRouter.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Template, UserSetting } from "@akashnetwork/cloudmos-shared/dbSchemas/user";
import { privateMiddleware } from "@src/middlewares/privateMiddleware";
import { Hono } from "hono";

import { privateMiddleware } from "@src/middlewares/privateMiddleware";

export const dashboardRouter = new Hono();

dashboardRouter.use("*", privateMiddleware);

dashboardRouter.get("/stats", async (c) => {
dashboardRouter.get("/stats", async c => {
const userCountRequest = UserSetting.count();
const publicTemplateCountRequest = Template.count({
where: { isPublic: true }
Expand Down
7 changes: 4 additions & 3 deletions apps/api/src/routers/internalRouter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { OpenAPIHono } from "@hono/zod-openapi";
import { swaggerUI } from "@hono/swagger-ui";
import routes from "../routes/internal";
import { OpenAPIHono } from "@hono/zod-openapi";

import { env } from "@src/utils/env";
import routes from "../routes/internal";

export const internalRouter = new OpenAPIHono();

Expand All @@ -19,4 +20,4 @@ const swaggerInstance = swaggerUI({ url: `/internal/doc` });

internalRouter.get(`/swagger`, swaggerInstance);

routes.forEach((route) => internalRouter.route(`/`, route));
routes.forEach(route => internalRouter.route(`/`, route));
Loading
Loading