import FakeRest from 'fakerest' ; import { DataProvider } from 'ra-core' ; function log ( type , resource , params , response ) { if ( console . group ) { console . groupCollapsed ( type , resource , JSON . stringify ( params ) ) ; console . log ( response ) ; console . groupEnd ( ) ; } else { console . log ( 'FakeRest request ' , type , resource , params ) ; console . log ( 'FakeRest response' , response ) ; } } example export default ( data , loggingEnabled = false ) : DataProvider => { const restServer = new FakeRest . Server ( ) ; restServer . init ( data ) ; if ( typeof window !== 'undefined' ) { ( window as any ) . restServer = restServer ; } function getResponse ( type , resource , params ) { switch ( type ) { case 'getList' : { const { page , perPage } = params . pagination ; const { field , order } = params . sort ; const query = { sort : [ field , order ] , range : [ ( page - 1 ) * perPage , page * perPage - 1 ] , filter : params . filter , } ; return { data : restServer . getAll ( resource , query ) , total : restServer . getCount ( resource , { filter : params . filter , } ) , } ; } case 'getOne' : return { data : restServer . getOne ( resource , params . id , { ... params } ) , } ; case 'getMany' : return { data : restServer . getAll ( resource , { filter : { id : params . ids } , } ) , } ; case 'getManyReference' : { const { page , perPage } = params . pagination ; const { field , order } = params . sort ; const query = { sort : [ field , order ] , range : [ ( page - 1 ) * perPage , page * perPage - 1 ] , filter : { ... params . filter , [ params . target ] : params . id } , } ; return { data : restServer . getAll ( resource , query ) , total : restServer . getCount ( resource , { filter : query . filter , } ) , } ; } case 'update' : return { data : restServer . updateOne ( resource , params . id , { ... params . data , } ) , } ; case 'updateMany' : params . ids . forEach ( id => restServer . updateOne ( resource , id , { ... params . data , } ) ) ; return { data : params . ids } ; case 'create' : return { data : restServer . addOne ( resource , { ... params . data } ) , } ; case 'delete' : return { data : restServer . removeOne ( resource , params . id ) } ; case 'deleteMany' : params . ids . forEach ( id => restServer . removeOne ( resource , id ) ) ; return { data : params . ids } ; default : return false ; } } param { String } type param { String } resource param { Object } params returns { Promise } const handle = ( type , resource , params ) : Promise < any > => { const collection = restServer . getCollection ( resource ) ; if ( ! collection && type !== 'create' ) { const error = new UndefinedResourceError ( `Undefined collection "${ resource }"` ) ; error . code = 1 ; return Promise . reject ( error ) ; } let response ; try { response = getResponse ( type , resource , params ) ; } catch ( error ) { console . error ( error ) ; return Promise . reject ( error ) ; } if ( loggingEnabled ) { log ( type , resource , params , response ) ; } return Promise . resolve ( response ) ; } ; return { getList : ( resource , params ) => handle ( 'getList' , resource , params ) , getOne : ( resource , params ) => handle ( 'getOne' , resource , params ) , getMany : ( resource , params ) => handle ( 'getMany' , resource , params ) , getManyReference : ( resource , params ) => handle ( 'getManyReference' , resource , params ) , update : ( resource , params ) => handle ( 'update' , resource , params ) , updateMany : ( resource , params ) => handle ( 'updateMany' , resource , params ) , create : ( resource , params ) => handle ( 'create' , resource , params ) , delete : ( resource , params ) => handle ( 'delete' , resource , params ) , deleteMany : ( resource , params ) => handle ( 'deleteMany' , resource , params ) , } ; } ; class UndefinedResourceError extends Error { code : number ; }