From 8081c86ec6b4d24ce9c1dde27308ffd1519b98db Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Thu, 8 Aug 2024 22:07:05 -0700 Subject: [PATCH] Better TS type generation --- package-lock.json | 8 +++--- package.json | 2 +- plugin-runtime-types/package.json | 2 +- .../src/gen/GrpcConnection.ts | 3 +++ plugin-runtime-types/src/gen/GrpcEvent.ts | 4 +++ plugin-runtime-types/src/gen/GrpcEventType.ts | 3 +++ plugin-runtime-types/src/gen/KeyValue.ts | 3 +++ plugin-runtime-types/src/gen/Model.ts | 5 +++- plugin-runtime-types/src/index.ts | 23 +++++++++------- src-tauri/yaak_models/src/models.rs | 20 -------------- src-tauri/yaak_plugin_runtime/src/events.rs | 11 +++++--- src-web/lib/models.ts | 26 +------------------ 12 files changed, 45 insertions(+), 65 deletions(-) create mode 100644 plugin-runtime-types/src/gen/GrpcConnection.ts create mode 100644 plugin-runtime-types/src/gen/GrpcEvent.ts create mode 100644 plugin-runtime-types/src/gen/GrpcEventType.ts create mode 100644 plugin-runtime-types/src/gen/KeyValue.ts diff --git a/package-lock.json b/package-lock.json index 8fb32b2d..4c1eea80 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26,7 +26,7 @@ "@tauri-apps/plugin-fs": "^2.0.0-rc.0", "@tauri-apps/plugin-os": "^2.0.0-rc.0", "@tauri-apps/plugin-shell": "^2.0.0-rc.0", - "@yaakapp/api": "^0.1.0-beta.9", + "@yaakapp/api": "^0.1.2", "buffer": "^6.0.3", "classnames": "^2.3.2", "cm6-graphql": "^0.0.9", @@ -2981,9 +2981,9 @@ "integrity": "sha512-N8tkAACJx2ww8vFMneJmaAgmjAG1tnVBZJRLRcx061tmsLRZHSEZSLuGWnwPtunsSLvSqXQ2wfp7Mgqg1I+2dQ==" }, "node_modules/@yaakapp/api": { - "version": "0.1.0-beta.9", - "resolved": "https://registry.npmjs.org/@yaakapp/api/-/api-0.1.0-beta.9.tgz", - "integrity": "sha512-aNh1e2mUOV7GlIiVjDu6GSdvPPH1mz0k3coJs0jP1UhDr2V63RUC/phf00ei4odAO1VGlS7ISrtXCRJ7rK93aQ==", + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/@yaakapp/api/-/api-0.1.2.tgz", + "integrity": "sha512-VEs+H9ZJjp/MFHKbqTdG/eGtvjDZ36P1pmzEYm6VYGIN2Bfw3OyXzkn7vgIg1savX0LU/U5kI88AYXGMr5Fjdw==", "dependencies": { "@types/node": "^22.0.0" } diff --git a/package.json b/package.json index 095d8b56..633d2a16 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "@tauri-apps/plugin-fs": "^2.0.0-rc.0", "@tauri-apps/plugin-os": "^2.0.0-rc.0", "@tauri-apps/plugin-shell": "^2.0.0-rc.0", - "@yaakapp/api": "^0.1.0-beta.9", + "@yaakapp/api": "^0.1.2", "buffer": "^6.0.3", "classnames": "^2.3.2", "cm6-graphql": "^0.0.9", diff --git a/plugin-runtime-types/package.json b/plugin-runtime-types/package.json index c194cf3f..89b63eaf 100644 --- a/plugin-runtime-types/package.json +++ b/plugin-runtime-types/package.json @@ -1,6 +1,6 @@ { "name": "@yaakapp/api", - "version": "0.1.0-beta.9", + "version": "0.1.2", "main": "lib/index.js", "typings": "./lib/index.d.ts", "files": [ diff --git a/plugin-runtime-types/src/gen/GrpcConnection.ts b/plugin-runtime-types/src/gen/GrpcConnection.ts new file mode 100644 index 00000000..595b5d00 --- /dev/null +++ b/plugin-runtime-types/src/gen/GrpcConnection.ts @@ -0,0 +1,3 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export type GrpcConnection = { id: string, model: "grpc_connection", workspaceId: string, requestId: string, createdAt: string, updatedAt: string, service: string, method: string, elapsed: number, status: number, url: string, error: string | null, trailers: { [key: string]: string }, }; diff --git a/plugin-runtime-types/src/gen/GrpcEvent.ts b/plugin-runtime-types/src/gen/GrpcEvent.ts new file mode 100644 index 00000000..67028185 --- /dev/null +++ b/plugin-runtime-types/src/gen/GrpcEvent.ts @@ -0,0 +1,4 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { GrpcEventType } from "./GrpcEventType"; + +export type GrpcEvent = { id: string, model: "grpc_event", workspaceId: string, requestId: string, connectionId: string, createdAt: string, content: string, eventType: GrpcEventType, metadata: { [key: string]: string }, status: number | null, error: string | null, }; diff --git a/plugin-runtime-types/src/gen/GrpcEventType.ts b/plugin-runtime-types/src/gen/GrpcEventType.ts new file mode 100644 index 00000000..00d8db9b --- /dev/null +++ b/plugin-runtime-types/src/gen/GrpcEventType.ts @@ -0,0 +1,3 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export type GrpcEventType = "info" | "error" | "client_message" | "server_message" | "connection_start" | "connection_end"; diff --git a/plugin-runtime-types/src/gen/KeyValue.ts b/plugin-runtime-types/src/gen/KeyValue.ts new file mode 100644 index 00000000..301df19c --- /dev/null +++ b/plugin-runtime-types/src/gen/KeyValue.ts @@ -0,0 +1,3 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export type KeyValue = { model: "key_value", createdAt: string, updatedAt: string, namespace: string, key: string, value: string, }; diff --git a/plugin-runtime-types/src/gen/Model.ts b/plugin-runtime-types/src/gen/Model.ts index 1e9c82d5..ed1b1a28 100644 --- a/plugin-runtime-types/src/gen/Model.ts +++ b/plugin-runtime-types/src/gen/Model.ts @@ -1,9 +1,12 @@ // This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. import type { Environment } from "./Environment"; import type { Folder } from "./Folder"; +import type { GrpcConnection } from "./GrpcConnection"; +import type { GrpcEvent } from "./GrpcEvent"; import type { GrpcRequest } from "./GrpcRequest"; import type { HttpRequest } from "./HttpRequest"; import type { HttpResponse } from "./HttpResponse"; +import type { KeyValue } from "./KeyValue"; import type { Workspace } from "./Workspace"; -export type Model = { "workspace": Workspace } | { "environment": Environment } | { "folder": Folder } | { "httpRequest": HttpRequest } | { "httpResponse": HttpResponse } | { "grpcRequest": GrpcRequest }; +export type Model = Environment | Folder | GrpcConnection | GrpcEvent | GrpcRequest | HttpRequest | HttpResponse | KeyValue | Workspace; diff --git a/plugin-runtime-types/src/index.ts b/plugin-runtime-types/src/index.ts index c76addc2..b6a6fb4f 100644 --- a/plugin-runtime-types/src/index.ts +++ b/plugin-runtime-types/src/index.ts @@ -1,25 +1,30 @@ export type * from './plugins'; export type * from './themes'; +export * from './gen/BootRequest'; +export * from './gen/BootResponse'; +export * from './gen/EmptyResponse'; export * from './gen/Environment'; export * from './gen/EnvironmentVariable'; +export * from './gen/ExportHttpRequestRequest'; +export * from './gen/ExportHttpRequestResponse'; +export * from './gen/FilterRequest'; +export * from './gen/FilterResponse'; export * from './gen/Folder'; +export * from './gen/GrpcConnection'; +export * from './gen/GrpcEvent'; export * from './gen/GrpcMetadataEntry'; export * from './gen/GrpcRequest'; export * from './gen/HttpRequest'; export * from './gen/HttpRequestHeader'; export * from './gen/HttpResponse'; +export * from './gen/HttpResponseHeader'; export * from './gen/HttpUrlParameter'; -export * from './gen/BootRequest'; -export * from './gen/BootResponse'; -export * from './gen/EmptyResponse'; -export * from './gen/ExportHttpRequestRequest'; -export * from './gen/ExportHttpRequestResponse'; -export * from './gen/FilterRequest'; -export * from './gen/FilterResponse'; export * from './gen/ImportRequest'; -export * from './gen/InternalEvent'; -export * from './gen/InternalEventPayload'; export * from './gen/ImportResources'; export * from './gen/ImportResponse'; +export * from './gen/InternalEvent'; +export * from './gen/InternalEventPayload'; +export * from './gen/KeyValue'; +export * from './gen/Model'; export * from './gen/Workspace'; diff --git a/src-tauri/yaak_models/src/models.rs b/src-tauri/yaak_models/src/models.rs index ceaca243..ef8da95a 100644 --- a/src-tauri/yaak_models/src/models.rs +++ b/src-tauri/yaak_models/src/models.rs @@ -8,7 +8,6 @@ use ts_rs::TS; #[derive(Debug, Clone, Serialize, Deserialize, Default, TS)] #[serde(default, rename_all = "camelCase")] -#[ts(export)] pub struct Settings { pub id: String, #[ts(type = "\"settings\"")] @@ -72,7 +71,6 @@ impl<'s> TryFrom<&Row<'s>> for Settings { #[derive(Debug, Clone, Serialize, Deserialize, Default, TS)] #[serde(default, rename_all = "camelCase")] -#[ts(export)] pub struct Workspace { pub id: String, #[ts(type = "\"workspace\"")] @@ -140,7 +138,6 @@ impl Workspace { } #[derive(Debug, Clone, Serialize, Deserialize, TS)] -#[ts(export)] enum CookieDomain { HostOnly(String), Suffix(String), @@ -149,14 +146,12 @@ enum CookieDomain { } #[derive(Debug, Clone, Serialize, Deserialize, TS)] -#[ts(export)] enum CookieExpires { AtUtc(String), SessionEnd, } #[derive(Debug, Clone, Serialize, Deserialize, TS)] -#[ts(export)] pub struct Cookie { raw_cookie: String, domain: CookieDomain, @@ -166,7 +161,6 @@ pub struct Cookie { #[derive(Debug, Clone, Serialize, Deserialize, Default, TS)] #[serde(default, rename_all = "camelCase")] -#[ts(export)] pub struct CookieJar { pub id: String, #[ts(type = "\"cookie_jar\"")] @@ -210,7 +204,6 @@ impl<'s> TryFrom<&Row<'s>> for CookieJar { #[derive(Debug, Clone, Serialize, Deserialize, Default, TS)] #[serde(default, rename_all = "camelCase")] -#[ts(export)] pub struct Environment { pub id: String, pub workspace_id: String, @@ -254,7 +247,6 @@ impl<'s> TryFrom<&Row<'s>> for Environment { #[derive(Debug, Clone, Serialize, Deserialize, Default, TS)] #[serde(default, rename_all = "camelCase")] -#[ts(export)] pub struct EnvironmentVariable { #[serde(default = "default_true")] #[ts(optional, as = "Option")] @@ -265,7 +257,6 @@ pub struct EnvironmentVariable { #[derive(Debug, Clone, Serialize, Deserialize, Default, TS)] #[serde(default, rename_all = "camelCase")] -#[ts(export)] pub struct Folder { pub created_at: NaiveDateTime, pub updated_at: NaiveDateTime, @@ -311,7 +302,6 @@ impl<'s> TryFrom<&Row<'s>> for Folder { #[derive(Debug, Clone, Serialize, Deserialize, Default, TS)] #[serde(default, rename_all = "camelCase")] -#[ts(export)] pub struct HttpRequestHeader { #[serde(default = "default_true")] #[ts(optional, as = "Option")] @@ -322,7 +312,6 @@ pub struct HttpRequestHeader { #[derive(Debug, Clone, Serialize, Deserialize, Default, TS)] #[serde(default, rename_all = "camelCase")] -#[ts(export)] pub struct HttpUrlParameter { #[serde(default = "default_true")] #[ts(optional, as = "Option")] @@ -333,7 +322,6 @@ pub struct HttpUrlParameter { #[derive(Debug, Clone, Serialize, Deserialize, Default, TS)] #[serde(default, rename_all = "camelCase")] -#[ts(export)] pub struct HttpRequest { pub created_at: NaiveDateTime, pub updated_at: NaiveDateTime, @@ -410,7 +398,6 @@ impl<'s> TryFrom<&Row<'s>> for HttpRequest { #[derive(Debug, Clone, Serialize, Deserialize, Default, TS)] #[serde(default, rename_all = "camelCase")] -#[ts(export)] pub struct HttpResponseHeader { pub name: String, pub value: String, @@ -418,7 +405,6 @@ pub struct HttpResponseHeader { #[derive(Debug, Clone, Serialize, Deserialize, Default, TS)] #[serde(default, rename_all = "camelCase")] -#[ts(export)] pub struct HttpResponse { pub id: String, #[ts(type = "\"http_response\"")] @@ -501,7 +487,6 @@ impl HttpResponse { #[derive(Debug, Clone, Serialize, Deserialize, Default, TS)] #[serde(default, rename_all = "camelCase")] -#[ts(export)] pub struct GrpcMetadataEntry { #[serde(default = "default_true")] #[ts(optional, as = "Option")] @@ -512,7 +497,6 @@ pub struct GrpcMetadataEntry { #[derive(Debug, Clone, Serialize, Deserialize, Default, TS)] #[serde(default, rename_all = "camelCase")] -#[ts(export)] pub struct GrpcRequest { pub id: String, #[ts(type = "\"grpc_request\"")] @@ -582,7 +566,6 @@ impl<'s> TryFrom<&Row<'s>> for GrpcRequest { #[derive(Debug, Clone, Serialize, Deserialize, Default, TS)] #[serde(default, rename_all = "camelCase")] -#[ts(export)] pub struct GrpcConnection { pub id: String, #[ts(type = "\"grpc_connection\"")] @@ -644,7 +627,6 @@ impl<'s> TryFrom<&Row<'s>> for GrpcConnection { #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, TS)] #[serde(rename_all = "snake_case")] -#[ts(export)] pub enum GrpcEventType { Info, Error, @@ -662,7 +644,6 @@ impl Default for GrpcEventType { #[derive(Debug, Clone, Serialize, Deserialize, Default, TS)] #[serde(default, rename_all = "camelCase")] -#[ts(export)] pub struct GrpcEvent { pub id: String, #[ts(type = "\"grpc_event\"")] @@ -720,7 +701,6 @@ impl<'s> TryFrom<&Row<'s>> for GrpcEvent { #[derive(Debug, Clone, Serialize, Deserialize, Default, TS)] #[serde(default, rename_all = "camelCase")] -#[ts(export)] pub struct KeyValue { #[ts(type = "\"key_value\"")] pub model: String, diff --git a/src-tauri/yaak_plugin_runtime/src/events.rs b/src-tauri/yaak_plugin_runtime/src/events.rs index cc00dc01..0abcf7fc 100644 --- a/src-tauri/yaak_plugin_runtime/src/events.rs +++ b/src-tauri/yaak_plugin_runtime/src/events.rs @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize}; use serde_json::Value; use ts_rs::TS; -use yaak_models::models::{Environment, Folder, GrpcRequest, HttpRequest, HttpResponse, Workspace}; +use yaak_models::models::{Environment, Folder, GrpcConnection, GrpcEvent, GrpcRequest, HttpRequest, HttpResponse, KeyValue, Workspace}; #[derive(Debug, Clone, Serialize, Deserialize, TS)] #[serde(rename_all = "camelCase")] @@ -120,13 +120,16 @@ pub struct ImportResources { } #[derive(Debug, Clone, Serialize, Deserialize, TS)] -#[serde(rename_all = "camelCase")] +#[serde(rename_all = "camelCase", untagged)] #[ts(export)] pub enum Model { - Workspace(Workspace), Environment(Environment), Folder(Folder), + GrpcConnection(GrpcConnection), + GrpcEvent(GrpcEvent), + GrpcRequest(GrpcRequest), HttpRequest(HttpRequest), HttpResponse(HttpResponse), - GrpcRequest(GrpcRequest), + KeyValue(KeyValue), + Workspace(Workspace), } diff --git a/src-web/lib/models.ts b/src-web/lib/models.ts index 4e0fc4ba..2e730d86 100644 --- a/src-web/lib/models.ts +++ b/src-web/lib/models.ts @@ -1,15 +1,4 @@ -import type { - Environment, - Folder, - GrpcConnection, - GrpcEvent, - GrpcRequest, - HttpRequest, - HttpResponse, - KeyValue, - Workspace, - HttpResponseHeader, -} from '@yaakapp/api'; +import type { GrpcConnection, HttpResponse, HttpResponseHeader, Model } from '@yaakapp/api'; import type { Cookie } from './models/Cookie'; import type { CookieJar } from './models/CookieJar'; import type { Settings } from './models/Settings'; @@ -29,19 +18,6 @@ export const AUTH_TYPE_NONE = null; export const AUTH_TYPE_BASIC = 'basic'; export const AUTH_TYPE_BEARER = 'bearer'; -export type Model = - | Settings - | Workspace - | Folder - | GrpcConnection - | GrpcRequest - | GrpcEvent - | HttpRequest - | HttpResponse - | KeyValue - | Environment - | CookieJar; - export function cookieDomain(cookie: Cookie): string { if (cookie.domain === 'NotPresent' || cookie.domain === 'Empty') { return 'n/a';