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

@pagoda-tools/openapi

v1.0.0-beta.18

Published

openapi

Downloads

9

Readme

@pagoda-tools/openapi

安装

# install npm
npm install @pagoda-tools/openapi -S

# install yarn
yarn add @pagoda-tools/openapi -S

文档编写示例

  1. 根目录新增配置文件 openapi.config.js
import { defineOpenapiConfig } from '@pagoda-tools/openapi';

export default defineOpenapiConfig({
  info: {
    title: '测试文档标题',
    version: '1.0.0',
  },
  server: [
    {
      url: 'http://www.xxx.com',
      description: '测试环境',
    },
  ],
});
  1. 在 src 任意目录下新建 xx.dto.ts,示例代码如下:
import {
  defineApi,
  IsInt,
  IsString,
  Type,
  ValidateNested,
} from '@pagoda-tools/openapi';

class User {
  @IsString()
  name: string;
}

export class TestQuery {
  @JSONSchema({
    description: '字段描述',
  })
  a: string;
}

export class TestBody {
  @IsInt()
  @JSONSchema({
    description: '字段描述',
  })
  a: number;

  @ValidateNested({ each: true })
  @Type(() => User)
  @JSONSchema({
    description: '字段描述',
  })
  users: User[];
}

export class TestResponse {
  @IsInt()
  a: number;
}

export default defineApi({
  method: 'get',
  info: {
    summary: '接口名称',
  },
  query: TestQuery,
  body: TestBody,
  response: TestResponse,
});
  1. package.jsonscripts 中新增命令
{
  "scripts": {
    "doc-serve": "pd-openapi serve",
    "export-doc-json": "pd-openapi json"
  }
}
  1. 执行 npm run doc-serve

openapi.config.js 配置项

interface OpenapiConfig {
  info?: {
    // 文档标题
    title?: string;
    // 文档描述
    description?: string;
    // 文档版本
    version?: string;
  };
  server?: {
    // 接口环境地址
    url: string;
    // 接口环境地址描述
    description?: string;
  }[];
  compilerOptions?: {
    // 导出文件的文件名 默认为 openapi-json.json
    outputFileName?: string;
    // 导出的openapi版本 默认为 3.0
    openapi?: '2.0' | '3.0';
  };
}

serverless 的中间件中添加参数校验

// 本代码仅作为示例代码 可能需要根据项目实际情况自行调整
const url = require('url');
const path = require('path');
const { validateByConfig } = require('@pagoda-tools/swagger-ui');
const { PagodaServerless } = require('pagoda-serverless');

const pdServerless = new PagodaServerless(/*...*/);

async function main() {
  const pathname = url.parse(req.originalUrl).pathname;
  const api = path.parse(pathname).name;
  const apiDir = path.join(
    pathname.replace(`/${RUNTIME_ENV}/platform/`, ''),
    '..'
  );
  const configPath = `../${apiDir}/dto/${api}.dto.js`;

  // 判断是否存在对应接口的 dto 文件
  if (existsSync(path.join(__dirname, configPath))) {
    const swaggerConfig = require(configPath).default;

    const errors = await validateByConfig(swaggerConfig, req);

    if (errors?.length) {
      const error = errors[0];
      const { constraints } = error;

      throw new pdServerless.ParamsException({
        prop: error.property,
        message: constraints[Object.keys(constraints)[0]],
      });
    }
  }

  middlewareCallback(true);
}

pdServerless.executeMain(main, {
  returnRes: false,
});

serverless 的中编写 dto 结构示例

.
├── src
│   ├── test
│   │   ├── dto
|   |   |   ├── getUser.dto.js
|   |   |   ├── getList.dto.js
|   |   ├── getUser.js
|   |   ├── getList.js

相关链接