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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@qianphong/utils

v0.0.13

Published

Personal utils

Downloads

39

Readme

@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 | - | | apiPrefix | 否 | api 的前缀 | string | - | | serversPath | 否 | 生成的文件夹的路径 | string | - | | schemaPath | 否 | Swagger 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