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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@creekjs/openapi

v0.0.4

Published

基于 OpenAPI 规范自动生成前端服务代码的工具包,支持多种数据源。

Downloads

13

Readme

@creekjs/openapi

基于 OpenAPI 规范自动生成前端服务代码的工具包,支持多种数据源。

特性

  • 🚀 支持 Swagger/OpenAPI 规范
  • 🔄 自动生成 TypeScript 类型定义
  • 📡 生成对应的 API 服务函数
  • 🎯 支持多种数据源(Swagger、YApi)
  • 🛠️ 基于 @umijs/openapi 的强大功能

安装

npm install @creekjs/openapi --save-dev
# 或
yarn add @creekjs/openapi -D

基础用法

import { generateService, OpenApiChannel } from '@creekjs/openapi';

// 从 Swagger 生成服务
await generateService({
  openApiChannel: OpenApiChannel.swagger,
  schemaPath: 'https://api.example.com/swagger.json',
  serversPath: './src/services',
  requestLibPath: '@/utils/request'
});

API 参考

OpenApiChannel 枚举

enum OpenApiChannel {
  swagger = 'swagger',
  yapi = 'yapi'  // 暂未实现
}

generateService(options)

生成 API 服务代码。

参数类型:

interface OpenApiGenerateServiceProps extends BaseOpenApiGenerateServiceProps {
  openApiChannel: OpenApiChannel;
}

interface BaseOpenApiGenerateServiceProps {
  requestLibPath?: string;        // 请求库路径
  requestOptionsType?: string;    // 请求选项类型
  requestImportStatement?: string; // 请求导入语句
  apiPrefix?: string | ((params: ApiPrefixParams) => string); // API 前缀
  serversPath?: string;           // 服务输出路径
  schemaPath?: string;            // OpenAPI 规范文件路径
  projectName?: string;           // 项目名称
  namespace?: string;             // 命名空间
  enumStyle?: 'string-literal' | 'enum'; // 枚举样式
  nullable?: boolean;             // 是否允许 null
  templatesFolder?: string;       // 模板文件夹
  dataFields?: string[];          // 数据字段
  isCamelCase?: boolean;          // 是否使用驼峰命名
}

interface ApiPrefixParams {
  path: string;
  method: string;
  namespace: string;
  functionName: string;
  autoExclude?: boolean;
}

使用示例

基础配置

import { generateService, OpenApiChannel } from '@creekjs/openapi';

// 基础配置
const config = {
  openApiChannel: OpenApiChannel.swagger,
  schemaPath: 'https://petstore.swagger.io/v2/swagger.json',
  serversPath: './src/services',
  requestLibPath: '@/utils/request',
  projectName: 'petstore'
};

await generateService(config);

高级配置

// 自定义 API 前缀
const advancedConfig = {
  openApiChannel: OpenApiChannel.swagger,
  schemaPath: './openapi.json',
  serversPath: './src/api',
  requestLibPath: '@creekjs/request',
  apiPrefix: ({ path, method, namespace }) => {
    return `/${namespace}${path}`;
  },
  enumStyle: 'string-literal' as const,
  isCamelCase: true,
  nullable: false,
  dataFields: ['data', 'result']
};

await generateService(advancedConfig);

多配置支持

// 支持数组配置,一次生成多个服务
const configs = [
  {
    openApiChannel: OpenApiChannel.swagger,
    schemaPath: 'https://api1.example.com/swagger.json',
    serversPath: './src/services/api1',
    namespace: 'api1'
  },
  {
    openApiChannel: OpenApiChannel.swagger,
    schemaPath: 'https://api2.example.com/swagger.json',
    serversPath: './src/services/api2',
    namespace: 'api2'
  }
];

for (const config of configs) {
  await generateService(config);
}

生成的代码结构

执行后会在指定目录生成以下文件: