0.0.1-alpha.7 • Published 9 months ago

@142vip/common v0.0.1-alpha.7

Weekly downloads
-
License
MIT
Repository
-
Last release
9 months ago

@142vip/common

随机字符串

封装随机字符串生成函数,常用的有:

  • generateRandomStr
  • generateRandomStrWithSpecials
  • generateRandomStrWithLetter

分页

请求

封装了分页请求中的常见的currentPagepageSize字段。

/**
 * 分页时的请求参数
 * - 可继承
 */
export interface PaginationParams {
  currentPage: number
  pageSize: number
}

响应

分页数据的响应结构,继承了 PaginationParams的请求参数中的currentPage和pageSize字段,同时添加了totalPage字段和data字段,data字段为分页数据,支持泛型。

/**
 * 分页相应参数
 */
export interface PaginationResponseItem<T> extends PaginationParams {
  totalPage: number
  data: T[]
}

当分页接口返回数据时,常见的结构为:

{
  "currentPage": 1,
  "pageSize": 10,
  "totalPage": 1,
  // data字段支持泛型,常见是数组对象
  "data": []
}

接口响应

通用结构

export interface VipBaseResponse {
  traceId: string
  code: HttpStatus
  success: boolean
  message: string
}

请求成功

/**
 * 成功时,接口返回结构
 */
export interface VipSuccessResponse<T> extends VipBaseResponse {
  result: T
}

请求失败

/**
 * 异常时,接口返回结构
 */
export interface VipErrorResponse<T = string> extends VipBaseResponse {
  // 生产环境,严禁暴露这个字段
  detailErrorMessage?: T
}

通用脚本

开发

建议在项目下创建scripts目录,放置一些通用的脚本,dev脚本可以用来在开发环境下启动项目,例如:

#!/usr/bin/env node

/**
 * Usage:
 * - ./scripts/dev oauth-login
 */
const { execShell } = require('@142vip/common')
const { promptList } = require('@142vip/common')
const { devApps } = require('../142vip')

/**
 * 本地运行服务
 */
;(async () => {
  let appName = process.argv[2]
  if (appName == null) {
    appName = await promptList(devApps, '选择repo模块在开发环境下运行')
  }
  await execShell({
    command: `npx turbo run dev --filter=${appName} --color --only`,
    description: `开发环境运行${appName}`
  })
})()

在终端直接使用./scripts/dev即可执行该命令

编译

建议在项目下创建scripts目录,放置一些通用的脚本,build脚本可以用来在开发环境下启动项目,例如:

#!/usr/bin/env node

/**
 * Usage:
 * - ./scripts/build oauth-client
 */

const { execShell } = require('@142vip/common')
const { promptList } = require('@142vip/common')
const { buildApps } = require('../142vip');

(async () => {
  let appName = process.argv[2]
  if (appName != null) {
    if (!buildApps.includes(appName)) {
      appName = await promptList(buildApps, '选择repo模块进行构建...')
    }
  }
  // 编译
  execShell({
    command: `npx turbo run build ${appName == null ? '' : `--filter=${appName}`} --color --only`,
    description: `编译项目${appName == null ? buildApps.join(',') : appName}`
  })
})()

在终端直接使用./scripts/build即可执行该命令

参考

证书

MIT

Copyright (c) 2019-present, 142vip 储凡