Environments data model
1000
src-tauri/Cargo.lock
generated
@@ -25,8 +25,6 @@ http = "0.2.8"
|
|||||||
reqwest = { version = "0.11.14", features = ["json"] }
|
reqwest = { version = "0.11.14", features = ["json"] }
|
||||||
tokio = { version = "1.25.0", features = ["sync"] }
|
tokio = { version = "1.25.0", features = ["sync"] }
|
||||||
futures = "0.3.26"
|
futures = "0.3.26"
|
||||||
deno_core = "0.222.0"
|
|
||||||
deno_ast = { version = "0.29.5", features = ["transpiling"] }
|
|
||||||
sqlx = { version = "0.6.2", features = ["sqlite", "runtime-tokio-rustls", "json", "chrono", "time", "offline"] }
|
sqlx = { version = "0.6.2", features = ["sqlite", "runtime-tokio-rustls", "json", "chrono", "time", "offline"] }
|
||||||
uuid = "1.3.0"
|
uuid = "1.3.0"
|
||||||
rand = "0.8.5"
|
rand = "0.8.5"
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 15 KiB |
|
Before Width: | Height: | Size: 39 KiB After Width: | Height: | Size: 39 KiB |
|
Before Width: | Height: | Size: 2.3 KiB After Width: | Height: | Size: 2.3 KiB |
|
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 17 KiB After Width: | Height: | Size: 17 KiB |
|
Before Width: | Height: | Size: 18 KiB After Width: | Height: | Size: 18 KiB |
|
Before Width: | Height: | Size: 46 KiB After Width: | Height: | Size: 46 KiB |
|
Before Width: | Height: | Size: 2.1 KiB After Width: | Height: | Size: 2.1 KiB |
|
Before Width: | Height: | Size: 52 KiB After Width: | Height: | Size: 52 KiB |
|
Before Width: | Height: | Size: 3.6 KiB After Width: | Height: | Size: 3.6 KiB |
|
Before Width: | Height: | Size: 7.0 KiB After Width: | Height: | Size: 7.0 KiB |
|
Before Width: | Height: | Size: 9.3 KiB After Width: | Height: | Size: 9.3 KiB |
|
Before Width: | Height: | Size: 4.3 KiB After Width: | Height: | Size: 4.3 KiB |
|
Before Width: | Height: | Size: 56 KiB After Width: | Height: | Size: 56 KiB |
|
Before Width: | Height: | Size: 106 KiB After Width: | Height: | Size: 106 KiB |
@@ -1 +0,0 @@
|
|||||||
Deno.core.opAsync('op_hello', 'Deno');
|
|
||||||
@@ -20,7 +20,7 @@ use reqwest::redirect::Policy;
|
|||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use sqlx::migrate::Migrator;
|
use sqlx::migrate::Migrator;
|
||||||
use sqlx::sqlite::SqlitePoolOptions;
|
use sqlx::sqlite::SqlitePoolOptions;
|
||||||
use sqlx::types::Json;
|
use sqlx::types::{Json, JsonValue};
|
||||||
use sqlx::{Pool, Sqlite};
|
use sqlx::{Pool, Sqlite};
|
||||||
use tauri::regex::Regex;
|
use tauri::regex::Regex;
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
@@ -34,7 +34,6 @@ use window_ext::WindowExt;
|
|||||||
use crate::models::generate_id;
|
use crate::models::generate_id;
|
||||||
|
|
||||||
mod models;
|
mod models;
|
||||||
mod runtime;
|
|
||||||
mod window_ext;
|
mod window_ext;
|
||||||
|
|
||||||
#[derive(serde::Serialize)]
|
#[derive(serde::Serialize)]
|
||||||
@@ -192,24 +191,6 @@ async fn actually_send_ephemeral_request(
|
|||||||
|
|
||||||
let raw_response = client.execute(sendable_req).await;
|
let raw_response = client.execute(sendable_req).await;
|
||||||
|
|
||||||
let plugin_rel_path = "plugins/plugin.ts";
|
|
||||||
let plugin_path = match app_handle.path_resolver().resolve_resource(plugin_rel_path) {
|
|
||||||
Some(p) => p,
|
|
||||||
None => {
|
|
||||||
return response_err(
|
|
||||||
response,
|
|
||||||
format!("Plugin not found at {}", plugin_rel_path),
|
|
||||||
&app_handle,
|
|
||||||
pool,
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Err(e) = runtime::run_plugin_sync(plugin_path.to_str().unwrap()) {
|
|
||||||
return response_err(response, e.to_string(), &app_handle, pool).await;
|
|
||||||
}
|
|
||||||
|
|
||||||
match raw_response {
|
match raw_response {
|
||||||
Ok(v) => {
|
Ok(v) => {
|
||||||
let mut response = response.clone();
|
let mut response = response.clone();
|
||||||
@@ -478,6 +459,29 @@ async fn requests(
|
|||||||
.map_err(|e| e.to_string())
|
.map_err(|e| e.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
async fn environments(
|
||||||
|
workspace_id: &str,
|
||||||
|
db_instance: State<'_, Mutex<Pool<Sqlite>>>,
|
||||||
|
) -> Result<Vec<models::Environment>, String> {
|
||||||
|
let pool = &*db_instance.lock().await;
|
||||||
|
let environments = models::find_environments(workspace_id, pool)
|
||||||
|
.await
|
||||||
|
.expect("Failed to find environments");
|
||||||
|
|
||||||
|
println!("");
|
||||||
|
if environments.is_empty() {
|
||||||
|
println!("CREATING DEFAULT ENVIRONMENT");
|
||||||
|
let data: HashMap<String, JsonValue> = HashMap::new();
|
||||||
|
let environment = models::create_environment(workspace_id, "Default", data, pool)
|
||||||
|
.await
|
||||||
|
.expect("Failed to create default environment");
|
||||||
|
Ok(vec![environment])
|
||||||
|
} else {
|
||||||
|
Ok(environments)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
async fn get_request(
|
async fn get_request(
|
||||||
id: &str,
|
id: &str,
|
||||||
@@ -603,10 +607,12 @@ fn main() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
.invoke_handler(tauri::generate_handler![
|
.invoke_handler(tauri::generate_handler![
|
||||||
new_window,
|
|
||||||
workspaces,
|
workspaces,
|
||||||
get_request,
|
environments,
|
||||||
requests,
|
requests,
|
||||||
|
responses,
|
||||||
|
new_window,
|
||||||
|
get_request,
|
||||||
send_request,
|
send_request,
|
||||||
send_ephemeral_request,
|
send_ephemeral_request,
|
||||||
duplicate_request,
|
duplicate_request,
|
||||||
@@ -617,7 +623,6 @@ fn main() {
|
|||||||
update_workspace,
|
update_workspace,
|
||||||
update_request,
|
update_request,
|
||||||
delete_request,
|
delete_request,
|
||||||
responses,
|
|
||||||
get_key_value,
|
get_key_value,
|
||||||
set_key_value,
|
set_key_value,
|
||||||
delete_response,
|
delete_response,
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
(function (globalThis) {
|
|
||||||
// Deno.core.print(Object.keys(Deno.core).join('\n'));
|
|
||||||
|
|
||||||
function argsToMessage(...args) {
|
|
||||||
return args.map((arg) => JSON.stringify(arg)).join(' ');
|
|
||||||
}
|
|
||||||
|
|
||||||
globalThis.console = {
|
|
||||||
log: (...args) => {
|
|
||||||
Deno.core.print(`[log]: ${argsToMessage(...args)}\n`, false);
|
|
||||||
},
|
|
||||||
error: (...args) => {
|
|
||||||
Deno.core.print(`[err]: ${argsToMessage(...args)}\n`, true);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
})(globalThis);
|
|
||||||
@@ -1,149 +0,0 @@
|
|||||||
use std::cell::RefCell;
|
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::pin::Pin;
|
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
use deno_ast::{MediaType, ParseParams, SourceTextInfo};
|
|
||||||
use deno_core::anyhow::{anyhow, bail, Error};
|
|
||||||
use deno_core::error::AnyError;
|
|
||||||
use deno_core::futures::FutureExt;
|
|
||||||
use deno_core::{
|
|
||||||
resolve_import, Extension, JsRuntime, ModuleLoader, ModuleSource, ModuleSourceFuture,
|
|
||||||
ModuleSpecifier, ModuleType, ResolutionKind, RuntimeOptions, SourceMapGetter,
|
|
||||||
};
|
|
||||||
use futures::executor;
|
|
||||||
|
|
||||||
pub fn run_plugin_sync(file_path: &str) -> Result<(), AnyError> {
|
|
||||||
executor::block_on(run_plugin(file_path))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn run_plugin(file_path: &str) -> Result<(), AnyError> {
|
|
||||||
let extension = Extension {
|
|
||||||
name: "runtime",
|
|
||||||
// ops: std::borrow::Cow::Borrowed(&[op_hello::DECL]),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let source_map_store = SourceMapStore(Rc::new(RefCell::new(HashMap::new())));
|
|
||||||
|
|
||||||
// Initialize a runtime instance
|
|
||||||
let mut runtime = JsRuntime::new(RuntimeOptions {
|
|
||||||
module_loader: Some(Rc::new(TypescriptModuleLoader {
|
|
||||||
source_maps: source_map_store.clone(),
|
|
||||||
})),
|
|
||||||
extensions: vec![extension],
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
|
|
||||||
runtime
|
|
||||||
.execute_script_static("<runtime>", include_str!("runtime.js"))
|
|
||||||
.expect("Failed to execute runtime.js");
|
|
||||||
|
|
||||||
let current_dir = &std::env::current_dir().expect("Unable to get CWD");
|
|
||||||
let main_module =
|
|
||||||
deno_core::resolve_path(file_path, current_dir).expect("Failed to resolve path");
|
|
||||||
let mod_id = runtime
|
|
||||||
.load_main_module(&main_module, None)
|
|
||||||
.await
|
|
||||||
.expect("Failed to load main module");
|
|
||||||
let result = runtime.mod_evaluate(mod_id);
|
|
||||||
runtime
|
|
||||||
.run_event_loop(false)
|
|
||||||
.await
|
|
||||||
.expect("Failed to run event loop");
|
|
||||||
result.await?
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
struct SourceMapStore(Rc<RefCell<HashMap<String, Vec<u8>>>>);
|
|
||||||
|
|
||||||
impl SourceMapGetter for SourceMapStore {
|
|
||||||
fn get_source_map(&self, specifier: &str) -> Option<Vec<u8>> {
|
|
||||||
self.0.borrow().get(specifier).cloned()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_source_line(&self, _file_name: &str, _line_number: usize) -> Option<String> {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct TypescriptModuleLoader {
|
|
||||||
source_maps: SourceMapStore,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ModuleLoader for TypescriptModuleLoader {
|
|
||||||
fn resolve(
|
|
||||||
&self,
|
|
||||||
specifier: &str,
|
|
||||||
referrer: &str,
|
|
||||||
_kind: ResolutionKind,
|
|
||||||
) -> Result<ModuleSpecifier, Error> {
|
|
||||||
Ok(resolve_import(specifier, referrer)?)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn load(
|
|
||||||
&self,
|
|
||||||
module_specifier: &ModuleSpecifier,
|
|
||||||
_maybe_referrer: Option<&ModuleSpecifier>,
|
|
||||||
_is_dyn_import: bool,
|
|
||||||
) -> Pin<Box<ModuleSourceFuture>> {
|
|
||||||
let source_maps = self.source_maps.clone();
|
|
||||||
fn load(
|
|
||||||
source_maps: SourceMapStore,
|
|
||||||
module_specifier: &ModuleSpecifier,
|
|
||||||
) -> Result<ModuleSource, AnyError> {
|
|
||||||
let path = module_specifier
|
|
||||||
.to_file_path()
|
|
||||||
.map_err(|_| anyhow!("Only file:// URLs are supported."))?;
|
|
||||||
|
|
||||||
let media_type = MediaType::from_path(&path);
|
|
||||||
let (module_type, should_transpile) = match MediaType::from_path(&path) {
|
|
||||||
MediaType::JavaScript | MediaType::Mjs | MediaType::Cjs => {
|
|
||||||
(ModuleType::JavaScript, false)
|
|
||||||
}
|
|
||||||
MediaType::Jsx => (ModuleType::JavaScript, true),
|
|
||||||
MediaType::TypeScript
|
|
||||||
| MediaType::Mts
|
|
||||||
| MediaType::Cts
|
|
||||||
| MediaType::Dts
|
|
||||||
| MediaType::Dmts
|
|
||||||
| MediaType::Dcts
|
|
||||||
| MediaType::Tsx => (ModuleType::JavaScript, true),
|
|
||||||
MediaType::Json => (ModuleType::Json, false),
|
|
||||||
_ => bail!("Unknown extension {:?}", path.extension()),
|
|
||||||
};
|
|
||||||
|
|
||||||
let code = std::fs::read_to_string(&path)?;
|
|
||||||
let code = if should_transpile {
|
|
||||||
let parsed = deno_ast::parse_module(ParseParams {
|
|
||||||
specifier: module_specifier.to_string(),
|
|
||||||
text_info: SourceTextInfo::from_string(code),
|
|
||||||
media_type,
|
|
||||||
capture_tokens: false,
|
|
||||||
scope_analysis: false,
|
|
||||||
maybe_syntax: None,
|
|
||||||
})?;
|
|
||||||
let res = parsed.transpile(&deno_ast::EmitOptions {
|
|
||||||
inline_source_map: false,
|
|
||||||
source_map: true,
|
|
||||||
inline_sources: true,
|
|
||||||
..Default::default()
|
|
||||||
})?;
|
|
||||||
let source_map = res.source_map.unwrap();
|
|
||||||
source_maps
|
|
||||||
.0
|
|
||||||
.borrow_mut()
|
|
||||||
.insert(module_specifier.to_string(), source_map.into_bytes());
|
|
||||||
res.text
|
|
||||||
} else {
|
|
||||||
code
|
|
||||||
};
|
|
||||||
Ok(ModuleSource::new(
|
|
||||||
module_type,
|
|
||||||
code.into(),
|
|
||||||
module_specifier,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
|
|
||||||
futures::future::ready(load(source_maps, module_specifier)).boxed_local()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
},
|
},
|
||||||
"package": {
|
"package": {
|
||||||
"productName": "Yaak",
|
"productName": "Yaak",
|
||||||
"version": "2023.0.19"
|
"version": "2023.0.20"
|
||||||
},
|
},
|
||||||
"tauri": {
|
"tauri": {
|
||||||
"windows": [],
|
"windows": [],
|
||||||
@@ -18,8 +18,10 @@
|
|||||||
"all": true
|
"all": true
|
||||||
},
|
},
|
||||||
"protocol": {
|
"protocol": {
|
||||||
"assetScope": ["$APPDATA/responses/*"],
|
"assetScope": [
|
||||||
"asset": true
|
"$APPDATA/responses/*"
|
||||||
|
],
|
||||||
|
"asset": true
|
||||||
},
|
},
|
||||||
"fs": {
|
"fs": {
|
||||||
"readFile": true,
|
"readFile": true,
|
||||||
@@ -51,7 +53,6 @@
|
|||||||
"identifier": "co.schier.yaak",
|
"identifier": "co.schier.yaak",
|
||||||
"longDescription": "The best cross-platform visual API client",
|
"longDescription": "The best cross-platform visual API client",
|
||||||
"resources": [
|
"resources": [
|
||||||
"plugins/*",
|
|
||||||
"migrations/*"
|
"migrations/*"
|
||||||
],
|
],
|
||||||
"shortDescription": "The best API client",
|
"shortDescription": "The best API client",
|
||||||
@@ -76,8 +77,7 @@
|
|||||||
"timestampUrl": ""
|
"timestampUrl": ""
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"security": {
|
"security": {},
|
||||||
},
|
|
||||||
"systemTray": {
|
"systemTray": {
|
||||||
"iconAsTemplate": true,
|
"iconAsTemplate": true,
|
||||||
"iconPath": "icons/icon.png"
|
"iconPath": "icons/icon.png"
|
||||||
|
|||||||
@@ -83,12 +83,12 @@ export const WorkspaceActionsDropdown = memo(function WorkspaceDropdown({ classN
|
|||||||
workspaces.length <= 1
|
workspaces.length <= 1
|
||||||
? []
|
? []
|
||||||
: [
|
: [
|
||||||
...workspaceItems,
|
...workspaceItems,
|
||||||
{
|
{
|
||||||
type: 'separator',
|
type: 'separator',
|
||||||
label: activeWorkspace?.name,
|
label: activeWorkspace?.name,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
return [
|
return [
|
||||||
...activeWorkspaceItems,
|
...activeWorkspaceItems,
|
||||||
|
|||||||
22
src-web/hooks/useEnvironments.ts
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import { useQuery } from '@tanstack/react-query';
|
||||||
|
import { invoke } from '@tauri-apps/api';
|
||||||
|
import type { Environment } from '../lib/models';
|
||||||
|
import { useActiveWorkspaceId } from './useActiveWorkspaceId';
|
||||||
|
|
||||||
|
export function environmentsQueryKey({ workspaceId }: { workspaceId: string }) {
|
||||||
|
return ['environments', { workspaceId }];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useEnvironments() {
|
||||||
|
const workspaceId = useActiveWorkspaceId();
|
||||||
|
return (
|
||||||
|
useQuery({
|
||||||
|
enabled: workspaceId != null,
|
||||||
|
queryKey: environmentsQueryKey({ workspaceId: workspaceId ?? 'n/a' }),
|
||||||
|
queryFn: async () => {
|
||||||
|
if (workspaceId == null) return [];
|
||||||
|
return (await invoke('environments', { workspaceId })) as Environment[];
|
||||||
|
},
|
||||||
|
}).data ?? []
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,3 +1,15 @@
|
|||||||
|
|
||||||
|
export const BODY_TYPE_NONE = null;
|
||||||
|
export const BODY_TYPE_GRAPHQL = 'graphql';
|
||||||
|
export const BODY_TYPE_JSON = 'application/json';
|
||||||
|
export const BODY_TYPE_XML = 'text/xml';
|
||||||
|
|
||||||
|
export const AUTH_TYPE_NONE = null;
|
||||||
|
export const AUTH_TYPE_BASIC = 'basic';
|
||||||
|
export const AUTH_TYPE_BEARER = 'bearer';
|
||||||
|
|
||||||
|
export type Model = Workspace | HttpRequest | HttpResponse | KeyValue | Environment;
|
||||||
|
|
||||||
export interface BaseModel {
|
export interface BaseModel {
|
||||||
readonly id: string;
|
readonly id: string;
|
||||||
readonly createdAt: string;
|
readonly createdAt: string;
|
||||||
@@ -16,25 +28,11 @@ export interface HttpHeader {
|
|||||||
enabled?: boolean;
|
enabled?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const BODY_TYPE_NONE = null;
|
export interface Environment extends BaseModel {
|
||||||
export const BODY_TYPE_GRAPHQL = 'graphql';
|
readonly workspaceId: string;
|
||||||
export const BODY_TYPE_JSON = 'application/json';
|
readonly model: 'environment';
|
||||||
export const BODY_TYPE_XML = 'text/xml';
|
name: string;
|
||||||
|
data: Record<string, string | number | boolean | null | undefined>;
|
||||||
export const AUTH_TYPE_NONE = null;
|
|
||||||
export const AUTH_TYPE_BASIC = 'basic';
|
|
||||||
export const AUTH_TYPE_BEARER = 'bearer';
|
|
||||||
|
|
||||||
export type Model = Workspace | HttpRequest | HttpResponse | KeyValue;
|
|
||||||
|
|
||||||
export function modelsEq(a: Model, b: Model) {
|
|
||||||
if (a.model === 'key_value' && b.model === 'key_value') {
|
|
||||||
return a.key === b.key && a.namespace === b.namespace;
|
|
||||||
}
|
|
||||||
if ('id' in a && 'id' in b) {
|
|
||||||
return a.id === b.id;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface HttpRequest extends BaseModel {
|
export interface HttpRequest extends BaseModel {
|
||||||
@@ -78,3 +76,13 @@ export interface HttpResponse extends BaseModel {
|
|||||||
export function isResponseLoading(response: HttpResponse): boolean {
|
export function isResponseLoading(response: HttpResponse): boolean {
|
||||||
return !(response.body || response.status || response.error);
|
return !(response.body || response.status || response.error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function modelsEq(a: Model, b: Model) {
|
||||||
|
if (a.model === 'key_value' && b.model === 'key_value') {
|
||||||
|
return a.key === b.key && a.namespace === b.namespace;
|
||||||
|
}
|
||||||
|
if ('id' in a && 'id' in b) {
|
||||||
|
return a.id === b.id;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|||||||