@cslug/http 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cslug/http",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Router, middleware, HttpContext, sessions, CSRF, error rendering on Bun.serve.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -29,7 +29,7 @@
29
29
  "bun": ">=1.3.14"
30
30
  },
31
31
  "peerDependencies": {
32
- "@cslug/core": "0.3.0",
33
- "@cslug/validation": "0.3.0"
32
+ "@cslug/core": "0.4.0",
33
+ "@cslug/validation": "0.4.0"
34
34
  }
35
35
  }
package/src/commands.ts CHANGED
@@ -53,21 +53,24 @@ export class RoutesCommand extends Command {
53
53
  }
54
54
 
55
55
  handle({ json, filter }: Command.Args<typeof RoutesCommand>) {
56
- const rows = this.app
57
- .resolve(Router)
58
- .routes.filter((r) => !filter || r.path.includes(filter) || r.name?.includes(filter))
59
- .map((r) => ({
60
- method: r.method,
61
- path: `${r.domain ? `//${r.domain}` : ''}${r.path}`,
62
- name: r.name ?? '',
63
- action: actionName(r.action),
64
- middleware: r.middleware.map((m) => (typeof m === 'function' ? m.name || 'fn' : String(m))).join(', '),
65
- }))
56
+ const rows = routeRows(this.app.resolve(Router)).filter(
57
+ (r) => !filter || r.path.includes(filter) || r.name.includes(filter),
58
+ )
66
59
  if (json) this.line(JSON.stringify(rows))
67
- else this.table(rows)
60
+ else this.table(rows.map((r) => ({ ...r, middleware: r.middleware.join(', ') })))
68
61
  }
69
62
  }
70
63
 
64
+ /** The routes as `cslug routes` and `cslug describe routes` show them. */
65
+ export const routeRows = (router: Router) =>
66
+ router.routes.map((r) => ({
67
+ method: r.method,
68
+ path: `${r.domain ? `//${r.domain}` : ''}${r.path}`,
69
+ name: r.name ?? '',
70
+ action: actionName(r.action),
71
+ middleware: r.middleware.map((m) => (typeof m === 'function' ? m.name || 'fn' : String(m))),
72
+ }))
73
+
71
74
  const actionName = (action: unknown): string => {
72
75
  if (Array.isArray(action)) return `${(action[0] as { name?: string })?.name}.${String(action[1])}`
73
76
  return typeof action === 'function' ? action.name || 'closure' : 'closure'
package/src/provider.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { existsSync } from 'node:fs'
2
2
  import { join } from 'node:path'
3
3
  import { Logger, ServiceProvider } from '@cslug/core'
4
- import { httpCommands } from './commands'
4
+ import { httpCommands, routeRows } from './commands'
5
5
  import { ExceptionHandler } from './errors'
6
6
  import { HttpKernel } from './kernel'
7
7
  import { bodyLimit, cors, csrf, securityHeaders, session } from './middleware'
@@ -38,6 +38,7 @@ export const API_MIDDLEWARE = 'http.middleware.api'
38
38
  export class HttpProvider extends ServiceProvider {
39
39
  register() {
40
40
  this.commands(httpCommands)
41
+ this.describe('routes', () => routeRows(this.app.resolve(Router)))
41
42
  this.app.singleton(Router, () => new Router())
42
43
  this.app.singleton(ExceptionHandler, (c) => new ExceptionHandler(this.app, c.resolve(Logger)))
43
44
  this.app.singleton(