npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

type-properties

v0.0.31

Published

Runtime properties for TypeScript

Downloads

6

Readme

TypeProperties

The TypeProperties is an experimental project that should help you create better TypeScript NodeJS Rest APIs. It enables you to create serializeable models that are JSON type safe and fully type checked.

The TypeProperties is a solution for "writing type once", this is particularly useful when Metadata Reflect API is not enough, as Reflect is only coerce when types are solely based on typeof and instanceof. In all the other cases when you wrap your type into an array or make it nullable with union types, Reflect stops preserving types.

Example usage

import {  
  Properties,
  Property,
  Factory,
  Nullable,
  ListOf,
  Descr,
  Default,
} from '@type-properties/core';

enum UserStatus {
    ACTIVE = 0;
    INACTIVE = 1;
}


@Properties()  
export class UserProps extends UserIdentifierProperties {  
    
  @Descr('The user id')  
  id = Property[0](
    type => String,
  );  
  
  @Descr(`The user email`)  
  email = Property[3](
    type => Nullable(String),
  );  
    
  @Descr(`The user given name`)  
  givenName = Property[1](
    type => String,
  );  
    
  @Descr(`The user family name`)  
  familyName = Property[2](
    type => String,  
  );  
    
  displayName = Property[4](  
    type => String,  
    init => `${this.givenName} ${this.familyName}`,
  );  
    
  @Default(value => new Date()) 
  registeredAt = Property[5](
    type => Date,
  );  
    
  followers = Property[6](
    type => ListOf(User),
    init => [],
  );
  
  @Default(value => 'ACTIVE')
  status? = Property[7](
    type => UserStatus,
  );

}  
  
@Descr(`The user type`)  
export class User extends Factory(UserProps) {}  
  
const user = new User({  
  id: '1',  
  givenName: 'John',  
  familyName: 'Doe',  
});  
  
const parsedUser = User.fromJSONString(JSON.stringify(user));

if (
  parsedUser instanceof User
    && parsedUser instanceof UserProps,
    && parsedUser.registeredAt instanceof Date // witch means it unserializes Date objects
) {  
  // this will be true  
}
TypeProperties gives you more abstraction to model types
// Static members
export interface UserStatic {
  /**
   * The properties class
   */
  Properties: UserProperties;
  /**
   * The properties factory serializer
   */
  Serializer: JSONSerializer;

  /**
   * Get factory property definitions
   */
  getProperties(): ArrayLikePropertyDefinitions;

  /**
   * Get factory property names
   */
  getPropertyNames(): TupleKeys<UserProperties>;

  /**
   * Transform plain object to properties factory
   * @param obj
   */
  fromPlainObject(obj: PlainObject<UserProperties>): User;

  /**
   * Transform json serialized object into properties factory
   * @param json
   */
  fromJSON(json: { [key: string]: any } | any[]): User;

  /**
   * Transform json serialized string into properties factory
   * @param jsonString
   */
  fromJSONString(jsonString: any): User;

  /**
   * Transform property tuple values into properties factory
   * @param values
   */
  fromValues(values: TupleValues<UserProperties>): User;

  /**
   * Transform property tuple values into properties factory with strict values
   * @param values
   */
  fromStrictValues(values: StrictTupleValues<UserProperties>): User;

  /**
   * Encodes properties factory to base64 string
   * @param forge
   * @param encoding
   */
  stringify(forge: Forge<UserProperties>, encoding?: 'hex' | 'base64'): string;

  /**
   * Parse a base64 string into properties factory
   * @param str
   * @param encoding
   */
  parse(str: string, encoding?: 'hex' | 'base64'): User;

  /**
   * Encodes properties factory into array of bytes
   * @param forge
   * @param encoding
   */
  encode(forge: Forge<UserProperties>): Uint8Array;

  /**
   * Decodes array of bytes into properties factory
   * @param bytes
   */
  decode(bytes: Uint8Array): User;
}

// Prototype members
interface User {
  /**
   * Transform properties factory into tuple of property values
   */
  toValues(): TupleValues<UserProperties>;

  /**
   * Return all property keys
   */
  toKeys(): TupleKeys<UserProperties>;

  /**
   * Transform properties factory into plain object
   */
  toPlainObject(): PlainObject<UserProperties>;

  /**
   * Transform properties factory into json ready object
   */
  toJSON(): { [key: string]: any };

  /**
   * Transform properties factory into json ready tuple of values
   */
  toJSONValues(): any[];

  /**
   * Stringify
   * @param encoding
   */
  stringify(encoding?: 'hex' | 'base64'): string;

  /**
   * Transform serializeable factory into bytes
   */
  encode(): Uint8Array;
}