Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: index barrel file #87

Merged
merged 3 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/index-barrel-file.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"qubit": patch
---

Alter router type generation to re-export all types.
2 changes: 2 additions & 0 deletions examples/authentication/auth-demo/src/bindings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@

import type { Query } from "@qubit-rs/client";

export type { Query } from "@qubit-rs/client";

export type QubitServer = { echo_cookie: Query<[], string>, secret_endpoint: Query<[], string> };
10 changes: 10 additions & 0 deletions examples/chaos/bindings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,14 @@ import type { Metadata } from "./Metadata.ts";
import type { User } from "./User.ts";
import type { Test } from "./Test.ts";

export type { Query } from "@qubit-rs/client";
export type { Mutation } from "@qubit-rs/client";
export type { Subscription } from "@qubit-rs/client";
export type { NestedStruct } from "./NestedStruct.ts";
export type { MyEnum } from "./MyEnum.ts";
export type { UniqueType } from "./UniqueType.ts";
export type { Metadata } from "./Metadata.ts";
export type { User } from "./User.ts";
export type { Test } from "./Test.ts";

export type QubitServer = { version: Query<[], string>, count: Mutation<[], number>, countdown: Subscription<[min: number, max: number, ], number>, array: Query<[], Array<string>>, enum_test: Query<[], MyEnum>, array_type: Query<[], Array<UniqueType>>, user: { someHandler: Query<[_id: string, ], User>, create: Mutation<[name: string, email: string, age: number, ], User>, list: Query<[], Array<Test>>, asdf: Query<[], null> } };
5 changes: 5 additions & 0 deletions examples/chat-room-react/src/bindings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ import type { Mutation } from "@qubit-rs/client";
import type { Subscription } from "@qubit-rs/client";
import type { ChatMessage } from "./ChatMessage.ts";

export type { Query } from "@qubit-rs/client";
export type { Mutation } from "@qubit-rs/client";
export type { Subscription } from "@qubit-rs/client";
export type { ChatMessage } from "./ChatMessage.ts";

export type QubitServer = { get_name: Query<[], string>, send_message: Mutation<[message: string, ], null>, list_online: Subscription<[], Array<string>>, list_messages: Subscription<[], Array<ChatMessage>> };
4 changes: 4 additions & 0 deletions examples/counter/bindings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ import type { Mutation } from "@qubit-rs/client";
import type { Query } from "@qubit-rs/client";
import type { Subscription } from "@qubit-rs/client";

export type { Mutation } from "@qubit-rs/client";
export type { Query } from "@qubit-rs/client";
export type { Subscription } from "@qubit-rs/client";

export type QubitServer = { increment: Mutation<[], null>, decrement: Mutation<[], null>, add: Mutation<[n: number, ], null>, get: Query<[], number>, countdown: Subscription<[], number> };
2 changes: 2 additions & 0 deletions examples/hello-world/bindings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@

import type { Query } from "@qubit-rs/client";

export type { Query } from "@qubit-rs/client";

export type QubitServer = { hello_world: Query<[], string> };
18 changes: 12 additions & 6 deletions src/server/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ where
let header = String::from(include_str!("../header.txt"));

// Export all the dependencies, and create their import statements
let (imports, _types) = self
let (imports, exports, _types) = self
.get_handlers()
.into_iter()
.flat_map(|handler| {
Expand All @@ -79,10 +79,10 @@ where
.chain((handler.qubit_types)().into_iter().map(|ty| ty.to_ts()))
})
.fold(
(String::new(), HashSet::new()),
|(mut imports, mut types), ty| {
(String::new(), String::new(), HashSet::new()),
|(mut imports, mut exports, mut types), ty| {
if types.contains(&ty) {
return (imports, types);
return (imports, exports, types);
}

let (package, ty_name) = ty;
Expand All @@ -93,9 +93,15 @@ where
)
.unwrap();

writeln!(
&mut exports,
r#"export type {{ {ty_name} }} from "{package}";"#,
)
.unwrap();

types.insert((package, ty_name));

(imports, types)
(imports, exports, types)
},
);

Expand All @@ -105,7 +111,7 @@ where
// Write out index file
fs::write(
out_dir.join("index.ts"),
[header, imports, server_type]
[header, imports, exports, server_type]
.into_iter()
.filter(|part| !part.is_empty())
.collect::<Vec<_>>()
Expand Down