Pass types through context callbacks.
This commit is contained in:
parent
47292b8ab4
commit
e3a4c34f8d
|
@ -19,7 +19,7 @@ async function populateFromDB(appId: string) {
|
||||||
return doWithDB(
|
return doWithDB(
|
||||||
appId,
|
appId,
|
||||||
(db: Database) => {
|
(db: Database) => {
|
||||||
return db.get(DocumentType.APP_METADATA)
|
return db.get<App>(DocumentType.APP_METADATA)
|
||||||
},
|
},
|
||||||
{ skip_setup: true }
|
{ skip_setup: true }
|
||||||
)
|
)
|
||||||
|
|
|
@ -4,7 +4,7 @@ import { ContextMap } from "./types"
|
||||||
export default class Context {
|
export default class Context {
|
||||||
static storage = new AsyncLocalStorage<ContextMap>()
|
static storage = new AsyncLocalStorage<ContextMap>()
|
||||||
|
|
||||||
static run(context: ContextMap, func: any) {
|
static run<T>(context: ContextMap, func: () => T) {
|
||||||
return Context.storage.run(context, () => func())
|
return Context.storage.run(context, () => func())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -98,17 +98,17 @@ function updateContext(updates: ContextMap): ContextMap {
|
||||||
return context
|
return context
|
||||||
}
|
}
|
||||||
|
|
||||||
async function newContext(updates: ContextMap, task: any) {
|
async function newContext<T>(updates: ContextMap, task: () => T) {
|
||||||
// see if there already is a context setup
|
// see if there already is a context setup
|
||||||
let context: ContextMap = updateContext(updates)
|
let context: ContextMap = updateContext(updates)
|
||||||
return Context.run(context, task)
|
return Context.run(context, task)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function doInAutomationContext(params: {
|
export async function doInAutomationContext<T>(params: {
|
||||||
appId: string
|
appId: string
|
||||||
automationId: string
|
automationId: string
|
||||||
task: any
|
task: () => T
|
||||||
}): Promise<any> {
|
}): Promise<T> {
|
||||||
const tenantId = getTenantIDFromAppID(params.appId)
|
const tenantId = getTenantIDFromAppID(params.appId)
|
||||||
return newContext(
|
return newContext(
|
||||||
{
|
{
|
||||||
|
@ -144,10 +144,10 @@ export async function doInTenant<T>(
|
||||||
return newContext(updates, task)
|
return newContext(updates, task)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function doInAppContext(
|
export async function doInAppContext<T>(
|
||||||
appId: string | null,
|
appId: string | null,
|
||||||
task: any
|
task: () => T
|
||||||
): Promise<any> {
|
): Promise<T> {
|
||||||
if (!appId && !env.isTest()) {
|
if (!appId && !env.isTest()) {
|
||||||
throw new Error("appId is required")
|
throw new Error("appId is required")
|
||||||
}
|
}
|
||||||
|
@ -165,10 +165,10 @@ export async function doInAppContext(
|
||||||
return newContext(updates, task)
|
return newContext(updates, task)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function doInIdentityContext(
|
export async function doInIdentityContext<T>(
|
||||||
identity: IdentityContext,
|
identity: IdentityContext,
|
||||||
task: any
|
task: () => T
|
||||||
): Promise<any> {
|
): Promise<T> {
|
||||||
if (!identity) {
|
if (!identity) {
|
||||||
throw new Error("identity is required")
|
throw new Error("identity is required")
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { directCouchQuery, DatabaseImpl } from "./couch"
|
import { directCouchQuery, DatabaseImpl } from "./couch"
|
||||||
import { CouchFindOptions, Database } from "@budibase/types"
|
import { CouchFindOptions, Database, DatabaseOpts } from "@budibase/types"
|
||||||
|
|
||||||
export function getDB(dbName: string, opts?: any): Database {
|
export function getDB(dbName: string, opts?: DatabaseOpts): Database {
|
||||||
return new DatabaseImpl(dbName, opts)
|
return new DatabaseImpl(dbName, opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ export function getDB(dbName: string, opts?: any): Database {
|
||||||
export async function doWithDB<T>(
|
export async function doWithDB<T>(
|
||||||
dbName: string,
|
dbName: string,
|
||||||
cb: (db: Database) => Promise<T>,
|
cb: (db: Database) => Promise<T>,
|
||||||
opts = {}
|
opts?: DatabaseOpts
|
||||||
) {
|
) {
|
||||||
const db = getDB(dbName, opts)
|
const db = getDB(dbName, opts)
|
||||||
// need this to be async so that we can correctly close DB after all
|
// need this to be async so that we can correctly close DB after all
|
||||||
|
|
|
@ -94,7 +94,7 @@ export async function externalTrigger(
|
||||||
automation: Automation,
|
automation: Automation,
|
||||||
params: { fields: Record<string, any>; timeout?: number },
|
params: { fields: Record<string, any>; timeout?: number },
|
||||||
{ getResponses }: { getResponses?: boolean } = {}
|
{ getResponses }: { getResponses?: boolean } = {}
|
||||||
) {
|
): Promise<any> {
|
||||||
if (
|
if (
|
||||||
automation.definition != null &&
|
automation.definition != null &&
|
||||||
automation.definition.trigger != null &&
|
automation.definition.trigger != null &&
|
||||||
|
|
|
@ -510,13 +510,14 @@ class TestConfiguration {
|
||||||
// create dev app
|
// create dev app
|
||||||
// clear any old app
|
// clear any old app
|
||||||
this.appId = null
|
this.appId = null
|
||||||
await context.doInAppContext(null, async () => {
|
this.app = await context.doInAppContext(null, async () => {
|
||||||
this.app = await this._req(
|
const app = await this._req(
|
||||||
{ name: appName },
|
{ name: appName },
|
||||||
null,
|
null,
|
||||||
controllers.app.create
|
controllers.app.create
|
||||||
)
|
)
|
||||||
this.appId = this.app?.appId!
|
this.appId = app.appId!
|
||||||
|
return app
|
||||||
})
|
})
|
||||||
return await context.doInAppContext(this.appId, async () => {
|
return await context.doInAppContext(this.appId, async () => {
|
||||||
// create production app
|
// create production app
|
||||||
|
@ -525,7 +526,7 @@ class TestConfiguration {
|
||||||
this.allApps.push(this.prodApp)
|
this.allApps.push(this.prodApp)
|
||||||
this.allApps.push(this.app)
|
this.allApps.push(this.app)
|
||||||
|
|
||||||
return this.app
|
return this.app!
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -537,7 +538,7 @@ class TestConfiguration {
|
||||||
|
|
||||||
return context.doInAppContext(prodAppId, async () => {
|
return context.doInAppContext(prodAppId, async () => {
|
||||||
const db = context.getProdAppDB()
|
const db = context.getProdAppDB()
|
||||||
return await db.get(dbCore.DocumentType.APP_METADATA)
|
return await db.get<App>(dbCore.DocumentType.APP_METADATA)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -241,7 +241,7 @@ class Orchestrator {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async execute() {
|
async execute(): Promise<any> {
|
||||||
// this will retrieve from context created at start of thread
|
// this will retrieve from context created at start of thread
|
||||||
this._context.env = await sdkUtils.getEnvironmentVariables()
|
this._context.env = await sdkUtils.getEnvironmentVariables()
|
||||||
let automation = this._automation
|
let automation = this._automation
|
||||||
|
|
Loading…
Reference in New Issue