import fakeRestProvider from 'ra-data-fakerest' ; import { cacheDataProviderProxy } from 'react-admin' ; import data from './data' ; import addUploadFeature from './addUploadFeature' ; const dataProvider = fakeRestProvider ( data , true ) ; const uploadCapableDataProvider = addUploadFeature ( dataProvider ) ; const sometimesFailsDataProvider = new Proxy ( uploadCapableDataProvider , { get : ( target , name ) => ( resource , params ) => { if ( localStorage . getItem ( 'session_ended' ) ) { const error = new Error ( 'Session ended' ) as ResponseError ; error . status = 403 ; return Promise . reject ( error ) ; } if ( resource === 'posts' && params . data && params . data . title === 'f00bar' ) { return Promise . reject ( new Error ( 'this title cannot be used' ) ) ; } return uploadCapableDataProvider [ name ] ( resource , params ) ; } , } ) ; const delayedDataProvider = new Proxy ( sometimesFailsDataProvider , { get : ( target , name ) => ( resource , params ) => new Promise ( resolve => setTimeout ( ( ) => resolve ( sometimesFailsDataProvider [ name ] ( resource , params ) ) , 300 ) ) , } ) ; interface ResponseError extends Error { status ? : number ; } export default cacheDataProviderProxy ( delayedDataProvider ) ;