Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into channel
Browse files Browse the repository at this point in the history
  • Loading branch information
oscartbeaumont committed Jul 31, 2024
2 parents ac54fec + afe62fd commit ed72657
Show file tree
Hide file tree
Showing 25 changed files with 880 additions and 927 deletions.
324 changes: 154 additions & 170 deletions Cargo.lock

Large diffs are not rendered by default.

25 changes: 18 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,22 @@ rustc-args = ["--cfg", "docsrs"]
rustdoc-args = ["--cfg", "docsrs"]

[features]
javascript = ["specta/typescript", "specta/js_doc"]
typescript = ["specta/typescript", "specta/js_doc"]
javascript = ["dep:specta-typescript"]
typescript = ["dep:specta-typescript"]

# TODO: https://github.com/tauri-apps/tauri/pull/10435
UNSTABLE_channels = []

[dependencies]
# Public
specta = { workspace = true, features = ["function"] }
specta-typescript = { workspace = true, optional = true }
specta-util = { workspace = true }
tauri-specta-macros = { version = "=2.0.0-rc.5", path = "./macros" }
serde = "1"
serde_json = "1"
thiserror = "1"
tauri = { workspace = true, no-default-features = true, features = ["specta"] }
tauri = { workspace = true, default-features = false, features = ["specta"] }

# Private
heck = "0.5.0"
Expand All @@ -43,9 +48,15 @@ members = [
]

[workspace.dependencies]
specta = { version = "=2.0.0-rc.13" }
# tauri = { git = "https://github.com/brendonovich/tauri", branch = "generic-channel" }
tauri = { version = "=2.0.0-beta.22" }
tauri = { version = "=2.0.0-beta.25" }
specta = { version = "=2.0.0-rc.16" }
specta-util = { version = "0.0.3" }
specta-typescript = { version = "0.0.3" }

[patch.crates-io]
tauri = { git = "https://github.com/brendonovich/tauri", branch = "generic-channel" }
# tauri = { git = "https://github.com/oscartbeaumont/tauri.git", branch = "fix-channel-specta-remote-impl" }
# tauri-build = { git = "https://github.com/oscartbeaumont/tauri.git", branch = "fix-channel-specta-remote-impl" }

specta = { path = "../specta/specta" }
specta-util = { path = "../specta/specta-util" }
specta-typescript = { path = "../specta/specta-typescript" }
8 changes: 4 additions & 4 deletions docs/v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
> However, it's safe to use as long as you lock your versions.
```bash
cargo add tauri@=2.0.0-beta.22
cargo add specta@=2.0.0-rc.12
cargo add tauri@=2.0.0-beta.25
cargo add specta@=2.0.0-rc.16
cargo add tauri-specta@=2.0.0-rc.11 --features javascript,typescript
```

Expand Down Expand Up @@ -187,7 +187,7 @@ let builder = ts::builder()
.types(TypeCollection::default().register::<Custom>()); // < call `register` as much as you want.
```

`register` only supports [named types](https://docs.rs/specta/2.0.0-rc.12/specta/type/trait.NamedType.html) as otherwise you would run into the following problem:
`register` only supports [named types](https://docs.rs/specta/2.0.0-rc.16/specta/type/trait.NamedType.html) as otherwise you would run into the following problem:
```rust
// vvv - What would this be without a name?
export ... = {};
Expand All @@ -202,7 +202,7 @@ It may be useful to export a constant from your Rust into your Typescript. You c
```rust
let builder = ts::builder()
// < your commands and events are probaly here
.types(StaticCollection::default().register("myConstant", 42)); // < call `register` as much as you want.
.statics(StaticCollection::default().register("myConstant", 42)); // < call `register` as much as you want.
```

This value must implement [`serde::Serialize`](https://docs.rs/serde/latest/serde/trait.Serialize.html).
Expand Down
4 changes: 2 additions & 2 deletions examples/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"devDependencies": {
"@tauri-apps/cli": "next",
"typescript": "^5.4.5",
"vite": "^5.2.10"
"typescript": "^5.5.4",
"vite": "^5.3.5"
}
}
8 changes: 5 additions & 3 deletions examples/app/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ rust-version = "1.57"
publish = false

[build-dependencies]
tauri-build = { version = "2.0.0-beta.17", features = [] }
tauri-build = { version = "2.0.0-beta.18", features = [] }

[dependencies]
serde_json = "1.0"
specta = { workspace = true }
serde = { version = "1.0", features = ["derive"] }
tauri = { workspace = true, features = [] }
tauri-specta = { path = "../../../", features = ["typescript", "javascript"] }
tauri-plugin-os = "2.0.0-beta.3"
thiserror = "1.0.60"
specta-util = { workspace = true }
specta-typescript = { workspace = true }
tauri-plugin-os = "^2.0.0-beta.3"
thiserror = "1"

[features]
default = ["custom-protocol"]
Expand Down
8 changes: 6 additions & 2 deletions examples/app/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
)]

use serde::{Deserialize, Serialize};
use specta::{Type, TypeCollection};
use specta::Type;
use specta_util::TypeCollection;
use tauri_specta::*;
use thiserror::Error;

Expand Down Expand Up @@ -129,7 +130,10 @@ fn main() {
])
.events(tauri_specta::collect_events![crate::DemoEvent, EmptyEvent])
.types(TypeCollection::default().register::<Custom>())
.config(specta::ts::ExportConfig::default().formatter(specta::ts::formatter::prettier))
.config(
specta_typescript::ExportConfig::default()
.formatter(specta_typescript::formatter::prettier),
)
.types(TypeCollection::default().register::<Testing>())
.statics(StaticCollection::default().register("universalConstant", 42))
.header("/* These are my Tauri Specta Bindings! */");
Expand Down
89 changes: 46 additions & 43 deletions examples/app/src/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ emptyEvent: "empty-event"

/** user-defined statics **/

const universalConstant = 42 as const;
export const universalConstant = 42 as const;

/** user-defined types **/

Expand All @@ -79,59 +79,62 @@ export type Testing = { a: string }

/** tauri-specta globals **/

import { invoke as TAURI_INVOKE } from "@tauri-apps/api/core";
import {
invoke as TAURI_INVOKE,
Channel as TAURI_CHANNEL,
} from "@tauri-apps/api/core";
import * as TAURI_API_EVENT from "@tauri-apps/api/event";
import { type WebviewWindow as __WebviewWindow__ } from "@tauri-apps/api/webviewWindow";

type __EventObj__<T> = {
listen: (
cb: TAURI_API_EVENT.EventCallback<T>
) => ReturnType<typeof TAURI_API_EVENT.listen<T>>;
once: (
cb: TAURI_API_EVENT.EventCallback<T>
) => ReturnType<typeof TAURI_API_EVENT.once<T>>;
emit: T extends null
? (payload?: T) => ReturnType<typeof TAURI_API_EVENT.emit>
: (payload: T) => ReturnType<typeof TAURI_API_EVENT.emit>;
listen: (
cb: TAURI_API_EVENT.EventCallback<T>,
) => ReturnType<typeof TAURI_API_EVENT.listen<T>>;
once: (
cb: TAURI_API_EVENT.EventCallback<T>,
) => ReturnType<typeof TAURI_API_EVENT.once<T>>;
emit: T extends null
? (payload?: T) => ReturnType<typeof TAURI_API_EVENT.emit>
: (payload: T) => ReturnType<typeof TAURI_API_EVENT.emit>;
};

export type Result<T, E> =
| { status: "ok"; data: T }
| { status: "error"; error: E };
| { status: "ok"; data: T }
| { status: "error"; error: E };

function __makeEvents__<T extends Record<string, any>>(
mappings: Record<keyof T, string>
mappings: Record<keyof T, string>,
) {
return new Proxy(
{} as unknown as {
[K in keyof T]: __EventObj__<T[K]> & {
(handle: __WebviewWindow__): __EventObj__<T[K]>;
};
},
{
get: (_, event) => {
const name = mappings[event as keyof T];
return new Proxy(
{} as unknown as {
[K in keyof T]: __EventObj__<T[K]> & {
(handle: __WebviewWindow__): __EventObj__<T[K]>;
};
},
{
get: (_, event) => {
const name = mappings[event as keyof T];

return new Proxy((() => {}) as any, {
apply: (_, __, [window]: [__WebviewWindow__]) => ({
listen: (arg: any) => window.listen(name, arg),
once: (arg: any) => window.once(name, arg),
emit: (arg: any) => window.emit(name, arg),
}),
get: (_, command: keyof __EventObj__<any>) => {
switch (command) {
case "listen":
return (arg: any) => TAURI_API_EVENT.listen(name, arg);
case "once":
return (arg: any) => TAURI_API_EVENT.once(name, arg);
case "emit":
return (arg: any) => TAURI_API_EVENT.emit(name, arg);
}
},
});
},
}
);
return new Proxy((() => {}) as any, {
apply: (_, __, [window]: [__WebviewWindow__]) => ({
listen: (arg: any) => window.listen(name, arg),
once: (arg: any) => window.once(name, arg),
emit: (arg: any) => window.emit(name, arg),
}),
get: (_, command: keyof __EventObj__<any>) => {
switch (command) {
case "listen":
return (arg: any) => TAURI_API_EVENT.listen(name, arg);
case "once":
return (arg: any) => TAURI_API_EVENT.once(name, arg);
case "emit":
return (arg: any) => TAURI_API_EVENT.emit(name, arg);
}
},
});
},
},
);
}


4 changes: 2 additions & 2 deletions examples/app/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getCurrent } from "@tauri-apps/api/webviewWindow";
import { getCurrentWebview } from "@tauri-apps/api/webview";
import { commands, events } from "./bindings";

const appWindow = getCurrent();
const appWindow = getCurrentWebview();

let greetInputEl: HTMLInputElement | null;
let greetMsgEl: HTMLElement | null;
Expand Down
6 changes: 3 additions & 3 deletions examples/custom-plugin/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
"license": "MIT",
"dependencies": {
"@tauri-apps/api": "next",
"solid-js": "^1.8.17",
"solid-js": "^1.8.19",
"tauri-specta-custom-plugin": "workspace:*"
},
"devDependencies": {
"@tauri-apps/cli": "next",
"typescript": "^5.4.5",
"vite": "^5.2.10",
"typescript": "^5.5.4",
"vite": "^5.3.5",
"vite-plugin-solid": "^2.10.2"
}
}
2 changes: 1 addition & 1 deletion examples/custom-plugin/app/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ repository = ""
edition = "2021"

[build-dependencies]
tauri-build = { version = "2.0.0-beta.17", features = [] }
tauri-build = { version = "2.0.0-beta.18", features = [] }

[dependencies]
tauri = { workspace = true, features = [] }
Expand Down
5 changes: 4 additions & 1 deletion examples/custom-plugin/plugin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ edition = "2021"
links = "tauri-plugin-specta-example"

[build-dependencies]
tauri-plugin = { version = "=2.0.0-beta.17", features = ["build"] }
tauri-plugin = { version = "2.0.0-beta.18", features = ["build"] }

[dependencies]
rand = "0.8.5"
serde = "1"
specta = { workspace = true }
tauri = { workspace = true }
tauri-specta = { path = "../../../", features = ["typescript"] }

[dev-dependencies]
specta-typescript = { workspace = true }
9 changes: 9 additions & 0 deletions examples/custom-plugin/plugin/bindings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

// This file was generated by [tauri-specta](https://github.com/oscartbeaumont/tauri-specta). Do not edit this file manually.

/** user-defined commands **/

export const commands = {
/**
* Adds two numbers, returning the result.
Expand All @@ -9,12 +12,18 @@ return await TAURI_INVOKE("plugin:specta-example|add_numbers", { a, b });
}
}

/** user-defined events **/

export const events = __makeEvents__<{
randomNumber: RandomNumber
}>({
randomNumber: "plugin:specta-example:random-number"
})

/** user-defined statics **/



/** user-defined types **/

export type RandomNumber = number
Expand Down
2 changes: 1 addition & 1 deletion examples/custom-plugin/plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"codegen": "cargo test && pnpm build"
},
"devDependencies": {
"typescript": "^5.4.5"
"typescript": "^5.5.4"
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,36 @@
| Permission | Description |
|------|-----|
|`allow-add-numbers`|Enables the add_numbers command without any pre-configured scope.|
|`deny-add-numbers`|Denies the add_numbers command without any pre-configured scope.|

### Permission Table

<table>
<tr>
<th>Identifier</th>
<th>Description</th>
</tr>


<tr>
<td>

`specta-example:allow-add-numbers`

</td>
<td>

Enables the add_numbers command without any pre-configured scope.

</td>
</tr>

<tr>
<td>

`specta-example:deny-add-numbers`

</td>
<td>

Denies the add_numbers command without any pre-configured scope.

</td>
</tr>
</table>
Loading

0 comments on commit ed72657

Please sign in to comment.