mirror of
https://github.com/ritz078/transform.git
synced 2026-11-22 09:01:31 +03:00
38 lines
1.2 KiB
JavaScript
38 lines
1.2 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";
|
|
import defaultText from "../utils/dummy-json";
|
|
|
|
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
|
|
leftTitle="JSON"
|
|
rightTitle="TypeScript Interface"
|
|
getTransformedValue={this.getTransformedValue}
|
|
name={"ts_interface"}
|
|
defaultText={defaultText}
|
|
leftMode="json"
|
|
rightMode="typescript"
|
|
url={this.props.url}
|
|
/>
|
|
</Layout>
|
|
);
|
|
}
|
|
}
|