Stone.js API
    Preparing search index...

    Class Resource<Model, Output, EventType, PrincipalType>Abstract

    What a resource does: turn a domain model into the shape a caller is allowed to see, and say what that shape is.

    Saying it is the point. A projection written as code answers "what does this return?" only by being read and trusted; a projection written as a schema answers it to a person, to @stone-js/openapi, and to the resource itself, which validates against it before anything leaves. One declaration, three consumers, and no way for the documentation to drift from the response.

    Type Parameters

    Implements

    Index
    • Type Parameters

      Parameters

      • dependencies: ResourceDependencies = {}

        Auto-wired services.

        One name, bound by this module's own blueprint, so the container resolves it like any other service and this constructor reads it plainly. That is the whole point: a dependency read off a container that never bound it is not optional, it throws, which is what made every container-resolved resource fail. The answer was to register the checker, not to test for its presence.

        One name and no more, because destructuring reads each one: a resource must not have to know which services happen to be bound. The violation policy is configuration, and it travels with the request in the context, from stone.resources.onViolation. Substituting the dialect is a matter of binding contractChecker yourself.

      Returns Resource<Model, Output, EventType, PrincipalType>

    • Project a collection.

      Sequential rather than concurrent: data() may reach a database or an API, and a hundred models turning into a hundred simultaneous calls is a denial of service an application performs on itself. A resource that wants concurrency batches inside its own data(), where it knows the cost.

      Parameters

      Returns Promise<Output[]>

      The projected collection.

    • Optional: shape or complete the model before it meets the schema.

      A method signature, deliberately, against the repository's own lint rule: a property-typed function is contravariant on its parameters, so a subclass narrowing the context was rejected, and TypeScript separately refuses a method where the base declared a property, so async data (model, context) {} was rejected too. The rule's soundness argument is theoretical for an extension point; the cost was measured, in an application that could not type its resources.

      Parameters

      Returns unknown

    • Project one model.

      The order is the design: complete the data, choose the contract the caller asked for, hold the result against it, then narrow. Validation happens before narrowing, so the promise is checked against everything the resource produced rather than against whatever survived a query parameter.

      Parameters

      Returns Promise<Output>

      The projected output.

      When the data breaks the contract and the policy is throw.

    • Hold the data against the contract, and return what the contract describes.

      The schema is the projection: its parsed value is the output, so a field the contract does not mention is not exposed, whatever the model gains later.

      Parameters

      • data: unknown

        The completed data.

      • schema: unknown

        The contract.

      • context: ResourceContext

        The resource context.

      Returns Promise<unknown>

      The projected value.

      When the data breaks the contract and the policy is throw.

    • The schema to hold this projection against: the requested fragment when the resource exposes one, the full contract otherwise.

      An unknown fragment falls back to the full contract rather than failing. A caller guessing ?view=nonsense is asking a question, not attacking: answering the documented shape is more useful than a 500, and the fragments a resource exposes are published in the contract anyway.

      Parameters

      Returns Promise<unknown>

      The schema.

    • Include a value only when condition holds (otherwise the field is dropped).

      Type Parameters

      • T

      Parameters

      • condition: boolean

        Whether to include the value.

      • value: T | (() => T)

        The value, or a lazy factory (only evaluated when included).

      Returns T

      The value, or undefined.

    • Include a value only when the relation was requested through context.include.

      Type Parameters

      • T

      Parameters

      • context: ResourceContext

        The resource context.

      • name: string

        The relation name.

      • value: T | (() => T)

        The value, or a lazy factory (only evaluated when included).

      Returns T

      The value, or undefined.