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

@zenweb/helper

v4.1.10

Published

Zenweb Helper module

Downloads

269

Readme

helper - 数据转换与验证

依赖模块

  • @zenweb/inject
  • @zenweb/result
  • @zenweb/messagecode

配置项

| 配置项 | 类型 | 默认值 | 功能 | | ----- | --- | ----- | ---- | | page | PageOption | 详见 PageOption | 配置默认的分页参数

PageOption

| 配置项 | 类型 | 默认值 | 功能 | | ----- | --- | ----- | ---- | | defaultLimit | number | 20 | 默认限制条数 | maxLimit | number | 100 | 最大条数 | minLimit | number | 1 | 最小条数 | allowOrder | string[] | 无 | 允许排序的字段 | defaultOrder | string \| string[] | 无 | 默认排序字段

可注入对象

  • request
    • TypeCastHelper
    • QueryHelper
    • ParamHelper

全局模式

| 方法 | 功能 | | ----- | ---- | | helperBase(target: Class) | 生成一个基于 HelperBase 类的快捷方式对象 | $query | 取得请求地址中 query 参数值 | $param | 取得请求地址中路径参数值

功能说明

QueryHelper

  • get: ctx.query 的参数转换
  • page: ctx.query 分页参数的检查并处理

ParamHelper

  • get: router 模块 ctx.params 的参数转换
  • page: ctx.params 分页参数的检查并处理

TypeCastHelper

  • pick: 从输入数据中挑选数据转换&验证
  • page: 分页参数的检查并处理

本模块使用 typecasts 包进行数据转换与校验 TypeCastPickOption 参数的详细说明请前往 typecasts 查看

使用样例

import { mapping, QueryHelper } from 'zenweb';

export class TestController {
  /**
   * 例如客户端提交的查询参数为:
   * a=1&b=hello&c=1,2,3&d=yes&e=a|b|c
   */
  @mapping()
  test(query: QueryHelper) {
    const data = query.get({
      a: '!int', // 转换为整数类型, “!”感叹号开头代表此参数必须并且不能为null
      b: 'trim', // 去除字符串两端空字符
      c: 'int[]', // 转换为整形列表
      d: 'bool', // 转换为布尔
      e: {
        // 可以设置更多转换选项
        type: 'string', // 数组元素类型为字符串
        splitter: '|',  // 数组切割字符串
        minItems: 3,    // 最多允许出现几个元素
        // 可以设置数据验证规则
        validate: {
          regexp: /\w+/i,
          // 更多验证规则详见文档
        },
      },
      // 对象嵌套
      objname: {
        type: {
          id: '!int',
          name: 'string',
        }
      },
    });
    /*
      输出结果:
      {
        a: 1,
        b: "hello",
        c: [1, 2, 3],
        d: true,
        e: ["a", "b", "c"]
      }
    */
    return data;
  }

  /**
   * 分页处理
   * limit=10&offset=5&order=-id
   */
  @mapping()
  page(query: QueryHelper) {
    const data = query.page({
      // 以下都为可选参数
      input: ctx.query,     // 输入数据源,默认使用 ctx.query
      defaultLimit: 20,     // 默认条数限制,默认 20
      maxLimit: 100,        // 最大限制条数,默认 100
      minLimit: 1,          // 最少限制条数,默认 1
      maxOrder: 1,          // 允许同时排序的字段数量,默认取 allowOrder 的长度
      defaultOrder: '-id',  // 默认排序字段,在客户端没有提供指定参数时的默认值
      allowOrder: ['id'],   // 允许排序的字段名称,注意不要加上 -前缀
    });
    /*
    输出结果:
    {
      limit: 10,
      offset: 5,
      order: ["-id"]
    }
    */
    return data;
  }
}