Skip to content

Commit

Permalink
[refacto]: buildApiRequester iso with backend
Browse files Browse the repository at this point in the history
  • Loading branch information
llucasspot committed Jan 29, 2024
1 parent 933adf0 commit 5ebf7bf
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { AuthApiInterface } from "@swagger/api";
import { Configuration } from "@swagger/configuration";
import { buildApiRequester } from "@utils/api.utils";
import { inject, singleton } from "tsyringe";

import { EditProfileBody, ProfileApiServiceI } from "./ProfileApiServiceI";
Expand All @@ -22,9 +20,8 @@ export class ProfileApiMockService
protected storageService: StorageService,
) {
const baseUrl = configurationService.getWiremockApiUrl();
super(new Configuration(), baseUrl, buildApiRequester(baseUrl), () =>
this.storageService.getAuthenticateToken(),
);
const tokenGetter = () => this.storageService.getAuthenticateToken();
super(baseUrl, tokenGetter);
}

override createProfile() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { AuthApiInterface } from "@swagger/api";
import { Configuration } from "@swagger/configuration";
import { buildApiRequester } from "@utils/api.utils";
import { inject, singleton } from "tsyringe";

import { EditProfileBody, ProfileApiServiceI } from "./ProfileApiServiceI";
Expand All @@ -20,9 +18,8 @@ export class ProfileApiService
protected storageService: StorageService,
) {
const baseUrl = configurationService.getApiUrl().replace("/api", "");
super(new Configuration(), baseUrl, buildApiRequester(baseUrl), () =>
this.storageService.getAuthenticateToken(),
);
const tokenGetter = () => this.storageService.getAuthenticateToken();
super(baseUrl, tokenGetter);
}

async editProfile(editedProfileBody: EditProfileBody) {
Expand Down
10 changes: 6 additions & 4 deletions expo-boomboomapp/src/api/ProfileApiService/ProfileApiServiceI.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AuthApi, AuthApiInterface, CreateProfileRequest } from "@swagger/api";
import { Configuration } from "@swagger/configuration";
import { AxiosInstance } from "axios/index";
import { buildApiRequester } from "@utils/api.utils";
import * as FileSystem from "expo-file-system";

export type EditProfileBody = Partial<CreateProfileRequest>;
Expand All @@ -10,12 +10,14 @@ export abstract class ProfileApiServiceI
implements AuthApiInterface
{
constructor(
configuration: Configuration,
basePath: string,
axios: AxiosInstance,
private authTokenGetter: () => Promise<string | null>,
) {
super(configuration, basePath, axios);
super(
new Configuration(),
basePath,
buildApiRequester(basePath, authTokenGetter),
);
}

async uploadAvatarByUri(uri: string): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { inject, singleton } from "tsyringe";

import { SpotifyApiServiceI } from "./SpotifyApiServiceI";
import ConfigurationService from "../../services/ConfigurationService/ConfigurationService";
import StorageService from "../../services/StorageService/StorageService";
import ServiceInterface from "../../tsyringe/ServiceInterface";

@singleton()
Expand All @@ -15,8 +16,15 @@ export class SpotifyApiMockService
constructor(
@inject(ServiceInterface.ConfigurationService)
protected configurationService: ConfigurationService,
@inject(ServiceInterface.StorageServiceI)
protected storageService: StorageService,
) {
const baseUrl = configurationService.getWiremockApiUrl();
super(new Configuration(), baseUrl, buildApiRequester(baseUrl));
const tokenGetter = () => this.storageService.getAuthenticateToken();
super(
new Configuration(),
baseUrl,
buildApiRequester(baseUrl, tokenGetter),
);
}
}
10 changes: 9 additions & 1 deletion expo-boomboomapp/src/api/SpotifyApiService/SpotifyApiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { inject, singleton } from "tsyringe";

import { SpotifyApiServiceI } from "./SpotifyApiServiceI";
import ConfigurationService from "../../services/ConfigurationService/ConfigurationService";
import StorageService from "../../services/StorageService/StorageService";
import ServiceInterface from "../../tsyringe/ServiceInterface";

@singleton()
Expand All @@ -15,8 +16,15 @@ export class SpotifyApiService
constructor(
@inject(ServiceInterface.ConfigurationService)
protected configurationService: ConfigurationService,
@inject(ServiceInterface.StorageServiceI)
protected storageService: StorageService,
) {
const baseUrl = configurationService.getApiUrl().replace("/api", "");
super(new Configuration(), baseUrl, buildApiRequester(baseUrl));
const tokenGetter = () => this.storageService.getAuthenticateToken();
super(
new Configuration(),
baseUrl,
buildApiRequester(baseUrl, tokenGetter),
);
}
}
10 changes: 9 additions & 1 deletion expo-boomboomapp/src/api/UserApiService/UserApiMockService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { v4 as uuidv4 } from "uuid";

import { user_helena, user_isabella, user_jessica } from "../../mocks/mokes";
import ConfigurationService from "../../services/ConfigurationService/ConfigurationService";
import StorageService from "../../services/StorageService/StorageService";
import ServiceInterface from "../../tsyringe/ServiceInterface";
import { buildAxiosMockResponse } from "../utils";

Expand All @@ -19,9 +20,16 @@ export class UserApiMockService extends UserApi implements UserApiInterface {
constructor(
@inject(ServiceInterface.ConfigurationService)
protected configurationService: ConfigurationService,
@inject(ServiceInterface.StorageServiceI)
protected storageService: StorageService,
) {
const baseUrl = configurationService.getWiremockApiUrl();
super(new Configuration(), baseUrl, buildApiRequester(baseUrl));
const tokenGetter = () => this.storageService.getAuthenticateToken();
super(
new Configuration(),
baseUrl,
buildApiRequester(baseUrl, tokenGetter),
);
}

override async apiUsersGet(
Expand Down
10 changes: 9 additions & 1 deletion expo-boomboomapp/src/api/UserApiService/UserApiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@ import { buildApiRequester } from "@utils/api.utils";
import { inject, singleton } from "tsyringe";

import ConfigurationService from "../../services/ConfigurationService/ConfigurationService";
import StorageService from "../../services/StorageService/StorageService";
import ServiceInterface from "../../tsyringe/ServiceInterface";

@singleton()
export class UserApiService extends UserApi implements UserApiInterface {
constructor(
@inject(ServiceInterface.ConfigurationService)
protected configurationService: ConfigurationService,
@inject(ServiceInterface.StorageServiceI)
protected storageService: StorageService,
) {
const baseUrl = configurationService.getApiUrl().replace("/api", "");
super(new Configuration(), baseUrl, buildApiRequester(baseUrl));
const tokenGetter = () => this.storageService.getAuthenticateToken();
super(
new Configuration(),
baseUrl,
buildApiRequester(baseUrl, tokenGetter),
);
}
}
14 changes: 5 additions & 9 deletions expo-boomboomapp/src/utils/api.utils.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import axios, { AxiosInstance } from "axios";

import StorageService from "../services/StorageService/StorageService";
import ServiceInterface from "../tsyringe/ServiceInterface";
import { getGlobalInstance } from "../tsyringe/diUtils";

export function buildApiRequester(baseURL: string): AxiosInstance {
export function buildApiRequester(
baseURL: string,
tokenGetter: () => Promise<string | null>,
): AxiosInstance {
const apiRequester = axios.create({
baseURL,
});

apiRequester.interceptors.request.use(async (config) => {
const storageService = getGlobalInstance<StorageService>(
ServiceInterface.StorageServiceI,
);
try {
const token = await storageService.getAuthenticateToken();
const token = await tokenGetter();
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
Expand Down

0 comments on commit 5ebf7bf

Please sign in to comment.