forked from tsai/budibase
13 changed files with 266 additions and 11 deletions
@ -0,0 +1,61 @@ |
|||
import { ERROR } from "../state/standardState"; |
|||
import {loadRecord} from "./loadRecord"; |
|||
import {listRecords} from "./listRecords"; |
|||
|
|||
export const createApi = ({rootPath, setState, getState}) => { |
|||
|
|||
const apiCall = (method) => ({url, body, notFound, badRequest, forbidden}) => { |
|||
fetch(url, { |
|||
method: method, |
|||
headers: { |
|||
'Content-Type': 'application/json', |
|||
}, |
|||
body: body && JSON.stringify(body), |
|||
credentials: "same-origin" |
|||
}).then(r => { |
|||
switch (r.status) { |
|||
case 200: |
|||
return r.json(); |
|||
case 404: |
|||
return error(notFound || `${url} Not found`); |
|||
case 400: |
|||
return error(badRequest || `${url} Bad Request`); |
|||
case 403: |
|||
return error(forbidden || `${url} Forbidden`); |
|||
default: |
|||
if(r.status.toString().startsWith("2") |
|||
|| r.status.toString().startsWith("3")) |
|||
return r.json() |
|||
else |
|||
return error(`${url} - ${r.statusText}`); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
const post = apiCall("POST"); |
|||
const get = apiCall("GET"); |
|||
const patch = apiCall("PATCH"); |
|||
const del = apiCall("DELETE"); |
|||
|
|||
const ERROR_MEMBER = "##error"; |
|||
const error = message => { |
|||
const e = {}; |
|||
e[ERROR_MEMBER] = message; |
|||
setState(ERROR, message); |
|||
return e; |
|||
} |
|||
|
|||
const isSuccess = obj => !!obj[ERROR_MEMBER]; |
|||
|
|||
const apiOpts = { |
|||
rootPath, setState, getState, isSuccess, error, |
|||
post, get, patch, delete:del |
|||
}; |
|||
|
|||
return { |
|||
loadRecord:loadRecord(apiOpts), |
|||
listRecords: listRecords(api) |
|||
} |
|||
} |
|||
|
|||
|
|||
@ -0,0 +1,18 @@ |
|||
export const listRecords = api => async ({indexKey, statePath}) => { |
|||
if(!recordKey) { |
|||
api.error("Load Record: record key not set"); |
|||
return; |
|||
} |
|||
|
|||
if(!statePath) { |
|||
api.error("Load Record: state path not set"); |
|||
return; |
|||
} |
|||
|
|||
const records = get({ |
|||
url:`${rootPath}/api/listRecords/${indexKey}` |
|||
}); |
|||
|
|||
if(api.isSuccess(records)) |
|||
api.setState(statePath, records); |
|||
} |
|||
@ -0,0 +1,21 @@ |
|||
|
|||
|
|||
export const loadRecord = (api) => async ({recordKey, statePath}) => { |
|||
|
|||
if(!recordKey) { |
|||
api.error("Load Record: record key not set"); |
|||
return; |
|||
} |
|||
|
|||
if(!statePath) { |
|||
api.error("Load Record: state path not set"); |
|||
return; |
|||
} |
|||
|
|||
const record = await get({ |
|||
url:`${rootPath}/api/record/${key}` |
|||
}); |
|||
|
|||
if(api.isSuccess(record)) |
|||
api.setState(statePath, record); |
|||
} |
|||
@ -0,0 +1,29 @@ |
|||
|
|||
|
|||
export const saveRecord = (api) => async ({statePath}) => { |
|||
|
|||
if(!statePath) { |
|||
api.error("Load Record: state path not set"); |
|||
return; |
|||
} |
|||
|
|||
const recordtoSave = api.getState(statePath); |
|||
|
|||
if(!recordtoSave) { |
|||
api.error(`there is no record in state: ${statePath}`); |
|||
return; |
|||
} |
|||
|
|||
if(!recordtoSave.key) { |
|||
api.error(`item in state does not appear to be a record - it has no key (${statePath})`); |
|||
return; |
|||
} |
|||
|
|||
const savedRecord = await post({ |
|||
url:`${rootPath}/api/record/${recordtoSave.key}`, |
|||
body: recordtoSave |
|||
}); |
|||
|
|||
if(api.isSuccess(record)) |
|||
api.setState(statePath, savedRecord); |
|||
} |
|||
@ -0,0 +1,15 @@ |
|||
|
|||
|
|||
export const createCoreApp = (appDefinition, user) => { |
|||
const app = { |
|||
datastore: null, |
|||
crypto:null, |
|||
publish: () => {}, |
|||
hierarchy: appDefinition.hierarchy, |
|||
actions: appDefinition.actions |
|||
}; |
|||
|
|||
app.asUser(user); |
|||
|
|||
return app; |
|||
} |
|||
@ -0,0 +1,17 @@ |
|||
import { createCoreApp } from "./createCoreApp" |
|||
import { |
|||
getNew, getNewChild |
|||
} from "../../../core/src/recordApi/getNew"; |
|||
|
|||
export const createCoreApi = (appDefinition, user) => { |
|||
|
|||
const app = createCoreApp(appDefinition, user); |
|||
|
|||
return { |
|||
recordApi: { |
|||
getNew: getNew(app), |
|||
getNewChild: getNewChild(app) |
|||
} |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,63 @@ |
|||
import { ERROR } from "./standardState"; |
|||
|
|||
export const getNewChildRecordToState = (store, coreApi) => |
|||
({recordKey, collectionName,childRecordType,statePath}) => { |
|||
const error = errorHandler(setState); |
|||
try { |
|||
if(!recordKey) { |
|||
error("getNewChild > recordKey not set"); |
|||
return; |
|||
} |
|||
|
|||
if(!collectionName) { |
|||
error("getNewChild > collectionName not set"); |
|||
return; |
|||
} |
|||
|
|||
if(!childRecordType) { |
|||
error("getNewChild > childRecordType not set"); |
|||
return; |
|||
} |
|||
|
|||
if(!statePath) { |
|||
error("getNewChild > statePath not set"); |
|||
return; |
|||
} |
|||
|
|||
const rec = coreApi.recordApi.getNewChild(recordKey, collectionName, childRecordType); |
|||
setState(store, statePath, rec); |
|||
} |
|||
catch(e) { |
|||
error(e.message); |
|||
} |
|||
} |
|||
|
|||
|
|||
export const getNewRecordToState = (store, coreApi) => |
|||
({collectionKey,childRecordType,statePath}) => { |
|||
const error = errorHandler(setState); |
|||
try { |
|||
if(!collectionKey) { |
|||
error("getNewChild > collectionKey not set"); |
|||
return; |
|||
} |
|||
|
|||
if(!childRecordType) { |
|||
error("getNewChild > childRecordType not set"); |
|||
return; |
|||
} |
|||
|
|||
if(!statePath) { |
|||
error("getNewChild > statePath not set"); |
|||
return; |
|||
} |
|||
|
|||
const rec = coreApi.recordApi.getNew(collectionKey, childRecordType); |
|||
setState(store, statePath, rec); |
|||
} |
|||
catch(e) { |
|||
error(e.message); |
|||
} |
|||
} |
|||
|
|||
const errorHandler = setState => message => setState(ERROR, message); |
|||
@ -0,0 +1,2 @@ |
|||
|
|||
export const ERROR = "##error_message"; |
|||
Loading…
Reference in new issue