route.d.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import {ValueOrPromise} from '../types.js';
  2. import {HookRegistry} from '../hooks/index.js';
  3. import {RequestContext} from '../request-context.js';
  4. /**
  5. * Http method.
  6. */
  7. export declare const HttpMethod: {
  8. GET: 'GET';
  9. POST: 'POST';
  10. PUT: 'PUT';
  11. PATCH: 'PATCH';
  12. DELETE: 'DELETE';
  13. };
  14. /**
  15. * Type of HttpMethod.
  16. */
  17. export type HttpMethod = (typeof HttpMethod)[keyof typeof HttpMethod];
  18. /**
  19. * Route handler.
  20. */
  21. export type RouteHandler = (ctx: RequestContext) => ValueOrPromise<unknown>;
  22. /**
  23. * Route pre-handler.
  24. */
  25. export type RoutePreHandler = RouteHandler;
  26. /**
  27. * Route post-handler.
  28. */
  29. export type RoutePostHandler<T = unknown, U = unknown> = (
  30. ctx: RequestContext,
  31. data: T,
  32. ) => ValueOrPromise<U>;
  33. /**
  34. * Route meta.
  35. */
  36. export type RouteMeta = {
  37. [key: string]: unknown;
  38. };
  39. /**
  40. * Route definition.
  41. */
  42. export type RouteDefinition = {
  43. method: string;
  44. path: string;
  45. handler: RouteHandler;
  46. preHandler?: RoutePreHandler | RoutePreHandler[];
  47. postHandler?: RoutePostHandler | RoutePostHandler[];
  48. meta?: RouteMeta;
  49. };
  50. /**
  51. * Route.
  52. */
  53. export declare class Route {
  54. /**
  55. * Method.
  56. */
  57. get method(): string;
  58. /**
  59. * Path.
  60. */
  61. get path(): string;
  62. /**
  63. * Meta.
  64. */
  65. get meta(): RouteMeta;
  66. /**
  67. * Handler.
  68. */
  69. get handler(): RouteHandler;
  70. /**
  71. * Hook registry.
  72. */
  73. get hookRegistry(): HookRegistry;
  74. /**
  75. * Constructor.
  76. *
  77. * @param routeDef
  78. */
  79. constructor(routeDef: RouteDefinition);
  80. /**
  81. * Handle.
  82. *
  83. * @param context
  84. */
  85. handle(context: RequestContext): ValueOrPromise<unknown>;
  86. }