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

@dogmajs/core

v0.0.33

Published

Serializable object for TypeScript

Downloads

4

Readme

Dogma

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

The Dogma is a also 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 Dogma from 'dogmajs';

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

Dogma(UserStatus, {
  name: 'UserStatus',
  comment: `The User status`,
});

@Dogma({
  comment: `A resource owner can be an User or an Organization`,
})
export abstract class ResourceOwner extends Dogma.Object {

  @Dogma.comment `The resource owner id`
  id = Dogma[0].required(String);
  
  @Dogma.comment `The resource owner display name`
  displayName = Dogma[1].required(String); 
 
}

@Dogma({
  comment: `The global user`,
  implements: [ResourceOwner],
})  
export class User extends Dogma.Object {  
    
  @Dogma.comment `The user's id`
  readonly id = Dogma[0].required(String);  
  
  @Dogma.comment `The user's email`
  readonly email = Dogma[3].optional(String);  
    
  @Dogma.comment `The user's given name or first name`
  readonly givenName = Dogma[1].required(String);  
    
  @Dogma.comment `The user's family name`
  readonly familyName = Dogma[2].required(String);  
  
  @Dogma.comment `The user's display name`
  readonly displayName = Dogma[4].optional(String, () => `${this.givenName} ${this.familyName}`);  
  
  @Dogma.comment `The user's date of registration`
  readonly registeredAt = Dogma[5].optional(Date, () => new Date());  
    
  @Dogma.comment `The user's followers`
  readonly followers? = Dogma[6].repeated(User);
  
  @Dogma.comment `The user's status`
  readonly status = Dogma[7].optional(UserStatus, UserStatus.ACTIVE);

}  

const user = User.forge({  
  id: '1',
  givenName: 'John',  
  familyName: 'Doe',  
});  
  
const parsedUser = User.parse(JSON.stringify(user));

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

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

  /**
   * Get factory property names
   */
  getPropertyNames(): TupleKeys<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: string): User;

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

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

  /**
   * Encodes properties factory to base64 string
   * @param forge
   * @param encoding
   */
  stringify(forge: Forge<User>, 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 value
   * @param encoding
   */
  encode(value: User): 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;
}