import axios , { AxiosInstance , AxiosRequestConfig , AxiosResponse , CancelTokenSource } from 'axios' ; import { injectable } from 'inversify' ; import stringify from 'json-stable-stringify' ; import IRepositoryModel from './IRepositoryModel' ; type MethodType = 'GET' | 'DELETE' | 'POST' | 'PUT' ; /**
 * RepositoryModel
 * axios のラッパー
 */ @ injectable ( ) class RepositoryModel implements IRepositoryModel { private repo : AxiosInstance ; private textRepo : AxiosInstance ; private cancelSourceIndex : { [ key ] : CancelTokenSource ; } = { } ; constructor ( ) { this . repo = axios . create ( { baseURL : RepositoryModel . BASE_API , headers : { 'Content-Type' : 'application/json' , } , responseType : 'json' , } ) ; this . textRepo = axios . create ( { baseURL : RepositoryModel . BASE_API , headers : { 'Content-Type' : 'text/plain' , } , responseType : 'text' , } ) ; } param type param url param config return public get ( url , config ? ) { const key = this . setToken ( 'GET' , url , config ) ; const result = this . repo . get ( url , config ) ; delete this . cancelSourceIndex [ key ] ; return result ; } param type param url param config return public getText ( url , config ? ) { const key = this . setToken ( 'GET' , url , config ) ; const result = this . textRepo . get ( url , config ) ; delete this . cancelSourceIndex [ key ] ; return result ; } param type param url param config return public delete ( url , config ? ) { const key = this . setToken ( 'DELETE' , url , config ) ; const result = this . repo . delete ( url , config ) ; delete this . cancelSourceIndex [ key ] ; return result ; } param type param url param config return public put ( url , data ? , config ? ) { const key = this . setToken ( 'PUT' , url , config ) ; const result = this . repo . put ( url , data , config ) ; delete this . cancelSourceIndex [ key ] ; return result ; } param type param url param config return public post ( url , data ? , config ? ) { const key = this . setToken ( 'POST' , url , config ) ; const result = this . repo . post ( url , data , config ) ; delete this . cancelSourceIndex [ key ] ; return result ; } param type param url param config return private setToken ( type , url , config ? ) { const sourceKey = this . getCancelTokenKey ( type , url , config ) ; const oldTokenSource = this . cancelSourceIndex [ sourceKey ] ; if ( typeof oldTokenSource !== 'undefined' ) { oldTokenSource . cancel ( ) ; } const tokenSource = axios . CancelToken . source ( ) ; this . cancelSourceIndex [ sourceKey ] = tokenSource ; if ( typeof config === 'undefined' ) { config = { cancelToken : tokenSource . token , } ; } else { config . cancelToken = tokenSource . token ; } return sourceKey ; } param type param url param config private getCancelTokenKey ( type , url , config ? ) { let key = `${ type }:${ url }` ; if ( typeof config !== 'undefined' ) { key += `:${ stringify ( config ) }}` ; } return key ; } } namespace RepositoryModel { export const BASE_API = './api' ; } export default RepositoryModel ;