@blgc/openapi-router

中文文档
0.1.0发布于 2024/12/22MITTypeScriptESM + CJS近 30 天下载 1772

Thin wrapper around the router of web frameworks like Express and Hono, offering OpenAPI typesafety and seamless integration with validation libraries such as Valibot and Zod

此软件包已被标记为弃用

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

原始使用文档
文档目录5 节
  1. 📚 Examples
  2. 🌟 Motivation
  3. 📖 Usage
  4. ExpressJs
  5. Hono

@blgc/openapi-router banner

GitHub License NPM bundle minzipped size NPM total downloads Join Discord

Status: Experimental

@blgc/openapi-router is a thin wrapper around the router of web frameworks like Express and Hono, offering OpenAPI typesafety and seamless integration with validation libraries such as Valibot and Zod.

  • Typesafe: Build with TypeScript for strong type safety and support for openapi-typescript types
  • Framework Agnostic: Compatible with Express, Hono, and other popular web frameworks
  • Validation-First: Integrates with Valibot, Zod, and other validators to ensure requests adhere to expected types
  • Modular & Extendable: Easily extendable with features like withExpress(), ..

📚 Examples

🌟 Motivation

Create a typesafe and straightforward wrapper around web framework routers, seamlessly integrating with OpenAPI schemas using openapi-typescript and ensuring type validation with libraries like Zod and Valibot.

📖 Usage

ExpressJs

@blgc/openapi-router wraps around the Express router to deliver full typesafety and enforce your OpenAPI-Schema with validators.

While TypeScript types ensure compile-time safety, they don't enforce runtime schema validation. For runtime compliance, you need to integrate with validation libraries like Zod or Valibot. Although you must define the validation rules manually, they are type-safe to ensure these rules are correctly defined.

import { createExpressOpenApiRouter } from '@blgc/openapi-router';
import { Router } from 'express';
import * as v from 'valibot';
import { vValidator } from 'validation-adapters/valibot';
import { paths } from './gen/v1'; // OpenAPI-generated types
import { PetSchema } from './schemas'; // Custom reusable Zod schema for validation

export const router: Router = Router();
export const openApiRouter = createExpressOpenApiRouter<paths>(router);

// GET /pet/{petId}
openApiRouter.get('/pet/{petId}', {
    pathValidator: vValidator(
        v.object({
            petId: v.number() // Validate that petId is a number
        })
    ),
    handler: (req, res) => {
        const { petId } = req.params; // Access validated params
        res.send({ name: 'Falko', photoUrls: [] });
    }
});

// POST /pet
openApiRouter.post('/pet', {
    bodyValidator: vValidator(PetSchema), // Validate request body using PetSchema
    handler: (req, res) => {
        const { name, photoUrls } = req.body; // Access validated body data
        res.send({ name, photoUrls });
    }
});

Full example

Hono

@blgc/openapi-router wraps around the Hono router to deliver full typesafety and enforce your OpenAPI-Schema with validators.

While TypeScript types ensure compile-time safety, they don't enforce runtime schema validation. For runtime compliance, you need to integrate with validation libraries like Zod or Valibot. Although you must define the validation rules manually, they are type-safe to ensure these rules are correctly defined.

Hono's TypeScript integration provides type suggestions for c.json() based on generically defined response types, but doesn't enforce these types at compile-time. For example, c.json('') won't raise a type error even if the expected type is {someType: string}. This is due to Hono's internal use of TypedResponse<T>, which infers but doesn't strictly enforce the passed generic type. Hono Discussion

import { createHonoOpenApiRouter } from '@blgc/openapi-router';
import { Hono } from 'hono';
import { zValidator } from 'validation-adapters/zod';
import * as z from 'zod';
import { paths } from './gen/v1'; // OpenAPI-generated types
import { PetSchema } from './schemas'; // Custom reusable Zod schema for validation

export const router = new Hono();
export const openApiRouter = createHonoOpenApiRouter<paths>(router);

// GET /pet/{petId}
openApiRouter.get('/pet/{petId}', {
    pathValidator: zValidator(
        z.object({
            petId: z.number() // Validate that petId is a number
        })
    ),
    handler: (c) => {
        const { petId } = c.req.valid('param'); // Access validated params
        return c.json({ name: 'Falko', photoUrls: [] });
    }
});

// POST /pet
openApiRouter.post('/pet', {
    bodyValidator: zValidator(PetSchema), // Validate request body using PetSchema
    handler: (c) => {
        const { name, photoUrls } = c.req.valid('json'); // Access validated body data
        return c.json({ name, photoUrls });
    }
});

Full example

版本与发布记录

@blgc/openapi-router 共发布 14 个版本,首次发布于 2024/08/02;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.0latest2024/12/221.9 年前
0.0.232024/12/221.9 年前
0.0.222024/11/281.9 年前
0.0.212024/11/062 年前
0.0.202024/09/192 年前
0.0.192024/08/292 年前

关于 @blgc/openapi-router 的常见问题

@blgc/openapi-router 是什么?

Thin wrapper around the router of web frameworks like Express and Hono, offering OpenAPI typesafety and seamless integration with validation libraries such as Valibot and Zod注意:该包已被作者标记为弃用——Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

如何安装 @blgc/openapi-router?

在项目目录运行 npm install @blgc/openapi-router,或使用 pnpm add / yarn add / bun add。

@blgc/openapi-router 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@blgc/openapi-router 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@blgc/openapi-router 可以商用吗?

@blgc/openapi-router 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@blgc/openapi-router 的下载量有多少?

根据 npm 官方下载统计,@blgc/openapi-router 在 2026/10/09 – 2026/11/07 这 30 天内被下载 1,772 次,日均约 59 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@blgc/openapi-router 还在维护吗?

最新版本 0.1.0 发布于 2024/12/22(1.9 年前);至今共发布 14 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/08/02。作者已将其标记为弃用,新项目建议寻找替代方案。