forge-codegen 0.10.2

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

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

import { getForgeClient, createSubscriptionStore } from "@forge-rs/svelte";

// Queries
export const count = (): Promise<number> =>
  getForgeClient().call("count", null);
export const echoString = (args: { value: string }): Promise<string> =>
  getForgeClient().call("echo_string", args);
export const firstName = (args: { id: number }): Promise<string | null> =>
  getForgeClient().call("first_name", args);
export const listIds = (): Promise<number[]> =>
  getForgeClient().call("list_ids", null);
export const listOptionalStrings = (): Promise<Array<string | null>> =>
  getForgeClient().call("list_optional_strings", null);

// Subscriptions
export const countStore$ = () =>
  createSubscriptionStore<null, number>("count", null);
export const echoStringStore$ = (args: { value: string }) =>
  createSubscriptionStore<{ value: string }, string>("echo_string", args);
export const firstNameStore$ = (args: { id: number }) =>
  createSubscriptionStore<{ id: number }, string | null>("first_name", args);
export const listIdsStore$ = () =>
  createSubscriptionStore<null, number[]>("list_ids", null);
export const listOptionalStringsStore$ = () =>
  createSubscriptionStore<null, Array<string | null>>("list_optional_strings", null);

// Mutations
export const setFlag = (args: { flag: boolean }): Promise<boolean> =>
  getForgeClient().call("set_flag", args);
export const weightedSum = (args: { weights: number[] }): Promise<number> =>
  getForgeClient().call("weighted_sum", 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 { countStore$, echoStringStore$, firstNameStore$, listIdsStore$, listOptionalStringsStore$, setFlag, weightedSum } from "./api";
import { toReactive, type ReactiveQuery, toReactiveMutation, type ReactiveMutation } from "./runes.svelte";
export const count$ = (): ReactiveQuery<number> =>
  toReactive(countStore$());
export const echoString$ = (args: { value: string }): ReactiveQuery<string> =>
  toReactive(echoStringStore$(args));
export const firstName$ = (args: { id: number }): ReactiveQuery<string | null> =>
  toReactive(firstNameStore$(args));
export const listIds$ = (): ReactiveQuery<number[]> =>
  toReactive(listIdsStore$());
export const listOptionalStrings$ = (): ReactiveQuery<Array<string | null>> =>
  toReactive(listOptionalStringsStore$());
export const setFlag$ = (): ReactiveMutation<{ flag: boolean }, boolean> =>
  toReactiveMutation(setFlag);
export const weightedSum$ = (): ReactiveMutation<{ weights: number[] }, number> =>
  toReactiveMutation(weightedSum);

=== 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 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 count(client: &ForgeClient) -> Result<i64, ForgeClientError> {
    client.call("count", ()).await
}

pub fn use_count() -> QueryState<i64> {
    use_forge_query("count", ())
}

pub fn use_count_subscription() -> SubscriptionState<i64> {
    use_forge_subscription("count", ())
}
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub struct EchoStringParams {
    pub value: String,
}
impl EchoStringParams {
    pub fn new(value: impl Into<String>) -> Self {
        Self {
            value: value.into(),
        }
    }
}

pub async fn echo_string(client: &ForgeClient, args: EchoStringParams) -> Result<String, ForgeClientError> {
    client.call("echo_string", args).await
}

pub fn use_echo_string(args: EchoStringParams) -> QueryState<String> {
    use_forge_query("echo_string", args)
}

pub fn use_echo_string_subscription(args: EchoStringParams) -> SubscriptionState<String> {
    use_forge_subscription("echo_string", args)
}
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub struct FirstNameParams {
    pub id: i32,
}
impl FirstNameParams {
    pub fn new(id: i32) -> Self {
        Self {
            id,
        }
    }
}

pub async fn first_name(client: &ForgeClient, args: FirstNameParams) -> Result<Option<String>, ForgeClientError> {
    client.call("first_name", args).await
}

pub fn use_first_name(args: FirstNameParams) -> QueryState<Option<String>> {
    use_forge_query("first_name", args)
}

pub fn use_first_name_subscription(args: FirstNameParams) -> SubscriptionState<Option<String>> {
    use_forge_subscription("first_name", args)
}
pub async fn list_ids(client: &ForgeClient) -> Result<Vec<i32>, ForgeClientError> {
    client.call("list_ids", ()).await
}

pub fn use_list_ids() -> QueryState<Vec<i32>> {
    use_forge_query("list_ids", ())
}

pub fn use_list_ids_subscription() -> SubscriptionState<Vec<i32>> {
    use_forge_subscription("list_ids", ())
}
pub async fn list_optional_strings(client: &ForgeClient) -> Result<Vec<Option<String>>, ForgeClientError> {
    client.call("list_optional_strings", ()).await
}

pub fn use_list_optional_strings() -> QueryState<Vec<Option<String>>> {
    use_forge_query("list_optional_strings", ())
}

pub fn use_list_optional_strings_subscription() -> SubscriptionState<Vec<Option<String>>> {
    use_forge_subscription("list_optional_strings", ())
}
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub struct SetFlagParams {
    pub flag: bool,
}
impl SetFlagParams {
    pub fn new(flag: bool) -> Self {
        Self {
            flag,
        }
    }
}

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

pub fn use_set_flag() -> Mutation<SetFlagParams, bool> {
    use_forge_mutation("set_flag")
}
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub struct WeightedSumParams {
    pub weights: Vec<f64>,
}
impl WeightedSumParams {
    pub fn new(weights: Vec<f64>) -> Self {
        Self {
            weights,
        }
    }
}

pub async fn weighted_sum(client: &ForgeClient, args: WeightedSumParams) -> Result<f64, ForgeClientError> {
    client.call("weighted_sum", args).await
}

pub fn use_weighted_sum() -> Mutation<WeightedSumParams, f64> {
    use_forge_mutation("weighted_sum")
}

=== 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};