forge-codegen 0.10.2

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

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

import { getForgeClient } from "@forge-rs/svelte";
import type {
  AvatarInput,
} from "./types";

// Mutations
export const uploadAvatar = (args: AvatarInput): Promise<string> =>
  getForgeClient().call("upload_avatar", args);
export const uploadFile = (args: { file: File | Blob }): Promise<string> =>
  getForgeClient().call("upload_file", args);

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

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

import { uploadAvatar, uploadFile } from "./api";
import { toReactiveMutation, type ReactiveMutation } from "./runes.svelte";
import type { AvatarInput } from "./types";
export const uploadAvatar$ = (): ReactiveMutation<AvatarInput, string> =>
  toReactiveMutation(uploadAvatar);
export const uploadFile$ = (): ReactiveMutation<{ file: File | Blob }, string> =>
  toReactiveMutation(uploadFile);

=== 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 AvatarInput {
  name: string;
  file: File | Blob;
}

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, ForgeUpload, 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 upload_avatar(client: &ForgeClient, args: AvatarInput) -> Result<String, ForgeClientError> {
    client.call("upload_avatar", args).await
}

pub fn use_upload_avatar() -> Mutation<AvatarInput, String> {
    use_forge_mutation("upload_avatar")
}
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub struct UploadFileParams {
    pub file: ForgeUpload,
}
impl UploadFileParams {
    pub fn new(file: ForgeUpload) -> Self {
        Self {
            file,
        }
    }
}

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

pub fn use_upload_file() -> Mutation<UploadFileParams, String> {
    use_forge_mutation("upload_file")
}

=== 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};
use forge_dioxus::ForgeUpload;

#[derive(Debug, Clone)]
pub struct AvatarInput {
    pub name: String,
    pub file: ForgeUpload,
}

impl AvatarInput {
    pub fn new(name: impl Into<String>, file: ForgeUpload) -> Self {
        Self {
            name: name.into(),
            file: file,
        }
    }
}