Skip to content

Commit e307643

Browse files
committed
feat: http routes documentation
1 parent 77c4923 commit e307643

File tree

7 files changed

+34
-15
lines changed

7 files changed

+34
-15
lines changed

zerva-http/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,18 @@
6262
"bytes": "3.1.2",
6363
"compressible": "^2.0.18",
6464
"cors": "^2.8.5",
65-
"express": "^4.19.2",
65+
"express": "^4.20.0",
6666
"helmet": "^7.1.0",
6767
"mime": "^4.0.4",
6868
"negotiator": "^0.6.3",
6969
"on-headers": "^1.0.2",
7070
"vary": "^1.1.2",
71-
"zeed": "^0.24.16"
71+
"zeed": "^0.24.17"
7272
},
7373
"devDependencies": {
7474
"@zerva/bin": "workspace:*",
7575
"tsup": "^8.2.4",
76-
"typescript": "^5.5.4",
76+
"typescript": "^5.6.2",
7777
"vitest": "^2.0.5"
7878
}
7979
}

zerva-http/src/accepts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
* MIT Licensed
77
*/
88

9-
import Negotiator from 'negotiator'
109
import mime from 'mime'
10+
import Negotiator from 'negotiator'
1111

1212
function Accepts(this: any, req: any) {
1313
if (!(this instanceof Accepts)) {

zerva-http/src/compression.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
* MIT Licensed
88
*/
99

10-
import zlib from 'node:zlib'
11-
import type { IncomingMessage } from 'node:http'
12-
import type { Readable } from 'node:stream'
1310
import { Buffer } from 'node:buffer'
11+
import zlib from 'node:zlib'
1412
import bytes from 'bytes'
1513
import compressible from 'compressible'
1614
import onHeaders from 'on-headers'
1715
import vary from 'vary'
16+
import type { IncomingMessage } from 'node:http'
17+
import type { Readable } from 'node:stream'
1818
import accepts from './accepts'
1919

2020
const cacheControlNoTransformRegExp = /(?:^|,)\s*?no-transform\s*?(?:,|$)/

zerva-http/src/http.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
import fs from 'node:fs'
44
import httpModule from 'node:http'
55
import httpsModule from 'node:https'
6-
import type { AddressInfo } from 'node:net'
76
import process from 'node:process'
87
import { emit, on, register } from '@zerva/core'
98
import corsDefault from 'cors'
109
import express from 'express'
11-
import type { HelmetOptions } from 'helmet'
1210
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'
1314
import type { LogConfig } from 'zeed'
14-
import { LogLevelInfo, LoggerFromConfig, isLocalHost, isString, promisify, valueToBoolean } from 'zeed'
1515
import { compressionMiddleware } from './compression'
1616
import type { Express, NextFunction, Request, Response, Server, zervaHttpGetHandler, zervaHttpHandlerModes, zervaHttpInterface, zervaHttpPaths } from './types'
1717

18-
export * from './types'
1918
export * from './status'
19+
export * from './types'
2020

2121
const moduleName = 'http'
2222

@@ -179,14 +179,25 @@ export function useHttp(config?: {
179179
log.error('client request error', err)
180180
})
181181

182+
interface ZervaHttpRouteDescription {
183+
path: string
184+
method: string
185+
description: string
186+
}
187+
188+
const routes: ZervaHttpRouteDescription[] = []
189+
182190
function smartRequestHandler(
183191
mode: zervaHttpHandlerModes,
184192
path: zervaHttpPaths,
185193
handlers: zervaHttpGetHandler[],
186-
): void {
194+
) {
187195
if (isString(path) && !path.startsWith('/'))
188196
path = `/${path}`
189197

198+
const route: ZervaHttpRouteDescription = { path: String(path), method: mode, description: '' }
199+
routes.push(route)
200+
190201
log(`register ${mode.toUpperCase()} ${path}`)
191202

192203
let suffix: string | undefined
@@ -243,6 +254,12 @@ export function useHttp(config?: {
243254
}
244255
})
245256
}
257+
258+
return {
259+
description(description: string) {
260+
route.description = description
261+
},
262+
}
246263
}
247264

248265
function addStatic(path: zervaHttpPaths, fsPath: string): void {
@@ -286,6 +303,7 @@ export function useHttp(config?: {
286303
addStatic,
287304
static: addStatic,
288305
STATIC: addStatic,
306+
routes,
289307
} as any)
290308
})
291309

@@ -315,6 +333,7 @@ export function useHttp(config?: {
315333
addStatic,
316334
static: addStatic,
317335
STATIC: addStatic,
336+
routes,
318337
} as any)
319338
server.listen({ host, port }, () => {
320339
const { port, family, address } = server.address() as AddressInfo

zerva-http/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export * from './types'
21
export * from './http'
32
export * from './status'
3+
export * from './types'

zerva-http/src/status.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { LoggerInterface } from 'zeed'
21
import { Logger } from 'zeed'
2+
import type { LoggerInterface } from 'zeed'
33

44
const log: LoggerInterface = Logger('status')
55

zerva-http/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { Server } from 'node:http'
21
import type { Express, NextFunction, Request, RequestHandler, Response } from 'express'
2+
import type { Server } from 'node:http'
33

44
export type { Server }
55
export type { Express, NextFunction, Request, RequestHandler, Response }

0 commit comments

Comments
 (0)