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

@radist2s/quicktype-mobx-state-tree

v1.0.0

Published

Quicktype MobX State Tree language renderer

Downloads

21

Readme

quicktype-mobx-state-tree

Quicktype to Mobx State Tree bindings

Version Downloads/week License

About

quicktypemst generates strongly-typed MobX State Tree models from JSON, JSON Schema, and GraphQL queries, making it a breeze to work with JSON type-safely.

Based on quicktype. To use quicktypemst effectively, it is better to read original quicktype documentation first.

Installation

Install globally

$ npm install -g @radist2s/quicktype-mobx-state-tree

Install locally

$ npm install @radist2s/quicktype-mobx-state-tree --save-dev

CLI Usage

$ quicktypemst [--out FILE] FILE|URL ...
$ quicktypemst --help

Examples

JSON to MST

$ quicktypemst https://blockchain.info/latestblock -o latestblock.ts

Output file: latestblock.ts

/* tslint:disable */
import {types} from 'mobx-state-tree'

export const Latestblock = types.model('Latestblock', {
    hash:        types.string,
    time:        types.number,
    block_index: types.number,
    height:      types.number,
    txIndexes:   types.array(types.frozen()),
})

JSON Schema to MST

Input file: types.schema.json

{
    "$id": "types.schema.json",
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "properties": {
        "number": {
            "type": "integer"
        },
        "string": {
            "type": "string"
        },
        "date": {
            "type": "string",
            "format": "date-time"
        },
        "enum": {
            "type": "string",
            "enum": ["first", "second", "third"],
            "default": "first"
        }
    }
}

Command

$ quicktypemst -s schema types.schema.json -o types.ts

Output file: types.ts

import {types} from 'mobx-state-tree'

export enum Enum {
    First = 'first',
    Second = 'second',
    Third = 'third'
}

export const Types = types.model('Types', {
    date: types.maybe(types.Date),
    enum: types.maybe(types.enumeration<Enum>('Enum', Object.values(Enum))),
    number: types.maybe(types.number),
    string: types.maybe(types.string)
})

Custom types module source

Sometimes you may need to use custom types for models. For example, let's override Date type by custom type with snapshot pre and postprocessors. The example below will allow you to apply JSON snapshots with the specified date as string in format like 2020-04-04T19:34:58.843Z

$ quicktypemst -s schema types.schema.json -o types.ts --types-module ./my-types.ts

Output file: types.ts

import {types} from './my-types'

...

Custom types module file: my-types.ts

import {types as mstTypes} from 'mobx-state-tree'

type DateSnapshotType = string | null
type DateType = Date | null

export function typeDate() {
    return mstTypes.custom<DateSnapshotType, DateType>({
        name: 'Date',
        fromSnapshot(snapshot: DateSnapshotType): DateType {
            return snapshot === null ? null : new Date(snapshot)
        },
        getValidationMessage(snapshot: string | null): string {
            const isValidTimestamp = snapshot === null || /[^\d\.]+/.test(String(snapshot))

            return isValidTimestamp ? '' : `'${snapshot}' doesn't look like a valid timestamp`
        },
        isTargetType(value: DateSnapshotType | DateType): boolean {
            return value instanceof Date || value === null
        },
        toSnapshot(value: DateType): DateSnapshotType {
            return value ? value.toISOString() : null
        }
    })
}

const {Date: MstDate, ...mstPartialProps} = mstTypes

export const types = {
    ...mstPartialProps,
    get Date() {
        return typeDate()
    }
}

Project npm automation

package.json

{
    "scripts": {
        "gen-models": "quicktypemst -s schema --src src/rest.schema.json -o build/gen/rest.ts --types-module src/custom-types.ts"
    }
}
Usage
$ npm run gen-models