mirror of
https://github.com/sergeyyarkov/educt-client.git
synced 2026-10-11 10:19:22 +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 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,
|
||||||
});
|
});
|
||||||
|
|||||||
@ -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);
|
||||||
|
|||||||
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user