|
| 1 | +# json-to-ts-cvr |
| 2 | + |
| 3 | +[](https://www.npmjs.com/package/json-to-ts-cvr) |
| 4 | +[](https://opensource.org/licenses/MIT) |
| 5 | +[](https://github.com/krikera/json-typescript/actions/workflows/ci.yml) |
| 6 | + |
| 7 | +> A CLI tool to convert JSON structures into TypeScript interfaces or classes |
| 8 | +
|
| 9 | +## Installation |
| 10 | + |
| 11 | +```bash |
| 12 | +# Install globally |
| 13 | +npm install -g json-to-ts-cvr |
| 14 | + |
| 15 | +# Or use with npx |
| 16 | +npx json-to-ts-cvr [options] |
| 17 | +``` |
| 18 | + |
| 19 | +## Features |
| 20 | + |
| 21 | +- Convert JSON to TypeScript interfaces or classes |
| 22 | +- Support for nested objects and arrays |
| 23 | +- Generate proper type definitions (string, number, boolean, etc.) |
| 24 | +- Auto-generate interface names from property names |
| 25 | +- Customize interface/class names with prefixes |
| 26 | +- Handle nullable fields |
| 27 | + |
| 28 | +## Usage |
| 29 | + |
| 30 | +### Convert JSON string to TypeScript interface |
| 31 | + |
| 32 | +```bash |
| 33 | +json-to-ts-cvr '{"name": "John", "age": 25}' |
| 34 | +``` |
| 35 | + |
| 36 | +Output: |
| 37 | +```typescript |
| 38 | +interface Root { |
| 39 | + name: string; |
| 40 | + age: number; |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +### Convert JSON from a file and save to a TypeScript file |
| 45 | + |
| 46 | +```bash |
| 47 | +json-to-ts-cvr -i data.json -o types.ts |
| 48 | +``` |
| 49 | + |
| 50 | +### Generate TypeScript classes instead of interfaces |
| 51 | + |
| 52 | +```bash |
| 53 | +json-to-ts-cvr -i data.json --class |
| 54 | +``` |
| 55 | + |
| 56 | +Output: |
| 57 | +```typescript |
| 58 | +class Root { |
| 59 | + name: string; |
| 60 | + age: number; |
| 61 | + |
| 62 | + constructor(data: any) { |
| 63 | + this.name = data.name; |
| 64 | + this.age = data.age; |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +### Add a prefix to all interface/class names |
| 70 | + |
| 71 | +```bash |
| 72 | +json-to-ts-cvr -i data.json --prefix I |
| 73 | +``` |
| 74 | + |
| 75 | +Output: |
| 76 | +```typescript |
| 77 | +interface IRoot { |
| 78 | + name: string; |
| 79 | + age: number; |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +### Handle null values as union types |
| 84 | + |
| 85 | +```bash |
| 86 | +json-to-ts-cvr -i data.json --union-null |
| 87 | +``` |
| 88 | + |
| 89 | +Output: |
| 90 | +```typescript |
| 91 | +interface Root { |
| 92 | + name: string; |
| 93 | + nickname: null; |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +## Options |
| 98 | + |
| 99 | +| Option | Alias | Description | |
| 100 | +|--------|-------|-------------| |
| 101 | +| `--input <file>` | `-i` | Input JSON file path | |
| 102 | +| `--output <file>` | `-o` | Output TypeScript file path | |
| 103 | +| `--class` | | Generate TypeScript classes instead of interfaces | |
| 104 | +| `--union-null` | | Represent nullable fields as union types (field: string \| null) | |
| 105 | +| `--prefix <prefix>` | | Prepend a custom prefix to all interface/class names | |
| 106 | +| `--help` | `-h` | Display help information | |
| 107 | +| `--version` | `-v` | Display version information | |
| 108 | + |
| 109 | +## Examples |
| 110 | + |
| 111 | +### Complex nested objects |
| 112 | + |
| 113 | +Input: |
| 114 | +```json |
| 115 | +{ |
| 116 | + "user": { |
| 117 | + "name": "John", |
| 118 | + "address": { |
| 119 | + "city": "New York", |
| 120 | + "zipCode": 10001 |
| 121 | + }, |
| 122 | + "hobbies": ["reading", "gaming"] |
| 123 | + } |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +Output: |
| 128 | +```typescript |
| 129 | +interface Root { |
| 130 | + user: User; |
| 131 | +} |
| 132 | + |
| 133 | +interface User { |
| 134 | + name: string; |
| 135 | + address: Address; |
| 136 | + hobbies: string[]; |
| 137 | +} |
| 138 | + |
| 139 | +interface Address { |
| 140 | + city: string; |
| 141 | + zipCode: number; |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +## Development |
| 146 | + |
| 147 | +```bash |
| 148 | +# Clone the repository |
| 149 | +git clone https://github.com/krikera/json-typescript.git |
| 150 | +cd json-typescript |
| 151 | + |
| 152 | +# Install dependencies |
| 153 | +npm install |
| 154 | + |
| 155 | +# Build the project |
| 156 | +npm run build |
| 157 | + |
| 158 | +# Run tests |
| 159 | +npm test |
| 160 | +``` |
| 161 | + |
| 162 | +## Contributing |
| 163 | + |
| 164 | +Contributions are welcome! Please feel free to submit a Pull Request. |
| 165 | + |
| 166 | +1. Fork the repository |
| 167 | +2. Create your feature branch (`git checkout -b feature/amazing-feature`) |
| 168 | +3. Commit your changes (`git commit -m 'Add some amazing feature'`) |
| 169 | +4. Push to the branch (`git push origin feature/amazing-feature`) |
| 170 | +5. Open a Pull Request |
| 171 | + |
| 172 | +## Publishing |
| 173 | + |
| 174 | +To publish to npm: |
| 175 | + |
| 176 | +```bash |
| 177 | +# Login to npm |
| 178 | +npm login |
| 179 | + |
| 180 | +# Publish package |
| 181 | +npm publish |
| 182 | +``` |
| 183 | + |
| 184 | +## License |
| 185 | + |
| 186 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
0 commit comments