Quicktype to Mobx State Tree bindings
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.
$ npm install -g @radist2s/quicktype-mobx-state-tree
$ npm install @radist2s/quicktype-mobx-state-tree --save-dev
$ quicktypemst [--out FILE] FILE|URL ...
$ quicktypemst --help
$ 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()),
})
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)
})
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()
}
}
package.json
{
"scripts": {
"gen-models": "quicktypemst -s schema --src src/rest.schema.json -o build/gen/rest.ts --types-module src/custom-types.ts"
}
}
$ npm run gen-models