mirror of
https://github.com/ritz078/transform.git
synced 2026-11-29 20:20:18 +03:00
44 lines
1.1 KiB
JavaScript
44 lines
1.1 KiB
JavaScript
import React, { PureComponent } from 'react'
|
|
import dynamic from 'next/dynamic'
|
|
import { Json2dts, toValidJSON } from '../utils/json-to-ts-flow'
|
|
import Layout from '../components/Layout'
|
|
import ConversionPanel from '../components/ConversionPanel'
|
|
|
|
const defaultText = `
|
|
{
|
|
"foo": "bar",
|
|
"ids": [1, 2, 3],
|
|
"hello": {
|
|
"x": "lkjlj",
|
|
"y": "kjhkjhkjh"
|
|
}
|
|
}
|
|
`
|
|
|
|
export default class Json2Ts extends PureComponent {
|
|
getTransformedValue = (newValue) => {
|
|
const converter = new Json2dts()
|
|
let text2Obj = JSON.parse(toValidJSON(newValue))
|
|
if (typeof text2Obj !== 'object' || Array.isArray(text2Obj)) {
|
|
throw new Error('Pass a valid JSON Object. Arrays are not acceptable even though they are valid JSON.')
|
|
}
|
|
converter.parse(text2Obj, 'RootJson')
|
|
return converter.getCode()
|
|
}
|
|
|
|
render () {
|
|
return (
|
|
<Layout pathname={this.props.url.pathname}>
|
|
<ConversionPanel
|
|
getTransformedValue={this.getTransformedValue}
|
|
name={'ts_interface'}
|
|
defaultText={defaultText}
|
|
leftMode="json"
|
|
rightMode="typescript"
|
|
url={this.props.url}
|
|
/>
|
|
</Layout>
|
|
)
|
|
}
|
|
}
|