zerot
    Preparing search index...

    Function contract

    • The main contract decorator. Applies a set of pre-conditions, post-conditions, and invariants to a method.

      Type Parameters

      • Method extends (...args: any[]) => Promise<any>
      • TInput = Parameters<Method>[0]

        The expected type of the input to the decorated method.

      • TContext extends AuthContext = Parameters<Method> extends [any, C]
            ? C extends AuthContext ? C<C> : AuthContext
            : AuthContext

        The expected type of the authentication context.

      • TOutput = Method extends (...args: any[]) => Promise<R> ? R : ReturnType<Method>

        The expected type of the output of the decorated method.

      Parameters

      Returns (
          target: any,
          propertyName: string,
          descriptor: PropertyDescriptor,
      ) => PropertyDescriptor

      A method decorator function.

      class UserService {
      @contract<UserCreateInput, User, AuthContext>({
      requires: [auth("admin"), validates(UserSchema)],
      ensures: [returns(UserSchema)],
      invariants: [(input, output) => output.id === input.id],
      layer: "business"
      })
      async createUser(input: UserCreateInput, context?: AuthContext): Promise<User> {
      // Business logic to create a user
      return { id: "123", name: input.name, email: input.email, roles: ["user"] };
      }
      }