AdonisJS API
    Preparing search index...

    Class Item<Transformer, MaxDepth, Variant>

    Represents a single transformed item from source data. This class is typically created using the BaseTransformer.transform() method when passing a single object.

    Type Parameters

    • Transformer extends Record<string, any>

      The transformer class used to transform the item

    • MaxDepth extends Next[number]

      Maximum depth for nested transformations

    • Variant extends string

      The transformer method variant to use for serialization

      class UserTransformer extends BaseTransformer<User> {
      toObject() {
      return { id: this.resource.id, name: this.resource.name }
      }
      }

      const item = UserTransformer.transform(userData)
      const serialized = await serialize(item)
    Index
    • Creates a new Item instance. This constructor is typically not called directly. Use BaseTransformer.transform() instead.

      Type Parameters

      • Transformer extends Record<string, any>

        The transformer class used to transform the item

      • MaxDepth extends 1 | 2 | 3 | 4 | 5 | 6

        Maximum depth for nested transformations

      • Variant extends string

        The transformer method variant to use for serialization

        class UserTransformer extends BaseTransformer<User> {
        toObject() {
        return { id: this.resource.id, name: this.resource.name }
        }
        }

        const item = UserTransformer.transform(userData)
        const serialized = await serialize(item)

      Parameters

      • transformerData: [resource: any, ...rest: any[]]

        The data item to be transformed

      • transformer: new (...args: any[]) => Transformer

        Constructor for the transformer class

      • maxDepth: MaxDepth

        Maximum depth for nested transformations

      • variant: Variant

        Variant method name to use for transformation

      • OptionaldebuggingError: RuntimeException

        Error thrown when the resource is undefined. Create it at the transformer call site to get a useful stack trace

      Returns Item<Transformer, MaxDepth, Variant>

    $type: "item" = 'item'

    Type identifier for the item resource

    transformer: new (...args: any[]) => Transformer

    Constructor for the transformer class

    maxDepth: MaxDepth

    Maximum depth for nested transformations

    variant: Variant

    Variant method name to use for transformation

    • Specify the maximum depth of nested relationships to be resolved when serializing the item.

      This is useful to prevent infinite recursion or to limit the amount of data fetched for deeply nested relationships. Relationships beyond this depth will be excluded from the serialized output.

      Type Parameters

      • T extends 1 | 2 | 3 | 4 | 5 | 6

      Parameters

      • value: T

        Maximum depth level for nested transformations (valid values: 1, 2, 3, 4, 5, or 6)

        // Limit depth to 3 levels
        const user = UserTransformer.transform(userData)
        .depth(3)

        // This allows: user -> posts -> comments -> author (3 levels)
        // But excludes: user -> posts -> comments -> author -> profile (4 levels)

      Returns Item<Transformer, T, Variant>

    • Specify which transformer variant method to use for serialization.

      Transformers can define multiple methods (variants) for different representations of the same data (e.g., toObject, toSummary, toDetailed). This method allows you to choose which variant to use for this item.

      Type Parameters

      • V extends string

      Parameters

      • value: V

        Name of the transformer variant method to use (must be a method name that returns ResourceData or Promise)

        // Use default toObject variant
        const user = UserTransformer.transform(userData)

        // Use custom toSummary variant for lighter payload
        const userSummary = UserTransformer.transform(userData)
        .useVariant('toSummary')

        // Use detailed variant for admin view
        const detailedUser = UserTransformer.transform(userData)
        .useVariant('toDetailed')

      Returns Item<Transformer, MaxDepth, V>

    • Resolve the item by transforming it and resolving nested relationships. This method is typically called internally by the serializer function.

      The resolution process:

      1. Validates that the transformer data is not undefined
      2. Creates a transformer instance with the data
      3. Calls the variant method on the transformer
      4. Recursively resolves nested relationships up to the specified depth
      5. Returns the resolved object

      Parameters

      • container: ContainerResolver<any>

        AdonisJS container resolver for dependency injection

      • depth: number

        Current depth level in the transformation tree

      • OptionalmaxDepth: number

        Optional maximum depth override. When set to -1, uses unlimited depth

      Returns Promise<InferData<Transformer, Variant, -1, 0>>