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

json-to-flowtype-generator

v0.9.3

Published

Generate Flow types from JSON or static JS objects/lists.

Downloads

14

Readme

json-to-flowtype-generator

This is a small tool to generate flow types from arbitrary JSON or static JS objects/arrays. The tool was designed with the following use cases in mind:

  • Generating base types for API calls
  • Quickly generating types from arbitrary JS objects, lists or JSON

It provides easy interaction by defaulting to using the current clipboard contents, effectively making the workflow:

  1. Copy your data
  2. Run json-to-flow in a terminal
  3. The generated type has now been copied to the clipboard

It can also operate directly on files, as in the example below:

Example usage

The tool does some semi-intelligent merging of objects etc in lists in order to identify both maybe-keys and keys that can have multiple types. Check out the GIF above and compare the objects in friends of the raw data to friends in the generated type. Notice how the tool has marked keys not present in all objects as maybe types.

Installation

npm install -g json-to-flowtype-generator
json-to-flow -h

Usage

# This will use the file apiResponse.json to generate a Flow type, and let you set options for the type interactively.
json-to-flow --file apiResponse.json --interactive
# This will use the current clipboard content to generate a Flow type, make the type read only, and name it ApiResponse.
json-to-flow --clipboard --read-only --name ApiResponse

All commands have shortcuts, check out json-to-flow -h.

Editor integrations

I personally use IntelliJ/WebStorm, but other editor integrations are very welcome contributions.

IntelliJ/WebStorm/JetBrains

You can configure an External Tool that runs json-to-flow --clipboard + your desired config. Then simply copy an object/array/some JSON and run the external tool.

Merging of lists and objects

The tools tries to be as smart as possible with merging lists and objects. For example, it assumes that objects discovered in the same list are of the same type, merging the objects into one and sensing what properties are present on all objects, and what are not and therefore are optional. This is desirable all cases where the objects returned in a list is in fact of the same type. Currently there's no way to turn this off, but it might come in a later version.

Example:

{
    friends: [
        {
            name: "Name", // This is present in all objects
            email: "[email protected]" // But this isn't
        },
        {
            name: "Name 2"
        },
        {
            name: "Name 3"
        }
        ....
    ]
}

Becomes:

type Type = {|
    +friends: $ReadOnlyArray<{|
        +name: string,
        +email: ?string
    |]>
|}

It also handles the same key having multiple types of values in different objects in the same list. Check out the tests for more info.