@metreeca/blue - v0.10.0
    Preparing search index...

    Function resource

    Creates resource shapes.

    • Creates a resource shape from property definitions.

      Accepts Member values including full Property definitions, naked SetShape values for the concise syntax, and Id/Type markers. Property IRIs are resolved against defaultNamespace; use the constraints overload to declare a different default namespace or to inherit from parent shapes.

      Tip

      name: required(string()) is equivalent to name: property(required(string())).

      Type Parameters

      • E extends Members

        The entries record type

      Parameters

      • entries: E

        The property definitions, mapping property names to entries

      Returns Omit<ResourceShape, "model"> & { model: Prototype<E> }

      An immutable resource shape with the specified entries

      If entry definitions are invalid (for example, duplicate id/type markers)

      const Person = resource({
      name: required(string()), // naked range
      age: property(optional(integer())) // full property
      });
    • Creates a resource shape from constraints and property definitions.

      Accepts Member values including full Property definitions, naked SetShape values for the concise syntax, and Id/Type markers. The constraints argument may declare a custom namespace, target classes, identifier patterns, resource-level validators, or parent shapes via extends.

      Tip

      When constraints includes extends, parent shapes are recursively flattened and merged into the returned shape. Consumers can work with the result directly without traversing the inheritance chain. The extends field is retained for reference, but all inherited constraints are already resolved.

      Note

      This function is idempotent: the returned shape is branded and won't be re-flattened if used as a parent in another shape.

      Type Parameters

      • const C extends ResourceConstraints

        The constraints type, used to infer the inherited model

      • E extends Members

        The entries record type

      • M extends Merged<
            Merged<
                {
                    [K in string
                    | number
                    | symbol as undefined extends {
                        readonly [K in string
                        | number
                        | symbol]: Slot<E[K]>
                    }[K]
                        ? never
                        : K]: { readonly [K in string
                    | number
                    | symbol]: Slot<E[K]> }[K]
                } & {
                    [K in string
                    | number
                    | symbol as undefined extends {
                        readonly [K in string
                        | number
                        | symbol]: Slot<E[K]>
                    }[K]
                        ? K
                        : never]?: { readonly [K in string
                    | number
                    | symbol]: Slot<E[K]> }[K]
                },
            > & Omit<Inheritance<C>, keyof E>,
        > = Merged<
            Merged<
                {
                    [K in string
                    | number
                    | symbol as undefined extends {
                        readonly [K in string
                        | number
                        | symbol]: Slot<E[K]>
                    }[K]
                        ? never
                        : K]: { readonly [K in string
                    | number
                    | symbol]: Slot<E[K]> }[K]
                } & {
                    [K in string
                    | number
                    | symbol as undefined extends {
                        readonly [K in string
                        | number
                        | symbol]: Slot<E[K]>
                    }[K]
                        ? K
                        : never]?: { readonly [K in string
                    | number
                    | symbol]: Slot<E[K]> }[K]
                },
            > & Omit<Inheritance<C>, keyof E>,
        >

        The composed model type, combining local entries with inherited entries

      Parameters

      • constraints: C & {
            validators?: readonly [
                (value: M) => Trace | undefined,
                (value: M) => Trace | undefined,
            ];
        }

        Shape constraints including namespace, name, validators, and optionally extends

      • entries: E & Override<E, Inheritance<C>>

        The property definitions, mapping property names to entries

      Returns Omit<ResourceShape, "model"> & { model: M }

      An immutable resource shape with inherited constraints and entries flattened and merged

      If entry definitions are invalid or inherited constraints are incompatible

      // without inheritance
      const Person = resource({ namespace: schema }, {
      name: required(string()) // naked range
      });

      // with inheritance
      const Employee = resource({ extends: Person }, {
      department: required(string()) // naked range
      });