0.0.13 • Published 11 months ago

@qianphong/utils v0.0.13

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

@qianphong/utils

npm version npm downloads

Install

npm i @qianphong/utils
# Or pnpm
pnpm i @qianphong/utils

Documentation

API 自动生成(基于 swagger 描述文件)

package.jsonscripts 中添加 openapi 命令

{
  "scripts": {
    "openapi": "openapi -schemaPath http://localhost:3000/swagger/docs/all"
  }
}

执行 npm run openapi[project]/src/service/ 下生成 api 目录,包含依据描述文件http://localhost:3000/swagger/docs/all生成方法和类型。

参数

属性必填备注类型默认值
requestLibPath自定义请求方法路径string-
requestImportStatement自定义请求方法表达式string-
apiPrefixapi 的前缀string-
serversPath生成的文件夹的路径string-
schemaPathSwagger 2.0 或 OpenAPI 3.0 的地址string-
projectName项目名称string-
namespace命名空间名称string-

更多参数请参考 openapi2typescript

MyHttp

基于 Axios 二次封装

import { MyHttp } from "@qianphong/utils"
// Step 1 - Setup
MyHttp.setup({
  requestInterceptor(config) {
    // Do something before request is sent
    return config
  },
  handleError(error, cb) {
    // Do something with request error
    return Promise.reject(error)
  },
})

// Step 2 - Create instance
const http = new MyHttp({ baseURL: "/api", timeout: 10000 })

// Step3 - Package request
export function request<T>(url: string, config?: any) {
  return new Promise<T>((resolve, reject) => {
    http.axiosInstance
      .request({
        url,
        ...config,
      })
      .then((response: any) => {
        // Handle business error
        if (response?.success) {
          resolve(response)
        } else {
          reject(response)
        }
      })
      .catch(reject)
  })
}

2. Cipher

使用 crypto-js 库实现 AES 加密和解密的代码示例。该库提供了多种加密算法、编码方式和填充模式,可以在浏览器和 Node.js 环境中使用。

  1. 安装 crypto-js 库
pnpm i crypto-js
  1. 导入 AesEncryption 类和相关函数:
import {
  AesEncryption,
  encryptByBase64,
  decodeByBase64,
  encryptByMd5,
} from "@qianphong/utils"
  1. 实例化 AesEncryption 类,并传入加密参数:
const aes = new AesEncryption({ key: "1111111111111" })

其中,key 为加密密钥,长度为 16、24 或 32 个字符。

  1. 使用 encrypt 方法对需要加密的文本进行加密:
const cipherText = aes.encrypt("Hello, world!")
console.log(cipherText)
// 输出: "U2FsdGVkX1+4Dd5pJqKX2g=="
  1. 使用 decrypt 方法对加密后的文本进行解密:
const plainText = aes.decrypt("U2FsdGVkX1+4Dd5pJqKX2g==")
console.log(plainText)
// 输出: "Hello, world!"
  1. 使用 encryptByBase64 方法对文本进行 Base64 编码:
const base64Text = encryptByBase64("Hello, world!")
console.log(base64Text)
// 输出: "SGVsbG8sIHdvcmxkIQ=="
  1. 使用 decodeByBase64 方法对 Base64 编码后的文本进行解码:
const plainText = decodeByBase64("SGVsbG8sIHdvcmxkIQ==")
console.log(plainText)
// 输出: "Hello, world!"
  1. 使用 encryptByMd5 方法对文本进行 MD5 加密:
const md5Text = encryptByMd5("Hello, world!")
console.log(md5Text)
// 输出: "86fb269d190d2c85f6e0468ceca42a20"

注意:MD5 是一种单向加密算法,无法通过加密后的结果进行解密,只能用于验证数据的完整性。因此,在实际应用中,应该将加密后的结果与预先保存的 MD5 值进行比较,以确保数据的完整性。

3. Cache

import { WebStorage } from "@qianphong/utils"

const storage = new WebStorage({
  prefixKey: "WEB__",
  encryption: import.meta.env.PROD ?  encryption: {
    key: "11111111111111",
  } : undefined,
})

提供一个 WebStorage 类,可以在 sessionStorage 或 localStorage 中存储缓存数据,并提供设置、获取和删除缓方法。构造函数接受一个对象作为参数,该对象包含以下属性:

  • prefixKey:缓存键的前缀,默认为空字符串。
  • storage:要使用的 Web 存储实例,默认为 localStorage。
  • timeout:缓存过期时间(以秒为单位),默认为 undefined,表示缓存永不过期。
  • encryption:加密参数,用于加密缓存数据。如果未提供,则不加密。

该类提供以下方法:

  • set(key: string, value: any, expire?: number):设置缓存。key 为缓存键,value 为缓存值,expire 为缓存过期时间(以秒为单位),可选。如果未提供 expire 参数,则使用构造函数中提供的 timeout 属性值。
  • get(key: string, def?: any):获取缓存。key 为缓存键,def 为默认值,可选。如果缓存不存在,则返回 def 值。如果缓存已过期,则自动删除缓存并返回 def 值。
  • remove(key: string):删除指定的缓存。
  • clear():删除该实例中的所有缓存。

License

MIT © qianphong

0.0.10

11 months ago

0.0.11

11 months ago

0.0.12

11 months ago

0.0.13

11 months ago

0.0.1

11 months ago

0.0.3

11 months ago

0.0.2

11 months ago

0.0.9

11 months ago

0.0.8

11 months ago

0.0.5

11 months ago

0.0.4

11 months ago

0.0.7

11 months ago

0.0.6

11 months ago

1.0.1

3 years ago

1.0.0

3 years ago