@gravito/core 1.6.0 → 2.0.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.
Files changed (171) hide show
  1. package/README.md +100 -6
  2. package/README.zh-TW.md +101 -6
  3. package/dist/Application.d.ts +256 -0
  4. package/dist/CommandKernel.d.ts +33 -0
  5. package/dist/ConfigManager.d.ts +65 -0
  6. package/dist/Container/RequestScopeManager.d.ts +62 -0
  7. package/dist/Container/RequestScopeMetrics.d.ts +144 -0
  8. package/dist/Container.d.ts +153 -0
  9. package/dist/ErrorHandler.d.ts +66 -0
  10. package/dist/Event.d.ts +5 -0
  11. package/dist/EventManager.d.ts +123 -0
  12. package/dist/GlobalErrorHandlers.d.ts +47 -0
  13. package/dist/GravitoServer.d.ts +28 -0
  14. package/dist/HookManager.d.ts +435 -0
  15. package/dist/Listener.d.ts +4 -0
  16. package/dist/Logger.d.ts +20 -0
  17. package/dist/PlanetCore.d.ts +402 -0
  18. package/dist/RequestContext.d.ts +97 -0
  19. package/dist/Route.d.ts +36 -0
  20. package/dist/Router.d.ts +270 -0
  21. package/dist/ServiceProvider.d.ts +178 -0
  22. package/dist/adapters/GravitoEngineAdapter.d.ts +27 -0
  23. package/dist/adapters/bun/AdaptiveAdapter.d.ts +99 -0
  24. package/dist/adapters/bun/BunContext.d.ts +54 -0
  25. package/dist/adapters/bun/BunNativeAdapter.d.ts +66 -0
  26. package/dist/adapters/bun/BunRequest.d.ts +31 -0
  27. package/dist/adapters/bun/BunWebSocketHandler.d.ts +48 -0
  28. package/dist/adapters/bun/RadixNode.d.ts +19 -0
  29. package/dist/adapters/bun/RadixRouter.d.ts +32 -0
  30. package/dist/adapters/bun/index.d.ts +7 -0
  31. package/dist/adapters/bun/types.d.ts +20 -0
  32. package/dist/adapters/index.d.ts +12 -0
  33. package/dist/adapters/types.d.ts +235 -0
  34. package/dist/binary/BinaryUtils.d.ts +105 -0
  35. package/dist/binary/index.d.ts +5 -0
  36. package/dist/cli/queue-commands.d.ts +6 -0
  37. package/dist/compat/async-local-storage.browser.d.ts +9 -0
  38. package/dist/compat/async-local-storage.d.ts +7 -0
  39. package/dist/compat/crypto.browser.d.ts +5 -0
  40. package/dist/compat/crypto.d.ts +6 -0
  41. package/dist/compat.cjs +42 -11
  42. package/dist/compat.cjs.map +9 -0
  43. package/dist/compat.d.ts +23 -1
  44. package/dist/compat.js +3 -0
  45. package/dist/compat.js.map +9 -0
  46. package/dist/engine/AOTRouter.d.ts +139 -0
  47. package/dist/engine/FastContext.d.ts +141 -0
  48. package/dist/engine/Gravito.d.ts +131 -0
  49. package/dist/engine/MinimalContext.d.ts +102 -0
  50. package/dist/engine/analyzer.d.ts +113 -0
  51. package/dist/engine/constants.d.ts +23 -0
  52. package/dist/engine/index.cjs +758 -532
  53. package/dist/engine/index.cjs.map +22 -0
  54. package/dist/engine/index.d.ts +14 -690
  55. package/dist/engine/index.js +758 -508
  56. package/dist/engine/index.js.map +22 -0
  57. package/dist/engine/path.d.ts +26 -0
  58. package/dist/engine/pool.d.ts +83 -0
  59. package/dist/engine/types.d.ts +149 -0
  60. package/dist/error-handling/RequestScopeErrorContext.d.ts +126 -0
  61. package/dist/events/BackpressureManager.d.ts +215 -0
  62. package/dist/events/CircuitBreaker.d.ts +229 -0
  63. package/dist/events/DeadLetterQueue.d.ts +219 -0
  64. package/dist/events/EventBackend.d.ts +12 -0
  65. package/dist/events/EventOptions.d.ts +204 -0
  66. package/dist/events/EventPriorityQueue.d.ts +63 -0
  67. package/dist/events/FlowControlStrategy.d.ts +109 -0
  68. package/dist/events/IdempotencyCache.d.ts +60 -0
  69. package/dist/events/MessageQueueBridge.d.ts +184 -0
  70. package/dist/events/PriorityEscalationManager.d.ts +82 -0
  71. package/dist/events/RetryScheduler.d.ts +104 -0
  72. package/dist/events/WorkerPool.d.ts +98 -0
  73. package/dist/events/WorkerPoolConfig.d.ts +153 -0
  74. package/dist/events/WorkerPoolMetrics.d.ts +65 -0
  75. package/dist/events/aggregation/AggregationWindow.d.ts +77 -0
  76. package/dist/events/aggregation/DeduplicationManager.d.ts +135 -0
  77. package/dist/events/aggregation/EventAggregationManager.d.ts +108 -0
  78. package/dist/events/aggregation/EventBatcher.d.ts +99 -0
  79. package/dist/events/aggregation/index.d.ts +10 -0
  80. package/dist/events/aggregation/types.d.ts +117 -0
  81. package/dist/events/index.d.ts +26 -0
  82. package/dist/events/observability/EventMetrics.d.ts +132 -0
  83. package/dist/events/observability/EventTracer.d.ts +68 -0
  84. package/dist/events/observability/EventTracing.d.ts +161 -0
  85. package/dist/events/observability/OTelEventMetrics.d.ts +332 -0
  86. package/dist/events/observability/ObservableHookManager.d.ts +108 -0
  87. package/dist/events/observability/StreamWorkerMetrics.d.ts +76 -0
  88. package/dist/events/observability/index.d.ts +24 -0
  89. package/dist/events/observability/metrics-types.d.ts +16 -0
  90. package/dist/events/queue-core.d.ts +77 -0
  91. package/dist/events/task-executor.d.ts +51 -0
  92. package/dist/events/types.d.ts +134 -0
  93. package/dist/exceptions/AuthenticationException.d.ts +8 -0
  94. package/dist/exceptions/AuthorizationException.d.ts +8 -0
  95. package/dist/exceptions/CircularDependencyException.d.ts +9 -0
  96. package/dist/exceptions/GravitoException.d.ts +23 -0
  97. package/dist/exceptions/HttpException.d.ts +9 -0
  98. package/dist/exceptions/ModelNotFoundException.d.ts +10 -0
  99. package/dist/exceptions/ValidationException.d.ts +22 -0
  100. package/dist/exceptions/index.d.ts +7 -0
  101. package/dist/ffi/NativeAccelerator.d.ts +69 -0
  102. package/dist/ffi/NativeHasher.d.ts +139 -0
  103. package/dist/ffi/cbor-fallback.d.ts +96 -0
  104. package/dist/ffi/hash-fallback.d.ts +33 -0
  105. package/dist/ffi/index.cjs +621 -0
  106. package/dist/ffi/index.cjs.map +14 -0
  107. package/dist/ffi/index.d.ts +10 -0
  108. package/dist/ffi/index.js +602 -0
  109. package/dist/ffi/index.js.map +14 -0
  110. package/dist/ffi/types.d.ts +135 -0
  111. package/dist/health/HealthProvider.d.ts +67 -0
  112. package/dist/helpers/Arr.d.ts +19 -0
  113. package/dist/helpers/Str.d.ts +38 -0
  114. package/dist/helpers/data.d.ts +25 -0
  115. package/dist/helpers/errors.d.ts +34 -0
  116. package/dist/helpers/response.d.ts +41 -0
  117. package/dist/helpers.d.ts +338 -0
  118. package/dist/hooks/ActionManager.d.ts +132 -0
  119. package/dist/hooks/AsyncDetector.d.ts +84 -0
  120. package/dist/hooks/FilterManager.d.ts +71 -0
  121. package/dist/hooks/MigrationWarner.d.ts +24 -0
  122. package/dist/hooks/dlq-operations.d.ts +60 -0
  123. package/dist/hooks/index.d.ts +11 -0
  124. package/dist/hooks/types.d.ts +107 -0
  125. package/dist/http/CookieJar.d.ts +51 -0
  126. package/dist/http/cookie.d.ts +29 -0
  127. package/dist/http/index.d.ts +12 -0
  128. package/dist/{compat-C4Src6NN.d.cts → http/types.d.ts} +48 -16
  129. package/dist/index.browser.d.ts +34 -0
  130. package/dist/index.cjs +12909 -9492
  131. package/dist/index.cjs.map +168 -0
  132. package/dist/index.d.ts +58 -8707
  133. package/dist/index.js +12906 -9381
  134. package/dist/index.js.map +168 -0
  135. package/dist/observability/QueueDashboard.d.ts +136 -0
  136. package/dist/observability/contracts.d.ts +137 -0
  137. package/dist/observability/index.d.ts +13 -0
  138. package/dist/reliability/DeadLetterQueueManager.d.ts +349 -0
  139. package/dist/reliability/RetryPolicy.d.ts +217 -0
  140. package/dist/reliability/index.d.ts +6 -0
  141. package/dist/router/ControllerDispatcher.d.ts +12 -0
  142. package/dist/router/RequestValidator.d.ts +20 -0
  143. package/dist/runtime/adapter-bun.d.ts +12 -0
  144. package/dist/runtime/adapter-deno.d.ts +12 -0
  145. package/dist/runtime/adapter-node.d.ts +12 -0
  146. package/dist/runtime/adapter-unknown.d.ts +13 -0
  147. package/dist/runtime/archive.d.ts +17 -0
  148. package/dist/runtime/compression.d.ts +21 -0
  149. package/dist/runtime/deep-equals.d.ts +56 -0
  150. package/dist/runtime/detection.d.ts +22 -0
  151. package/dist/runtime/escape.d.ts +34 -0
  152. package/dist/runtime/index.browser.d.ts +20 -0
  153. package/dist/runtime/index.d.ts +44 -0
  154. package/dist/runtime/markdown.d.ts +44 -0
  155. package/dist/runtime/types.d.ts +436 -0
  156. package/dist/runtime-helpers.d.ts +67 -0
  157. package/dist/runtime.d.ts +11 -0
  158. package/dist/security/Encrypter.d.ts +33 -0
  159. package/dist/security/Hasher.d.ts +29 -0
  160. package/dist/testing/HttpTester.d.ts +39 -0
  161. package/dist/testing/TestResponse.d.ts +78 -0
  162. package/dist/testing/index.d.ts +2 -0
  163. package/dist/transpiler-utils.d.ts +170 -0
  164. package/dist/types/events.d.ts +94 -0
  165. package/dist/types.d.ts +13 -0
  166. package/package.json +23 -53
  167. package/src/ffi/native/cbor.c +1148 -0
  168. package/dist/compat-C4Src6NN.d.ts +0 -357
  169. package/dist/compat.d.cts +0 -1
  170. package/dist/engine/index.d.cts +0 -702
  171. package/dist/index.d.cts +0 -8734
@@ -0,0 +1,402 @@
1
+ /**
2
+ * @fileoverview PlanetCore - The Heart of Gravito Framework
3
+ *
4
+ * The micro-kernel that orchestrates the entire Galaxy Architecture.
5
+ * Manages HTTP routing, middleware, error handling, and orbit integration.
6
+ *
7
+ * @module @gravito/core
8
+ * @since 1.0.0
9
+ */
10
+ import { type HttpAdapter } from './adapters/types';
11
+ import { ConfigManager } from './ConfigManager';
12
+ import { Container } from './Container';
13
+ import { EventManager } from './EventManager';
14
+ import { type RegisterGlobalErrorHandlersOptions } from './GlobalErrorHandlers';
15
+ import { HookManager } from './HookManager';
16
+ import type { fail } from './helpers/response';
17
+ import type { ContentfulStatusCode, GravitoContext } from './http/types';
18
+ import { type Logger } from './Logger';
19
+ import { type ObservabilityProvider } from './observability/contracts';
20
+ import type { ServiceProvider } from './ServiceProvider';
21
+ /**
22
+ * CacheService interface for orbit-injected cache
23
+ * Orbits implementing cache should conform to this interface
24
+ */
25
+ export interface CacheService {
26
+ get<T = unknown>(key: string): Promise<T | null>;
27
+ set(key: string, value: unknown, ttl?: number): Promise<void>;
28
+ delete(key: string): Promise<void>;
29
+ clear(): Promise<void>;
30
+ remember<T>(key: string, ttl: number, callback: () => Promise<T>): Promise<T>;
31
+ }
32
+ /**
33
+ * Interface for View Rendering Service
34
+ * @public
35
+ */
36
+ export interface ViewService {
37
+ render(view: string, data?: Record<string, unknown>, options?: Record<string, unknown>): string;
38
+ }
39
+ /**
40
+ * Context passed to error handlers
41
+ * @public
42
+ */
43
+ export type ErrorHandlerContext = {
44
+ core: PlanetCore;
45
+ c: GravitoContext;
46
+ error: unknown;
47
+ isProduction: boolean;
48
+ accept: string;
49
+ wantsHtml: boolean;
50
+ status: ContentfulStatusCode;
51
+ payload: ReturnType<typeof fail>;
52
+ logLevel?: 'error' | 'warn' | 'info' | 'none';
53
+ logMessage?: string;
54
+ html?: {
55
+ templates: string[];
56
+ data: Record<string, unknown>;
57
+ };
58
+ };
59
+ /**
60
+ * Interface for Gravito Orbit (Plugin/Module)
61
+ * @public
62
+ */
63
+ export interface GravitoOrbit {
64
+ install(core: PlanetCore): void | Promise<void>;
65
+ }
66
+ /**
67
+ * Configuration for booting PlanetCore
68
+ * @public
69
+ */
70
+ export type GravitoConfig = {
71
+ logger?: Logger;
72
+ config?: Record<string, unknown>;
73
+ orbits?: (new () => GravitoOrbit)[] | GravitoOrbit[];
74
+ /**
75
+ * HTTP Adapter to use. Defaults to BunNativeAdapter in Bun environments.
76
+ * In non-Bun environments, must be provided explicitly.
77
+ * @example
78
+ * ```typescript
79
+ * import { PhotonAdapter } from '@gravito/photon/adapter'
80
+ * new PlanetCore({ adapter: new PhotonAdapter() })
81
+ * ```
82
+ * See https://gravito.dev/guides/http-adapters for available adapters.
83
+ * @since 2.0.0
84
+ */
85
+ adapter?: HttpAdapter;
86
+ /**
87
+ * Dependency Injection Container. If provided, PlanetCore will use this
88
+ * container instead of creating a new one. This allows sharing a container
89
+ * between Application and PlanetCore.
90
+ * @since 2.0.0
91
+ */
92
+ container?: Container;
93
+ /**
94
+ * Observability configuration for event system.
95
+ * @since 2.1.0
96
+ */
97
+ observability?: {
98
+ /**
99
+ * Enable event system observability (metrics, tracing).
100
+ * @default false
101
+ */
102
+ enabled?: boolean;
103
+ /**
104
+ * Enable OpenTelemetry distributed tracing.
105
+ * @default false
106
+ */
107
+ tracing?: boolean;
108
+ /**
109
+ * Prefix for metric names.
110
+ * @default 'gravito_event_'
111
+ */
112
+ metricsPrefix?: string;
113
+ /**
114
+ * Prometheus metrics configuration.
115
+ */
116
+ prometheus?: {
117
+ /**
118
+ * Enable Prometheus metrics endpoint.
119
+ * @default true
120
+ */
121
+ enabled?: boolean;
122
+ /**
123
+ * Port for Prometheus metrics endpoint.
124
+ * @default 9090
125
+ */
126
+ port?: number;
127
+ /**
128
+ * Endpoint path for metrics.
129
+ * @default '/metrics'
130
+ */
131
+ endpoint?: string;
132
+ };
133
+ };
134
+ /**
135
+ * Observability provider for distributed tracing and metrics.
136
+ * If provided, this will be used instead of the default OTel setup.
137
+ * @since 2.2.0
138
+ */
139
+ observabilityProvider?: ObservabilityProvider;
140
+ };
141
+ import { Router } from './Router';
142
+ import { Encrypter } from './security/Encrypter';
143
+ import { BunHasher } from './security/Hasher';
144
+ /**
145
+ * PlanetCore - The Heart of Gravito Framework
146
+ *
147
+ * The micro-kernel that orchestrates the entire Galaxy Architecture.
148
+ * Manages HTTP routing, middleware, error handling, and orbit integration.
149
+ * @public
150
+ */
151
+ export declare class PlanetCore {
152
+ /**
153
+ * The HTTP adapter used by this core instance.
154
+ * @since 2.0.0
155
+ */
156
+ private _adapter;
157
+ /**
158
+ * Access the underlying Photon app instance.
159
+ * @deprecated Use adapter methods for new code. This property is kept for backward compatibility.
160
+ */
161
+ get app(): unknown;
162
+ /**
163
+ * Get the HTTP adapter instance.
164
+ * @since 2.0.0
165
+ */
166
+ get adapter(): HttpAdapter;
167
+ logger: Logger;
168
+ config: ConfigManager;
169
+ hooks: HookManager;
170
+ events: EventManager;
171
+ router: Router;
172
+ container: Container;
173
+ /** @deprecated Use core.container instead */
174
+ services: Map<string, unknown>;
175
+ encrypter?: Encrypter;
176
+ hasher: BunHasher;
177
+ /**
178
+ * Observability provider for distributed tracing and metrics.
179
+ * @since 2.2.0
180
+ */
181
+ observabilityProvider: ObservabilityProvider;
182
+ private providers;
183
+ private deferredProviders;
184
+ private bootedProviders;
185
+ private isShuttingDown;
186
+ /**
187
+ * Initialize observability asynchronously (metrics, tracing, Prometheus).
188
+ * This is called from constructor but doesn't block initialization.
189
+ *
190
+ * Phase 2.2 Update: Now uses the observabilityProvider passed from @gravito/monitor
191
+ * or falls back to OTel implementation if available for backward compatibility.
192
+ *
193
+ * @internal
194
+ */
195
+ private initializeObservabilityAsync;
196
+ /**
197
+ * Initialize Prometheus metrics asynchronously.
198
+ *
199
+ * @internal
200
+ * @deprecated Prometheus setup has been moved to @gravito/monitor
201
+ */
202
+ private initializePrometheusAsync;
203
+ /**
204
+ * Register a service provider to the core.
205
+ *
206
+ * Service providers are the central place to configure your application.
207
+ * They bind services to the container and bootstrap application features.
208
+ *
209
+ * @param provider - The ServiceProvider instance to register.
210
+ * @returns The PlanetCore instance for chaining.
211
+ *
212
+ * @example
213
+ * ```typescript
214
+ * core.register(new DatabaseServiceProvider());
215
+ * ```
216
+ */
217
+ register(provider: ServiceProvider): this;
218
+ /**
219
+ * Bootstrap the application by registering and booting providers.
220
+ *
221
+ * This method orchestrates the two-phase startup sequence:
222
+ * 1. Registration: Calls `register()` on all providers to bind services.
223
+ * 2. Booting: Calls `boot()` on all providers once all bindings are ready.
224
+ *
225
+ * This method must be called before the application starts handling requests.
226
+ *
227
+ * @returns Promise that resolves when bootstrapping is complete.
228
+ * @throws Error if a deferred provider has an asynchronous register method.
229
+ *
230
+ * @example
231
+ * ```typescript
232
+ * await core.bootstrap();
233
+ * ```
234
+ */
235
+ bootstrap(): Promise<void>;
236
+ /**
237
+ * Called when the application is ready to accept requests.
238
+ *
239
+ * Invokes the `onReady()` lifecycle hook on all providers.
240
+ * Called automatically at the end of `bootstrap()`.
241
+ *
242
+ * @returns Promise that resolves when all providers are ready.
243
+ *
244
+ * @example
245
+ * ```typescript
246
+ * await core.ready();
247
+ * ```
248
+ */
249
+ ready(): Promise<void>;
250
+ /**
251
+ * Gracefully shutdown the application.
252
+ *
253
+ * Invokes the `onShutdown()` lifecycle hook on all providers in reverse order (LIFO).
254
+ * Should be called when the application receives a termination signal.
255
+ *
256
+ * @returns Promise that resolves when all providers have shut down.
257
+ *
258
+ * @example
259
+ * ```typescript
260
+ * process.on('SIGTERM', () => core.shutdown());
261
+ * ```
262
+ */
263
+ shutdown(): Promise<void>;
264
+ /**
265
+ * Setup deferred provider resolution.
266
+ * Wraps container.make to auto-register deferred providers on first request.
267
+ *
268
+ * @internal
269
+ */
270
+ private setupDeferredProviderResolution;
271
+ /**
272
+ * Register a deferred provider on-demand.
273
+ *
274
+ * @internal
275
+ */
276
+ private registerDeferredProvider;
277
+ /**
278
+ * Boot a single provider if not already booted.
279
+ *
280
+ * @internal
281
+ */
282
+ private bootProvider;
283
+ constructor(options?: {
284
+ logger?: Logger;
285
+ config?: Record<string, unknown>;
286
+ adapter?: HttpAdapter;
287
+ container?: Container;
288
+ observabilityProvider?: ObservabilityProvider;
289
+ });
290
+ /**
291
+ * Setup process signal handlers for graceful shutdown
292
+ *
293
+ * @internal
294
+ */
295
+ private setupSignalHandlers;
296
+ /**
297
+ * Programmatically register an infrastructure module (Orbit).
298
+ * @since 2.0.0
299
+ *
300
+ * @param orbit - The orbit class or instance to register.
301
+ * @returns The PlanetCore instance for chaining.
302
+ *
303
+ * @example
304
+ * ```typescript
305
+ * await core.orbit(OrbitCache);
306
+ * ```
307
+ */
308
+ orbit(orbit: GravitoOrbit | (new () => GravitoOrbit)): Promise<this>;
309
+ /**
310
+ * Programmatically register a feature module (Satellite).
311
+ * Alias for register() with provider support.
312
+ * @since 2.0.0
313
+ *
314
+ * @param satellite - The provider or setup function.
315
+ * @returns The PlanetCore instance for chaining.
316
+ *
317
+ * @example
318
+ * ```typescript
319
+ * await core.use(new AuthProvider());
320
+ * ```
321
+ */
322
+ use(satellite: ServiceProvider | ((core: PlanetCore) => void | Promise<void>)): Promise<this>;
323
+ /**
324
+ * Register a global error handler for process-level exceptions.
325
+ *
326
+ * Captures `unhandledRejection` and `uncaughtException` to prevent process crashes
327
+ * and allow for graceful shutdown or error reporting.
328
+ *
329
+ * @param options - Configuration for global error handling.
330
+ * @returns A function to unregister the global error handlers.
331
+ *
332
+ * @example
333
+ * ```typescript
334
+ * const unregister = core.registerGlobalErrorHandlers({
335
+ * exitOnFatal: true
336
+ * });
337
+ * ```
338
+ */
339
+ registerGlobalErrorHandlers(options?: Omit<RegisterGlobalErrorHandlersOptions, 'core'>): () => void;
340
+ /**
341
+ * Predictive Route Warming (JIT Optimization).
342
+ *
343
+ * Pre-compiles or warms up the specified paths in the HTTP adapter to reduce
344
+ * latency for the first request to these endpoints.
345
+ *
346
+ * @param paths - List of paths to warm up.
347
+ * @returns Promise that resolves when warming is complete.
348
+ *
349
+ * @example
350
+ * ```typescript
351
+ * await core.warmup(['/api/v1/products', '/api/v1/categories']);
352
+ * ```
353
+ */
354
+ warmup(paths: string[]): Promise<void>;
355
+ /**
356
+ * Boot the application with a configuration object (IoC style default entry)
357
+ *
358
+ * @param config - The Gravito configuration object.
359
+ * @returns A Promise resolving to the booted PlanetCore instance.
360
+ *
361
+ * @example
362
+ * ```typescript
363
+ * const core = await PlanetCore.boot(config);
364
+ * ```
365
+ */
366
+ static boot(config: GravitoConfig): Promise<PlanetCore>;
367
+ /**
368
+ * Mount an Orbit (a PlanetCore instance or native app) to a specific URL path.
369
+ *
370
+ * This allows for micro-service like composition where different parts of the
371
+ * application can be developed as independent Orbits and mounted together.
372
+ *
373
+ * @param path - The URL path to mount the orbit at.
374
+ * @param orbitApp - The application instance (PlanetCore, HttpAdapter, or native app).
375
+ *
376
+ * @example
377
+ * ```typescript
378
+ * const blogOrbit = new PlanetCore();
379
+ * core.mountOrbit('/blog', blogOrbit);
380
+ * ```
381
+ */
382
+ mountOrbit(path: string, orbitApp: unknown): void;
383
+ /**
384
+ * Start the core (Liftoff).
385
+ *
386
+ * Returns a config object for `Bun.serve`.
387
+ *
388
+ * @param port - Optional port number (defaults to config or 3000).
389
+ * @returns An object compatible with Bun.serve({ ... }).
390
+ *
391
+ * @example
392
+ * ```typescript
393
+ * export default core.liftoff(3000);
394
+ * ```
395
+ */
396
+ liftoff(port?: number): {
397
+ port: number;
398
+ fetch: (request: Request, server?: unknown) => Response | Promise<Response>;
399
+ core: PlanetCore;
400
+ websocket?: HttpAdapter['websocket'];
401
+ };
402
+ }
@@ -0,0 +1,97 @@
1
+ /**
2
+ * @fileoverview RequestContext - AsyncLocalStorage-based request context management
3
+ *
4
+ * Allows deep service layers to access request-scoped data (requestId, userId, etc.)
5
+ * without passing parameters through the call stack.
6
+ *
7
+ * @module @gravito/core/RequestContext
8
+ * @since 2.2.0
9
+ */
10
+ /**
11
+ * 請求上下文資料介面
12
+ * 包含請求相關的唯一識別碼和上下文資訊
13
+ * @public
14
+ */
15
+ export interface RequestContextData {
16
+ /** 唯一的請求識別碼 */
17
+ requestId: string;
18
+ /** 使用者 ID(可選) */
19
+ userId?: string;
20
+ /** 租戶 ID(可選) */
21
+ tenantId?: string;
22
+ /** 追蹤 ID(可選) */
23
+ traceId?: string;
24
+ /** 自訂欄位 */
25
+ [key: string]: unknown;
26
+ }
27
+ /**
28
+ * RequestContext 物件 - 管理請求上下文的 API
29
+ *
30
+ * 提供在非同步鏈中存取和設定請求上下文的方法
31
+ * @public
32
+ */
33
+ export declare const RequestContext: {
34
+ /**
35
+ * 在給定的上下文中執行函式
36
+ *
37
+ * 確保函式及其所有非同步呼叫都在同一個上下文中執行
38
+ *
39
+ * @param data - 請求上下文資料
40
+ * @param fn - 要執行的函式(可以是非同步的或同步的)
41
+ * @returns 如果函式是非同步的,返回 Promise;如果是同步的,直接返回值
42
+ *
43
+ * @example
44
+ * ```typescript
45
+ * await RequestContext.run({ requestId: 'req-123' }, async () => {
46
+ * const userId = RequestContext.get()?.userId;
47
+ * });
48
+ *
49
+ * const result = RequestContext.run({ requestId: 'req-123' }, () => {
50
+ * return 'sync-result';
51
+ * });
52
+ * ```
53
+ */
54
+ run<T>(data: RequestContextData, fn: () => T | Promise<T>): T | Promise<T>;
55
+ /**
56
+ * 獲取當前請求的上下文資料
57
+ *
58
+ * @returns 請求上下文資料,如果不在請求上下文中則返回 undefined
59
+ *
60
+ * @example
61
+ * ```typescript
62
+ * const context = RequestContext.get();
63
+ * if (context) {
64
+ * console.log(context.requestId);
65
+ * }
66
+ * ```
67
+ */
68
+ get(): RequestContextData | undefined;
69
+ /**
70
+ * 獲取當前請求的上下文資料,如果不存在則拋出錯誤
71
+ *
72
+ * @returns 請求上下文資料
73
+ * @throws Error 如果不在請求上下文中
74
+ *
75
+ * @example
76
+ * ```typescript
77
+ * const context = RequestContext.getOrThrow();
78
+ * console.log(context.requestId); // 保證不為 undefined
79
+ * ```
80
+ */
81
+ getOrThrow(): RequestContextData;
82
+ /**
83
+ * 在當前上下文中設定或修改值
84
+ *
85
+ * @param key - 要設定的欄位名稱
86
+ * @param value - 要設定的值
87
+ * @throws Error 如果不在請求上下文中
88
+ *
89
+ * @example
90
+ * ```typescript
91
+ * RequestContext.set('userId', 'user-456');
92
+ * const userId = RequestContext.get()?.userId; // 'user-456'
93
+ * ```
94
+ */
95
+ set(key: string, value: unknown): void;
96
+ };
97
+ export default RequestContext;
@@ -0,0 +1,36 @@
1
+ import type { GravitoMiddleware } from './http/types';
2
+ import type { ControllerClass, FormRequestClass, ResourceOptions, RouteHandler, RouteOptions, Router } from './Router';
3
+ /**
4
+ * Route definition helper.
5
+ * Represents a registered route and allows method chaining for middleware/names.
6
+ * @public
7
+ */
8
+ export declare class Route {
9
+ private router;
10
+ private method;
11
+ private path;
12
+ private options;
13
+ constructor(router: Router, method: string, path: string, options: RouteOptions);
14
+ /**
15
+ * Name the route
16
+ */
17
+ name(name: string): this;
18
+ static get(path: string, handler: RouteHandler): Route;
19
+ static get(path: string, request: FormRequestClass, handler: RouteHandler): Route;
20
+ static get(path: string, middleware: GravitoMiddleware | GravitoMiddleware[], handler: RouteHandler): Route;
21
+ static post(path: string, handler: RouteHandler): Route;
22
+ static post(path: string, request: FormRequestClass, handler: RouteHandler): Route;
23
+ static post(path: string, middleware: GravitoMiddleware | GravitoMiddleware[], handler: RouteHandler): Route;
24
+ static put(path: string, handler: RouteHandler): Route;
25
+ static put(path: string, request: FormRequestClass, handler: RouteHandler): Route;
26
+ static put(path: string, middleware: GravitoMiddleware | GravitoMiddleware[], handler: RouteHandler): Route;
27
+ static delete(path: string, handler: RouteHandler): Route;
28
+ static delete(path: string, request: FormRequestClass, handler: RouteHandler): Route;
29
+ static delete(path: string, middleware: GravitoMiddleware | GravitoMiddleware[], handler: RouteHandler): Route;
30
+ static patch(path: string, handler: RouteHandler): Route;
31
+ static patch(path: string, request: FormRequestClass, handler: RouteHandler): Route;
32
+ static patch(path: string, middleware: GravitoMiddleware | GravitoMiddleware[], handler: RouteHandler): Route;
33
+ static resource(name: string, controller: ControllerClass, options?: ResourceOptions): void;
34
+ static prefix(path: string): import("./Router").RouteGroup;
35
+ static middleware(...handlers: (GravitoMiddleware | GravitoMiddleware[])[]): import("./Router").RouteGroup;
36
+ }