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

@suey/pkg-utils

v0.1.7

Published

Compatible universal functions.

Downloads

95

Readme

设置 npm 源

npm config set registry https://registry.npmjs.org

安装

npm install @suey/pkg-utils --save

pnpm install @suey/pkg-utils --save

import { createApiRequest } from '@suey/pkg-utils';
import { inflate } from 'pako'; // pnpm install pako --save

const isOkStatus = (status: number): boolean => {
  if (status >= 200 && status < 300) return true;
  if (status === 400) console.log('失败的请求');
  if (status === 403) console.log('服务器拒绝了此请求');
  if (status === 404) console.log('目标地址未找到');
  if (status >= 500) console.log('服务器内部错误');
  return false;
}

export { REQ_METHODS, type RequestConfig } from '@suey/pkg-utils';

// 请求额外配置项, 当你的请求通过拦截器时, 你可以通过 hConfig 获取到
export interface HConfig {
  needAuth?: boolean;
}

const api = '/api';

/**
 * createApiRequest<T, K> => 这是 axios 的创建和拦截器
 * T: 请求时的额外配置
 * K: 返回结果 data 中的类型定义, 这里的后端假定返回封装了data中的数据为如下格式
 */
export const { apiGet, apiPost, request, createApi } = createApiRequest<HConfig, {
  status: number; // 返回的状态码, 这里可以是后端自定义的
  flag: string; // 返回标志
  data: any;
  more?: {
    pako?: boolean; // 是否被压缩了
  }
}>(api, {}, {
  onFulfilled: config => {
    // 处理请求时附带的额外参数, 如果你有这种需要附带参数的, 只需要在这里全局处理即可
    if (config.hConfig?.needAuth) {
      if (!config.headers) config.headers = {};
      if (getToken()) config.headers['authorization'] = 'Bearer ' + 'token....';
    }
  },
  onRejected: config => Promise.reject(config)
}, {
  onFulfilled: response => { // 对于封装的响应体做解析返回
    if (response.data.status && response.data.flag) {
      const status = response.data.status;

      // 统一进行解压缩
      if (response.data.more && response.data.more.pako === true) response.data.data = JSON.parse(inflate(response.data.data as Buffer, { to: 'string' }));

      if (!isOkStatus(+status)) return Promise.reject(response.data);
      return Promise.resolve(response.data);
    }
    return Promise.resolve(response);
  },
  onRejected: err => {
    if (err.data && err.data.status && err.data.flag) err.data = err.data.data as any;
    return Promise.reject(err);
  }
});
// @suey/pkg-utils

// 进行一个加密操作, 将指定的字符串进行加密, 并返回加密结果
export declare const aesEncryptAlgorithm: (value: string, encryptKey: string) => string;
// 进行一个解密操作, 将指定的字符串进行解密, 并返回解密结果
export declare const aesDecryptAlgorithm: (text: string, encryptKey: string) => string;
// 返回 AES 加密之后的字符串结果, 加密对象会被先JSON.stringify 转化为字符串进行加密
export declare const aesEncrypt: <T>(value: T, key?: string) => string;
// 返回 AES 解密之后的结果, 返回的结果不固定
export declare const aesDecrypt: <T>(text: string, key?: string) => T;
// 使用 MD5 加密, 该操作是不可逆的
export declare const md5Encrypt: (...args: string[]) => string;
 /** 创建 RSA key的时候, 创建多少字节的, 默认为 512 字节 */
interface RsaKeyOptions { bytes?: number; }
// 初始化一个 RSA 的公钥和密钥
export declare const rsaGetKey: (options?: RsaKeyOptions) => any[];
// 使用 RSA 加密, 使用该加密之前需要先生成加密和解密密钥
export declare const rsaEncryptAlgorithm: (text: string, publicKey: string) => string;
// 使用 RSA 解密, 使用该解密之前需要先生成加密和解密密钥
export declare const rsaDecryptAlgorithm: (text: string, privateKey: string) => string;

export type IsType = (target: unknown) => boolean;
export declare const isBoolean: IsType;
export declare const isObject: IsType;
export declare const isFunction: IsType;
export declare const isString: IsType;
export declare const isNull: IsType;
export declare const isUndefined: IsType;
export declare const isDate: IsType;
export declare const isDef: <T>(target: T) => boolean;
export declare const isUnDef: <T>(target: T) => boolean;
export declare const isPromise: (target: any | never) => boolean;
export declare const isDecimal: (str: string) => boolean;
export declare const isPhone: (str: string) => boolean;
export declare const isEmail: (str: string) => boolean;
export declare const isClass: (target: any) => boolean;
export declare const isExternal: (path: string) => boolean;
export declare const isArray: (arg: any) => boolean;
export declare const isHttpUrl: (path: string) => boolean;
export declare const isValidURL: (url: string) => boolean;