Automation API typing.
This commit is contained in:
parent
b6a28bf56c
commit
b2b74aa048
|
@ -13,6 +13,22 @@ import {
|
|||
UserCtx,
|
||||
DeleteAutomationResponse,
|
||||
FetchAutomationResponse,
|
||||
GetAutomationTriggerDefinitionsResponse,
|
||||
GetAutomationStepDefinitionsResponse,
|
||||
GetAutomationActionDefinitionsResponse,
|
||||
FindAutomationResponse,
|
||||
UpdateAutomationRequest,
|
||||
UpdateAutomationResponse,
|
||||
CreateAutomationRequest,
|
||||
CreateAutomationResponse,
|
||||
SearchAutomationLogsRequest,
|
||||
SearchAutomationLogsResponse,
|
||||
ClearAutomationLogRequest,
|
||||
ClearAutomationLogResponse,
|
||||
TriggerAutomationRequest,
|
||||
TriggerAutomationResponse,
|
||||
TestAutomationRequest,
|
||||
TestAutomationResponse,
|
||||
} from "@budibase/types"
|
||||
import { getActionDefinitions as actionDefs } from "../../automations/actions"
|
||||
import sdk from "../../sdk"
|
||||
|
@ -34,7 +50,7 @@ function getTriggerDefinitions() {
|
|||
*************************/
|
||||
|
||||
export async function create(
|
||||
ctx: UserCtx<Automation, { message: string; automation: Automation }>
|
||||
ctx: UserCtx<CreateAutomationRequest, CreateAutomationResponse>
|
||||
) {
|
||||
let automation = ctx.request.body
|
||||
automation.appId = ctx.appId
|
||||
|
@ -55,7 +71,9 @@ export async function create(
|
|||
builderSocket?.emitAutomationUpdate(ctx, automation)
|
||||
}
|
||||
|
||||
export async function update(ctx: UserCtx) {
|
||||
export async function update(
|
||||
ctx: UserCtx<UpdateAutomationRequest, UpdateAutomationResponse>
|
||||
) {
|
||||
let automation = ctx.request.body
|
||||
automation.appId = ctx.appId
|
||||
|
||||
|
@ -80,7 +98,7 @@ export async function fetch(ctx: UserCtx<void, FetchAutomationResponse>) {
|
|||
ctx.body = { automations }
|
||||
}
|
||||
|
||||
export async function find(ctx: UserCtx) {
|
||||
export async function find(ctx: UserCtx<void, FindAutomationResponse>) {
|
||||
ctx.body = await sdk.automations.get(ctx.params.id)
|
||||
}
|
||||
|
||||
|
@ -96,11 +114,15 @@ export async function destroy(ctx: UserCtx<void, DeleteAutomationResponse>) {
|
|||
builderSocket?.emitAutomationDeletion(ctx, automationId)
|
||||
}
|
||||
|
||||
export async function logSearch(ctx: UserCtx) {
|
||||
export async function logSearch(
|
||||
ctx: UserCtx<SearchAutomationLogsRequest, SearchAutomationLogsResponse>
|
||||
) {
|
||||
ctx.body = await automations.logs.logSearch(ctx.request.body)
|
||||
}
|
||||
|
||||
export async function clearLogError(ctx: UserCtx) {
|
||||
export async function clearLogError(
|
||||
ctx: UserCtx<ClearAutomationLogRequest, ClearAutomationLogResponse>
|
||||
) {
|
||||
const { automationId, appId } = ctx.request.body
|
||||
await context.doInAppContext(appId, async () => {
|
||||
const db = context.getProdAppDB()
|
||||
|
@ -119,15 +141,21 @@ export async function clearLogError(ctx: UserCtx) {
|
|||
})
|
||||
}
|
||||
|
||||
export async function getActionList(ctx: UserCtx) {
|
||||
export async function getActionList(
|
||||
ctx: UserCtx<void, GetAutomationActionDefinitionsResponse>
|
||||
) {
|
||||
ctx.body = await getActionDefinitions()
|
||||
}
|
||||
|
||||
export async function getTriggerList(ctx: UserCtx) {
|
||||
export async function getTriggerList(
|
||||
ctx: UserCtx<void, GetAutomationTriggerDefinitionsResponse>
|
||||
) {
|
||||
ctx.body = getTriggerDefinitions()
|
||||
}
|
||||
|
||||
export async function getDefinitionList(ctx: UserCtx) {
|
||||
export async function getDefinitionList(
|
||||
ctx: UserCtx<void, GetAutomationStepDefinitionsResponse>
|
||||
) {
|
||||
ctx.body = {
|
||||
trigger: getTriggerDefinitions(),
|
||||
action: await getActionDefinitions(),
|
||||
|
@ -140,7 +168,9 @@ export async function getDefinitionList(ctx: UserCtx) {
|
|||
* *
|
||||
*********************/
|
||||
|
||||
export async function trigger(ctx: UserCtx) {
|
||||
export async function trigger(
|
||||
ctx: UserCtx<TriggerAutomationRequest, TriggerAutomationResponse>
|
||||
) {
|
||||
const db = context.getAppDB()
|
||||
let automation = await db.get<Automation>(ctx.params.id)
|
||||
|
||||
|
@ -185,7 +215,7 @@ export async function trigger(ctx: UserCtx) {
|
|||
}
|
||||
}
|
||||
|
||||
function prepareTestInput(input: any) {
|
||||
function prepareTestInput(input: TestAutomationRequest) {
|
||||
// prepare the test parameters
|
||||
if (input.id && input.row) {
|
||||
input.row._id = input.id
|
||||
|
@ -196,7 +226,9 @@ function prepareTestInput(input: any) {
|
|||
return input
|
||||
}
|
||||
|
||||
export async function test(ctx: UserCtx) {
|
||||
export async function test(
|
||||
ctx: UserCtx<TestAutomationRequest, TestAutomationResponse>
|
||||
) {
|
||||
const db = context.getAppDB()
|
||||
let automation = await db.get<Automation>(ctx.params.id)
|
||||
await setTestFlag(automation._id!)
|
||||
|
|
|
@ -98,7 +98,9 @@ if (env.SELF_HOSTED) {
|
|||
BUILTIN_ACTION_DEFINITIONS["EXECUTE_BASH"] = bash.definition
|
||||
}
|
||||
|
||||
export async function getActionDefinitions() {
|
||||
export async function getActionDefinitions(): Promise<
|
||||
Record<keyof typeof AutomationActionStepId, AutomationStepDefinition>
|
||||
> {
|
||||
if (await features.flags.isEnabled(FeatureFlag.AUTOMATION_BRANCHING)) {
|
||||
BUILTIN_ACTION_DEFINITIONS["BRANCH"] = branch.definition
|
||||
}
|
||||
|
|
|
@ -148,7 +148,7 @@ export async function externalTrigger(
|
|||
user?: UserBindings
|
||||
},
|
||||
{ getResponses }: { getResponses?: boolean } = {}
|
||||
): Promise<any> {
|
||||
): Promise<Record<string, any>> {
|
||||
if (automation.disabled) {
|
||||
throw new Error("Automation is disabled")
|
||||
}
|
||||
|
|
|
@ -9,9 +9,11 @@ import { cloneDeep } from "lodash/fp"
|
|||
import { quotas } from "@budibase/pro"
|
||||
import {
|
||||
Automation,
|
||||
AutomationActionStepId,
|
||||
AutomationJob,
|
||||
AutomationStepDefinition,
|
||||
AutomationTriggerDefinition,
|
||||
AutomationTriggerStepId,
|
||||
} from "@budibase/types"
|
||||
import { automationsEnabled } from "../features"
|
||||
import { helpers, REBOOT_CRON } from "@budibase/shared-core"
|
||||
|
@ -120,19 +122,21 @@ export async function updateTestHistory(
|
|||
)
|
||||
}
|
||||
|
||||
export function removeDeprecated(
|
||||
definitions: Record<
|
||||
export function removeDeprecated<
|
||||
T extends
|
||||
| Record<keyof typeof AutomationTriggerStepId, AutomationTriggerDefinition>
|
||||
| Record<keyof typeof AutomationActionStepId, AutomationStepDefinition>
|
||||
>(definitions: T): T {
|
||||
const base: Record<
|
||||
string,
|
||||
AutomationStepDefinition | AutomationTriggerDefinition
|
||||
>
|
||||
) {
|
||||
const base = cloneDeep(definitions)
|
||||
AutomationTriggerDefinition | AutomationStepDefinition
|
||||
> = cloneDeep(definitions)
|
||||
for (let key of Object.keys(base)) {
|
||||
if (base[key].deprecated) {
|
||||
delete base[key]
|
||||
}
|
||||
}
|
||||
return base
|
||||
return base as T
|
||||
}
|
||||
|
||||
// end the repetition and the job itself
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
import {
|
||||
AutomationActionStepId,
|
||||
AutomationStepDefinition,
|
||||
AutomationTriggerDefinition,
|
||||
AutomationTriggerStepId,
|
||||
} from "../../../documents"
|
||||
|
||||
export type GetAutomationTriggerDefinitionsResponse = Record<
|
||||
keyof typeof AutomationTriggerStepId,
|
||||
AutomationTriggerDefinition
|
||||
>
|
||||
|
||||
export type GetAutomationActionDefinitionsResponse = Record<
|
||||
keyof typeof AutomationActionStepId,
|
||||
AutomationStepDefinition
|
||||
>
|
||||
|
||||
export interface GetAutomationStepDefinitionsResponse {
|
||||
trigger: GetAutomationTriggerDefinitionsResponse
|
||||
action: GetAutomationActionDefinitionsResponse
|
||||
}
|
|
@ -8,3 +8,4 @@ export * from "./permission"
|
|||
export * from "./attachment"
|
||||
export * from "./user"
|
||||
export * from "./rowAction"
|
||||
export * from "./automation"
|
||||
|
|
|
@ -1,8 +1,58 @@
|
|||
import { DocumentDestroyResponse } from "@budibase/nano"
|
||||
import { Automation } from "../../documents"
|
||||
import {
|
||||
Automation,
|
||||
AutomationLogPage,
|
||||
AutomationStatus,
|
||||
Row,
|
||||
} from "../../documents"
|
||||
|
||||
export interface DeleteAutomationResponse extends DocumentDestroyResponse {}
|
||||
|
||||
export interface FetchAutomationResponse {
|
||||
automations: Automation[]
|
||||
}
|
||||
|
||||
export interface FindAutomationResponse extends Automation {}
|
||||
|
||||
export interface UpdateAutomationRequest extends Automation {}
|
||||
export interface UpdateAutomationResponse {
|
||||
message: string
|
||||
automation: Automation
|
||||
}
|
||||
|
||||
export interface CreateAutomationRequest extends Automation {}
|
||||
export interface CreateAutomationResponse {
|
||||
message: string
|
||||
automation: Automation
|
||||
}
|
||||
|
||||
export interface SearchAutomationLogsRequest {
|
||||
startDate?: string
|
||||
status?: AutomationStatus
|
||||
automationId?: string
|
||||
page?: string
|
||||
}
|
||||
export interface SearchAutomationLogsResponse extends AutomationLogPage {}
|
||||
|
||||
export interface ClearAutomationLogRequest {
|
||||
automationId: string
|
||||
appId: string
|
||||
}
|
||||
export interface ClearAutomationLogResponse {
|
||||
message: string
|
||||
}
|
||||
|
||||
export interface TriggerAutomationRequest {
|
||||
fields: Record<string, any>
|
||||
// time in seconds
|
||||
timeout: number
|
||||
}
|
||||
export type TriggerAutomationResponse = Record<string, any> | undefined
|
||||
|
||||
export interface TestAutomationRequest {
|
||||
id?: string
|
||||
revision?: string
|
||||
fields: Record<string, any>
|
||||
row?: Row
|
||||
}
|
||||
export interface TestAutomationResponse {}
|
||||
|
|
|
@ -311,6 +311,7 @@ export type AutomationStep =
|
|||
type EmptyInputs = {}
|
||||
export type AutomationStepDefinition = Omit<AutomationStep, "id" | "inputs"> & {
|
||||
inputs: EmptyInputs
|
||||
deprecated?: boolean
|
||||
}
|
||||
|
||||
export type AutomationTriggerDefinition = Omit<
|
||||
|
@ -318,6 +319,7 @@ export type AutomationTriggerDefinition = Omit<
|
|||
"id" | "inputs"
|
||||
> & {
|
||||
inputs: EmptyInputs
|
||||
deprecated?: boolean
|
||||
}
|
||||
|
||||
export type AutomationTriggerInputs<T extends AutomationTriggerStepId> =
|
||||
|
|
Loading…
Reference in New Issue