Foscia

website·Indexed 32 days ago

https://foscia.dev/

A TypeScript-based framework providing tools for serialization, deserialization, and API implementations including REST, JSON:API, and HTTP. It includes a suite of packages such as @foscia/serialization, @foscia/shared, and @foscia/test for mocking and testing.

Tokens
95.8K
Snippets
692
Records
863
Agent score
47%

What's inside Foscia

  1. Overview of Foscia API consumption ORM

    Foscia acts as an ORM for API consumption, allowing developers to interact with standardized APIs (HTTP, REST, or JSON:API) without manually managing serialization and error handling. It provides type safety via TypeScript, a caching system to prevent redundant fetches, and lifecycle hooks for operations like saving or deleting.
  2. Overview of Foscia API client

    Foscia is a framework-agnostic, type-safe, and modular API/data client for JS/TS that acts as an intermediary layer (similar to an ORM) between an application and its data source (API or database). It is built with functional programming principles and is fully tree-shakable. Key features include:

    • Support for REST and JSON:API backends via built-in adapters.
    • Strong typing of data structures using TypeScript.
    • Lifecycle change tracking via instance snapshots.
    • Hook registration for operations like saving or deleting.
    • Integrated caching system to prevent redundant data fetching.
    • Built-in CLI for rapid setup.
  3. Understand Foscia's functional approach to Models and Builders

    Unlike frameworks that use large Model and Builder classes, Foscia uses simple functions to affect model instances and their context. This architectural choice ensures that unused model helpers, action enhancers, or runners are tree-shaken from the final production bundle, reducing bundle size.
  4. Use the JsonApiDocument type for JSON:API responses

    The JsonApiDocument type alias defines the top-level structure of a JSON:API document. It is an object that can contain the primary data, error messages, included related resources, and metadata about the API version and the request.
    // Example of a JsonApiDocument structure
    interface JsonApiDocument {
      data?: JsonApiResource[] | JsonApiResource | JsonApiNewResource | null;
      errors?: JsonApiError[];
      included?: JsonApiResource[];
      jsonapi?: {
        meta?: JsonApiMeta;
        version?: string;
      };
      links?: JsonApiLinks;
      meta?: JsonApiMeta;
    }
  5. Understand Foscia's integration architecture

    Foscia integrates into an application as a layer between the UI/business logic and the data source. The integration workflow consists of:

    1. Defining the data structure using models (attributes and relations).
    2. Customizing data interactions using actions (such as serialization).
    3. Establishing a clear exchange with the data source via adapters.
  6. Register a saving hook for a Model instance

    Use onSaving(model, callback) to register a callback that triggers when a specific Model instance is about to be saved. The function returns a cleanup function that, when called, removes the registered hook.
    import { onSaving } from '@foscia/core';
    
    // Example usage
    const stopListening = onSaving(myModel, (model) => {
      console.log('Model is saving...', model);
    });
    
    // To remove the hook:
    stopListening();
  7. Register a saving hook for a ModelComposable

    Use onSaving(composable, callback) to register a callback that triggers when a ModelComposable is about to be saved. The function returns a cleanup function to unsubscribe from the hook.
    import { onSaving } from '@foscia/core';
    
    const stopListening = onSaving(myComposable, (composable) => {
      console.log('Composable is saving...');
    });
  8. Register a saving hook for a ModelFactory

    Use onSaving(factory, callback) to register a callback that triggers when a ModelFactory is about to be saved. The function returns a cleanup function to unsubscribe from the hook.
    import { onSaving } from '@foscia/core';
    
    const stopListening = onSaving(myFactory, (factory) => {
      console.log('Factory is saving...');
    });
  9. Use the ModelSnapshot<M> type for model snapshots

    The ModelSnapshot<M> type alias represents a snapshot of a model class or instance. It is a generic object where M (defaulting to any) represents the model type. It contains metadata about the model's state and its raw values.
    // Example of the ModelSnapshot structure
    type ModelSnapshot<M = any> = {
      $exists: boolean;
      $loaded: Dictionary<true>;
      $model: ModelClass;
      $raw: any;
      $values: Partial<ModelValues<M>>;
    };