Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zth/json-to-flowtype-generator
Generate Flow types from JSON or static JS objects/arrays.
https://github.com/zth/json-to-flowtype-generator
flowtype javascript json
Last synced: about 2 months ago
JSON representation
Generate Flow types from JSON or static JS objects/arrays.
- Host: GitHub
- URL: https://github.com/zth/json-to-flowtype-generator
- Owner: zth
- License: mit
- Created: 2017-12-10T13:16:26.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2017-12-12T17:01:41.000Z (almost 7 years ago)
- Last Synced: 2024-10-03T00:40:31.369Z (2 months ago)
- Topics: flowtype, javascript, json
- Language: JavaScript
- Size: 79.1 KB
- Stars: 10
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- stars - zth/json-to-flowtype-generator
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 JSONIt 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 clipboardIt can also operate directly on files, as in the example below:
![Example usage](https://raw.githubusercontent.com/zth/json-to-flowtype-generator/master/example.gif "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.