mirror of https://github.com/Budibase/budibase.git
38 changed files with 634 additions and 318 deletions
@ -1,37 +1,27 @@ |
|||
import { writable } from "svelte/store" |
|||
import api from "builderStore/api" |
|||
import { API } from "api" |
|||
|
|||
export function createFlagsStore() { |
|||
const { subscribe, set } = writable({}) |
|||
|
|||
return { |
|||
subscribe, |
|||
const actions = { |
|||
fetch: async () => { |
|||
const { doc, response } = await getFlags() |
|||
set(doc) |
|||
return response |
|||
const flags = await API.getFlags() |
|||
set(flags) |
|||
}, |
|||
updateFlag: async (flag, value) => { |
|||
const response = await api.post("/api/users/flags", { |
|||
await API.updateFlag({ |
|||
flag, |
|||
value, |
|||
}) |
|||
if (response.status === 200) { |
|||
const { doc } = await getFlags() |
|||
set(doc) |
|||
} |
|||
return response |
|||
await actions.fetch() |
|||
}, |
|||
} |
|||
} |
|||
|
|||
async function getFlags() { |
|||
const response = await api.get("/api/users/flags") |
|||
let doc = {} |
|||
if (response.status === 200) { |
|||
doc = await response.json() |
|||
return { |
|||
subscribe, |
|||
...actions, |
|||
} |
|||
return { doc, response } |
|||
} |
|||
|
|||
export const flags = createFlagsStore() |
|||
|
|||
@ -1,30 +1,32 @@ |
|||
import { writable } from "svelte/store" |
|||
import api from "builderStore/api" |
|||
import { API } from "api" |
|||
|
|||
export function createRolesStore() { |
|||
const { subscribe, update, set } = writable([]) |
|||
|
|||
return { |
|||
subscribe, |
|||
const actions = { |
|||
fetch: async () => { |
|||
set(await getRoles()) |
|||
const roles = await API.getRoles() |
|||
set(roles) |
|||
}, |
|||
delete: async role => { |
|||
const response = await api.delete(`/api/roles/${role._id}/${role._rev}`) |
|||
await API.deleteRole({ |
|||
roleId: role?._id, |
|||
roleRev: role?._rev, |
|||
}) |
|||
update(state => state.filter(existing => existing._id !== role._id)) |
|||
return response |
|||
}, |
|||
save: async role => { |
|||
const response = await api.post("/api/roles", role) |
|||
set(await getRoles()) |
|||
return response |
|||
const savedRole = await API.saveRole(role) |
|||
await actions.fetch() |
|||
return savedRole |
|||
}, |
|||
} |
|||
} |
|||
|
|||
async function getRoles() { |
|||
const response = await api.get("/api/roles") |
|||
return await response.json() |
|||
return { |
|||
subscribe, |
|||
...actions, |
|||
} |
|||
} |
|||
|
|||
export const roles = createRolesStore() |
|||
|
|||
@ -0,0 +1,57 @@ |
|||
export const buildDatasourceEndpoints = API => ({ |
|||
/** |
|||
* Gets a list of datasources. |
|||
*/ |
|||
getDatasources: async () => { |
|||
return await API.get({ |
|||
url: "/api/datasources", |
|||
}) |
|||
}, |
|||
|
|||
/** |
|||
* Prompts the server to build the schema for a datasource. |
|||
* @param datasourceId the datasource ID to build the schema for |
|||
*/ |
|||
buildDatasourceSchema: async datasourceId => { |
|||
return await API.post({ |
|||
url: `/api/datasources/${datasourceId}/schema`, |
|||
}) |
|||
}, |
|||
|
|||
/** |
|||
* Creates a datasource |
|||
* @param datasource the datasource to create |
|||
* @param fetchSchema whether to fetch the schema or not |
|||
*/ |
|||
createDatasource: async ({ datasource, fetchSchema }) => { |
|||
return await API.post({ |
|||
url: "/api/datasources", |
|||
body: { |
|||
datasource, |
|||
fetchSchema, |
|||
}, |
|||
}) |
|||
}, |
|||
|
|||
/** |
|||
* Updates a datasource |
|||
* @param datasource the datasource to update |
|||
*/ |
|||
updateDatasource: async datasource => { |
|||
return await API.put({ |
|||
url: `/api/datasources/${datasource._id}`, |
|||
body: datasource, |
|||
}) |
|||
}, |
|||
|
|||
/** |
|||
* Deletes a datasource. |
|||
* @param datasourceId the ID of the ddtasource to delete |
|||
* @param datasourceRev the rev of the datasource to delete |
|||
*/ |
|||
deleteDatasource: async ({ datasourceId, datasourceRev }) => { |
|||
return await API.delete({ |
|||
url: `/api/datasources/${datasourceId}/${datasourceRev}`, |
|||
}) |
|||
}, |
|||
}) |
|||
@ -0,0 +1,25 @@ |
|||
export const buildFlagEndpoints = API => ({ |
|||
/** |
|||
* Gets the current user flags object. |
|||
*/ |
|||
getFlags: async () => { |
|||
return await API.get({ |
|||
url: "/api/users/flags", |
|||
}) |
|||
}, |
|||
|
|||
/** |
|||
* Updates a flag for the current user. |
|||
* @param flag the flag to update |
|||
* @param value the value to set the flag to |
|||
*/ |
|||
updateFlag: async ({ flag, value }) => { |
|||
return await API.post({ |
|||
url: "/api/users/flags", |
|||
body: { |
|||
flag, |
|||
value, |
|||
}, |
|||
}) |
|||
}, |
|||
}) |
|||
@ -0,0 +1,24 @@ |
|||
export const buildPermissionsEndpoints = API => ({ |
|||
/** |
|||
* Gets the permission required to access a specific resource |
|||
* @param resourceId the resource ID to check |
|||
*/ |
|||
getPermissionForResource: async resourceId => { |
|||
return await API.get({ |
|||
url: `/api/permission/${resourceId}`, |
|||
}) |
|||
}, |
|||
|
|||
/** |
|||
* Updates the permissions for a certain resource |
|||
* @param resourceId the ID of the resource to update |
|||
* @param roleId the ID of the role to update the permissions of |
|||
* @param level the level to assign the role for this resource |
|||
* @return {Promise<*>} |
|||
*/ |
|||
updatePermissionForResource: async ({ resourceId, roleId, level }) => { |
|||
return await API.post({ |
|||
url: `/api/permission/${roleId}/${resourceId}/${level}`, |
|||
}) |
|||
}, |
|||
}) |
|||
@ -0,0 +1,32 @@ |
|||
export const buildRoleEndpoints = API => ({ |
|||
/** |
|||
* Deletes a role. |
|||
* @param roleId the ID of the role to delete |
|||
* @param roleRev the rev of the role to delete |
|||
*/ |
|||
deleteRole: async ({ roleId, roleRev }) => { |
|||
return await API.delete({ |
|||
url: `/api/roles/${roleId}/${roleRev}`, |
|||
}) |
|||
}, |
|||
|
|||
/** |
|||
* Saves a role. |
|||
* @param role the role to save |
|||
*/ |
|||
saveRole: async role => { |
|||
return await API.post({ |
|||
url: "/api/roles", |
|||
body: role, |
|||
}) |
|||
}, |
|||
|
|||
/** |
|||
* Gets a list of roles. |
|||
*/ |
|||
getRoles: async () => { |
|||
return await API.get({ |
|||
url: "/api/roles", |
|||
}) |
|||
}, |
|||
}) |
|||
@ -0,0 +1,28 @@ |
|||
export const buildTemplateEndpoints = API => ({ |
|||
/** |
|||
* Gets the list of email template definitions. |
|||
*/ |
|||
getEmailTemplateDefinitions: async () => { |
|||
return await API.get({ url: "/api/global/template/definitions" }) |
|||
}, |
|||
|
|||
/** |
|||
* Gets the list of email templates. |
|||
*/ |
|||
getEmailTemplates: async () => { |
|||
return await API.get({ url: "/api/global/template/email" }) |
|||
}, |
|||
|
|||
/** |
|||
* Saves an email template. |
|||
* @param template the template to save |
|||
*/ |
|||
saveEmailTemplate: async template => { |
|||
return await API.post({ |
|||
url: "/api/global/template", |
|||
body: { |
|||
template, |
|||
}, |
|||
}) |
|||
}, |
|||
}) |
|||
Loading…
Reference in new issue