mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-29 21:51:59 +02:00
gRPC Support (#20)
This commit is contained in:
@@ -9,6 +9,9 @@ export function trackEvent(
|
||||
| 'Workspace'
|
||||
| 'Environment'
|
||||
| 'Folder'
|
||||
| 'GrpcMessage'
|
||||
| 'GrpcConnection'
|
||||
| 'GrpcRequest'
|
||||
| 'HttpRequest'
|
||||
| 'HttpResponse'
|
||||
| 'KeyValue',
|
||||
@@ -25,7 +28,7 @@ export function trackEvent(
|
||||
| 'Duplicate',
|
||||
attributes: Record<string, string | number> = {},
|
||||
) {
|
||||
invoke('track_event', {
|
||||
invoke('cmd_track_event', {
|
||||
resource: resource,
|
||||
action,
|
||||
attributes,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { HttpRequest } from './models';
|
||||
import type { GrpcRequest, HttpRequest } from './models';
|
||||
|
||||
export function fallbackRequestName(r: HttpRequest | null): string {
|
||||
export function fallbackRequestName(r: HttpRequest | GrpcRequest | null): string {
|
||||
if (r == null) return '';
|
||||
|
||||
if (r.name) {
|
||||
@@ -9,17 +9,22 @@ export function fallbackRequestName(r: HttpRequest | null): string {
|
||||
|
||||
const withoutVariables = r.url.replace(/\$\{\[[^\]]+]}/g, '');
|
||||
if (withoutVariables.trim() === '') {
|
||||
return 'New Request';
|
||||
return r.model === 'http_request' ? 'New HTTP Request' : 'new gRPC Request';
|
||||
}
|
||||
|
||||
const fixedUrl = r.url.match(/^https?:\/\//) ? r.url : 'http://' + r.url;
|
||||
|
||||
try {
|
||||
const url = new URL(fixedUrl);
|
||||
const pathname = url.pathname === '/' ? '' : url.pathname;
|
||||
return `${url.host}${pathname}`;
|
||||
} catch (_) {
|
||||
// Nothing
|
||||
if (r.model === 'grpc_request' && r.service != null && r.method != null) {
|
||||
const shortService = r.service.split('.').pop();
|
||||
return `${shortService}/${r.method}`;
|
||||
} else {
|
||||
try {
|
||||
const url = new URL(fixedUrl);
|
||||
const pathname = url.pathname === '/' ? '' : url.pathname;
|
||||
return `${url.host}${pathname}`;
|
||||
} catch (_) {
|
||||
// Nothing
|
||||
}
|
||||
}
|
||||
|
||||
return r.url;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
export function tryFormatJson(text: string): string {
|
||||
export function tryFormatJson(text: string, pretty = true): string {
|
||||
try {
|
||||
return JSON.stringify(JSON.parse(text), null, 2);
|
||||
if (pretty) return JSON.stringify(JSON.parse(text), null, 2);
|
||||
else return JSON.stringify(JSON.parse(text));
|
||||
} catch (_) {
|
||||
return text;
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ export async function setKeyValue<T>({
|
||||
key: string | string[];
|
||||
value: T;
|
||||
}): Promise<void> {
|
||||
await invoke('set_key_value', {
|
||||
await invoke('cmd_set_key_value', {
|
||||
namespace,
|
||||
key: buildKeyValueKey(key),
|
||||
value: JSON.stringify(value),
|
||||
@@ -30,7 +30,7 @@ export async function getKeyValue<T>({
|
||||
key: string | string[];
|
||||
fallback: T;
|
||||
}) {
|
||||
const kv = (await invoke('get_key_value', {
|
||||
const kv = (await invoke('cmd_get_key_value', {
|
||||
namespace,
|
||||
key: buildKeyValueKey(key),
|
||||
})) as KeyValue | null;
|
||||
|
||||
@@ -1,9 +1,19 @@
|
||||
import { sleep } from './sleep';
|
||||
|
||||
/** Ensures a promise takes at least a certain number of milliseconds to resolve */
|
||||
export async function minPromiseMillis<T>(promise: Promise<T>, millis: number) {
|
||||
const start = Date.now();
|
||||
const result = await promise;
|
||||
let result;
|
||||
let err;
|
||||
|
||||
try {
|
||||
result = await promise;
|
||||
} catch (e) {
|
||||
err = e;
|
||||
}
|
||||
|
||||
const delayFor = millis - (Date.now() - start);
|
||||
await sleep(delayFor);
|
||||
return result;
|
||||
if (err) throw err;
|
||||
else return result;
|
||||
}
|
||||
|
||||
@@ -12,6 +12,9 @@ export const AUTH_TYPE_BEARER = 'bearer';
|
||||
export type Model =
|
||||
| Settings
|
||||
| Workspace
|
||||
| GrpcConnection
|
||||
| GrpcRequest
|
||||
| GrpcMessage
|
||||
| HttpRequest
|
||||
| HttpResponse
|
||||
| KeyValue
|
||||
@@ -101,6 +104,38 @@ export interface HttpUrlParameter {
|
||||
enabled?: boolean;
|
||||
}
|
||||
|
||||
export interface GrpcRequest extends BaseModel {
|
||||
readonly workspaceId: string;
|
||||
readonly model: 'grpc_request';
|
||||
folderId: string | null;
|
||||
sortPriority: number;
|
||||
name: string;
|
||||
url: string;
|
||||
service: string | null;
|
||||
method: string | null;
|
||||
message: string;
|
||||
protoFiles: string[];
|
||||
}
|
||||
|
||||
export interface GrpcMessage extends BaseModel {
|
||||
readonly workspaceId: string;
|
||||
readonly requestId: string;
|
||||
readonly connectionId: string;
|
||||
readonly model: 'grpc_message';
|
||||
message: string;
|
||||
isServer: boolean;
|
||||
isInfo: boolean;
|
||||
}
|
||||
|
||||
export interface GrpcConnection extends BaseModel {
|
||||
readonly workspaceId: string;
|
||||
readonly requestId: string;
|
||||
readonly model: 'grpc_connection';
|
||||
service: string;
|
||||
method: string;
|
||||
elapsed: number;
|
||||
}
|
||||
|
||||
export interface HttpRequest extends BaseModel {
|
||||
readonly workspaceId: string;
|
||||
readonly model: 'http_request';
|
||||
|
||||
@@ -7,5 +7,5 @@ export async function sendEphemeralRequest(
|
||||
): Promise<HttpResponse> {
|
||||
// Remove some things that we don't want to associate
|
||||
const newRequest = { ...request };
|
||||
return invoke('send_ephemeral_request', { request: newRequest, environmentId });
|
||||
return invoke('cmd_send_ephemeral_request', { request: newRequest, environmentId });
|
||||
}
|
||||
|
||||
@@ -1,13 +1,30 @@
|
||||
import { invoke } from '@tauri-apps/api';
|
||||
import type { CookieJar, Environment, Folder, HttpRequest, Settings, Workspace } from './models';
|
||||
import type {
|
||||
CookieJar,
|
||||
Environment,
|
||||
Folder,
|
||||
GrpcRequest,
|
||||
HttpRequest,
|
||||
Settings,
|
||||
Workspace,
|
||||
} from './models';
|
||||
|
||||
export async function getSettings(): Promise<Settings> {
|
||||
return invoke('get_settings', {});
|
||||
return invoke('cmd_get_settings', {});
|
||||
}
|
||||
|
||||
export async function getRequest(id: string | null): Promise<HttpRequest | null> {
|
||||
export async function getGrpcRequest(id: string | null): Promise<GrpcRequest | null> {
|
||||
if (id === null) return null;
|
||||
const request: HttpRequest = (await invoke('get_request', { id })) ?? null;
|
||||
const request: GrpcRequest = (await invoke('cmd_get_grpc_request', { id })) ?? null;
|
||||
if (request == null) {
|
||||
return null;
|
||||
}
|
||||
return request;
|
||||
}
|
||||
|
||||
export async function getHttpRequest(id: string | null): Promise<HttpRequest | null> {
|
||||
if (id === null) return null;
|
||||
const request: HttpRequest = (await invoke('cmd_get_http_request', { id })) ?? null;
|
||||
if (request == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -16,7 +33,7 @@ export async function getRequest(id: string | null): Promise<HttpRequest | null>
|
||||
|
||||
export async function getEnvironment(id: string | null): Promise<Environment | null> {
|
||||
if (id === null) return null;
|
||||
const environment: Environment = (await invoke('get_environment', { id })) ?? null;
|
||||
const environment: Environment = (await invoke('cmd_get_environment', { id })) ?? null;
|
||||
if (environment == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -25,7 +42,7 @@ export async function getEnvironment(id: string | null): Promise<Environment | n
|
||||
|
||||
export async function getFolder(id: string | null): Promise<Folder | null> {
|
||||
if (id === null) return null;
|
||||
const folder: Folder = (await invoke('get_folder', { id })) ?? null;
|
||||
const folder: Folder = (await invoke('cmd_get_folder', { id })) ?? null;
|
||||
if (folder == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -34,7 +51,7 @@ export async function getFolder(id: string | null): Promise<Folder | null> {
|
||||
|
||||
export async function getWorkspace(id: string | null): Promise<Workspace | null> {
|
||||
if (id === null) return null;
|
||||
const workspace: Workspace = (await invoke('get_workspace', { id })) ?? null;
|
||||
const workspace: Workspace = (await invoke('cmd_get_workspace', { id })) ?? null;
|
||||
if (workspace == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -43,7 +60,7 @@ export async function getWorkspace(id: string | null): Promise<Workspace | null>
|
||||
|
||||
export async function getCookieJar(id: string | null): Promise<CookieJar | null> {
|
||||
if (id === null) return null;
|
||||
const cookieJar: CookieJar = (await invoke('get_cookie_jar', { id })) ?? null;
|
||||
const cookieJar: CookieJar = (await invoke('cmd_get_cookie_jar', { id })) ?? null;
|
||||
if (cookieJar == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user