diff --git a/backend/windmill-worker/src/worker.rs b/backend/windmill-worker/src/worker.rs index daeb05d9c5..d4801cd617 100644 --- a/backend/windmill-worker/src/worker.rs +++ b/backend/windmill-worker/src/worker.rs @@ -89,8 +89,8 @@ use crate::{ bash_executor::{handle_bash_job, handle_powershell_job, ANSI_ESCAPE_RE}, bun_executor::{gen_lockfile, get_trusted_deps, handle_bun_job}, common::{ - build_args_map, get_cached_resource_value_if_valid, hash_args, read_result, save_in_cache, - write_file, NO_LOGS_AT_ALL, SLOW_LOGS, + build_args_map, get_cached_resource_value_if_valid, get_reserved_variables, hash_args, + read_result, save_in_cache, write_file, NO_LOGS_AT_ALL, SLOW_LOGS, }, deno_executor::{generate_deno_lock, handle_deno_job}, go_executor::{handle_go_job, install_go_dependencies}, @@ -3203,9 +3203,16 @@ async fn handle_code_execution_job( db, ) .await; + + let reserved_variables = get_reserved_variables(job, &client.get_token().await, db).await?; + let code = format!( - "const BASE_URL = '{base_internal_url}';\nconst WM_TOKEN = '{}';\n{}", - &client.get_token().await, + "const BASE_URL = '{base_internal_url}';\nconst BASE_INTERNAL_URL = '{base_internal_url}';\n{}\n{}", + reserved_variables + .iter() + .map(|(k, v)| format!("const {} = '{}';\n", k, v)) + .collect::>() + .join("\n"), inner_content ); let (result, ts_logs) = diff --git a/frontend/src/lib/components/EditorBar.svelte b/frontend/src/lib/components/EditorBar.svelte index a74d23d9ec..ca8e55569d 100644 --- a/frontend/src/lib/components/EditorBar.svelte +++ b/frontend/src/lib/components/EditorBar.svelte @@ -79,13 +79,27 @@ let showResourcePicker = false let showResourceTypePicker = false - $: showContextVarPicker = ['python3', 'bash', 'powershell', 'go', 'deno', 'bun'].includes( - lang ?? '' - ) - $: showVarPicker = ['python3', 'bash', 'powershell', 'go', 'deno', 'bun'].includes(lang ?? '') - $: showResourcePicker = ['python3', 'bash', 'powershell', 'go', 'deno', 'bun'].includes( + $: showContextVarPicker = [ + 'python3', + 'bash', + 'powershell', + 'go', + 'deno', + 'bun', + 'nativets' + ].includes(lang ?? '') + $: showVarPicker = ['python3', 'bash', 'powershell', 'go', 'deno', 'bun', 'nativets'].includes( lang ?? '' ) + $: showResourcePicker = [ + 'python3', + 'bash', + 'powershell', + 'go', + 'deno', + 'bun', + 'nativets' + ].includes(lang ?? '') $: showResourceTypePicker = ['typescript', 'javascript'].includes(scriptLangToEditorLang(lang)) || lang === 'python3' @@ -271,6 +285,8 @@ editor.insertAtCursor(`$${name}`) } else if (lang == 'powershell') { editor.insertAtCursor(`$Env:${name}`) + } else if (lang == 'nativets') { + editor.insertAtCursor(name) } sendUserToast(`${name} inserted at cursor`) }} @@ -316,6 +332,14 @@ editor.insertAtCursor( `\nInvoke-RestMethod -Headers $Headers -Uri "$Env:BASE_INTERNAL_URL/api/w/$Env:WM_WORKSPACE/variables/get_value/${path}"` ) + } else if (lang == 'nativets') { + editor.insertAtCursor( + 'await fetch(`${BASE_INTERNAL_URL}/api/w/${WM_WORKSPACE}/variables/get_value/' + + path + + '`, {\nheaders: { Authorization: `Bearer ${WM_TOKEN}` }' + ) + editor.arrowDown() + editor.insertAtCursor('.then(res => res.json())') } sendUserToast(`${name} inserted at cursor`) }} @@ -375,6 +399,14 @@ editor.insertAtCursor( `\nInvoke-RestMethod -Headers $Headers -Uri "$Env:BASE_INTERNAL_URL/api/w/$Env:WM_WORKSPACE/resources/get_value_interpolated/${path}"` ) + } else if (lang == 'nativets') { + editor.insertAtCursor( + 'await fetch(`${BASE_INTERNAL_URL}/api/w/${WM_WORKSPACE}/resources/get_value_interpolated/' + + path + + '`, {\nheaders: { Authorization: `Bearer ${WM_TOKEN}` }' + ) + editor.arrowDown() + editor.insertAtCursor('.then(res => res.json())') } sendUserToast(`${path} inserted at cursor`) }}