Validate before saving

This commit is contained in:
Adria Navarro 2025-03-19 15:50:18 +01:00
parent 71aecfb51b
commit 36060af731
3 changed files with 36 additions and 6 deletions

View File

@ -70,12 +70,28 @@
return keepOpen
}
const { data: configData } = validationResult
try {
const connectionValidation = await oauth2.validate({
id: config?.id,
url: configData.url,
clientId: configData.clientId,
clientSecret: configData.clientSecret,
})
if (!connectionValidation.valid) {
let message = "Connection settings could not be validated"
if (connectionValidation.message) {
message += `: ${connectionValidation.message}`
}
notifications.error(message)
return keepOpen
}
if (isCreation) {
await oauth2.create(validationResult.data)
await oauth2.create(configData)
notifications.success("Settings created.")
} else {
await oauth2.edit(config!.id, validationResult.data)
await oauth2.edit(config!.id, configData)
notifications.success("Settings saved.")
}
} catch (e: any) {

View File

@ -1,6 +1,7 @@
import { API } from "@/api"
import { BudiStore } from "@/stores/BudiStore"
import { OAuth2Config, UpsertOAuth2Config } from "@/types"
import { ValidateConfigRequest } from "@budibase/types"
interface OAuth2StoreState {
configs: OAuth2Config[]
@ -57,6 +58,10 @@ export class OAuth2Store extends BudiStore<OAuth2StoreState> {
await API.oauth2.delete(id)
await this.fetch()
}
async validate(config: ValidateConfigRequest) {
return await API.oauth2.validate(config)
}
}
export const oauth2 = new OAuth2Store()

View File

@ -3,6 +3,8 @@ import {
OAuth2ConfigResponse,
UpsertOAuth2ConfigRequest,
UpsertOAuth2ConfigResponse,
ValidateConfigRequest,
ValidateConfigResponse,
} from "@budibase/types"
import { BaseAPIClient } from "./types"
@ -16,6 +18,7 @@ export interface OAuth2Endpoints {
config: UpsertOAuth2ConfigRequest
) => Promise<UpsertOAuth2ConfigResponse>
delete: (id: string) => Promise<void>
validate: (config: ValidateConfigRequest) => Promise<ValidateConfigResponse>
}
export const buildOAuth2Endpoints = (API: BaseAPIClient): OAuth2Endpoints => ({
@ -32,8 +35,6 @@ export const buildOAuth2Endpoints = (API: BaseAPIClient): OAuth2Endpoints => ({
/**
* Creates a OAuth2 configuration.
* @param name the name of the row action
* @param tableId the ID of the table
*/
create: async config => {
return await API.post<
@ -49,8 +50,6 @@ export const buildOAuth2Endpoints = (API: BaseAPIClient): OAuth2Endpoints => ({
/**
* Updates an existing OAuth2 configuration.
* @param name the name of the row action
* @param tableId the ID of the table
*/
update: async (id, config) => {
return await API.put<UpsertOAuth2ConfigRequest, UpsertOAuth2ConfigResponse>(
@ -72,4 +71,14 @@ export const buildOAuth2Endpoints = (API: BaseAPIClient): OAuth2Endpoints => ({
url: `/api/oauth2/${id}`,
})
},
validate: async function (
config: ValidateConfigRequest
): Promise<ValidateConfigResponse> {
return await API.post<ValidateConfigRequest, ValidateConfigResponse>({
url: `/api/oauth2/validate`,
body: {
...config,
},
})
},
})