-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
65 lines (59 loc) · 2.16 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { ResourceApi, VariableApi, ServerConfiguration } from './windmill-api/index.ts'
import { createConfiguration, type Configuration as Configuration } from './windmill-api/configuration.ts'
export {
AdminApi, AuditApi, FlowApi, GranularAclApi, GroupApi,
JobApi, ResourceApi, VariableApi, ScriptApi, ScheduleApi, SettingsApi,
UserApi, WorkspaceApi
} from './windmill-api/index.ts'
/**
* Create a client configuration from env variables
* @returns client configuration
*/
export function createConf(): Configuration & { workspace_id: string } {
const token = Deno.env.get("WM_TOKEN") ?? 'no_token'
const base_url = Deno.env.get("BASE_URL") ?? 'http://localhost:8000'
return {
...createConfiguration({
baseServer: new ServerConfiguration(`${base_url}/api`, {}),
authMethods: { bearerAuth: { tokenProvider: { getToken() { return token } } } },
}), workspace_id: Deno.env.get("WM_WORKSPACE") ?? 'no_workspace'
}
}
/**
* Get a resource value by path
* @param path path of the resource
* @returns resource value
*/
export async function getResource(path: string): Promise<any> {
const conf = createConf()
const resource = await new ResourceApi(conf).getResource(conf.workspace_id, path)
return await transformLeaves(resource.value)
}
/**
* Get a variable by path
* @param path path of the variable
* @returns variable value
*/
export async function getVariable(path: string): Promise<string | undefined> {
const conf = createConf()
const variable = await new VariableApi(conf).getVariable(conf.workspace_id, path)
return variable.value
}
async function transformLeaves(d: { [key: string]: any }): Promise<{ [key: string]: any }> {
for (const k in d) {
d[k] = await _transformLeaf(d[k])
}
return d
}
const VAR_RESOURCE_PREFIX = "$var:"
async function _transformLeaf(v: any): Promise<any> {
if (typeof v === 'object') {
return transformLeaves(v)
}
else if (typeof v === 'string' && v.startsWith(VAR_RESOURCE_PREFIX)) {
const varName = v.substring(VAR_RESOURCE_PREFIX.length)
return await getVariable(varName)
} else {
return v
}
}