instance Id is gone from API module

This commit is contained in:
Michael Shanks 2020-06-19 17:19:30 +01:00
parent d7b09f5b65
commit 4dcd482a4f
13 changed files with 33 additions and 57 deletions

View File

@ -1,10 +1,6 @@
const apiCall = (method, instanceId) => async (url, body) => {
const apiCall = method => async (url, body) => {
const headers = {
"Content-Type": "application/json",
"x-user-agent": "Budibase Builder",
}
if (instanceId) {
headers["x-budibase-instanceid"] = instanceId
}
const response = await fetch(url, {
method: method,
@ -21,20 +17,10 @@ export const patch = apiCall("PATCH")
export const del = apiCall("DELETE")
export const put = apiCall("PUT")
// usage: api(instanceId).post(...) ... will supply instance Id in header
const api = instanceId => ({
post: apiCall("POST", instanceId),
get: apiCall("GET", instanceId),
patch: apiCall("PATCH", instanceId),
delete: apiCall("DELETE", instanceId),
put: apiCall("PUT", instanceId),
})
// usage: api.post(...)... will not supply instanceid in header
api.post = apiCall("POST")
api.get = apiCall("GET")
api.patch = apiCall("PATCH")
api.delete = apiCall("DELETE")
api.put = apiCall("PUT")
export default api
export default {
post: apiCall("POST"),
get: apiCall("GET"),
patch: apiCall("PATCH"),
delete: apiCall("DELETE"),
put: apiCall("PUT"),
}

View File

@ -24,8 +24,8 @@ export const getBackendUiStore = () => {
store.actions = {
database: {
select: async db => {
const modelsResponse = await api(db._id).get(`/api/models`)
const viewsResponse = await api(db._id).get(`/api/views`)
const modelsResponse = await api.get(`/api/models`)
const viewsResponse = await api.get(`/api/views`)
const models = await modelsResponse.json()
const views = await viewsResponse.json()
store.update(state => {

View File

@ -12,7 +12,7 @@ const workflowActions = store => ({
return state
})
},
create: async ({ instanceId, name }) => {
create: async ({ name }) => {
const workflow = {
name,
definition: {
@ -20,7 +20,7 @@ const workflowActions = store => ({
},
}
const CREATE_WORKFLOW_URL = `/api/workflows`
const response = await api(instanceId).post(CREATE_WORKFLOW_URL, workflow)
const response = await api.post(CREATE_WORKFLOW_URL, workflow)
const json = await response.json()
store.update(state => {
state.workflows = state.workflows.concat(json.workflow)
@ -28,9 +28,9 @@ const workflowActions = store => ({
return state
})
},
save: async ({ instanceId, workflow }) => {
save: async ({ workflow }) => {
const UPDATE_WORKFLOW_URL = `/api/workflows`
const response = await api(instanceId).put(UPDATE_WORKFLOW_URL, workflow)
const response = await api.put(UPDATE_WORKFLOW_URL, workflow)
const json = await response.json()
store.update(state => {
const existingIdx = state.workflows.findIndex(
@ -42,9 +42,9 @@ const workflowActions = store => ({
return state
})
},
update: async ({ instanceId, workflow }) => {
update: async ({ workflow }) => {
const UPDATE_WORKFLOW_URL = `/api/workflows`
const response = await api(instanceId).put(UPDATE_WORKFLOW_URL, workflow)
const response = await api.put(UPDATE_WORKFLOW_URL, workflow)
const json = await response.json()
store.update(state => {
const existingIdx = state.workflows.findIndex(
@ -55,10 +55,10 @@ const workflowActions = store => ({
return state
})
},
delete: async ({ instanceId, workflow }) => {
delete: async ({ workflow }) => {
const { _id, _rev } = workflow
const DELETE_WORKFLOW_URL = `/api/workflows/${_id}/${_rev}`
await api(instanceId).delete(DELETE_WORKFLOW_URL)
await api.delete(DELETE_WORKFLOW_URL)
store.update(state => {
const existingIdx = state.workflows.findIndex(

View File

@ -53,12 +53,10 @@
let views = []
let currentPage = 0
$: instanceId = $backendUiStore.selectedDatabase._id
$: {
if ($backendUiStore.selectedView) {
api
.fetchDataForView($backendUiStore.selectedView, instanceId)
.fetchDataForView($backendUiStore.selectedView)
.then(records => {
data = records || []
headers = Object.keys($backendUiStore.selectedModel.schema).filter(

View File

@ -1,8 +1,8 @@
import api from "builderStore/api"
export async function createUser(user, instanceId) {
export async function createUser(user) {
const CREATE_USER_URL = `/api/users`
const response = await api(instanceId).post(CREATE_USER_URL, user)
const response = await api.post(CREATE_USER_URL, user)
return await response.json()
}
@ -14,22 +14,22 @@ export async function createDatabase(appname, instanceName) {
return await response.json()
}
export async function deleteRecord(record, instanceId) {
export async function deleteRecord(record) {
const DELETE_RECORDS_URL = `/api/${record._modelId}/records/${record._id}/${record._rev}`
const response = await api(instanceId).delete(DELETE_RECORDS_URL)
const response = await api.delete(DELETE_RECORDS_URL)
return response
}
export async function saveRecord(record, instanceId, modelId) {
export async function saveRecord(record, modelId) {
const SAVE_RECORDS_URL = `/api/${modelId}/records`
const response = await api(instanceId).post(SAVE_RECORDS_URL, record)
const response = await api.post(SAVE_RECORDS_URL, record)
return await response.json()
}
export async function fetchDataForView(viewName, instanceId) {
export async function fetchDataForView(viewName) {
const FETCH_RECORDS_URL = `/api/views/${viewName}`
const response = await api(instanceId).get(FETCH_RECORDS_URL)
const response = await api.get(FETCH_RECORDS_URL)
return await response.json()
}

View File

@ -18,7 +18,6 @@
let fieldToEdit
$: modelFields = model.schema ? Object.entries(model.schema) : []
$: instanceId = $backendUiStore.selectedDatabase._id
function editField() {}
@ -28,7 +27,7 @@
async function saveModel() {
const SAVE_MODEL_URL = `/api/models`
const response = await api(instanceId).post(SAVE_MODEL_URL, model)
const response = await api.post(SAVE_MODEL_URL, model)
const newModel = await response.json()
backendUiStore.actions.models.create(newModel)
onClosed()

View File

@ -14,8 +14,6 @@
let errors = []
let selectedModel
$: instanceId = $backendUiStore.selectedDatabase._id
$: modelSchema = $backendUiStore.selectedModel
? Object.entries($backendUiStore.selectedModel.schema)
: []
@ -49,7 +47,6 @@
...record,
modelId: $backendUiStore.selectedModel._id,
},
instanceId,
$backendUiStore.selectedModel._id
)
if (recordResponse.errors) {

View File

@ -30,7 +30,7 @@
async function saveView() {
const SAVE_VIEW_URL = `/api/views`
const response = await api(instanceId).post(SAVE_VIEW_URL, view)
const response = await api.post(SAVE_VIEW_URL, view)
backendUiStore.update(state => {
state.views = [...state.views, response.view]
return state

View File

@ -10,12 +10,11 @@
let accessLevelId
$: valid = username && password && accessLevelId
$: instanceId = $backendUiStore.selectedDatabase._id
$: appId = $store.appId
async function createUser() {
const user = { name: username, username, password, accessLevelId }
const response = await api.createUser(user, instanceId)
const response = await api.createUser(user)
backendUiStore.actions.users.create(response)
onClosed()
}

View File

@ -6,7 +6,6 @@
export let record
export let onClosed
$: instanceId = $backendUiStore.selectedDatabase._id
</script>
<section>
@ -25,7 +24,7 @@
<ActionButton
alert
on:click={async () => {
await api.deleteRecord(record, instanceId)
await api.deleteRecord(record)
backendUiStore.actions.records.delete(record)
onClosed()
}}>

View File

@ -53,7 +53,7 @@
async function deleteModel(modelToDelete) {
const DELETE_MODEL_URL = `/api/models/${node._id}/${node._rev}`
const response = await api(instanceId).delete(DELETE_MODEL_URL)
const response = await api.delete(DELETE_MODEL_URL)
backendUiStore.update(state => {
state.models = state.models.filter(
model => model._id !== modelToDelete._id

View File

@ -12,12 +12,11 @@
$: currentAppInfo = {
appname: $store.appname,
instanceId: $backendUiStore.selectedDatabase._id,
}
async function fetchUsers() {
const FETCH_USERS_URL = `/api/users`
const response = await api(currentAppInfo.instanceId).get(FETCH_USERS_URL)
const response = await api.get(FETCH_USERS_URL)
const users = await response.json()
backendUiStore.update(state => {
state.users = users

View File

@ -37,7 +37,6 @@ exports.defaultHeaders = (appId, instanceId) => {
return {
Accept: "application/json",
Cookie: [`builder:token=${builderToken}`],
"x-user-agent": "Budibase Builder",
}
}