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

style(dx): centralize code style tools #232

Merged
merged 2 commits into from
Jun 8, 2024
Merged
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
  •  
  •  
  •  
24 changes: 24 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const path = require("path");
const baseConfig = require("@akashnetwork/dev-config/.eslintrc.base");
const tsConfig = require("@akashnetwork/dev-config/.eslintrc.ts");
const nextConfig = require("@akashnetwork/dev-config/.eslintrc.next");

module.exports = {
...baseConfig,
settings: {
next: {
rootDir: "apps/*"
}
},
overrides: [
...baseConfig.overrides,
...tsConfig.overrides,
...nextConfig.overrides.map(override => ({
...override,
files: ["apps/*-web/**/*.{ts,tsx}", "apps/landing/**/*.{ts,tsx}"],
rules: {
"@next/next/no-html-link-for-pages": ["error", ["deploy-web", "landing"].map(app => path.resolve(__dirname, `apps/${app}/src/pages`))]
}
}))
]
};
19 changes: 19 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const config = require("@akashnetwork/dev-config/.prettierrc");

module.exports = {
...config,
overrides: [
{
files: "./apps/deploy-web/**",
options: {
tailwindConfig: "./apps/deploy-web/tailwind.config.ts"
}
},
{
files: "./apps/stats-web/**",
options: {
tailwindConfig: "./apps/stats-web/tailwind.config.ts"
}
}
]
};
1 change: 1 addition & 0 deletions apps/api/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@akashnetwork/dev-config/.eslintrc.ts');
17 changes: 0 additions & 17 deletions apps/api/.eslintrc.json

This file was deleted.

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

This file was deleted.

1 change: 1 addition & 0 deletions apps/api/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("@akashnetwork/dev-config/.prettierrc");
6 changes: 2 additions & 4 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"start": "webpack --config webpack.dev.js --watch",
"build": "webpack --config webpack.prod.js",
"lint": "eslint .",
"format": "prettier --write ./*.{js,json} **/*.{ts,js,json}",
"test": "jest --selectProjects unit functional",
"test:watch": "jest --selectProjects unit functional --watch",
"test:cov": "jest --selectProjects unit functional --coverage",
Expand Down Expand Up @@ -63,6 +64,7 @@
"zod": "^3.22.4"
},
"devDependencies": {
"@akashnetwork/dev-config": "*",
"@faker-js/faker": "^8.4.1",
"@types/jest": "^29.5.12",
"@types/lodash": "^4.17.0",
Expand All @@ -72,15 +74,11 @@
"@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.8.2",
"prettier": "^3.2.5",
"supertest": "^6.1.5",
"ts-jest": "^29.1.2",
"ts-loader": "^9.2.5",
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