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

yapi-ts-util

v0.0.4

Published

## 动机

Downloads

4

Readme

yapi-ts-util ·

动机

根据 yapi 文档 自动导出 api 与 type,省去了人肉写一些接口 ts 类型,提高开发效率 yapi 适用于 typescript 团队

约定

  • 云端接口命名需要遵循 RESTful 风格
  • 接口命名除英文字符外只能使用/-
  • 生成的所有 Service 都是通过 ajax 调用
  • ajax 参数对应顺序: method, api, path, query, body, header
  • 未命名类型的默认为string
  • integer转换成number
  • 服务方法名的生成规则是Method➕Url,url 中存在 path 变量自动在变量前后新增within关键字
  • 若需要鉴权将 cookie 存储于根目录.cookie文件下

缺陷

  • yapi 中不存在泛型
  • 所有方法放在一个目录中
  • 服务方法是前端根据 url 生成,可能存在不准确

API

  • serverUrl: swagger json 地址
  • servicePath: 生成 service 与 type 的文件路径
  • requestImportExpression: ajax 导入模板
  • additionalPageHeader?: 页面头部信息,通常用于 disable eslint
  • apiRename?: 服务方法重命名,修改后将影响服务方法名
  • responseType?: 返回类型包装
  • basepath?: 接口统一前缀,对应 yapi 中的接口基本路径
  • ajaxName?: ajax 命名,默认为ajax
  • hiddenTypes?: 隐藏类型, 默认为空数组. pathquerybodyheadersformDataresponse
  • hiddenBodyInGET?: boolean 是否隐藏 body 在 get 方法中
  • customData?: 本地导入 json 解析
  • includePatterns?: string[], 指定包含接口,规则参考,若配置了 basepath,请加上 basepath
  • excludePatterns?: string[], 过滤指定接口,规则参考,若配置了 basepath,请加上 basepath
  • mock?: object, 是否生成 mock 数据
    • closeOptional?: boolean, 是否关闭可选类型
    • filename?: string, mock 文件名
    • path?: string, mock 文件路径
    • responseBodyTemplate?: string, mock 返回值模板, {value}代表 mock 数据
  • newEngine 使用新的引擎策略生成类型
  • enableMethodVersionSupport 启动对方法版本支持

使用

$ yarn add yapi-ts-engine --dev
  • 新建文件 api.js
const generate = require("yapi-ts-engine")

generate({
    serverUrl:"https://xxx",
    servicePath:"/output/api",
    requestImportExpression: "import { request } from '@/utils/fetch';"
});
  • package.json 新增 script: node api.js

prettier

  • 根目录下若存在prettier.config.js文件会自动执行

axios 案例

import axios, { AxiosRequestConfig, Method } from 'axios';

function createFormData<F>(forData: F): FormData | null {
  if (!forData) {
    return null;
  }
  const data = new FormData();
  Object.entries(forData).forEach(([name, value]) => {
    data.append(name, value);
  });
  return data;
}


function pathReplace<P>(url: string, path: P): string {
  if (!path) {
    return url;
  }
  let nextApi = url;
  Object.entries(path).forEach(([name, value]) => {
    const encodedValue = encodeURIComponent(value.toString());
    nextApi = nextApi.replace(`{${name}}`, encodedValue);
  });
  return nextApi;
}

export default async function ajax<P,Q,B,H,F,R>(method: Method, url: string, path:P, query:Q, body:B, headers:H, formData: F): Promise<R> {
  const response = await (await axios({
    method,
    url:`/api${pathReplace(url, path)}`,
    params: query,
    data: body || createFormData(formData),
    headers
  })).data;
  return response;
}