|
3 | 3 | import fs from 'node:fs' |
4 | 4 | import httpModule from 'node:http' |
5 | 5 | import httpsModule from 'node:https' |
6 | | -import type { AddressInfo } from 'node:net' |
7 | 6 | import process from 'node:process' |
8 | 7 | import { emit, on, register } from '@zerva/core' |
9 | 8 | import corsDefault from 'cors' |
10 | 9 | import express from 'express' |
11 | | -import type { HelmetOptions } from 'helmet' |
12 | 10 | import helmetDefault from 'helmet' |
| 11 | +import { isLocalHost, isString, LoggerFromConfig, LogLevelInfo, promisify, valueToBoolean } from 'zeed' |
| 12 | +import type { HelmetOptions } from 'helmet' |
| 13 | +import type { AddressInfo } from 'node:net' |
13 | 14 | import type { LogConfig } from 'zeed' |
14 | | -import { LogLevelInfo, LoggerFromConfig, isLocalHost, isString, promisify, valueToBoolean } from 'zeed' |
15 | 15 | import { compressionMiddleware } from './compression' |
16 | 16 | import type { Express, NextFunction, Request, Response, Server, zervaHttpGetHandler, zervaHttpHandlerModes, zervaHttpInterface, zervaHttpPaths } from './types' |
17 | 17 |
|
18 | | -export * from './types' |
19 | 18 | export * from './status' |
| 19 | +export * from './types' |
20 | 20 |
|
21 | 21 | const moduleName = 'http' |
22 | 22 |
|
@@ -179,14 +179,25 @@ export function useHttp(config?: { |
179 | 179 | log.error('client request error', err) |
180 | 180 | }) |
181 | 181 |
|
| 182 | + interface ZervaHttpRouteDescription { |
| 183 | + path: string |
| 184 | + method: string |
| 185 | + description: string |
| 186 | + } |
| 187 | + |
| 188 | + const routes: ZervaHttpRouteDescription[] = [] |
| 189 | + |
182 | 190 | function smartRequestHandler( |
183 | 191 | mode: zervaHttpHandlerModes, |
184 | 192 | path: zervaHttpPaths, |
185 | 193 | handlers: zervaHttpGetHandler[], |
186 | | - ): void { |
| 194 | + ) { |
187 | 195 | if (isString(path) && !path.startsWith('/')) |
188 | 196 | path = `/${path}` |
189 | 197 |
|
| 198 | + const route: ZervaHttpRouteDescription = { path: String(path), method: mode, description: '' } |
| 199 | + routes.push(route) |
| 200 | + |
190 | 201 | log(`register ${mode.toUpperCase()} ${path}`) |
191 | 202 |
|
192 | 203 | let suffix: string | undefined |
@@ -243,6 +254,12 @@ export function useHttp(config?: { |
243 | 254 | } |
244 | 255 | }) |
245 | 256 | } |
| 257 | + |
| 258 | + return { |
| 259 | + description(description: string) { |
| 260 | + route.description = description |
| 261 | + }, |
| 262 | + } |
246 | 263 | } |
247 | 264 |
|
248 | 265 | function addStatic(path: zervaHttpPaths, fsPath: string): void { |
@@ -286,6 +303,7 @@ export function useHttp(config?: { |
286 | 303 | addStatic, |
287 | 304 | static: addStatic, |
288 | 305 | STATIC: addStatic, |
| 306 | + routes, |
289 | 307 | } as any) |
290 | 308 | }) |
291 | 309 |
|
@@ -315,6 +333,7 @@ export function useHttp(config?: { |
315 | 333 | addStatic, |
316 | 334 | static: addStatic, |
317 | 335 | STATIC: addStatic, |
| 336 | + routes, |
318 | 337 | } as any) |
319 | 338 | server.listen({ host, port }, () => { |
320 | 339 | const { port, family, address } = server.address() as AddressInfo |
|
0 commit comments