request.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import axios, { AxiosRequestConfig, AxiosResponse, AxiosPromise } from 'axios'
  21. import { DEFAULT_JWT_TOKEN_EXPIRED } from 'app/globalConstants'
  22. let tokenExpired = DEFAULT_JWT_TOKEN_EXPIRED
  23. axios.defaults.validateStatus = function (status) {
  24. return status < 400
  25. }
  26. function parseJSON (response: AxiosResponse) {
  27. return response.data
  28. }
  29. function refreshToken (response: AxiosResponse) {
  30. const token = response.data.header && response.data.header.token
  31. if (token) {
  32. setToken(token)
  33. }
  34. return response
  35. }
  36. export function request (url: string, options?: AxiosRequestConfig): AxiosPromise<IDavinciResponse<object>>
  37. export function request (config: AxiosRequestConfig): AxiosPromise<IDavinciResponse<object>>
  38. export default function request (url: string | AxiosRequestConfig, options?: AxiosRequestConfig): AxiosPromise<IDavinciResponse<object>> {
  39. const axiosPromise =
  40. typeof url === 'string' ? axios(url, options) : axios(url)
  41. return axiosPromise
  42. .then(refreshToken)
  43. .then(parseJSON)
  44. }
  45. export function setToken (token: string) {
  46. localStorage.setItem('TOKEN', token)
  47. localStorage.setItem('TOKEN_EXPIRE', `${new Date().getTime() + tokenExpired}`)
  48. axios.defaults.headers.common['Authorization'] = `${token}`
  49. }
  50. function syncToken (e: StorageEvent) {
  51. const { key, newValue } = e
  52. if (key !== 'TOKEN') { return }
  53. if (!newValue) {
  54. delete axios.defaults.headers.common['Authorization']
  55. } else {
  56. axios.defaults.headers.common['Authorization'] = `${newValue}`
  57. }
  58. }
  59. export function removeToken () {
  60. localStorage.removeItem('TOKEN')
  61. localStorage.removeItem('TOKEN_EXPIRE')
  62. delete axios.defaults.headers.common['Authorization']
  63. }
  64. export function getToken () {
  65. return axios.defaults.headers.common['Authorization']
  66. }
  67. export function setTokenExpired(expired) {
  68. tokenExpired = Number(expired)
  69. }
  70. window.addEventListener('storage', syncToken)
  71. interface IDavinciResponseHeader {
  72. code: number
  73. msg: string
  74. token: string
  75. }
  76. export interface IDavinciResponse<T> {
  77. header: IDavinciResponseHeader,
  78. payload: T
  79. }