From bf952f8677b9aec3b310ed2a855f6ee57bc62648 Mon Sep 17 00:00:00 2001 From: Michael Shanks Date: Thu, 18 Jun 2020 16:59:31 +0100 Subject: [PATCH] instanceid removal --- packages/builder/src/builderStore/api.js | 42 ++++++---- .../builder/src/builderStore/store/backend.js | 4 +- .../builder/src/builderStore/store/index.js | 4 - .../src/builderStore/store/workflow/index.js | 20 ++--- .../components/database/ModelDataTable/api.js | 16 ++-- .../CreateEditModel/CreateEditModel.svelte | 4 +- .../modals/CreateEditView.svelte | 4 +- .../src/components/nav/HierarchyRow.svelte | 1 - .../nav/SchemaManagementDrawer.svelte | 4 +- .../src/components/nav/UsersList.svelte | 4 +- .../AppPreview/CurrentItemPreview.svelte | 1 + .../AppPreview/iframeTemplate.js | 16 ++-- .../SetupPanel/DeleteWorkflowModal.svelte | 1 - .../workflow/SetupPanel/SetupPanel.svelte | 1 - .../WorkflowBuilder/WorkflowBuilder.svelte | 1 - .../WorkflowPanel/BlockList/BlockList.svelte | 1 - .../WorkflowList/CreateWorkflowModal.svelte | 1 - .../WorkflowList/WorkflowList.svelte | 1 - .../WorkflowPanel/WorkflowPanel.svelte | 1 - packages/client/src/render/getAppId.js | 10 ++- packages/client/tests/testAppDef.js | 7 +- .../server/src/api/controllers/accesslevel.js | 16 ++-- .../server/src/api/controllers/application.js | 19 ++++- packages/server/src/api/controllers/auth.js | 13 +++- .../server/src/api/controllers/instance.js | 10 +-- packages/server/src/api/controllers/model.js | 8 +- packages/server/src/api/controllers/record.js | 16 ++-- packages/server/src/api/controllers/static.js | 19 +++-- packages/server/src/api/controllers/user.js | 10 +-- packages/server/src/api/controllers/view.js | 6 +- .../src/api/controllers/workflow/index.js | 8 +- packages/server/src/api/index.js | 5 -- packages/server/src/api/routes/accesslevel.js | 12 +-- packages/server/src/api/routes/instance.js | 2 +- packages/server/src/api/routes/model.js | 14 ++-- packages/server/src/api/routes/record.js | 12 +-- packages/server/src/api/routes/screen.js | 10 +-- .../src/api/routes/tests/accesslevel.spec.js | 28 +++---- .../src/api/routes/tests/application.spec.js | 14 ++-- .../src/api/routes/tests/couchTestUtils.js | 78 ++++++++++++++----- .../src/api/routes/tests/instance.spec.js | 6 +- .../server/src/api/routes/tests/model.spec.js | 14 ++-- .../src/api/routes/tests/record.spec.js | 36 ++++----- .../server/src/api/routes/tests/user.spec.js | 12 +-- .../server/src/api/routes/tests/view.spec.js | 6 +- .../src/api/routes/tests/workflow.spec.js | 8 +- packages/server/src/api/routes/user.js | 16 +--- packages/server/src/api/routes/view.js | 6 +- packages/server/src/api/routes/workflow.js | 14 ++-- .../server/src/middleware/authenticated.js | 26 +++++-- packages/server/src/middleware/authorized.js | 11 +-- packages/server/src/utilities/accessLevels.js | 10 ++- .../src/utilities/builder/setBuilderToken.js | 19 +++++ .../standard-components/src/DataChart.svelte | 3 +- .../standard-components/src/DataForm.svelte | 5 +- .../standard-components/src/DataList.svelte | 3 +- .../standard-components/src/DataTable.svelte | 3 +- packages/standard-components/src/List.svelte | 3 +- 58 files changed, 358 insertions(+), 287 deletions(-) create mode 100644 packages/server/src/utilities/builder/setBuilderToken.js diff --git a/packages/builder/src/builderStore/api.js b/packages/builder/src/builderStore/api.js index 3fcd35ce28..795aaf8cd7 100644 --- a/packages/builder/src/builderStore/api.js +++ b/packages/builder/src/builderStore/api.js @@ -1,17 +1,17 @@ -const apiCall = method => async (url, body) => { +const apiCall = (method, instanceId) => 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, - headers: { - "Content-Type": "application/json", - "x-user-agent": "Budibase Builder", - }, body: body && JSON.stringify(body), + headers, }) - // if (response.status === 500) { - // throw new Error("Server Error"); - // } - return response } @@ -21,10 +21,20 @@ export const patch = apiCall("PATCH") export const del = apiCall("DELETE") export const put = apiCall("PUT") -export default { - post, - get, - patch, - delete: del, - 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 diff --git a/packages/builder/src/builderStore/store/backend.js b/packages/builder/src/builderStore/store/backend.js index 9a5a7b1fbc..6efb980506 100644 --- a/packages/builder/src/builderStore/store/backend.js +++ b/packages/builder/src/builderStore/store/backend.js @@ -24,8 +24,8 @@ export const getBackendUiStore = () => { store.actions = { database: { select: async db => { - const modelsResponse = await api.get(`/api/${db._id}/models`) - const viewsResponse = await api.get(`/api/${db._id}/views`) + const modelsResponse = await api(db._id).get(`/api/models`) + const viewsResponse = await api(db._id).get(`/api/views`) const models = await modelsResponse.json() const views = await viewsResponse.json() store.update(state => { diff --git a/packages/builder/src/builderStore/store/index.js b/packages/builder/src/builderStore/store/index.js index 1f3ea9c0ef..0b17e8c61c 100644 --- a/packages/builder/src/builderStore/store/index.js +++ b/packages/builder/src/builderStore/store/index.js @@ -1,5 +1,4 @@ import { values } from "lodash/fp" -import { backendUiStore } from "builderStore" import * as backendStoreActions from "./backend" import { writable, get } from "svelte/store" import api from "../api" @@ -295,13 +294,10 @@ const addChildComponent = store => (componentToAdd, presetName) => { const presetProps = presetName ? component.presets[presetName] : {} - const instanceId = get(backendUiStore).selectedDatabase._id - const newComponent = createProps( component, { ...presetProps, - _instanceId: instanceId, }, state ) diff --git a/packages/builder/src/builderStore/store/workflow/index.js b/packages/builder/src/builderStore/store/workflow/index.js index 8d5e63b197..7c814c27ee 100644 --- a/packages/builder/src/builderStore/store/workflow/index.js +++ b/packages/builder/src/builderStore/store/workflow/index.js @@ -4,8 +4,8 @@ import Workflow from "./Workflow" const workflowActions = store => ({ fetch: async instanceId => { - const WORKFLOWS_URL = `/api/${instanceId}/workflows` - const workflowResponse = await api.get(WORKFLOWS_URL) + const WORKFLOWS_URL = `/api/workflows` + const workflowResponse = await api(instanceId).get(WORKFLOWS_URL) const json = await workflowResponse.json() store.update(state => { state.workflows = json @@ -19,8 +19,8 @@ const workflowActions = store => ({ steps: [], }, } - const CREATE_WORKFLOW_URL = `/api/${instanceId}/workflows` - const response = await api.post(CREATE_WORKFLOW_URL, workflow) + const CREATE_WORKFLOW_URL = `/api/workflows` + const response = await api(instanceId).post(CREATE_WORKFLOW_URL, workflow) const json = await response.json() store.update(state => { state.workflows = state.workflows.concat(json.workflow) @@ -29,8 +29,8 @@ const workflowActions = store => ({ }) }, save: async ({ instanceId, workflow }) => { - const UPDATE_WORKFLOW_URL = `/api/${instanceId}/workflows` - const response = await api.put(UPDATE_WORKFLOW_URL, workflow) + const UPDATE_WORKFLOW_URL = `/api/workflows` + const response = await api(instanceId).put(UPDATE_WORKFLOW_URL, workflow) const json = await response.json() store.update(state => { const existingIdx = state.workflows.findIndex( @@ -43,8 +43,8 @@ const workflowActions = store => ({ }) }, update: async ({ instanceId, workflow }) => { - const UPDATE_WORKFLOW_URL = `/api/${instanceId}/workflows` - const response = await api.put(UPDATE_WORKFLOW_URL, workflow) + const UPDATE_WORKFLOW_URL = `/api/workflows` + const response = await api(instanceId).put(UPDATE_WORKFLOW_URL, workflow) const json = await response.json() store.update(state => { const existingIdx = state.workflows.findIndex( @@ -57,8 +57,8 @@ const workflowActions = store => ({ }, delete: async ({ instanceId, workflow }) => { const { _id, _rev } = workflow - const DELETE_WORKFLOW_URL = `/api/${instanceId}/workflows/${_id}/${_rev}` - await api.delete(DELETE_WORKFLOW_URL) + const DELETE_WORKFLOW_URL = `/api/workflows/${_id}/${_rev}` + await api(instanceId).delete(DELETE_WORKFLOW_URL) store.update(state => { const existingIdx = state.workflows.findIndex( diff --git a/packages/builder/src/components/database/ModelDataTable/api.js b/packages/builder/src/components/database/ModelDataTable/api.js index cb98879567..9fb6651983 100644 --- a/packages/builder/src/components/database/ModelDataTable/api.js +++ b/packages/builder/src/components/database/ModelDataTable/api.js @@ -1,8 +1,8 @@ import api from "builderStore/api" export async function createUser(user, instanceId) { - const CREATE_USER_URL = `/api/${instanceId}/users` - const response = await api.post(CREATE_USER_URL, user) + const CREATE_USER_URL = `/api/users` + const response = await api(instanceId).post(CREATE_USER_URL, user) return await response.json() } @@ -15,21 +15,21 @@ export async function createDatabase(appname, instanceName) { } export async function deleteRecord(record, instanceId) { - const DELETE_RECORDS_URL = `/api/${instanceId}/${record._modelId}/records/${record._id}/${record._rev}` - const response = await api.delete(DELETE_RECORDS_URL) + const DELETE_RECORDS_URL = `/api/${record._modelId}/records/${record._id}/${record._rev}` + const response = await api(instanceId).delete(DELETE_RECORDS_URL) return response } export async function saveRecord(record, instanceId, modelId) { - const SAVE_RECORDS_URL = `/api/${instanceId}/${modelId}/records` - const response = await api.post(SAVE_RECORDS_URL, record) + const SAVE_RECORDS_URL = `/api/${modelId}/records` + const response = await api(instanceId).post(SAVE_RECORDS_URL, record) return await response.json() } export async function fetchDataForView(viewName, instanceId) { - const FETCH_RECORDS_URL = `/api/${instanceId}/views/${viewName}` + const FETCH_RECORDS_URL = `/api/views/${viewName}` - const response = await api.get(FETCH_RECORDS_URL) + const response = await api(instanceId).get(FETCH_RECORDS_URL) return await response.json() } diff --git a/packages/builder/src/components/database/ModelDataTable/modals/CreateEditModel/CreateEditModel.svelte b/packages/builder/src/components/database/ModelDataTable/modals/CreateEditModel/CreateEditModel.svelte index 6264b74e09..6fb6cc8a2f 100644 --- a/packages/builder/src/components/database/ModelDataTable/modals/CreateEditModel/CreateEditModel.svelte +++ b/packages/builder/src/components/database/ModelDataTable/modals/CreateEditModel/CreateEditModel.svelte @@ -27,8 +27,8 @@ function onFinishedFieldEdit() {} async function saveModel() { - const SAVE_MODEL_URL = `/api/${instanceId}/models` - const response = await api.post(SAVE_MODEL_URL, model) + const SAVE_MODEL_URL = `/api/models` + const response = await api(instanceId).post(SAVE_MODEL_URL, model) const newModel = await response.json() backendUiStore.actions.models.create(newModel) onClosed() diff --git a/packages/builder/src/components/database/ModelDataTable/modals/CreateEditView.svelte b/packages/builder/src/components/database/ModelDataTable/modals/CreateEditView.svelte index 4db445e54f..6f9f5dac1a 100644 --- a/packages/builder/src/components/database/ModelDataTable/modals/CreateEditView.svelte +++ b/packages/builder/src/components/database/ModelDataTable/modals/CreateEditView.svelte @@ -29,8 +29,8 @@ function deleteView() {} async function saveView() { - const SAVE_VIEW_URL = `/api/${instanceId}/views` - const response = await api.post(SAVE_VIEW_URL, view) + const SAVE_VIEW_URL = `/api/views` + const response = await api(instanceId).post(SAVE_VIEW_URL, view) backendUiStore.update(state => { state.views = [...state.views, response.view] return state diff --git a/packages/builder/src/components/nav/HierarchyRow.svelte b/packages/builder/src/components/nav/HierarchyRow.svelte index 8921ab74de..7653e46fd0 100644 --- a/packages/builder/src/components/nav/HierarchyRow.svelte +++ b/packages/builder/src/components/nav/HierarchyRow.svelte @@ -7,7 +7,6 @@ CreateEditModelModal, CreateEditViewModal, } from "components/database/ModelDataTable/modals" - import api from "builderStore/api" const { open, close } = getContext("simple-modal") diff --git a/packages/builder/src/components/nav/SchemaManagementDrawer.svelte b/packages/builder/src/components/nav/SchemaManagementDrawer.svelte index cc9e5d86a7..de7d010894 100644 --- a/packages/builder/src/components/nav/SchemaManagementDrawer.svelte +++ b/packages/builder/src/components/nav/SchemaManagementDrawer.svelte @@ -52,8 +52,8 @@ } async function deleteModel(modelToDelete) { - const DELETE_MODEL_URL = `/api/${instanceId}/models/${node._id}/${node._rev}` - const response = await api.delete(DELETE_MODEL_URL) + const DELETE_MODEL_URL = `/api/models/${node._id}/${node._rev}` + const response = await api(instanceId).delete(DELETE_MODEL_URL) backendUiStore.update(state => { state.models = state.models.filter( model => model._id !== modelToDelete._id diff --git a/packages/builder/src/components/nav/UsersList.svelte b/packages/builder/src/components/nav/UsersList.svelte index faef8a4234..29bc4a4ed3 100644 --- a/packages/builder/src/components/nav/UsersList.svelte +++ b/packages/builder/src/components/nav/UsersList.svelte @@ -16,8 +16,8 @@ } async function fetchUsers() { - const FETCH_USERS_URL = `/api/${currentAppInfo.instanceId}/users` - const response = await api.get(FETCH_USERS_URL) + const FETCH_USERS_URL = `/api/users` + const response = await api(currentAppInfo.instanceId).get(FETCH_USERS_URL) const users = await response.json() backendUiStore.update(state => { state.users = users diff --git a/packages/builder/src/components/userInterface/AppPreview/CurrentItemPreview.svelte b/packages/builder/src/components/userInterface/AppPreview/CurrentItemPreview.svelte index f97f91df6b..523ed25031 100644 --- a/packages/builder/src/components/userInterface/AppPreview/CurrentItemPreview.svelte +++ b/packages/builder/src/components/userInterface/AppPreview/CurrentItemPreview.svelte @@ -108,6 +108,7 @@ selectedComponentId, frontendDefinition, appId: $store.appId, + instanceId: $backendUiStore.selectedDatabase._id, }) ) } diff --git a/packages/builder/src/components/userInterface/AppPreview/iframeTemplate.js b/packages/builder/src/components/userInterface/AppPreview/iframeTemplate.js index b1bda4ab4f..c9272dddb2 100644 --- a/packages/builder/src/components/userInterface/AppPreview/iframeTemplate.js +++ b/packages/builder/src/components/userInterface/AppPreview/iframeTemplate.js @@ -20,6 +20,7 @@ export default ` font-weight: bold; } + diff --git a/packages/builder/src/components/workflow/SetupPanel/DeleteWorkflowModal.svelte b/packages/builder/src/components/workflow/SetupPanel/DeleteWorkflowModal.svelte index 9f51362419..4eed82bf25 100644 --- a/packages/builder/src/components/workflow/SetupPanel/DeleteWorkflowModal.svelte +++ b/packages/builder/src/components/workflow/SetupPanel/DeleteWorkflowModal.svelte @@ -1,7 +1,6 @@