refactor AppService exceptions && add JSDoc
parent
c44da28f4f
commit
13914f4283
|
|
@ -42,7 +42,7 @@ const AlbumCreate = ({
|
|||
album,
|
||||
user.token
|
||||
).then( response => {
|
||||
setMessage( response['info'] )
|
||||
setMessage( response['info'] + '\n' )
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -81,7 +81,7 @@ const AlbumCreate = ({
|
|||
titleInput.current.value = ''
|
||||
descriptionInput.current.value = ''
|
||||
|
||||
setConsoleHistory( consoleHistory + message + '\n' )
|
||||
setConsoleHistory( consoleHistory + message )
|
||||
setComponentVisible( false )
|
||||
setImage('')
|
||||
setImageInfo('Drop/Click\nfor upload album image...')
|
||||
|
|
|
|||
|
|
@ -25,11 +25,7 @@ const AlbumDelete = ({
|
|||
id,
|
||||
user.token
|
||||
).then( response => {
|
||||
if ( response.detail !== 'Not found.' ){
|
||||
setMessage('album delete success')
|
||||
} else{
|
||||
setMessage('album delete failed')
|
||||
}
|
||||
setMessage( response['info'] + '\n' )
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -45,7 +41,7 @@ const AlbumDelete = ({
|
|||
|
||||
idInput.current.value = ''
|
||||
|
||||
setConsoleHistory( consoleHistory + message + '\n' )
|
||||
setConsoleHistory( consoleHistory + message )
|
||||
setComponentVisible( false )
|
||||
setMessage('')
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,27 +13,26 @@ const AlbumGetAll = ({
|
|||
}) => {
|
||||
|
||||
const [message, setMessage] = useState('')
|
||||
const [oneRequest, setOne ] = useState(false)
|
||||
const [oneRequest, setOne] = useState(false)
|
||||
|
||||
useEffect(
|
||||
() => {
|
||||
if (componentVisible && oneRequest === false) {
|
||||
getAllAlbum()
|
||||
.then( response => {
|
||||
getAllAlbum().then(response => {
|
||||
setMessage(
|
||||
mapAlbumsToString( response['response'] )
|
||||
+ response['info']
|
||||
mapAlbumsToString(
|
||||
response['response']
|
||||
) + response['info'] + '\n'
|
||||
)
|
||||
console.log( response )
|
||||
})
|
||||
setOne( !oneRequest )
|
||||
setOne(!oneRequest)
|
||||
} else {
|
||||
activateConsoleInput()
|
||||
}
|
||||
if ( message !== '' ) {
|
||||
if (message !== '') {
|
||||
setConsoleHistory(consoleHistory + message)
|
||||
setComponentVisible(false)
|
||||
setOne( !oneRequest )
|
||||
setOne(!oneRequest)
|
||||
setMessage('')
|
||||
}
|
||||
}
|
||||
|
|
@ -42,7 +41,7 @@ const AlbumGetAll = ({
|
|||
const mapAlbumsToString = (albums) => {
|
||||
let list = '.albums\n'
|
||||
for (let i = 0; i < albums.length; i++) {
|
||||
if ( i !== albums.length - 1 )
|
||||
if (i !== albums.length - 1)
|
||||
list += '├── ' + albums[i].title + '\n'
|
||||
+ '│ ├── id: ' + albums[i].id + '\n'
|
||||
+ '│ ├── user id: ' + albums[i].user_id + '\n'
|
||||
|
|
|
|||
|
|
@ -22,15 +22,18 @@ const AlbumGetOne = ({
|
|||
setConsoleHistory( consoleHistory + 'album id: ' + inputValue + '\n')
|
||||
if ( inputValue >= 0 ) {
|
||||
getOneAlbum( inputValue ).then( response => {
|
||||
if ( response.detail !== 'Not found.' ){
|
||||
if ( response['info'] !== 'Not found.' ){
|
||||
setMessage(
|
||||
response.title + '\n'
|
||||
+ '├── id: ' + response.id + '\n'
|
||||
+ '├── user id: ' + response.user_id + '\n'
|
||||
+ '└── url: ' + response.url_code + '\n'
|
||||
response['response'].title + '\n'
|
||||
+ '├── id: ' + response['response'].id + '\n'
|
||||
+ '├── user id: ' + response['response'].user_id + '\n'
|
||||
+ '└── url: ' + response['response'].url_code + '\n'
|
||||
+ response['info'] + '\n'
|
||||
)
|
||||
} else{
|
||||
setMessage('album not found')
|
||||
setMessage(
|
||||
response['info'] + '\n'
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,10 +50,8 @@ const AlbumUpdate = ({
|
|||
id,
|
||||
album,
|
||||
user.token
|
||||
).then( () => {
|
||||
setMessage('album update success')
|
||||
}).catch( () => {
|
||||
setMessage('album update failed')
|
||||
).then( response => {
|
||||
setMessage( response['info'] + '\n' )
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -71,7 +69,7 @@ const AlbumUpdate = ({
|
|||
descriptionInput.current.value = ''
|
||||
imageInput.current.value = ''
|
||||
|
||||
setConsoleHistory( consoleHistory + message + '\n' )
|
||||
setConsoleHistory( consoleHistory + message )
|
||||
setComponentVisible( false )
|
||||
setMessage('')
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,38 +1,77 @@
|
|||
import { address } from './APIAddress'
|
||||
|
||||
// session Token
|
||||
// User Session Token
|
||||
|
||||
let defaultToken = 'empty token'
|
||||
|
||||
|
||||
// CRUD methods
|
||||
|
||||
/**
|
||||
* get list method
|
||||
* @param {string} endpoint - for example `user/`
|
||||
*/
|
||||
const _getList = async (endpoint) => {
|
||||
return await responseGD(address + endpoint, 'GET', defaultToken)
|
||||
}
|
||||
|
||||
/**
|
||||
* get one row / record
|
||||
* @param {string} endpoint - for example `user/`
|
||||
*/
|
||||
const _getOne = async (endpoint) => {
|
||||
return await responseGD(address + endpoint, 'GET', defaultToken)
|
||||
}
|
||||
|
||||
/**
|
||||
* universal post method
|
||||
* @param {string} endpoint - for example `user/`
|
||||
* @param {{}} body - body request
|
||||
* @param {string} token - token for verify user in API
|
||||
*/
|
||||
const _post = async (endpoint, body, token) => {
|
||||
return await responseCRU(address + endpoint, 'POST', body, token)
|
||||
}
|
||||
|
||||
/**
|
||||
* universal put method
|
||||
* @param {string} endpoint - for example `user/{id}/` where {id} is object id
|
||||
* @param {{}} body - body request
|
||||
* @param {string} token - token for verify user in API
|
||||
*/
|
||||
const _put = async (endpoint, body, token) => {
|
||||
return await responseCRU(address + endpoint, 'PUT', body, token)
|
||||
}
|
||||
|
||||
/**
|
||||
* universal patch method
|
||||
* @param {string} endpoint - for example `user/{id}/` where {id} is object id
|
||||
* @param {{}} body - body request
|
||||
* @param {string} token - token for verify user in API
|
||||
*/
|
||||
const _patch = async (endpoint, body, token) => {
|
||||
return await responseCRU(address + endpoint, 'PATCH', body, token)
|
||||
}
|
||||
|
||||
/**
|
||||
* universal delete method
|
||||
* @param {string} endpoint - for example `user/{id}/` where {id} is object id
|
||||
* @param {string} token - token for verify user in API
|
||||
*/
|
||||
const _delete = async (endpoint, token) => {
|
||||
return await responseGD(address + endpoint, 'DELETE', token)
|
||||
}
|
||||
|
||||
|
||||
// Utils
|
||||
// Fetch methods
|
||||
|
||||
/**
|
||||
* fetch `get` / `delete` type methods
|
||||
* @param {string} address - full endpoint address
|
||||
* @param {string} method - method like `get` / `delete`
|
||||
* @param {string} token - token for verify user in API
|
||||
*/
|
||||
const responseGD = async (address, method, token) => {
|
||||
try {
|
||||
const response = await fetch(address, {
|
||||
|
|
@ -45,12 +84,22 @@ const responseGD = async (address, method, token) => {
|
|||
"Content-Type": "application/json"
|
||||
}
|
||||
})
|
||||
return await responseExceptions( await response.json() )
|
||||
} catch ( error ) {
|
||||
return await responseExceptions(
|
||||
await response.json(),
|
||||
response.status
|
||||
)
|
||||
} catch (error) {
|
||||
return { info: error }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* fetch `post` / `put` / `patch` type methods
|
||||
* @param {string} address - full endpoint address
|
||||
* @param {string} method - method like `post` / `put` / `patch`
|
||||
* @param {{}} body - body of request
|
||||
* @param {string} token - token for verify user session in API
|
||||
*/
|
||||
const responseCRU = async (address, method, body, token) => {
|
||||
try {
|
||||
const response = await fetch(address, {
|
||||
|
|
@ -64,29 +113,55 @@ const responseCRU = async (address, method, body, token) => {
|
|||
"Content-Type": "application/json"
|
||||
}
|
||||
})
|
||||
return await responseExceptions( await response.json() )
|
||||
} catch ( error ) {
|
||||
return await responseExceptions(
|
||||
await response.json(),
|
||||
response.status
|
||||
)
|
||||
} catch (error) {
|
||||
return { info: error }
|
||||
}
|
||||
}
|
||||
|
||||
const responseExceptions = async ( response ) => {
|
||||
/**
|
||||
* fetch bonus exceptions ( not blank fields in request / bad requests )
|
||||
* @param {Response} response
|
||||
* @param {number} status
|
||||
*/
|
||||
const responseExceptions = async (response, status) => {
|
||||
try {
|
||||
//progressStream( response )
|
||||
if (status > 300) {
|
||||
let info = ''
|
||||
Object.keys(response).forEach(element => {
|
||||
if (element !== 'detail')
|
||||
info += element + ' - ' + response[element][0]
|
||||
else
|
||||
info += response[element]
|
||||
})
|
||||
return {
|
||||
response: response,
|
||||
info: info
|
||||
}
|
||||
}
|
||||
else
|
||||
return {
|
||||
response: response,
|
||||
info: 'operation success'
|
||||
}
|
||||
} catch {
|
||||
} catch (error) {
|
||||
return {
|
||||
response: response,
|
||||
info: 'operation failed'
|
||||
info: error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get CSRF Token
|
||||
|
||||
/**
|
||||
* get cookie method for CSRF verification
|
||||
* @param {string} name - name of handled cookie
|
||||
*/
|
||||
const getCookie = (name) => {
|
||||
if (!document.cookie) {
|
||||
return null;
|
||||
|
|
@ -106,7 +181,7 @@ const csrftoken = getCookie('csrftoken')
|
|||
// Get progress stream
|
||||
|
||||
/**
|
||||
* only use with fetch API in `then` statement
|
||||
* Fetch streaming (use in `then` statement) usefull for get request progress info
|
||||
* @param response - is a response from fetch
|
||||
*/
|
||||
export const progressStream = (response) => {
|
||||
|
|
@ -128,14 +203,14 @@ export const progressStream = (response) => {
|
|||
return
|
||||
}
|
||||
loaded += value.byteLength
|
||||
console.log( loaded / total * 100 )
|
||||
console.log(loaded / total * 100)
|
||||
controller.enqueue(value)
|
||||
read()
|
||||
})
|
||||
// .catch(error => {
|
||||
// console.error(error)
|
||||
// controller.error(error)
|
||||
// })
|
||||
.catch(error => {
|
||||
console.error(error)
|
||||
controller.error(error)
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
|
|||
|
|
@ -20,21 +20,17 @@ export const postAuth = (username, password) => async (dispatch) => {
|
|||
body,
|
||||
AppService.defaultToken
|
||||
).then( response => {
|
||||
try {
|
||||
serviceUser = {
|
||||
id: response.user.id,
|
||||
username: response.user.username,
|
||||
email: response.user.email,
|
||||
ip: response.user.ip,
|
||||
city: response.user.city,
|
||||
country: response.user.country,
|
||||
token: response.Authorization
|
||||
id: response['response'].user.id,
|
||||
username: response['response'].user.username,
|
||||
email: response['response'].user.email,
|
||||
ip: response['response'].user.ip,
|
||||
city: response['response'].user.city,
|
||||
country: response['response'].user.country,
|
||||
token: response['response'].Authorization
|
||||
}
|
||||
dispatch(actions.login(serviceUser))
|
||||
return { error: 'login success' }
|
||||
} catch {
|
||||
return { error: 'login failed' }
|
||||
}
|
||||
return response
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -43,12 +39,8 @@ export const deleteAuth = (token) => async (dispatch) => {
|
|||
endpoint + 'auth',
|
||||
token
|
||||
).then( () => {
|
||||
try {
|
||||
dispatch(actions.logout())
|
||||
return { error: 'logout success'}
|
||||
} catch {
|
||||
return { error: 'logout failed' }
|
||||
}
|
||||
return response
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -71,15 +63,15 @@ export const updateUser = (user, id, token) => async (dispatch) => {
|
|||
token
|
||||
).then( response => {
|
||||
serviceUser = {
|
||||
id: response.user.id,
|
||||
username: response.user.username,
|
||||
email: response.user.email,
|
||||
ip: response.user.ip,
|
||||
city: response.user.city,
|
||||
country: response.user.country,
|
||||
token: token
|
||||
id: response['response'].user.id,
|
||||
username: response['response'].user.username,
|
||||
email: response['response'].user.email,
|
||||
ip: response['response'].user.ip,
|
||||
city: response['response'].user.city,
|
||||
country: response['response'].user.country,
|
||||
token: response['response'].Authorization
|
||||
}
|
||||
dispatch(actions.login(serviceUser))
|
||||
dispatch(actions.login( serviceUser ))
|
||||
return response
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue