Update crud usages
This commit is contained in:
parent
0c35c2612f
commit
f0e26ecf6a
|
@ -200,3 +200,13 @@ export function getStartEndKeyURL(baseKey: any, tenantId?: string) {
|
||||||
export const getPluginParams = (pluginId?: string | null, otherProps = {}) => {
|
export const getPluginParams = (pluginId?: string | null, otherProps = {}) => {
|
||||||
return getDocParams(DocumentType.PLUGIN, pluginId, otherProps)
|
return getDocParams(DocumentType.PLUGIN, pluginId, otherProps)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets parameters for retrieving OAuth2 configs, this is a utility function for the getDocParams function.
|
||||||
|
*/
|
||||||
|
export const getOAuth2ConfigParams = (
|
||||||
|
configId?: string | null,
|
||||||
|
otherProps = {}
|
||||||
|
) => {
|
||||||
|
return getDocParams(DocumentType.OAUTH2_CONFIG, configId, otherProps)
|
||||||
|
}
|
||||||
|
|
|
@ -1,101 +1,77 @@
|
||||||
import { context, HTTPError, utils } from "@budibase/backend-core"
|
import { context, docIds, HTTPError, utils } from "@budibase/backend-core"
|
||||||
import {
|
import {
|
||||||
Database,
|
|
||||||
DocumentType,
|
DocumentType,
|
||||||
OAuth2Config,
|
OAuth2Config,
|
||||||
OAuth2Configs,
|
|
||||||
PASSWORD_REPLACEMENT,
|
PASSWORD_REPLACEMENT,
|
||||||
SEPARATOR,
|
SEPARATOR,
|
||||||
VirtualDocumentType,
|
WithRequired,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
|
|
||||||
async function getDocument(db: Database = context.getAppDB()) {
|
async function guardName(name: string, id?: string) {
|
||||||
const result = await db.tryGet<OAuth2Configs>(DocumentType.OAUTH2_CONFIG)
|
const existingConfigs = await fetch()
|
||||||
return result
|
|
||||||
|
if (existingConfigs.find(c => c.name === name && c._id !== id)) {
|
||||||
|
throw new HTTPError(
|
||||||
|
`OAuth2 config with name '${name}' is already taken.`,
|
||||||
|
400
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function fetch(): Promise<OAuth2Config[]> {
|
export async function fetch(): Promise<OAuth2Config[]> {
|
||||||
const result = await getDocument()
|
const db = context.getAppDB()
|
||||||
if (!result) {
|
const docs = await db.allDocs<OAuth2Config>(docIds.getOAuth2ConfigParams())
|
||||||
return []
|
const result = docs.rows.map(r => r.doc!)
|
||||||
}
|
return result
|
||||||
return Object.values(result.configs)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function create(
|
export async function create(
|
||||||
config: Omit<OAuth2Config, "id">
|
config: Omit<OAuth2Config, "id">
|
||||||
): Promise<OAuth2Config> {
|
): Promise<OAuth2Config> {
|
||||||
const db = context.getAppDB()
|
const db = context.getAppDB()
|
||||||
const doc: OAuth2Configs = (await getDocument(db)) ?? {
|
|
||||||
_id: DocumentType.OAUTH2_CONFIG,
|
|
||||||
configs: {},
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Object.values(doc.configs).find(c => c.name === config.name)) {
|
await guardName(config.name)
|
||||||
throw new HTTPError("Name already used", 400)
|
|
||||||
}
|
|
||||||
|
|
||||||
const id = `${VirtualDocumentType.OAUTH2_CONFIG}${SEPARATOR}${utils.newid()}`
|
const response = await db.put({
|
||||||
doc.configs[id] = {
|
id: `${DocumentType.OAUTH2_CONFIG}${SEPARATOR}${utils.newid()}`,
|
||||||
id,
|
...config,
|
||||||
|
})
|
||||||
|
return {
|
||||||
|
_id: response.id,
|
||||||
|
_rev: response.rev,
|
||||||
...config,
|
...config,
|
||||||
}
|
}
|
||||||
|
|
||||||
await db.put(doc)
|
|
||||||
return doc.configs[id]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function get(id: string): Promise<OAuth2Config | undefined> {
|
export async function get(id: string): Promise<OAuth2Config | undefined> {
|
||||||
const doc = await getDocument()
|
const db = context.getAppDB()
|
||||||
return doc?.configs?.[id]
|
return await db.tryGet(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function update(config: OAuth2Config): Promise<OAuth2Config> {
|
export async function update(
|
||||||
|
config: WithRequired<OAuth2Config, "_id" | "_rev">
|
||||||
|
): Promise<OAuth2Config> {
|
||||||
const db = context.getAppDB()
|
const db = context.getAppDB()
|
||||||
const doc: OAuth2Configs = (await getDocument(db)) ?? {
|
await guardName(config.name, config._id)
|
||||||
_id: DocumentType.OAUTH2_CONFIG,
|
|
||||||
configs: {},
|
const existing = await get(config._id)
|
||||||
|
if (!existing) {
|
||||||
|
throw new HTTPError(`OAuth2 config with id '${config._id}' not found.`, 404)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!doc.configs[config.id]) {
|
const toUpdate = {
|
||||||
throw new HTTPError(`OAuth2 config with id '${config.id}' not found.`, 404)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
Object.values(doc.configs).find(
|
|
||||||
c => c.name === config.name && c.id !== config.id
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
throw new HTTPError(
|
|
||||||
`OAuth2 config with name '${config.name}' is already taken.`,
|
|
||||||
400
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
doc.configs[config.id] = {
|
|
||||||
...config,
|
...config,
|
||||||
clientSecret:
|
clientSecret:
|
||||||
config.clientSecret === PASSWORD_REPLACEMENT
|
config.clientSecret === PASSWORD_REPLACEMENT
|
||||||
? doc.configs[config.id].clientSecret
|
? existing.clientSecret
|
||||||
: config.clientSecret,
|
: config.clientSecret,
|
||||||
}
|
}
|
||||||
|
|
||||||
await db.put(doc)
|
const result = await db.put(toUpdate)
|
||||||
return doc.configs[config.id]
|
return { ...toUpdate, _rev: result.rev }
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function remove(configId: string): Promise<void> {
|
export async function remove(configId: string, _rev: string): Promise<void> {
|
||||||
const db = context.getAppDB()
|
const db = context.getAppDB()
|
||||||
const doc: OAuth2Configs = (await getDocument(db)) ?? {
|
await db.remove(configId, _rev)
|
||||||
_id: DocumentType.OAUTH2_CONFIG,
|
|
||||||
configs: {},
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!doc.configs[configId]) {
|
|
||||||
throw new HTTPError(`OAuth2 config with id '${configId}' not found.`, 404)
|
|
||||||
}
|
|
||||||
|
|
||||||
delete doc.configs[configId]
|
|
||||||
|
|
||||||
await db.put(doc)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,15 +5,10 @@ export enum OAuth2CredentialsMethod {
|
||||||
BODY = "BODY",
|
BODY = "BODY",
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface OAuth2Config {
|
export interface OAuth2Config extends Document {
|
||||||
id: string
|
|
||||||
name: string
|
name: string
|
||||||
url: string
|
url: string
|
||||||
clientId: string
|
clientId: string
|
||||||
clientSecret: string
|
clientSecret: string
|
||||||
method: OAuth2CredentialsMethod
|
method: OAuth2CredentialsMethod
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface OAuth2Configs extends Document {
|
|
||||||
configs: Record<string, OAuth2Config>
|
|
||||||
}
|
|
||||||
|
|
|
@ -82,7 +82,6 @@ export enum InternalTable {
|
||||||
export enum VirtualDocumentType {
|
export enum VirtualDocumentType {
|
||||||
VIEW = "view",
|
VIEW = "view",
|
||||||
ROW_ACTION = "row_action",
|
ROW_ACTION = "row_action",
|
||||||
OAUTH2_CONFIG = "oauth2",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Because VirtualDocumentTypes can overlap, we need to make sure that we search
|
// Because VirtualDocumentTypes can overlap, we need to make sure that we search
|
||||||
|
|
Loading…
Reference in New Issue