simple-localstorage-data-service-stub
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -11,3 +11,2 @@ const getData = (url = '') => { | ||
const writeData = (data) => { | ||
console.log({data}) | ||
window.localStorage.setItem('farmyChallengeData', JSON.stringify(data)) | ||
@@ -24,16 +23,25 @@ } | ||
actionNames = data ? Object.keys(actions): []; | ||
console.log({data, resourceNames, actionNames}) | ||
} | ||
let storedData = window.localStorage.getItem('farmyChallengeData'); | ||
const initData = () => { | ||
return new Promise((resolve, reject) => { | ||
let storedData = window.localStorage.getItem('farmyChallengeData'); | ||
if (storedData) | ||
processResponse(storedData); | ||
else getData('data/savedData.json').then(r => { | ||
if (r?.length) | ||
processResponse(r); | ||
else | ||
getData('data/initialData.json').then(r => processResponse(r)) | ||
}); | ||
if (storedData) { | ||
processResponse(storedData); | ||
resolve(storedData); | ||
} | ||
else getData('data/savedData.json').then(r => { | ||
if (r?.length) { | ||
processResponse(r); | ||
resolve(r); | ||
} | ||
else | ||
getData('data/initialData.json').then(r => { | ||
processResponse(r); | ||
resolve(r); | ||
}) | ||
}).catch(e => console.error(e)); | ||
}) | ||
} | ||
@@ -56,24 +64,26 @@ const permissions = { | ||
let [resource, id] = url.split('/'); | ||
let dataPool = data[resource]; | ||
if (data) { | ||
let [resource, id] = url.split('/'); | ||
let dataPool = data[resource]; | ||
if (action === 'get') | ||
action = id ? 'show' : 'index'; | ||
console.log({resource, id, action, dataPool, payload, data}); | ||
if (action === 'get') | ||
action = id ? 'show' : 'index'; | ||
if (!dataPool) { | ||
reject(Error('Resource not found')); | ||
return; | ||
} | ||
let hasAction = !!actions[action]; | ||
let response = hasAction && hasPermission(resource, action) ? | ||
actions[action]({dataPool, resource, id, payload, data}) : | ||
null; | ||
if (!dataPool) { | ||
reject(Error('Resource not found')); | ||
return; | ||
} | ||
if (response) { | ||
resolve(response); | ||
let hasAction = !!actions[action]; | ||
let response = hasAction && hasPermission(resource, action) ? | ||
actions[action]({dataPool, resource, id, payload, data}) : | ||
null; | ||
if (response) { | ||
resolve(response); | ||
} else { | ||
reject(Error(hasAction ? 'Permission Denied' : 'Resource not found')) | ||
} | ||
} else { | ||
reject(Error(hasAction ? 'Permission Denied' : 'Resource not found')) | ||
initData().then(r => route({url, action, payload})) | ||
} | ||
@@ -80,0 +90,0 @@ }) |
{ | ||
"name": "simple-localstorage-data-service-stub", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "REST-API-esque stub service for frontend experiments", | ||
"main": "data-service.js", | ||
"files": [ | ||
"data" | ||
], | ||
"scripts": { | ||
@@ -10,0 +7,0 @@ "test": "echo \"Error: no test specified\" && exit 1" |
11101
437