Stone.js API
    Preparing search index...

    Interface IResource<Model, Output, EventType, PrincipalType>

    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.

    interface IResource<
        Model = unknown,
        Output = ResourceOutput,
        EventType = unknown,
        PrincipalType = unknown,
    > {
        collection(
            models: Model[],
            context?: ResourceContext<EventType, PrincipalType>,
        ): Promise<Output[]>;
        data?(
            model: Model,
            context: ResourceContext<EventType, PrincipalType>,
        ): unknown;
        fragments?(
            context: ResourceContext<EventType, PrincipalType>,
        ): Record<string, unknown> | Promise<Record<string, unknown>>;
        item(
            model: Model,
            context?: ResourceContext<EventType, PrincipalType>,
        ): Promise<Output>;
        response(
            models: Model | Model[],
            context?: ResourceContext<EventType, PrincipalType>,
            meta?: Record<string, unknown>,
        ): Promise<ResourceEnvelope<Output | Output[]>>;
        schema(context: ResourceContext<EventType, PrincipalType>): unknown;
    }

    Type Parameters

    • Model = unknown
    • Output = ResourceOutput
    • EventType = unknown
    • PrincipalType = unknown

    Implemented by

    Index
    • Optional hook to shape or complete the model before it meets the schema.

      Asynchronous, and resolved from the container, so it may reach any service: fetch a relation, translate a label, compute a total. Whatever it returns is what the schema then validates.

      Parameters

      Returns unknown

    • Named subsets a caller may ask for, each with its own schema.

      A fragment is not a filter: it is a contract of its own, documented and validated like the full one. That is what makes ?view=summary safe to expose.

      Parameters

      Returns Record<string, unknown> | Promise<Record<string, unknown>>