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

soapi-cli

v2.0.12

Published

接口平台前端开发使用的 sdk 开发工具

Downloads

795

Readme

soapi-cli

接口平台前端开发使用的 sdk 开发工具,用于将接口平台的 schema/route 自动化的生成前端 api 代码。

Install

npm install soapi-cli

or use yarn

yarn add soapi-cli

Usage

Configuration

在项目目录下创建.sdkrc.yml.sdkrc.json文件进行 sdk 配置

| field | type | default | description | | :------------- | :--------- | :------ | :------------------------------------------------------------ | | *platform | string | - | 接口平台地址 | | *output | string | - | 输出 sdk 的文件夹路径 | | node | boolean | false | 是否是 node 版本,node 版本 sdk 需要安装node-fetch | | *applications | object[] | - | 需要生成 sdk 的接口平台应用,详细结构请看下文 |

application

| field | type | default | description | | :------ | :----- | :------ | :--------------- | | *appId | number | - | 应用 id | | *token | string | - | 应用的授权 token |

example

YAML 格式配置如下:

platform: 'http://localhost:4003'
output: src/apis/
node: true
applications:
  - appId: 0
    token: 'yourToken'

JSON 格式配置如下:

{
  "platform": "http://localhost:4003",
  "output": "src/apis/",
  "node": false,
  "applications": [
    {
      "appId": 0,
      "token": "yourToken"
    }
  ]
}

CLI

| commander | description | | :---------------------- | :------------ | | npx soapi-cli upgrade | 生成/升级 sdk |

| options | description | example | | :------------- | :--------------------- | :------------------------------ | | --force | 强制更新全部 API | npx soapi-cli upgrade --force | | -h --help | show help | npx soapi-cli -h | | -v --version | show soapi-cli version | npx soapi-cli -v | | --debug | 展示 debug 信息 | npx soapi-cli --debug trace |

sdk 的使用方法

import getArticle from 'sdk/appName.get.api.article'
import app from 'sdk/appName'
import {
  setEnv,
  checkSchema,
  catchError,
} from '@soapi/middlewares'


/**
 * 全局中间件配置
 * 所有api接口调用时都会执行
 */
app
  /**
   * 配置接口运行环境
   * 不同的运行环境会调用不同的url
   * 各环境url配置请到接口平台,sdk插件中配置
   * 如果使用中间件进行设置,则默认优先取SDK_ENV,其次取NODE_ENV
   * 如果无法取得有效值,默认为`prod`环境。
   */
  .use(setEnv(process.env.NODE_ENV))
  .use(catchError(error => {
    console.log('what the fuck!')

    /**
     * 这里如果不抛出错误
     * sdk会认为您已经将错误正确的处理,并将继续执行
     * 这将导致sdk的api被调用时候,向外抛出的错误被中间件拦截并处理
     * 调用的地方将无法得知错误的发生,并按照正常调用处理
     */
    throw error
  }))

// use in saga
try {
  /**
   * 根据文档平台定义和入参对象的key自动设置headers,body,query和params
   * 入参对象中未匹配到的key将会放入body对象中
   */
  yield getArticle({ articleId: 1 })
} cache (e) {
  // handle error
}

// 指定body,支持链式调用
yield getArticle().send({ articleId: 1 })

// 指定headers,支持链式调用
yield getArticle().set('Content-Type', 'application/json').set({ Token: 'token' })

// 指定query,支持链式调用
yield getArticle().query('pageSize', 10).query({ current: 1 })

// 指定params,支持链式调用
yield getArticle().params('id', 1).params({ pid: 1 })

@soapi/middlewares 提供一些通用的 middleware,详见:http://gitlab.shein.com/nj_shein_fed/soapi/tree/master/modules/middlewares

工作流程

start=>start: 开始
end=>end: 结束
checkPackage=>condition: 是否安装soapi-cli
installPackage=>operation: 安装soapi-cli
editConfig=>operation: 编辑配置文件

save=>operation: 保存配置
runCli=>operation: 运行cli
checkField=>condition: CLI: 检查配置文件
checkApplicationSDKPlugin=>condition: 检查应用是否安装了sdk插件
alertInstalSDKPlugin=>operation: 警告“应用需要启动sdk服务插件,以配置应用的运行环境信息”
downloadSDK=>subroutine: CLI: 下载sdk需要的应用信息(需要授权key和applicationId)
至output目录
importSDK=>operation: import 'output/appName.xxx.xxx'

start->checkPackage(yes)->runCli

checkPackage(no, bottom)->installPackage->editConfig
editConfig->save
save->runCli->checkField(no)->editConfig
checkField(yes)->checkApplicationSDKPlugin(no)->alertInstalSDKPlugin->end
checkApplicationSDKPlugin(yes)->downloadSDK->importSDK->end

注意事项

  1. sdk 自动生成的代码依赖于fetchSymbol,如果运行环境不支持,请添加相应的 polyfill。
  2. sdk生成代码需要依赖 sdk-service 插件提供一些额外的配置信息。(如:不同环境的应用 url)请先在接口平台的应用中启动SDK插件