mirror of
https://github.com/sergeyyarkov/educt-client.git
synced 2026-10-11 02:09:08 +03:00
feat: api service
This commit is contained in:
parent
2b185c57de
commit
4fd3d65903
21556
package-lock.json
generated
21556
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
36
src/services/ApiService.ts
Normal file
36
src/services/ApiService.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,4 @@
|
||||
import axios from 'axios';
|
||||
import { API_BASE_URL } from '../constants';
|
||||
import ApiService from './ApiService';
|
||||
import { IDataResult } from '../interfaces';
|
||||
|
||||
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 {
|
||||
public apiService: ApiService;
|
||||
|
||||
constructor(apiService: ApiService) {
|
||||
this.apiService = apiService;
|
||||
}
|
||||
|
||||
public async requestLogin(
|
||||
login: string,
|
||||
password: string
|
||||
): Promise<ILoginResult> {
|
||||
const result = await api.post('v1/auth/login', {
|
||||
const result = await this.apiService.api.post('v1/auth/login', {
|
||||
login,
|
||||
password,
|
||||
});
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
import { makeAutoObservable } from 'mobx';
|
||||
import Cookies from 'js-cookie';
|
||||
import AuthService from '../services/AuthService';
|
||||
import RootStore from './RootStore';
|
||||
|
||||
export default class AuthStore {
|
||||
@ -10,11 +9,8 @@ export default class AuthStore {
|
||||
|
||||
public isLoggedIn: boolean = !!Cookies.get('logged_in');
|
||||
|
||||
public authService: AuthService;
|
||||
|
||||
constructor(root: RootStore) {
|
||||
this.root = root;
|
||||
this.authService = new AuthService();
|
||||
makeAutoObservable(this);
|
||||
}
|
||||
|
||||
@ -23,6 +19,7 @@ export default class AuthStore {
|
||||
}
|
||||
|
||||
public setIsLoggedIn(value: boolean) {
|
||||
Cookies.remove('logged_in');
|
||||
this.isLoggedIn = value;
|
||||
}
|
||||
|
||||
@ -32,7 +29,7 @@ export default class AuthStore {
|
||||
}
|
||||
|
||||
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.setToken(result.data.token);
|
||||
|
||||
@ -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';
|
||||
|
||||
export default class RootStore {
|
||||
// public api: AxiosInstance;
|
||||
import ApiService from '../services/ApiService';
|
||||
import AuthService from '../services/AuthService';
|
||||
|
||||
export default class RootStore {
|
||||
public authStore: AuthStore;
|
||||
|
||||
public apiService: ApiService;
|
||||
|
||||
public authService: AuthService;
|
||||
|
||||
constructor() {
|
||||
/**
|
||||
* Api root instance
|
||||
*/
|
||||
// this.api = axios.create({
|
||||
// baseURL: API_BASE_URL,
|
||||
// responseType: 'json',
|
||||
// });
|
||||
|
||||
/**
|
||||
* Auth store
|
||||
* Stores
|
||||
*/
|
||||
this.authStore = new AuthStore(this);
|
||||
|
||||
/**
|
||||
* Services
|
||||
*/
|
||||
this.apiService = new ApiService(this, {
|
||||
baseURL: API_BASE_URL,
|
||||
responseType: 'json',
|
||||
});
|
||||
this.authService = new AuthService(this.apiService);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user