New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

interface2class

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

interface2class

interface gen class

latest
Source
npmnpm
Version
0.1.2
Version published
Weekly downloads
2
-50%
Maintainers
1
Weekly downloads
 
Created
Source

interface2class

一个通过 interface 生成 class 的命令行工具,适用于 TSArkTS

安装

npm i interface2class -g

使用

  • 文件 demo.ts
enum Gender {
  MAN = 'men',
  WOMAN = 'women'
}

interface Response {
  code: number
  message: string
  data: User
}

interface User {
  nickname: string
  age: number
  avatar: ResourceStr
  createAt: Date 
  gender: Gender
  hobby: string[]
  follows: User[]
  isValid: 0 | 1
}
  • 执行命令
i2c ./demo.ts
  • 自动写入 demo.ts
enum Gender {
  MAN = 'men',
  WOMAN = 'women'
}

interface Response {
  code: number
  message: string
  data: User
}

interface User {
  nickname: string
  age: number
  avatar: ResourceStr
  createAt: Date 
  gender: Gender
  hobby: string[]
  follows: User[]
  isValid: 0 | 1
}

// auto gen →

export class ResponseModel implements Response {
  code: number = 0
  message: string = ''
  data: User = new UserModel({} as User)

  constructor(model: Response) {
    this.code = model.code
    this.message = model.message
    this.data = model.data
  }
}
export class UserModel implements User {
  nickname: string = ''
  age: number = 0
  avatar: ResourceStr = ''
  createAt: Date = new Date()
  gender: Gender = Gender.MAN
  hobby: string[] = []
  follows: User[] = []
  isValid: 0 | 1 = 0

  constructor(model: User) {
    this.nickname = model.nickname
    this.age = model.age
    this.avatar = model.avatar
    this.createAt = model.createAt
    this.gender = model.gender
    this.hobby = model.hobby
    this.follows = model.follows
    this.isValid = model.isValid
  }
}

格式化 Interface

如果出现生成 class 失败,请格式化后再生成

  • 文件 demo.ts
/**
* 报文数据
*/
export interface Data {
  /**
   * 总页数
   */
  pageTotal?: number;
  /**
   * 数据集合
   */
  rows?: Row[];
  /**
   * 总数
   */
  total?: number;
  [property: string]: any;
}
  • 执行命令
i2c format ./demo.ts
  • 格式化 demo.ts
/** 报文数据 */
export interface Data {
  /** 总页数 */
  pageTotal: number | null;
  /** 数据集合 */
  rows: Row[] | null;
  /** 总数 */
  total: number | null;
}

Keywords

interface

FAQs

Package last updated on 01 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts