Ancora sistemazioni con typescript....
This commit is contained in:
16
src/store/Api/ApiRoutes.ts
Normal file
16
src/store/Api/ApiRoutes.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
const ServerRoutes = {
|
||||
LOGIN: 'login_check',
|
||||
TOKEN_REFRESH: 'token/refresh',
|
||||
SIGNUP: 'register/',
|
||||
MOVING_LIST: 'announcements',
|
||||
MOVING_DETAIL: 'announcement/',
|
||||
MOVING_CREATE: 'announcement',
|
||||
MOVING_USER_INFOS: 'user/verify',
|
||||
PARTICIPATION_CREATE: 'participations',
|
||||
CREATE_NOTE: 'note_user',
|
||||
MOVERS_LIST: 'movers',
|
||||
NEW_MOVER: 'mover',
|
||||
USERS: 'users'
|
||||
}
|
||||
|
||||
export default ServerRoutes
|
||||
106
src/store/Api/ApiTypes.ts
Normal file
106
src/store/Api/ApiTypes.ts
Normal file
@@ -0,0 +1,106 @@
|
||||
// import { NotificationsStore, LoginStore } from '@store'
|
||||
|
||||
export class AxiosSuccess {
|
||||
public success: boolean = true
|
||||
public status: number
|
||||
public data: any
|
||||
|
||||
constructor(data: any) {
|
||||
this.data = data
|
||||
}
|
||||
}
|
||||
|
||||
export class AxiosError {
|
||||
public success: boolean = false
|
||||
public status: number
|
||||
public data: any
|
||||
|
||||
constructor(status: number, data?: any) {
|
||||
this.status = status
|
||||
this.data = data
|
||||
if (status !== 401) {
|
||||
// if (status == 0) message = 'Vérifiez votre connexion Internet';
|
||||
// NotificationsStore.actions.addNotification({ type: 'warning', message: message })
|
||||
}
|
||||
else {
|
||||
if (data.error && data.error.message !== 'Bad credentials') {
|
||||
// LoginStore.actions.disconnectRequest()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// export class ApiResponse {
|
||||
// public success: boolean = true;
|
||||
// public message?: string;
|
||||
// public data?: any;
|
||||
// public type: string;
|
||||
// constructor(fields: {message?: string, data?: any, type: any, success: boolean}) {
|
||||
// this.message = fields.message;
|
||||
// this.type = fields.type;
|
||||
// this.data = fields.data ? fields.data : {};
|
||||
// this.success = fields.success;
|
||||
// }
|
||||
|
||||
// yes() {
|
||||
// return Promise.resolve(this);
|
||||
// }
|
||||
// }
|
||||
|
||||
export interface IApiResponse {
|
||||
success: boolean
|
||||
message?: string
|
||||
data?: any
|
||||
type: string
|
||||
}
|
||||
|
||||
export class ApiResponse {
|
||||
public data?: any
|
||||
public message?: any
|
||||
constructor(fields: {message?: string, data?: any, type: any, success: boolean}) {
|
||||
let returnData: any = {}
|
||||
returnData.message = fields.message
|
||||
returnData.type = fields.type
|
||||
returnData.data = fields.data != null ? fields.data : {}
|
||||
returnData.success = fields.success
|
||||
if (fields.success) return <any>Promise.resolve(returnData)
|
||||
else return <any>Promise.reject(returnData)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
export class ApiSuccess extends ApiResponse {
|
||||
constructor(fields: {message?: string, data?: any} = {}) {
|
||||
super({
|
||||
success: true,
|
||||
type: 'success',
|
||||
message: fields.message,
|
||||
data: fields.data
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export class ApiError extends ApiResponse {
|
||||
constructor(fields: {message?: string, data?: any} = {}) {
|
||||
super({
|
||||
success: false,
|
||||
type: 'error',
|
||||
message: fields.message,
|
||||
data: fields.data
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export class ApiWarning extends ApiResponse {
|
||||
constructor(fields: {message?: string, data?: any} = {}) {
|
||||
super({
|
||||
success: false,
|
||||
type: 'warning',
|
||||
message: fields.message,
|
||||
data: fields.data
|
||||
})
|
||||
}
|
||||
}
|
||||
23
src/store/Api/Inst-Pao.ts
Normal file
23
src/store/Api/Inst-Pao.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import axios, { AxiosInstance, AxiosPromise, AxiosResponse, AxiosInterceptorManager } from 'axios'
|
||||
|
||||
async function sendRequest (url: string, lang: string, mytok: string, method: string, mydata: any) {
|
||||
console.log('LANG ' + lang)
|
||||
// let mytok: string = this.getTok()
|
||||
const authHeader = new Headers()
|
||||
|
||||
authHeader.append('content-type', 'application/json')
|
||||
authHeader.append('x-auth', mytok)
|
||||
authHeader.append('accept-language', lang)
|
||||
const configInit: RequestInit = {
|
||||
method: method,
|
||||
cache: 'no-cache',
|
||||
body: JSON.stringify(mydata),
|
||||
headers: authHeader
|
||||
}
|
||||
|
||||
const request: Promise<Response> = fetch(url, configInit)
|
||||
return request
|
||||
|
||||
}
|
||||
|
||||
export default sendRequest
|
||||
73
src/store/Api/Instance.ts
Normal file
73
src/store/Api/Instance.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import axios, { AxiosInstance, AxiosPromise, AxiosResponse, AxiosInterceptorManager } from 'axios'
|
||||
// import LoginModule from '../Modules/Auth/LoginStore'
|
||||
import router from '@router'
|
||||
import {clone} from 'lodash'
|
||||
import * as Types from './ApiTypes'
|
||||
|
||||
export const API_URL = process.env.API_URL
|
||||
export const APP_BASE = process.env.NODE_ENV === 'development' ? 'http://localhost:5000/' : 'http://51.254.123.205:5000/'
|
||||
export const axiosInstance: AxiosInstance = axios.create({
|
||||
baseURL: API_URL,
|
||||
headers: {
|
||||
'Accept': 'application/json'
|
||||
}
|
||||
})
|
||||
|
||||
axiosInstance.interceptors.response.use(
|
||||
(response) => {
|
||||
console.log(response)
|
||||
return response
|
||||
},
|
||||
(error) => {
|
||||
console.log(error.response.status)
|
||||
console.log('Request Error: ', error.response)
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
export const addAuthHeaders = () => {
|
||||
// axiosInstance.defaults.headers.Authorization = `Bearer ${LoginModule.state.userInfos.userToken}`
|
||||
}
|
||||
|
||||
export const removeAuthHeaders = () => {
|
||||
delete axiosInstance.defaults.headers.Authorization
|
||||
}
|
||||
|
||||
async function Request(type: string, path: string, payload: any, noAuth?: boolean): Promise<Types.AxiosSuccess | Types.AxiosError> {
|
||||
try {
|
||||
console.log(`Axios Request [${type}]:`, axiosInstance.defaults)
|
||||
let response: AxiosResponse
|
||||
if (type === 'post' || type === 'put') {
|
||||
response = await axiosInstance[type](path, payload, {
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
})
|
||||
console.log(new Types.AxiosSuccess(response.data))
|
||||
return new Types.AxiosSuccess(response.data)
|
||||
} else if (type === 'get' || type === 'delete') {
|
||||
// @ts-ignore
|
||||
response = await axiosInstance[type](path, {
|
||||
params: payload,
|
||||
headers: {'Content-Type': 'application/json'}
|
||||
})
|
||||
return new Types.AxiosSuccess(response.data)
|
||||
} else if (type === 'postFormData') {
|
||||
response = await axiosInstance.post(path, payload, {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data'
|
||||
}
|
||||
})
|
||||
return new Types.AxiosSuccess(response.data)
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
if (error.response) {
|
||||
return Promise.reject(new Types.AxiosError(error.response.status, error.response.data))
|
||||
} else {
|
||||
return Promise.reject(new Types.AxiosError(0))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default Request
|
||||
42
src/store/Api/index.ts
Normal file
42
src/store/Api/index.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import Request from './Instance'
|
||||
import sendRequest from './Inst-Pao'
|
||||
export * from './ApiTypes'
|
||||
import axios from 'axios'
|
||||
export {addAuthHeaders, removeAuthHeaders, API_URL, APP_BASE} from './Instance'
|
||||
// import {AlgoliaSearch} from './AlgoliaController'
|
||||
import Paths from '@paths'
|
||||
|
||||
|
||||
// const algoliaApi = new AlgoliaSearch()
|
||||
export namespace ApiTool {
|
||||
export async function post(path: string, payload?: any) {
|
||||
return await Request('post', path, payload)
|
||||
}
|
||||
export async function postFormData(path: string, payload?: any) {
|
||||
return await Request('postFormData', path, payload)
|
||||
}
|
||||
export async function get(path: string, payload?: any) {
|
||||
return await Request('get', path, payload)
|
||||
}
|
||||
export async function put(path: string, payload?: any) {
|
||||
return await Request('put', path, payload)
|
||||
}
|
||||
export async function Delete(path: string, payload: any) {
|
||||
return await Request('delete', path, payload)
|
||||
}
|
||||
export async function checkSession({token, refresh_token}) {
|
||||
return await axios.post(process.env.API_URL + Paths.TOKEN_REFRESH, {
|
||||
refresh_token
|
||||
}, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export async function SendReq(url: string, lang: string, mytok: string, method: string, mydata: any) {
|
||||
return await sendRequest(url, lang, mytok, method, mydata)
|
||||
}
|
||||
|
||||
}
|
||||
export default ApiTool
|
||||
Reference in New Issue
Block a user