feat: api service

This commit is contained in:
Sergey Yarkov 2021-08-19 23:17:05 +03:00
parent 2b185c57de
commit 4fd3d65903
5 changed files with 114 additions and 21532 deletions

21556
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,36 @@
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
import RootStore from '../stores/RootStore';
export default class ApiService {
public root: RootStore;
public api: AxiosInstance;
constructor(root: RootStore, config?: AxiosRequestConfig | undefined) {
this.root = root;
this.api = axios.create(config);
this.setupInterceptors();
}
private setupInterceptors() {
/**
* Response
*/
this.api.interceptors.response.use(undefined, (error) => {
/**
* Unauthorized
*/
if (
error.response.status === 401 ||
error.response.data.message === '401 Unauthorized'
) {
this.root.authStore.setIsLoggedIn(false);
}
return Promise.reject(error);
});
}
get config(): AxiosRequestConfig | undefined {
return this.config;
}
}

View File

@ -1,5 +1,4 @@
import axios from 'axios'; import ApiService from './ApiService';
import { API_BASE_URL } from '../constants';
import { IDataResult } from '../interfaces'; import { IDataResult } from '../interfaces';
interface ILoginResult extends IDataResult { interface ILoginResult extends IDataResult {
@ -10,17 +9,18 @@ interface ILoginResult extends IDataResult {
}; };
} }
const api = axios.create({
baseURL: API_BASE_URL,
responseType: 'json',
});
export default class AuthService { export default class AuthService {
public apiService: ApiService;
constructor(apiService: ApiService) {
this.apiService = apiService;
}
public async requestLogin( public async requestLogin(
login: string, login: string,
password: string password: string
): Promise<ILoginResult> { ): Promise<ILoginResult> {
const result = await api.post('v1/auth/login', { const result = await this.apiService.api.post('v1/auth/login', {
login, login,
password, password,
}); });

View File

@ -1,6 +1,5 @@
import { makeAutoObservable } from 'mobx'; import { makeAutoObservable } from 'mobx';
import Cookies from 'js-cookie'; import Cookies from 'js-cookie';
import AuthService from '../services/AuthService';
import RootStore from './RootStore'; import RootStore from './RootStore';
export default class AuthStore { export default class AuthStore {
@ -10,11 +9,8 @@ export default class AuthStore {
public isLoggedIn: boolean = !!Cookies.get('logged_in'); public isLoggedIn: boolean = !!Cookies.get('logged_in');
public authService: AuthService;
constructor(root: RootStore) { constructor(root: RootStore) {
this.root = root; this.root = root;
this.authService = new AuthService();
makeAutoObservable(this); makeAutoObservable(this);
} }
@ -23,6 +19,7 @@ export default class AuthStore {
} }
public setIsLoggedIn(value: boolean) { public setIsLoggedIn(value: boolean) {
Cookies.remove('logged_in');
this.isLoggedIn = value; this.isLoggedIn = value;
} }
@ -32,7 +29,7 @@ export default class AuthStore {
} }
public async login(login: string, password: string) { public async login(login: string, password: string) {
const result = await this.authService.requestLogin(login, password); const result = await this.root.authService.requestLogin(login, password);
this.setIsLoggedIn(true); this.setIsLoggedIn(true);
this.setToken(result.data.token); this.setToken(result.data.token);

View File

@ -1,24 +1,29 @@
// import axios, { AxiosInstance } from 'axios'; import { API_BASE_URL } from '../constants';
// import { API_BASE_URL } from '../constants';
import AuthStore from './AuthStore'; import AuthStore from './AuthStore';
export default class RootStore { import ApiService from '../services/ApiService';
// public api: AxiosInstance; import AuthService from '../services/AuthService';
export default class RootStore {
public authStore: AuthStore; public authStore: AuthStore;
public apiService: ApiService;
public authService: AuthService;
constructor() { constructor() {
/** /**
* Api root instance * Stores
*/
// this.api = axios.create({
// baseURL: API_BASE_URL,
// responseType: 'json',
// });
/**
* Auth store
*/ */
this.authStore = new AuthStore(this); this.authStore = new AuthStore(this);
/**
* Services
*/
this.apiService = new ApiService(this, {
baseURL: API_BASE_URL,
responseType: 'json',
});
this.authService = new AuthService(this.apiService);
} }
} }