Skip to content

Commit dcd28f0

Browse files
authored
feat: codegen and router refactor (#106)
2 parents f6b3535 + 0b46323 commit dcd28f0

File tree

43 files changed

+1755
-880
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1755
-880
lines changed

.changes/router-refactor.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"qubit": major
3+
---
4+
5+
**(BREAKING)** Refactor router to separate RPC functionality from type generation functionality.
6+
Now, use `router.as_codegen().write_type(path, TypeScript::new())` to generate types, and
7+
`router.as_rpc(ctx).into_service()` to build the service.

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ urlencoding = "2.1.3"
3838
derive_more = { version = "2.0.1", features = ["deref"] }
3939
linkme = "0.3.33"
4040
lazy_static = "1.5.0"
41+
thiserror = "2.0.12"
4142

4243
[features]
4344
ts-format = ["ts-rs/format"]

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,18 @@ async fn hello_world() -> String {
6565
let router = Router::new()
6666
.handler(hello_world);
6767

68-
router.write_bindings_to_dir("./bindings");
68+
router
69+
.as_codegen()
70+
.write_type("./bindings", TypeScript::new());
6971
```
7072

7173
3. Attach the Qubit router to an Axum router, and start it
7274

7375
```rs
7476
// Create a service and handle
75-
let (qubit_service, qubit_handle) = router.to_service(());
77+
let (qubit_service, qubit_handle) = router
78+
.as_rpc()
79+
.into_service(());
7680

7781
// Nest into an Axum router
7882
let axum_router = axum::Router::<()>::new()

examples/authentication/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/authentication/auth-demo/src/bindings.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
/* eslint-disable */
22
// @ts-nocheck
3-
/* @@@@@@@@@@@@@ & ###############
3+
/*
4+
@@@@@@@@@@@@@ & ###############
45
@@@@@@@@@@@@@@ &&& ###############
56
@@@@@@@@@@@@@@ &&&&& ###############
67
############### &&&&& ###############
78
######## Generated by Qubit! ########
89
############### &&&&& ###############
910
############### &&&&& @@@@@@@@@@@@@@
1011
############### && @@@@@@@@@@@@@@
11-
############### & @@@@@@@@@@@@@ */
12+
############### & @@@@@@@@@@@@@
1213
14+
*/
1315
import type { Query, Mutation, Subscription } from "@qubit-rs/client";
1416
export type QubitServer = { echo_cookie: Query<[], string>, secret_endpoint: Query<[], string>, };
15-

examples/authentication/src/main.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use std::net::SocketAddr;
22

33
use axum::{
4-
Form,
54
response::{IntoResponse, Response},
65
routing::post,
6+
Form,
77
};
88
use cookie::Cookie;
9-
use hyper::{StatusCode, header::SET_COOKIE};
10-
use qubit::{ErrorCode, Extensions, FromRequestExtensions, Router, RpcError, handler};
9+
use hyper::{header::SET_COOKIE, StatusCode};
10+
use qubit::{handler, ErrorCode, Extensions, FromRequestExtensions, Router, RpcError, TypeScript};
1111
use serde::Deserialize;
1212
use tokio::net::TcpListener;
1313
use tower::ServiceBuilder;
@@ -114,9 +114,12 @@ async fn main() {
114114
let router = Router::<()>::new()
115115
.handler(echo_cookie)
116116
.handler(secret_endpoint);
117-
router.generate_type("./auth-demo/src/bindings.ts").unwrap();
117+
router
118+
.as_codegen()
119+
.write_type("./auth-demo/src/bindings.ts", TypeScript::new())
120+
.unwrap();
118121

119-
let (qubit_service, handle) = router.into_service(());
122+
let (qubit_service, handle) = router.as_rpc(()).into_service();
120123

121124
let qubit_service = ServiceBuilder::new()
122125
.map_request(|mut req: hyper::Request<_>| {

examples/chaos/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/chaos/bindings.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
/* eslint-disable */
22
// @ts-nocheck
3-
/* @@@@@@@@@@@@@ & ###############
3+
/*
4+
@@@@@@@@@@@@@ & ###############
45
@@@@@@@@@@@@@@ &&& ###############
56
@@@@@@@@@@@@@@ &&&&& ###############
67
############### &&&&& ###############
78
######## Generated by Qubit! ########
89
############### &&&&& ###############
910
############### &&&&& @@@@@@@@@@@@@@
1011
############### && @@@@@@@@@@@@@@
11-
############### & @@@@@@@@@@@@@ */
12+
############### & @@@@@@@@@@@@@
1213
14+
*/
1315
import type { Query, Mutation, Subscription } from "@qubit-rs/client";
1416
export type User = { name: string, email: string, age: number, metadata: Metadata, };
17+
export type Test = { a: number, b: boolean, };
18+
export type Metadata = { param_a: string, param_b: number, param_c: boolean, more_metadata: Metadata | null, };
1519
export type UniqueType = { value: number, };
1620
export type NestedStruct = { a: number, b: boolean, };
17-
export type Test = { a: number, b: boolean, };
1821
export type MyEnum = "A" | { "B": number } | { "C": { field: number, } } | { "D": NestedStruct };
19-
export type Metadata = { param_a: string, param_b: number, param_c: boolean, more_metadata: Metadata | null, };
20-
export type QubitServer = { user: { asdf: Query<[], null>, create: Mutation<[name: string, email: string, age: number], User>, list: Query<[], Array<Test>>, someHandler: Query<[_id: string], User>, }, array: Query<[], Array<string>>, array_type: Query<[], Array<UniqueType>>, count: Mutation<[], number>, countdown: Subscription<[min: number, max: number], number>, enum_test: Query<[], MyEnum>, version: Query<[], string>, };
21-
22+
export type QubitServer = { array: Query<[], Array<string>>, array_type: Query<[], Array<UniqueType>>, count: Mutation<[], number>, countdown: Subscription<[min: number, max: number], number>, enum_test: Query<[], MyEnum>, version: Query<[], string>, user: { asdf: Query<[], null>, create: Mutation<[name: string, email: string, age: number], User>, list: Query<[], Array<Test>>, someHandler: Query<[_id: string], User>, }, };

examples/chaos/src/main.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use std::{
22
net::SocketAddr,
33
sync::{
4-
Arc,
54
atomic::{AtomicUsize, Ordering},
5+
Arc,
66
},
77
time::Duration,
88
};
99

10-
use futures::{Stream, StreamExt, stream};
10+
use futures::{stream, Stream, StreamExt};
1111
use qubit::*;
1212

1313
use axum::routing::get;
@@ -213,10 +213,12 @@ async fn main() {
213213
.nest("user", user::create_router());
214214

215215
// Save the router's bindings
216-
app.generate_type("./bindings.ts").unwrap();
216+
app.as_codegen()
217+
.write_type("./bindings.ts", TypeScript::new())
218+
.unwrap();
217219

218220
// Create a service and handle for the app
219-
let (app_service, app_handle) = app.into_service(AppCtx::default());
221+
let (app_service, app_handle) = app.as_rpc(AppCtx::default()).into_service();
220222

221223
// Set up the axum router
222224
let router = axum::Router::<()>::new()

0 commit comments

Comments
 (0)