forge-codegen 0.10.2

TypeScript code generator for the Forge framework
Documentation
---
source: crates/forge-codegen/tests/snapshot.rs
assertion_line: 33
expression: "run_fixture(include_str!(\"fixtures/custom_args.rs.txt\"), false)"
---

=== ts/api.ts ===
// @generated by FORGE - DO NOT EDIT

import { getForgeClient, createSubscriptionStore } from "@forge-rs/svelte";
import type {
  CreateTodoInput,
  Todo,
  UpdateTodoInput,
} from "./types";

// Queries
export const listTodos = (): Promise<Todo[]> =>
  getForgeClient().call("list_todos", null);

// Subscriptions
export const listTodosStore$ = () =>
  createSubscriptionStore<null, Todo[]>("list_todos", null);

// Mutations
export const createTodo = (args: CreateTodoInput): Promise<Todo> =>
  getForgeClient().call("create_todo", args);
export const deleteTodo = (args: { id: string }): Promise<boolean> =>
  getForgeClient().call("delete_todo", args);
export const updateTodo = (args: UpdateTodoInput): Promise<Todo> =>
  getForgeClient().call("update_todo", args);

=== ts/index.ts ===
// @generated by FORGE - DO NOT EDIT
export * from './types';
export * from './api';
export * from './stores';
export * from './runes.svelte';
export * from './reactive.svelte';
export { ForgeClient, ForgeClientError, createForgeClient, ForgeProvider } from '@forge-rs/svelte';

=== ts/reactive.svelte.ts ===
// @generated by FORGE - DO NOT EDIT

import { listTodosStore$, createTodo, deleteTodo, updateTodo } from "./api";
import { toReactive, type ReactiveQuery, toReactiveMutation, type ReactiveMutation } from "./runes.svelte";
import type { CreateTodoInput, Todo, UpdateTodoInput } from "./types";
export const listTodos$ = (): ReactiveQuery<Todo[]> =>
  toReactive(listTodosStore$());
export const createTodo$ = (): ReactiveMutation<CreateTodoInput, Todo> =>
  toReactiveMutation(createTodo);
export const deleteTodo$ = (): ReactiveMutation<{ id: string }, boolean> =>
  toReactiveMutation(deleteTodo);
export const updateTodo$ = (): ReactiveMutation<UpdateTodoInput, Todo> =>
  toReactiveMutation(updateTodo);

=== ts/runes.svelte.ts ===
// @generated by FORGE - DO NOT EDIT
import type { SubscriptionStore, ForgeError } from "@forge-rs/svelte";
import type { SubscriptionResult } from "@forge-rs/svelte";
import { ForgeClientError } from "@forge-rs/svelte";

export interface ReactiveQuery<T> extends SubscriptionResult<T> {
  unsubscribe: () => void;
}

export function toReactive<T>(store: SubscriptionStore<T>): ReactiveQuery<T> {
  const state: ReactiveQuery<T> = $state({
    loading: true,
    data: null,
    error: null,
    stale: false,
    unsubscribe: () => {},
  });

  $effect(() => {
    const unsubCallback = store.subscribe((s) => {
      state.loading = s.loading;
      state.data = s.data;
      state.error = s.error;
      state.stale = s.stale;
    });

    const cleanup = () => {
      unsubCallback();
      store.unsubscribe();
    };

    state.unsubscribe = cleanup;
    return cleanup;
  });

  return state;
}

export interface ReactiveMutation<TArgs, TResult> {
  mutate: (args: TArgs) => Promise<TResult>;
  pending: boolean;
  error: ForgeError | null;
}

export function toReactiveMutation<TArgs, TResult>(
  fn: (args: TArgs) => Promise<TResult>,
): ReactiveMutation<TArgs, TResult> {
  const state: ReactiveMutation<TArgs, TResult> = $state({
    mutate: async (args: TArgs) => {
      state.pending = true;
      state.error = null;
      try {
        return await fn(args);
      } catch (e) {
        const err =
          e instanceof ForgeClientError
            ? e
            : new ForgeClientError("UNKNOWN", String(e));
        state.error = err;
        throw e;
      } finally {
        state.pending = false;
      }
    },
    pending: false,
    error: null,
  });
  return state;
}

=== ts/stores.ts ===
// @generated by FORGE - DO NOT EDIT
export {
  getForgeClient,
  createConnectionStore,
  createQueryStore,
  createSubscriptionStore,
  createJobStore,
  createWorkflowStore,
} from '@forge-rs/svelte';
export type {
  Readable,
  ConnectionStatusStore,
  QueryStore,
  SubscriptionStore,
  JobStore,
  WorkflowStore,
} from '@forge-rs/svelte';

=== ts/types.ts ===
// @generated by FORGE - DO NOT EDIT

export interface CreateTodoInput {
  title: string;
}

export interface Todo {
  id: string;
  title: string;
  completed: boolean;
}

export interface UpdateTodoInput {
  id: string;
  title?: string;
  completed?: boolean;
}

export type Cursor = string;

export interface PageInfo {
  has_next_page: boolean;
  end_cursor?: Cursor;
  total_count?: number;
}

export interface Page<T> {
  items: T[];
  page_info: PageInfo;
}

export type { ForgeError, QueryResult, SubscriptionResult } from "@forge-rs/svelte";

=== dioxus/api.rs ===
// @generated by FORGE - DO NOT EDIT

#![allow(dead_code, unused_imports, clippy::redundant_field_names, clippy::too_many_arguments, clippy::wrong_self_convention)]

use forge_dioxus::{
    ForgeClient, ForgeClientError, Mutation, QueryState,
    SubscriptionState, JobExecutionState, TokenPair, WorkflowExecutionState,
};

use super::types::*;
use super::{
    use_forge_query, use_forge_subscription,
    use_forge_mutation, use_forge_job, use_forge_workflow,
};

pub async fn list_todos(client: &ForgeClient) -> Result<Vec<Todo>, ForgeClientError> {
    client.call("list_todos", ()).await
}

pub fn use_list_todos() -> QueryState<Vec<Todo>> {
    use_forge_query("list_todos", ())
}

pub fn use_list_todos_subscription() -> SubscriptionState<Vec<Todo>> {
    use_forge_subscription("list_todos", ())
}
pub async fn create_todo(client: &ForgeClient, args: CreateTodoInput) -> Result<Todo, ForgeClientError> {
    client.call("create_todo", args).await
}

pub fn use_create_todo() -> Mutation<CreateTodoInput, Todo> {
    use_forge_mutation("create_todo")
}
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub struct DeleteTodoParams {
    pub id: String,
}
impl DeleteTodoParams {
    pub fn new(id: impl Into<String>) -> Self {
        Self {
            id: id.into(),
        }
    }
}

pub async fn delete_todo(client: &ForgeClient, args: DeleteTodoParams) -> Result<bool, ForgeClientError> {
    client.call("delete_todo", args).await
}

pub fn use_delete_todo() -> Mutation<DeleteTodoParams, bool> {
    use_forge_mutation("delete_todo")
}
pub async fn update_todo(client: &ForgeClient, args: UpdateTodoInput) -> Result<Todo, ForgeClientError> {
    client.call("update_todo", args).await
}

pub fn use_update_todo() -> Mutation<UpdateTodoInput, Todo> {
    use_forge_mutation("update_todo")
}

=== dioxus/mod.rs ===
// @generated by FORGE - DO NOT EDIT

#![allow(dead_code, unused_imports)]

pub mod api;
pub mod types;

pub use api::*;
pub use forge_dioxus::{
    ConnectionState, ForgeAuth, ForgeAuthProvider, ForgeClient, ForgeClientConfig,
    ForgeClientError, ForgeError, ForgeProvider, ForgeUpload, JobExecutionState, Mutation,
    QueryState, SignalError, SubscriptionHandle, SubscriptionState, TokenPair, WorkflowExecutionState,
    use_auth_key, use_connection_state, use_forge_auth, use_forge_client, use_forge_job,
    use_forge_mutation, use_forge_query, use_forge_subscription, use_forge_workflow,
    use_viewer,
};
pub use types::*;

=== dioxus/types.rs ===
// @generated by FORGE - DO NOT EDIT

#![allow(dead_code, unused_imports, clippy::redundant_field_names, clippy::too_many_arguments)]

use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CreateTodoInput {
    pub title: String,
}

impl CreateTodoInput {
    pub fn new(title: impl Into<String>) -> Self {
        Self {
            title: title.into(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Todo {
    pub id: String,
    pub title: String,
    pub completed: bool,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct UpdateTodoInput {
    pub id: String,
    pub title: Option<String>,
    pub completed: Option<bool>,
}

impl UpdateTodoInput {
    pub fn new(id: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            title: None,
            completed: None,
        }
    }

    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }

    pub fn completed(mut self, completed: bool) -> Self {
        self.completed = Some(completed);
        self
    }
}