Skip to content

Refactor http services #1424

@stalniy

Description

@stalniy

Why

Currently all @akashnetwork/http-sdk services extend AxiosInstance. It breaks encapsulation and makes it harder to configure app because every service requires configuration of the same interceptors and configurations. Take a look at https://github.com/akash-network/console/blob/main/apps/deploy-web/src/services/http-factory/http-factory.service.ts#L16

Additionally switching to 1 or few axios instances would make it easier to configure axios to use fetch adapter -> #1423

What

Refactor http services to NOT extend axios instance and instead utilize composition:

Having one (or few) axios instance we could configure this once and pass it to every service. For example this code:

const apiConfig = { baseURL: config.BASE_API_MAINNET_URL };
  return createContainer({
    user: () =>
      withInterceptors(new UserHttpService(apiConfig), {
        request: [config.globalRequestMiddleware, authService.withAnonymousUserHeader],
        response: [
          response => {
            if (response.config.url?.startsWith("/v1/anonymous-users") && response.config.method === "post" && response.status === 200) {
              analyticsService.track("anonymous_user_created", { category: "user", label: "Anonymous User Created" });
            }
            return response;
          }
        ]
      }),

would become this:

  const mainnetAdapter = axios.create({ baseURL: config.BASE_API_MAINNET_URL });
  mainnetAdapter.intercetors.use([config.globalRequestMiddleware, authService.withAnonymousUserHeader], response => {
    if (response.config.url?.startsWith("/v1/anonymous-users") && response.config.method === "post" && response.status === 200) {
      analyticsService.track("anonymous_user_created", { category: "user", label: "Anonymous User Created" });
    }
    return response;
  });
  return createContainer({
    user: () => new UserHttpService(mainnetAdapter),
    stripe: () => new StripeService(mainnetAdapter),
  })

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions