@@ -110,10 +110,7 @@ export default (params?: LocalStorageDataProviderParams): DataProvider => {
110110 . update < RecordType > ( resource , params )
111111 . then ( response => {
112112 updateLocalStorage ( ( ) => {
113- const resourceData = getResourceCollection (
114- data ,
115- resource
116- ) ;
113+ const resourceData = getResourceCollection ( data , resource ) ;
117114 const index = resourceData . findIndex (
118115 record => record . id == params . id
119116 ) ;
@@ -142,32 +139,27 @@ export default (params?: LocalStorageDataProviderParams): DataProvider => {
142139 return Promise . reject ( error ) ;
143140 }
144141
145- return baseDataProvider
146- . updateMany ( resource , params )
147- . then ( response => {
148- updateLocalStorage ( ( ) => {
149- const resourceData = getResourceCollection (
150- data ,
151- resource
142+ return baseDataProvider . updateMany ( resource , params ) . then ( response => {
143+ updateLocalStorage ( ( ) => {
144+ const resourceData = getResourceCollection ( data , resource ) ;
145+ params . ids . forEach ( id => {
146+ const index = resourceData . findIndex (
147+ record => record . id == id
152148 ) ;
153- params . ids . forEach ( id => {
154- const index = resourceData . findIndex (
155- record => record . id == id
156- ) ;
157149
158- if ( index === - 1 ) {
159- return ;
160- }
150+ if ( index === - 1 ) {
151+ return ;
152+ }
161153
162- resourceData . splice ( index , 1 , {
163- ...resourceData [ index ] ,
164- ...params . data ,
165- } ) ;
154+ resourceData . splice ( index , 1 , {
155+ ...resourceData [ index ] ,
156+ ...params . data ,
166157 } ) ;
167158 } ) ;
168-
169- return response ;
170159 } ) ;
160+
161+ return response ;
162+ } ) ;
171163 } ,
172164 create : < RecordType extends Omit < RaRecord , 'id' > = any > (
173165 resource ,
@@ -201,10 +193,7 @@ export default (params?: LocalStorageDataProviderParams): DataProvider => {
201193 . delete < RecordType > ( resource , params )
202194 . then ( response => {
203195 updateLocalStorage ( ( ) => {
204- const resourceData = getResourceCollection (
205- data ,
206- resource
207- ) ;
196+ const resourceData = getResourceCollection ( data , resource ) ;
208197 const index = resourceData . findIndex (
209198 record => record . id == params . id
210199 ) ;
@@ -230,60 +219,44 @@ export default (params?: LocalStorageDataProviderParams): DataProvider => {
230219 return Promise . reject ( error ) ;
231220 }
232221
233- return baseDataProvider
234- . deleteMany ( resource , params )
235- . then ( response => {
236- updateLocalStorage ( ( ) => {
237- const resourceData = getResourceCollection (
238- data ,
239- resource
240- ) ;
241- const indexes = params . ids
242- . map ( id =>
243- resourceData . findIndex (
244- record => record . id == id
245- )
246- )
247- . filter ( index => index !== - 1 ) ;
222+ return baseDataProvider . deleteMany ( resource , params ) . then ( response => {
223+ updateLocalStorage ( ( ) => {
224+ const resourceData = getResourceCollection ( data , resource ) ;
225+ const indexes = params . ids
226+ . map ( id =>
227+ resourceData . findIndex ( record => record . id == id )
228+ )
229+ . filter ( index => index !== - 1 ) ;
248230
249- pullAt ( resourceData , indexes ) ;
250- } ) ;
251-
252- return response ;
231+ pullAt ( resourceData , indexes ) ;
253232 } ) ;
233+
234+ return response ;
235+ } ) ;
254236 } ,
255237 } ;
256238} ;
257239
258240const getResourceCollection = ( data , resource ) => {
259- const resourceData = data [ resource ] ;
260-
261- if ( ! resourceData ) {
241+ if ( ! Object . prototype . hasOwnProperty . call ( data , resource ) ) {
262242 throw new Error ( `Unknown resource key: ${ resource } ` ) ;
263243 }
264244
265- return resourceData ;
245+ return data [ resource ] ;
266246} ;
267247
268248const getOrCreateResourceCollection = ( data , resource ) => {
269- const resourceData = data [ resource ] ;
270- if ( resourceData ) {
271- return resourceData ;
249+ if ( ! Object . prototype . hasOwnProperty . call ( data , resource ) ) {
250+ data [ resource ] = [ ] ;
272251 }
273252
274- Object . defineProperty ( data , resource , {
275- value : [ ] ,
276- writable : true ,
277- enumerable : true ,
278- configurable : true ,
279- } ) ;
280-
281253 return data [ resource ] ;
282254} ;
283255
284256const checkResource = resource => {
285- if ( [ '__proto__' , 'constructor' , 'prototype' ] . includes ( resource ) ) {
286- // protection against prototype pollution
257+ // Reject "__proto__" so dynamic writes like data[resource] = value don't
258+ // mutate Object.prototype instead of creating a normal resource collection.
259+ if ( resource === '__proto__' ) {
287260 throw new Error ( `Invalid resource key: ${ resource } ` ) ;
288261 }
289262} ;
0 commit comments