This commit is contained in:
Adria Navarro 2025-03-12 15:10:22 +01:00
parent ecebfaff7a
commit 154689df32
6 changed files with 31 additions and 13 deletions
packages
server/src
api/controllers
integrations/tests
sdk/app/oauth2
tests/utilities/api
types/src
api/web/app
documents/app

View File

@ -1,5 +1,6 @@
import {
CreateOAuth2ConfigRequest,
CreateOAuth2ConfigResponse,
Ctx,
FetchOAuth2ConfigsResponse,
OAuth2Config,
@ -12,6 +13,7 @@ export async function fetch(ctx: Ctx<void, FetchOAuth2ConfigsResponse>) {
const response: FetchOAuth2ConfigsResponse = {
configs: (configs || []).map(c => ({
id: c.id,
name: c.name,
url: c.url,
})),
@ -19,12 +21,15 @@ export async function fetch(ctx: Ctx<void, FetchOAuth2ConfigsResponse>) {
ctx.body = response
}
export async function create(ctx: Ctx<CreateOAuth2ConfigRequest, void>) {
const newConfig: RequiredKeys<OAuth2Config> = {
export async function create(
ctx: Ctx<CreateOAuth2ConfigRequest, CreateOAuth2ConfigResponse>
) {
const newConfig: RequiredKeys<Omit<OAuth2Config, "id">> = {
name: ctx.request.body.name,
url: ctx.request.body.url,
}
await sdk.oauth2.create(newConfig)
const config = await sdk.oauth2.create(newConfig)
ctx.status = 201
ctx.body = { config }
}

View File

@ -5,7 +5,6 @@ import {
BasicRestAuthConfig,
BearerRestAuthConfig,
BodyType,
OAuth2RestAuthConfig,
RestAuthType,
} from "@budibase/types"
import { Response } from "node-fetch"
@ -278,11 +277,10 @@ describe("REST Integration", () => {
})
it("adds oAuth2 auth", async () => {
const oauthConfig = {
const { config: oauthConfig } = await config.api.oauth2.create({
name: generator.guid(),
url: generator.url(),
}
await config.api.oauth2.create(oauthConfig)
})
const token = generator.guid()
@ -300,7 +298,7 @@ describe("REST Integration", () => {
config.appId,
async () =>
await integration.read({
authConfigId: oauthConfig.name,
authConfigId: oauthConfig.id,
authConfigType: RestAuthType.OAUTH2,
})
)

View File

@ -1,4 +1,4 @@
import { context, HTTPError } from "@budibase/backend-core"
import { context, HTTPError, utils } from "@budibase/backend-core"
import {
Database,
DocumentType,
@ -19,7 +19,9 @@ export async function fetch(): Promise<OAuth2Config[]> {
return Object.values(result.configs)
}
export async function create(config: OAuth2Config) {
export async function create(
config: Omit<OAuth2Config, "id">
): Promise<OAuth2Config> {
const db = context.getAppDB()
const doc: OAuth2Configs = (await getDocument(db)) ?? {
_id: DocumentType.OAUTH2_CONFIG,
@ -30,8 +32,14 @@ export async function create(config: OAuth2Config) {
throw new HTTPError("Name already used", 400)
}
doc.configs[config.name] = config
const id = utils.newid()
doc.configs[id] = {
id,
...config,
}
await db.put(doc)
return doc.configs[id]
}
export async function get(id: string): Promise<OAuth2Config | undefined> {

View File

@ -1,5 +1,6 @@
import {
CreateOAuth2ConfigRequest,
CreateOAuth2ConfigResponse,
FetchOAuth2ConfigsResponse,
} from "@budibase/types"
import { Expectations, TestAPI } from "./base"
@ -15,7 +16,7 @@ export class OAuth2API extends TestAPI {
body: CreateOAuth2ConfigRequest,
expectations?: Expectations
) => {
return await this._post<CreateOAuth2ConfigRequest>("/api/oauth2", {
return await this._post<CreateOAuth2ConfigResponse>("/api/oauth2", {
body,
expectations: {
status: expectations?.status ?? 201,

View File

@ -1,4 +1,5 @@
interface OAuth2Config {
id: string
name: string
url: string
}
@ -7,4 +8,8 @@ export interface FetchOAuth2ConfigsResponse {
configs: OAuth2Config[]
}
export interface CreateOAuth2ConfigRequest extends OAuth2Config {}
export interface CreateOAuth2ConfigRequest extends Omit<OAuth2Config, "id"> {}
export interface CreateOAuth2ConfigResponse {
config: OAuth2Config
}

View File

@ -1,6 +1,7 @@
import { Document } from "../document"
export interface OAuth2Config {
id: string
name: string
url: string
}