Skip to content

Commit

Permalink
refactor(http): extract http services to the package
Browse files Browse the repository at this point in the history
refs #247
  • Loading branch information
ygrishajev committed Jul 30, 2024
1 parent 0aa2ccb commit 385bacd
Show file tree
Hide file tree
Showing 24 changed files with 215 additions and 102 deletions.
2 changes: 1 addition & 1 deletion apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@opentelemetry/sdk-node": "^0.52.1",
"@sentry/node": "^7.55.2",
"@supercharge/promise-pool": "^3.2.0",
"axios": "^0.27.2",
"axios": "^1.7.2",
"commander": "^12.1.0",
"cosmjs-types": "^0.9.0",
"date-fns": "^2.29.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ManagedUserWalletService, WalletInitializerService } from "@src/billing
import { TxSignerService } from "@src/billing/services/tx-signer/tx-signer.service";
import { WithTransaction } from "@src/core/services";

// TODO: authorize endpoints below
@singleton()
export class WalletController {
constructor(
Expand Down
10 changes: 7 additions & 3 deletions apps/api/src/db/dbConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ if (!csMap[env.Network]) {
}

const logger = new PostgresLoggerService({ orm: "sequelize" });
const logging = (msg: string) => logger.write(msg);

pg.defaults.parseInt8 = true;
export const chainDb = new Sequelize(csMap[env.Network], {
dialectModule: pg,
logging: (msg: string) => logger.write(msg),
logging,
logQueryParameters: true,
transactionType: DbTransaction.TYPES.IMMEDIATE,
define: {
timestamps: false,
Expand All @@ -47,7 +49,8 @@ export const chainDbs: { [key: string]: Sequelize } = Object.keys(chainDefinitio
...obj,
[chain]: new Sequelize(chainDefinitions[chain].connectionString, {
dialectModule: pg,
logging: (msg: string) => logger.write(msg),
logging,
logQueryParameters: true,
repositoryMode: true,
transactionType: DbTransaction.TYPES.IMMEDIATE,
define: {
Expand All @@ -62,7 +65,8 @@ export const chainDbs: { [key: string]: Sequelize } = Object.keys(chainDefinitio

export const userDb = new Sequelize(env.UserDatabaseCS, {
dialectModule: pg,
logging: (msg: string) => logger.write(msg),
logging,
logQueryParameters: true,
transactionType: DbTransaction.TYPES.IMMEDIATE,
define: {
timestamps: false,
Expand Down
4 changes: 2 additions & 2 deletions apps/deploy-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"dependencies": {
"@akashnetwork/akash-api": "^1.3.0",
"@akashnetwork/akashjs": "^0.10.0",
"@akashnetwork/ui": "*",
"@akashnetwork/http-sdk": "*",
"@akashnetwork/ui": "*",
"@auth0/nextjs-auth0": "^3.5.0",
"@chain-registry/types": "^0.41.3",
"@cosmjs/encoding": "^0.32.4",
Expand Down Expand Up @@ -52,7 +52,7 @@
"@tanstack/react-table": "^8.13.2",
"@textea/json-viewer": "^3.0.0",
"auth0": "^4.3.1",
"axios": "^0.27.2",
"axios": "^1.7.2",
"chain-registry": "^1.20.0",
"class-variance-authority": "^0.7.0",
"clsx": "^2.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use client";
import React, { useEffect, useMemo, useRef, useState } from "react";
import type { TxOutput } from "@akashnetwork/http-sdk";
import { Snackbar } from "@akashnetwork/ui/components";
import { EncodeObject } from "@cosmjs/proto-signing";
import { SigningStargateClient } from "@cosmjs/stargate";
Expand All @@ -18,7 +19,7 @@ import { useUsdcDenom } from "@src/hooks/useDenom";
import { useManagedWallet } from "@src/hooks/useManagedWallet";
import { getSelectedNetwork, useSelectedNetwork } from "@src/hooks/useSelectedNetwork";
import { useWhen } from "@src/hooks/useWhen";
import { txHttpService, TxOutput } from "@src/services/tx-http/tx-http.service";
import { txHttpService } from "@src/services/http/http.service";
import { AnalyticsEvents } from "@src/utils/analytics";
import { STATS_APP_URL, uAktDenom } from "@src/utils/constants";
import { customRegistry } from "@src/utils/customRegistry";
Expand Down
10 changes: 5 additions & 5 deletions apps/deploy-web/src/pages/api/auth/[...auth0].ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// pages/api/auth/[...auth0].js
import { handleAuth, handleLogin, handleProfile } from "@auth0/nextjs-auth0";
import axios, { AxiosRequestHeaders } from "axios";
import axios, { AxiosHeaders } from "axios";
import type { NextApiRequest, NextApiResponse } from "next";

import { BASE_API_MAINNET_URL } from "@src/utils/constants";
Expand All @@ -22,14 +22,14 @@ export default handleAuth({
try {
// TODO: Fix for console
const user_metadata = session.user["https://console.akash.network/user_metadata"];
const headers: AxiosRequestHeaders = {
const headers = new AxiosHeaders({
Authorization: `Bearer ${session.accessToken}`
};
});

const anonymousId = req.headers["x-anonymous-user-id"];

if (anonymousId) {
headers["X-ANONYMOUS-USER-ID"] = anonymousId as string;
headers.set("x-anonymous-user-id", anonymousId);
}

const userSettings = await axios.post(
Expand All @@ -41,7 +41,7 @@ export default handleAuth({
subscribedToNewsletter: user_metadata?.subscribedToNewsletter === "true"
},
{
headers
headers: headers.toJSON()
}
);

Expand Down
2 changes: 1 addition & 1 deletion apps/deploy-web/src/queries/useAnonymousUserQuery.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from "react";

import { useWhen } from "@src/hooks/useWhen";
import { userHttpService } from "@src/services/user-http/user-http.service";
import { userHttpService } from "@src/services/http/http.service";

export interface UserOutput {
id: string;
Expand Down
2 changes: 1 addition & 1 deletion apps/deploy-web/src/queries/useManagedWalletQuery.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useMutation, useQuery, useQueryClient } from "react-query";

import { managedWalletHttpService } from "@src/services/managed-wallet-http/managed-wallet-http.service";
import { managedWalletHttpService } from "@src/services/http/http.service";

const MANAGED_WALLET = "MANAGED_WALLET";

Expand Down
29 changes: 0 additions & 29 deletions apps/deploy-web/src/services/api-http/api-http.service.ts

This file was deleted.

9 changes: 6 additions & 3 deletions apps/deploy-web/src/services/auth/auth-http.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpService } from "@akashnetwork/http-sdk";
import type { UserProfile } from "@auth0/nextjs-auth0/client";
import { AxiosRequestHeaders } from "axios";
import { AxiosHeaders } from "axios";

import { ANONYMOUS_USER_KEY } from "@src/utils/constants";

Expand All @@ -14,9 +14,12 @@ export class AuthHttpService extends HttpService {
try {
const user = localStorage.getItem(ANONYMOUS_USER_KEY);
const anonymousUserId = user ? JSON.parse(user).id : undefined;
const headers: AxiosRequestHeaders = anonymousUserId ? { "X-ANONYMOUS-USER-ID": anonymousUserId } : {};
const headers = new AxiosHeaders();
if (anonymousUserId) {
headers.set("X-User-Id", anonymousUserId);
}

return this.extractData(await this.get<UserProfile | undefined>(url, { headers }));
return this.extractData(await this.get<UserProfile | undefined>(url, { headers: headers.toJSON() }));
} catch (error) {
console.warn("DEBUG error", error);
throw error;
Expand Down
10 changes: 10 additions & 0 deletions apps/deploy-web/src/services/http/http.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ManagedWalletHttpService, TxHttpService, UserHttpService } from "@akashnetwork/http-sdk";

import { BASE_API_URL } from "@src/utils/constants";
import { customRegistry } from "@src/utils/customRegistry";

const apiConfig = { baseURL: BASE_API_URL };

export const userHttpService = new UserHttpService(apiConfig);
export const txHttpService = new TxHttpService(customRegistry, apiConfig);
export const managedWalletHttpService = new ManagedWalletHttpService(apiConfig);
29 changes: 0 additions & 29 deletions apps/deploy-web/src/services/tx-http/tx-http.service.ts

This file was deleted.

2 changes: 1 addition & 1 deletion apps/indexer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@cosmjs/stargate": "^0.32.4",
"@sentry/node": "^7.52.0",
"async": "^3.2.4",
"axios": "^0.27.2",
"axios": "^1.7.2",
"cosmjs-types": "^0.9.0",
"date-fns": "^2.29.2",
"date-fns-tz": "^1.3.6",
Expand Down
2 changes: 1 addition & 1 deletion apps/provider-proxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dev": "npm run start"
},
"dependencies": {
"axios": "^1.3.0",
"axios": "^1.7.2",
"cors": "^2.8.5",
"express": "^4.18.2",
"node-fetch": "^2.6.9",
Expand Down
2 changes: 1 addition & 1 deletion apps/stats-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@radix-ui/react-slot": "^1.0.2",
"@tanstack/react-table": "^8.11.2",
"@textea/json-viewer": "^3.2.3",
"axios": "^1.6.1",
"axios": "^1.7.2",
"class-variance-authority": "^0.7.0",
"clsx": "^2.0.0",
"geist": "^1.3.0",
Expand Down
Loading

0 comments on commit 385bacd

Please sign in to comment.