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 = {}) => {
|
||||
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 {
|
||||
Database,
|
||||
DocumentType,
|
||||
OAuth2Config,
|
||||
OAuth2Configs,
|
||||
PASSWORD_REPLACEMENT,
|
||||
SEPARATOR,
|
||||
VirtualDocumentType,
|
||||
WithRequired,
|
||||
} from "@budibase/types"
|
||||
|
||||
async function getDocument(db: Database = context.getAppDB()) {
|
||||
const result = await db.tryGet<OAuth2Configs>(DocumentType.OAUTH2_CONFIG)
|
||||
return result
|
||||
async function guardName(name: string, id?: string) {
|
||||
const existingConfigs = await fetch()
|
||||
|
||||
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[]> {
|
||||
const result = await getDocument()
|
||||
if (!result) {
|
||||
return []
|
||||
}
|
||||
return Object.values(result.configs)
|
||||
const db = context.getAppDB()
|
||||
const docs = await db.allDocs<OAuth2Config>(docIds.getOAuth2ConfigParams())
|
||||
const result = docs.rows.map(r => r.doc!)
|
||||
return result
|
||||
}
|
||||
|
||||
export async function create(
|
||||
config: Omit<OAuth2Config, "id">
|
||||
): Promise<OAuth2Config> {
|
||||
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)) {
|
||||
throw new HTTPError("Name already used", 400)
|
||||
}
|
||||
await guardName(config.name)
|
||||
|
||||
const id = `${VirtualDocumentType.OAUTH2_CONFIG}${SEPARATOR}${utils.newid()}`
|
||||
doc.configs[id] = {
|
||||
id,
|
||||
const response = await db.put({
|
||||
id: `${DocumentType.OAUTH2_CONFIG}${SEPARATOR}${utils.newid()}`,
|
||||
...config,
|
||||
})
|
||||
return {
|
||||
_id: response.id,
|
||||
_rev: response.rev,
|
||||
...config,
|
||||
}
|
||||
|
||||
await db.put(doc)
|
||||
return doc.configs[id]
|
||||
}
|
||||
|
||||
export async function get(id: string): Promise<OAuth2Config | undefined> {
|
||||
const doc = await getDocument()
|
||||
return doc?.configs?.[id]
|
||||
const db = context.getAppDB()
|
||||
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 doc: OAuth2Configs = (await getDocument(db)) ?? {
|
||||
_id: DocumentType.OAUTH2_CONFIG,
|
||||
configs: {},
|
||||
await guardName(config.name, config._id)
|
||||
|
||||
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]) {
|
||||
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] = {
|
||||
const toUpdate = {
|
||||
...config,
|
||||
clientSecret:
|
||||
config.clientSecret === PASSWORD_REPLACEMENT
|
||||
? doc.configs[config.id].clientSecret
|
||||
? existing.clientSecret
|
||||
: config.clientSecret,
|
||||
}
|
||||
|
||||
await db.put(doc)
|
||||
return doc.configs[config.id]
|
||||
const result = await db.put(toUpdate)
|
||||
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 doc: OAuth2Configs = (await getDocument(db)) ?? {
|
||||
_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)
|
||||
await db.remove(configId, _rev)
|
||||
}
|
||||
|
|
|
@ -5,15 +5,10 @@ export enum OAuth2CredentialsMethod {
|
|||
BODY = "BODY",
|
||||
}
|
||||
|
||||
export interface OAuth2Config {
|
||||
id: string
|
||||
export interface OAuth2Config extends Document {
|
||||
name: string
|
||||
url: string
|
||||
clientId: string
|
||||
clientSecret: string
|
||||
method: OAuth2CredentialsMethod
|
||||
}
|
||||
|
||||
export interface OAuth2Configs extends Document {
|
||||
configs: Record<string, OAuth2Config>
|
||||
}
|
||||
|
|
|
@ -82,7 +82,6 @@ export enum InternalTable {
|
|||
export enum VirtualDocumentType {
|
||||
VIEW = "view",
|
||||
ROW_ACTION = "row_action",
|
||||
OAUTH2_CONFIG = "oauth2",
|
||||
}
|
||||
|
||||
// Because VirtualDocumentTypes can overlap, we need to make sure that we search
|
||||
|
|
Loading…
Reference in New Issue