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

jsontointerface

v3.0.2

Published

parse json to interface files of typescript

Downloads

4

Readme

jsontointerface

convert json data to interface files;
writen by @shincat.

despection

这是一个工具,主要用于typescript项目中,当后端返回的数据接口比较复杂时,省去了一个一个去写interface的步骤,而是直接根据后端返回的数据结构,遍历生成包含各个数据接口的interface描述,并保存为对应的ts文件。具体描述如下

install

npm install --save-dev jsontointerface

init初始化

 j2i init
 或者
 npx j2i init

然后在你的项目目录下就会出现一个 j2i.json 的配置文件;
和一个j2i的目录, 目录的大致结构

-root--
    |- j2i/
    |- node_modules/
    |- package.json
    |- j2i.json

j2i.json 为json2interface的配置文件 数据结构为:


{
    "files":{
        "filepath":"j2i",
        "filepathto":"D"
    },
    "options":{
        "readonly":true,
        "toD":true
    }
}

| 关键字段 | 描述 | 备注 | | ---- | ---- | ---- | |filepath | 需要解析的json方式的目录 | 默认为j2i,建议保持默认,字符串 | | filepathto | ts的interface 文件放置的位置 | 字符串,一个名叫userdata.d.json 将会在这个目录下编译成 Iuserdata.ts 的文件 | | readonly | 是否将interface 个各个字段描述为readonly| 默认为true,都为“只可读”| |toD|是否保存为“.d.ts”后缀文件|默认为true

sample

有一个名为 username.json的json文件,数据结构为:

{
    "apiname":"getusername",
    "code":"456",
    "result":{
        "code":"123",
        "data":{
            "username":"shincat",
            "userinfo":"from changzhou",
            "userage":"30",
            "list":["2"]
        }
    },
    "history":[{
        "time":"34"
    }]
}

将 其放置在 j2i目录下。 之后直接运行

j2i
或者
npx j2i

根据以上j2i.json的配置文件的配置项,将在D目录下生成一个 Iuserdata.d.ts的文件。这个文件的内容为:

interface Ickindex {
   readonly apiname?:string;
   readonly code?:string;
   readonly result?:IData0;
   readonly history?:Array<IData2>;
};
interface IData2 {
   readonly time?:string;
};
interface IData0 {
   readonly code?:string;
   readonly data?:IData1;
};
interface IData1 {
   readonly username?:string;
   readonly userinfo?:string;
   readonly userage?:string;
   readonly list?:Array<string>;
};

 export type {Ickindex,IData2,IData0,IData1}

之后你就可以愉快的在你的typescript中import 这些文件,在编译器中方便的获取对象属性啦。