Skip to content

Commit

Permalink
feat: index barrel file (#87)
Browse files Browse the repository at this point in the history
Re-export all types within `index.ts`.

Closes #81
  • Loading branch information
andogq authored Dec 2, 2024
2 parents aa17d51 + 6998257 commit 1ba21cd
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 6 deletions.
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

0 comments on commit 1ba21cd

Please sign in to comment.