|
| 1 | +import InstallPackagesCode from '@site/docs/components/_install-packages-code.mdx' |
| 2 | +import ExecPackageCommandCode from '@site/docs/components/_exec-package-command-code.mdx' |
| 3 | + |
| 4 | +# 在 NestJS 中使用 Keq |
| 5 | + |
| 6 | +本文介绍如何在 NestJS 项目中集成 Keq,以及如何使用 OpenAPI 自动生成类型安全的客户端代码。 |
| 7 | + |
| 8 | +## 安装依赖 |
| 9 | + |
| 10 | + |
| 11 | +<InstallPackagesCode packages="keq @keq-request/cli @keq-request/nestjs" /> |
| 12 | + |
| 13 | +## 注册 KeqModule |
| 14 | + |
| 15 | +在应用的根模块中注册 `KeqModule`: |
| 16 | + |
| 17 | +```typescript title="src/app.module.ts" |
| 18 | +import { Module } from '@nestjs/common' |
| 19 | +import { KeqModule } from '@keq-request/nestjs' |
| 20 | +import { validateStatusCode } from '@keq-request/exception' |
| 21 | + |
| 22 | +@Module({ |
| 23 | + imports: [ |
| 24 | + KeqModule.register({ // [!code ++] |
| 25 | + middlewares: [ // [!code ++] |
| 26 | + // 配置全局中间件 [!code ++] |
| 27 | + validateStatusCode(), // [!code ++] |
| 28 | + ] // [!code ++] |
| 29 | + }), // [!code ++] |
| 30 | + |
| 31 | + /* ...其他模块... */ |
| 32 | + ] |
| 33 | +}) |
| 34 | +export class AppModule {} |
| 35 | +``` |
| 36 | + |
| 37 | +注册后即可在 Service 中通过依赖注入使用 `KeqRequest`: |
| 38 | + |
| 39 | +```typescript title="src/app.service.ts" |
| 40 | +import { Injectable } from '@nestjs/common' |
| 41 | +import { KeqRequest } from 'keq' |
| 42 | + |
| 43 | +@Injectable() |
| 44 | +export class AppService { |
| 45 | + constructor(private readonly request: KeqRequest) {} |
| 46 | + |
| 47 | + async getCats() { |
| 48 | + const cats = await this.request.get('https://api.example.com/cats') |
| 49 | + return cats |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +## 使用 OpenAPI 生成 NestJS 模块 |
| 55 | + |
| 56 | +`@keq-request/cli` 支持从 OpenAPI 文档生成 NestJS 模块,提供完整的类型定义和依赖注入支持。 |
| 57 | + |
| 58 | +### 配置生成模式 |
| 59 | + |
| 60 | +在 `.keqrc.ts` 中设置 `mode` 为 `nestjs-module`: |
| 61 | + |
| 62 | +```typescript title=".keqrc.ts" |
| 63 | +import { defineKeqConfig, FileNamingStyle } from '@keq-request/cli' |
| 64 | + |
| 65 | +export default defineKeqConfig({ |
| 66 | + mode: 'nestjs-module', // [!code ++] |
| 67 | + outdir: "./src/apis", |
| 68 | + fileNamingStyle: FileNamingStyle.snakeCase, |
| 69 | + modules: { |
| 70 | + catService: "./cat-service-swagger.json", |
| 71 | + }, |
| 72 | +}) |
| 73 | +``` |
| 74 | + |
| 75 | +运行生成命令: |
| 76 | + |
| 77 | +<ExecPackageCommandCode command="keq build" /> |
| 78 | + |
| 79 | +### 注册生成的模块 |
| 80 | + |
| 81 | +将生成的模块导入到应用中: |
| 82 | + |
| 83 | +```typescript title="src/app.module.ts" |
| 84 | +import { Module } from '@nestjs/common' |
| 85 | +import { KeqModule } from '@keq-request/nestjs' |
| 86 | +import { CatServiceModule } from './apis/cat_service/cat_service.module' // [!code ++] |
| 87 | +
|
| 88 | +@Module({ |
| 89 | + imports: [ |
| 90 | + // 必须注册 KeqModule |
| 91 | + KeqModule.register({ |
| 92 | + // 配置全局中间件 |
| 93 | + middlewares: [ |
| 94 | + validateStatusCode(), |
| 95 | + ] |
| 96 | + }), |
| 97 | + |
| 98 | + // 注册生成的模块 |
| 99 | + CatServiceModule.register({ // [!code ++] |
| 100 | + // 模块级中间件配置 [!code ++] |
| 101 | + middlewares: [ // [!code ++] |
| 102 | + setBaseUrl('https://cat-api.example.com'), // [!code ++] |
| 103 | + appendHeader('Authorization', 'Bearer YOUR_TOKEN_HERE'), // [!code ++] |
| 104 | + ] // [!code ++] |
| 105 | + }), // [!code ++] |
| 106 | + ] |
| 107 | +}) |
| 108 | +export class AppModule {} |
| 109 | +``` |
| 110 | + |
| 111 | +:::tip 中间件执行顺序 |
| 112 | +请求发送时,中间件的执行顺序为:全局中间件 → 模块级中间件。 |
| 113 | +::: |
| 114 | + |
| 115 | +### 使用生成的客户端 |
| 116 | + |
| 117 | +```typescript title="src/cat.service.ts" |
| 118 | +import { Injectable } from '@nestjs/common' |
| 119 | +import { CatServiceClient } from './apis/cat_service/cat_service.client' |
| 120 | + |
| 121 | +@Injectable() |
| 122 | +export class CatService { |
| 123 | + constructor(private readonly catServiceClient: CatServiceClient) {} |
| 124 | + |
| 125 | + async getCats() { |
| 126 | + const response = await this.catServiceClient.getCats<200>() |
| 127 | + return response.body |
| 128 | + } |
| 129 | + |
| 130 | + async getCatById(id: string) { |
| 131 | + const response = await this.catServiceClient.getCatById<200>({ id }) |
| 132 | + return response.body |
| 133 | + } |
| 134 | +} |
| 135 | +``` |
0 commit comments