@gravito/echo 3.0.0 → 3.0.1

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 (81) hide show
  1. package/dist/core/src/Application.d.ts +189 -0
  2. package/dist/core/src/ConfigManager.d.ts +26 -0
  3. package/dist/core/src/Container.d.ts +44 -0
  4. package/dist/core/src/ErrorHandler.d.ts +63 -0
  5. package/dist/core/src/Event.d.ts +5 -0
  6. package/dist/core/src/EventManager.d.ts +123 -0
  7. package/dist/core/src/GlobalErrorHandlers.d.ts +47 -0
  8. package/dist/core/src/GravitoServer.d.ts +28 -0
  9. package/dist/core/src/HookManager.d.ts +82 -0
  10. package/dist/core/src/Listener.d.ts +4 -0
  11. package/dist/core/src/Logger.d.ts +20 -0
  12. package/dist/core/src/PlanetCore.d.ts +244 -0
  13. package/dist/core/src/Route.d.ts +36 -0
  14. package/dist/core/src/Router.d.ts +250 -0
  15. package/dist/core/src/ServiceProvider.d.ts +150 -0
  16. package/dist/core/src/adapters/GravitoEngineAdapter.d.ts +26 -0
  17. package/dist/core/src/adapters/PhotonAdapter.d.ts +170 -0
  18. package/dist/core/src/adapters/bun/BunContext.d.ts +45 -0
  19. package/dist/core/src/adapters/bun/BunNativeAdapter.d.ts +30 -0
  20. package/dist/core/src/adapters/bun/BunRequest.d.ts +31 -0
  21. package/dist/core/src/adapters/bun/RadixNode.d.ts +19 -0
  22. package/dist/core/src/adapters/bun/RadixRouter.d.ts +31 -0
  23. package/dist/core/src/adapters/bun/types.d.ts +20 -0
  24. package/dist/core/src/adapters/photon-types.d.ts +73 -0
  25. package/dist/core/src/adapters/types.d.ts +196 -0
  26. package/dist/core/src/engine/AOTRouter.d.ts +134 -0
  27. package/dist/core/src/engine/FastContext.d.ts +98 -0
  28. package/dist/core/src/engine/Gravito.d.ts +137 -0
  29. package/dist/core/src/engine/MinimalContext.d.ts +77 -0
  30. package/dist/core/src/engine/analyzer.d.ts +27 -0
  31. package/dist/core/src/engine/constants.d.ts +23 -0
  32. package/dist/core/src/engine/index.d.ts +26 -0
  33. package/dist/core/src/engine/path.d.ts +26 -0
  34. package/dist/core/src/engine/pool.d.ts +83 -0
  35. package/dist/core/src/engine/types.d.ts +138 -0
  36. package/dist/core/src/exceptions/AuthenticationException.d.ts +8 -0
  37. package/dist/core/src/exceptions/AuthorizationException.d.ts +8 -0
  38. package/dist/core/src/exceptions/GravitoException.d.ts +23 -0
  39. package/dist/core/src/exceptions/HttpException.d.ts +9 -0
  40. package/dist/core/src/exceptions/ModelNotFoundException.d.ts +10 -0
  41. package/dist/core/src/exceptions/ValidationException.d.ts +22 -0
  42. package/dist/core/src/exceptions/index.d.ts +6 -0
  43. package/dist/core/src/helpers/Arr.d.ts +19 -0
  44. package/dist/core/src/helpers/Str.d.ts +23 -0
  45. package/dist/core/src/helpers/data.d.ts +25 -0
  46. package/dist/core/src/helpers/errors.d.ts +34 -0
  47. package/dist/core/src/helpers/response.d.ts +41 -0
  48. package/dist/core/src/helpers.d.ts +338 -0
  49. package/dist/core/src/http/CookieJar.d.ts +51 -0
  50. package/dist/core/src/http/middleware/BodySizeLimit.d.ts +16 -0
  51. package/dist/core/src/http/middleware/Cors.d.ts +24 -0
  52. package/dist/core/src/http/middleware/Csrf.d.ts +23 -0
  53. package/dist/core/src/http/middleware/HeaderTokenGate.d.ts +28 -0
  54. package/dist/core/src/http/middleware/SecurityHeaders.d.ts +29 -0
  55. package/dist/core/src/http/middleware/ThrottleRequests.d.ts +18 -0
  56. package/dist/core/src/http/types.d.ts +334 -0
  57. package/dist/core/src/index.d.ts +66 -0
  58. package/dist/core/src/runtime.d.ts +119 -0
  59. package/dist/core/src/security/Encrypter.d.ts +33 -0
  60. package/dist/core/src/security/Hasher.d.ts +29 -0
  61. package/dist/core/src/testing/HttpTester.d.ts +39 -0
  62. package/dist/core/src/testing/TestResponse.d.ts +78 -0
  63. package/dist/core/src/testing/index.d.ts +2 -0
  64. package/dist/core/src/types/events.d.ts +94 -0
  65. package/dist/echo/src/OrbitEcho.d.ts +60 -0
  66. package/dist/echo/src/index.d.ts +48 -0
  67. package/dist/echo/src/providers/GenericProvider.d.ts +34 -0
  68. package/dist/echo/src/providers/GitHubProvider.d.ts +26 -0
  69. package/dist/echo/src/providers/StripeProvider.d.ts +30 -0
  70. package/dist/echo/src/providers/index.d.ts +3 -0
  71. package/dist/echo/src/receive/SignatureValidator.d.ts +34 -0
  72. package/dist/echo/src/receive/WebhookReceiver.d.ts +67 -0
  73. package/dist/echo/src/receive/index.d.ts +2 -0
  74. package/dist/echo/src/send/WebhookDispatcher.d.ts +54 -0
  75. package/dist/echo/src/send/index.d.ts +1 -0
  76. package/dist/echo/src/types.d.ts +164 -0
  77. package/dist/index.js +6 -2
  78. package/dist/index.js.map +3 -3
  79. package/dist/photon/src/index.d.ts +20 -0
  80. package/dist/photon/src/middleware/binary.d.ts +34 -0
  81. package/package.json +1 -1
@@ -0,0 +1,26 @@
1
+ import { Gravito } from '../engine/Gravito';
2
+ import type { GravitoContext, GravitoErrorHandler, GravitoHandler, GravitoMiddleware, GravitoNotFoundHandler, GravitoVariables, HttpMethod } from '../http/types';
3
+ import type { AdapterConfig, HttpAdapter, RouteDefinition } from './types';
4
+ /**
5
+ * GravitoEngineAdapter - Optimized adapter using the Standalone Gravito Engine
6
+ *
7
+ * This adapter is exclusively for Bun and provides the best performance
8
+ * by using the specialized Gravito engine with object pooling and AOT routing.
9
+ */
10
+ export declare class GravitoEngineAdapter<V extends GravitoVariables = GravitoVariables> implements HttpAdapter<V> {
11
+ readonly name = "gravito-engine";
12
+ readonly version = "1.0.0";
13
+ private engine;
14
+ constructor(config?: AdapterConfig);
15
+ get native(): Gravito;
16
+ route(method: HttpMethod, path: string, ...handlers: (GravitoHandler<V> | GravitoMiddleware<V>)[]): void;
17
+ routes(routes: RouteDefinition[]): void;
18
+ use(path: string, ...middleware: GravitoMiddleware<V>[]): void;
19
+ useGlobal(...middleware: GravitoMiddleware<V>[]): void;
20
+ mount(path: string, subAdapter: HttpAdapter<V>): void;
21
+ onError(handler: GravitoErrorHandler<V>): void;
22
+ onNotFound(handler: GravitoNotFoundHandler<V>): void;
23
+ fetch: (request: Request, _server?: unknown) => Response | Promise<Response>;
24
+ warmup(paths: string[]): Promise<void>;
25
+ createContext(_request: Request): GravitoContext<V>;
26
+ }
@@ -0,0 +1,170 @@
1
+ /**
2
+ * @fileoverview Photon Adapter Implementation
3
+ *
4
+ * This adapter wraps Photon to implement the Gravito HttpAdapter interface.
5
+ * It serves as the default adapter and reference implementation for others.
6
+ *
7
+ * @module @gravito/core/adapters/photon
8
+ * @since 2.0.0
9
+ */
10
+ import type { Context, Handler, MiddlewareHandler, Photon } from '@gravito/photon';
11
+ import type { GravitoContext, GravitoErrorHandler, GravitoHandler, GravitoMiddleware, GravitoNotFoundHandler, GravitoRequest, GravitoVariables, HttpMethod, ProxyOptions, StatusCode } from '../http/types';
12
+ import type { AdapterConfig, HttpAdapter, RouteDefinition } from './types';
13
+ /**
14
+ * Wraps Photon's request object to implement GravitoRequest
15
+ */
16
+ declare class PhotonRequestWrapper implements GravitoRequest {
17
+ photonCtx: Context;
18
+ /**
19
+ * Reset the wrapper for pooling
20
+ */
21
+ reset(photonCtx: Context): void;
22
+ /**
23
+ * Create a proxied instance to delegate to Photon's request
24
+ */
25
+ static create(photonCtx: Context): PhotonRequestWrapper;
26
+ /**
27
+ * Internal proxy wrapper
28
+ */
29
+ static wrap(instance: PhotonRequestWrapper): PhotonRequestWrapper;
30
+ get url(): string;
31
+ get method(): string;
32
+ get path(): string;
33
+ param(name: string): string | undefined;
34
+ params(): Record<string, string>;
35
+ query(name: string): string | undefined;
36
+ queries(): Record<string, string | string[]>;
37
+ header(name: string): string | undefined;
38
+ header(): Record<string, string>;
39
+ json<T = unknown>(): Promise<T>;
40
+ text(): Promise<string>;
41
+ formData(): Promise<FormData>;
42
+ arrayBuffer(): Promise<ArrayBuffer>;
43
+ parseBody<T = unknown>(): Promise<T>;
44
+ get raw(): Request;
45
+ valid<T = unknown>(target: string): T;
46
+ }
47
+ /**
48
+ * Wraps Photon's context to implement GravitoContext
49
+ */
50
+ declare class PhotonContextWrapper<V extends GravitoVariables = GravitoVariables> implements GravitoContext<V> {
51
+ private _req;
52
+ private photonCtx;
53
+ route: (name: string, params?: Record<string, any>, query?: Record<string, any>) => string;
54
+ /**
55
+ * Reset the wrapper for pooling
56
+ */
57
+ reset(photonCtx: Context): void;
58
+ /**
59
+ * Create a proxied instance to enable object destructuring of context variables
60
+ */
61
+ static create<V extends GravitoVariables = GravitoVariables>(photonCtx: Context): GravitoContext<V>;
62
+ /**
63
+ * Internal proxy wrapper
64
+ */
65
+ static wrap<V extends GravitoVariables = GravitoVariables>(instance: PhotonContextWrapper<V>): GravitoContext<V>;
66
+ get req(): GravitoRequest;
67
+ get params(): Record<string, string>;
68
+ get query(): Record<string, string | string[]>;
69
+ get data(): unknown;
70
+ set data(value: unknown);
71
+ back(status?: 301 | 302 | 303 | 307 | 308): Response;
72
+ json<T>(data: T, status?: number): Response;
73
+ text(text: string, status?: number): Response;
74
+ html(html: string, status?: number): Response;
75
+ redirect(url: string, status?: 301 | 302 | 303 | 307 | 308): Response;
76
+ body(data: BodyInit | null, status?: number): Response;
77
+ stream(stream: ReadableStream, status?: number): Response;
78
+ notFound(message?: string): Response;
79
+ forbidden(message?: string): Response;
80
+ unauthorized(message?: string): Response;
81
+ badRequest(message?: string): Response;
82
+ header(name: string): string | undefined;
83
+ header(name: string, value: string, options?: {
84
+ append?: boolean;
85
+ }): void;
86
+ status(code: StatusCode): void;
87
+ get<K extends keyof V>(key: K): V[K];
88
+ set<K extends keyof V>(key: K, value: V[K]): void;
89
+ get executionCtx(): ExecutionContext | undefined;
90
+ get env(): Record<string, unknown> | undefined;
91
+ get native(): Context;
92
+ forward(target: string, options?: ProxyOptions): Promise<Response>;
93
+ }
94
+ /**
95
+ * Convert a GravitoHandler to a Photon Handler
96
+ */
97
+ declare function toPhotonHandler<V extends GravitoVariables>(handler: GravitoHandler<V>): Handler;
98
+ /**
99
+ * Convert a GravitoMiddleware to a Photon MiddlewareHandler
100
+ */
101
+ declare function toPhotonMiddleware<V extends GravitoVariables>(middleware: GravitoMiddleware<V>): MiddlewareHandler;
102
+ /**
103
+ * Default HTTP adapter using the optimized Gravito Core Engine.
104
+ *
105
+ * This adapter provides a consistent interface that can be
106
+ * swapped out for other implementations without changing application code.
107
+ *
108
+ * @example
109
+ * ```typescript
110
+ * import { GravitoAdapter } from '@gravito/core'
111
+ *
112
+ * const adapter = new GravitoAdapter()
113
+ *
114
+ * // Register routes
115
+ * adapter.route('get', '/hello', async (ctx) => {
116
+ * return ctx.json({ message: 'Hello, World!' })
117
+ * })
118
+ * ```
119
+ */
120
+ export declare class PhotonAdapter<V extends GravitoVariables = GravitoVariables> implements HttpAdapter<V> {
121
+ private config;
122
+ readonly name = "photon";
123
+ readonly version = "1.0.0";
124
+ private app;
125
+ constructor(config?: AdapterConfig, photonInstance?: unknown);
126
+ /**
127
+ * Get the underlying Photon app instance
128
+ */
129
+ get native(): Photon;
130
+ /**
131
+ * Set the underlying Photon app instance
132
+ * Used by PlanetCore during initialization
133
+ */
134
+ setNative(app: Photon): void;
135
+ route(method: HttpMethod, path: string, ...handlers: (GravitoHandler<V> | GravitoMiddleware<V>)[]): void;
136
+ routes(routes: RouteDefinition[]): void;
137
+ use(path: string, ...middleware: GravitoMiddleware<V>[]): void;
138
+ useGlobal(...middleware: GravitoMiddleware<V>[]): void;
139
+ mount(path: string, subAdapter: HttpAdapter<V>): void;
140
+ onError(handler: GravitoErrorHandler<V>): void;
141
+ onNotFound(handler: GravitoNotFoundHandler<V>): void;
142
+ /**
143
+ * Predictive Route Warming (JIT Optimization)
144
+ */
145
+ warmup(paths: string[]): Promise<void>;
146
+ fetch: (request: Request, server?: unknown) => Response | Promise<Response>;
147
+ createContext(_request: Request): GravitoContext<V>;
148
+ init(): Promise<void>;
149
+ shutdown(): Promise<void>;
150
+ }
151
+ /**
152
+ * Create a new PhotonAdapter instance
153
+ */
154
+ export declare function createPhotonAdapter<V extends GravitoVariables = GravitoVariables>(config?: AdapterConfig): PhotonAdapter<V>;
155
+ export { PhotonContextWrapper, PhotonRequestWrapper, toPhotonHandler, toPhotonMiddleware };
156
+ /**
157
+ * Rebranded alias for PhotonAdapter.
158
+ * @category Rebranding
159
+ */
160
+ export declare const GravitoAdapter: typeof PhotonAdapter;
161
+ /**
162
+ * Rebranded alias for PhotonAdapter type.
163
+ * @category Rebranding
164
+ */
165
+ export type GravitoAdapter<V extends GravitoVariables = GravitoVariables> = PhotonAdapter<V>;
166
+ /**
167
+ * Rebranded alias for createPhotonAdapter.
168
+ * @category Rebranding
169
+ */
170
+ export declare const createGravitoAdapter: typeof createPhotonAdapter;
@@ -0,0 +1,45 @@
1
+ import type { ContentfulStatusCode, GravitoContext, GravitoVariables, ProxyOptions, StatusCode } from '../../http/types';
2
+ import { BunRequest } from './BunRequest';
3
+ /**
4
+ * Bun-optimized implementation of GravitoContext.
5
+ * @internal
6
+ */
7
+ export declare class BunContext<V extends GravitoVariables = GravitoVariables> implements GravitoContext<V> {
8
+ readonly env: Record<string, unknown>;
9
+ readonly req: BunRequest;
10
+ private _variables;
11
+ /**
12
+ * URL generator helper
13
+ */
14
+ route: (name: string, params?: Record<string, any>, query?: Record<string, any>) => string;
15
+ private _status;
16
+ private _headers;
17
+ private _executionCtx?;
18
+ res: Response | undefined;
19
+ readonly native: unknown;
20
+ constructor(request: Request, env?: Record<string, unknown>, executionCtx?: ExecutionContext);
21
+ /**
22
+ * Create a proxied instance to enable object destructuring of context variables
23
+ * This allows: async list({ userService }: Context)
24
+ */
25
+ static create<V extends GravitoVariables = GravitoVariables>(request: Request, env?: Record<string, unknown>, executionCtx?: ExecutionContext): GravitoContext<V>;
26
+ json<T>(data: T, status?: ContentfulStatusCode): Response;
27
+ text(text: string, status?: ContentfulStatusCode): Response;
28
+ html(html: string, status?: ContentfulStatusCode): Response;
29
+ redirect(url: string, status?: 301 | 302 | 303 | 307 | 308): Response;
30
+ body(data: BodyInit | null, status?: StatusCode): Response;
31
+ stream(stream: ReadableStream, status?: ContentfulStatusCode): Response;
32
+ notFound(message?: string): Response;
33
+ forbidden(message?: string): Response;
34
+ unauthorized(message?: string): Response;
35
+ badRequest(message?: string): Response;
36
+ forward(target: string, options?: ProxyOptions): Promise<Response>;
37
+ header(name: string, value: string, options?: {
38
+ append?: boolean;
39
+ }): void;
40
+ header(name: string): string | undefined;
41
+ status(code: StatusCode): void;
42
+ get<K extends keyof V>(key: K): V[K];
43
+ set<K extends keyof V>(key: K, value: V[K]): void;
44
+ get executionCtx(): ExecutionContext | undefined;
45
+ }
@@ -0,0 +1,30 @@
1
+ import type { GravitoContext, GravitoErrorHandler, GravitoHandler, GravitoMiddleware, GravitoNotFoundHandler, HttpMethod } from '../../http/types';
2
+ import type { HttpAdapter, RouteDefinition } from '../types';
3
+ /**
4
+ * Native Bun-optimized HTTP Adapter for Gravito.
5
+ * Uses Bun's standard Request/Response classes and efficient router.
6
+ * @public
7
+ */
8
+ export declare class BunNativeAdapter implements HttpAdapter {
9
+ readonly name = "bun-native";
10
+ readonly version = "0.0.1";
11
+ get native(): unknown;
12
+ private router;
13
+ private middlewares;
14
+ private errorHandler;
15
+ private notFoundHandler;
16
+ route(method: HttpMethod, path: string, ...handlers: (GravitoHandler | GravitoMiddleware)[]): void;
17
+ routes(routes: RouteDefinition[]): void;
18
+ use(path: string, ...middleware: GravitoMiddleware[]): void;
19
+ useGlobal(...middleware: GravitoMiddleware[]): void;
20
+ mount(path: string, subAdapter: HttpAdapter): void;
21
+ createContext(request: Request): GravitoContext;
22
+ onError(handler: GravitoErrorHandler): void;
23
+ onNotFound(handler: GravitoNotFoundHandler): void;
24
+ /**
25
+ * Predictive Route Warming (JIT Optimization)
26
+ */
27
+ warmup(paths: string[]): Promise<void>;
28
+ fetch(request: Request, _server?: unknown): Promise<Response>;
29
+ private executeChain;
30
+ }
@@ -0,0 +1,31 @@
1
+ import type { GravitoRequest, ValidationTarget } from '../../http/types';
2
+ /**
3
+ * Bun-optimized implementation of GravitoRequest.
4
+ * @internal
5
+ */
6
+ export declare class BunRequest implements GravitoRequest {
7
+ readonly raw: Request;
8
+ private _url;
9
+ private _params;
10
+ private _query;
11
+ private _validated;
12
+ constructor(raw: Request, params?: Record<string, string>);
13
+ setParams(params: Record<string, string>): void;
14
+ get url(): string;
15
+ get method(): string;
16
+ get path(): string;
17
+ param(name: string): string | undefined;
18
+ params(): Record<string, string>;
19
+ query(name: string): string | undefined;
20
+ queries(): Record<string, string | string[]>;
21
+ header(name: string): string | undefined;
22
+ header(): Record<string, string>;
23
+ json<T = unknown>(): Promise<T>;
24
+ text(): Promise<string>;
25
+ formData(): Promise<FormData>;
26
+ arrayBuffer(): Promise<ArrayBuffer>;
27
+ parseBody<T = unknown>(): Promise<T>;
28
+ setValidated(target: ValidationTarget, data: unknown): void;
29
+ valid<T = unknown>(target: ValidationTarget): T;
30
+ private parseQuery;
31
+ }
@@ -0,0 +1,19 @@
1
+ import type { HttpMethod } from '../../http/types';
2
+ import { NodeType, type RouteHandler } from './types';
3
+ /**
4
+ * Node in the Radix Router tree.
5
+ * @internal
6
+ */
7
+ export declare class RadixNode {
8
+ segment: string;
9
+ type: NodeType;
10
+ children: Map<string, RadixNode>;
11
+ paramChild: RadixNode | null;
12
+ wildcardChild: RadixNode | null;
13
+ handlers: Map<HttpMethod, RouteHandler[]>;
14
+ paramName: string | null;
15
+ regex: RegExp | null;
16
+ constructor(segment?: string, type?: NodeType);
17
+ toJSON(): any;
18
+ static fromJSON(json: any): RadixNode;
19
+ }
@@ -0,0 +1,31 @@
1
+ import type { HttpMethod } from '../../http/types';
2
+ import { type RouteHandler, type RouteMatch } from './types';
3
+ /**
4
+ * High-performance Radix Tree Router for Bun
5
+ */
6
+ export declare class RadixRouter {
7
+ private root;
8
+ private globalConstraints;
9
+ /**
10
+ * Add a generic parameter constraint
11
+ */
12
+ where(param: string, regex: RegExp): void;
13
+ /**
14
+ * Register a route
15
+ */
16
+ add(method: HttpMethod, path: string, handlers: RouteHandler[]): void;
17
+ /**
18
+ * Match a request
19
+ */
20
+ match(method: string, path: string): RouteMatch | null;
21
+ private matchRecursive;
22
+ private splitPath;
23
+ /**
24
+ * Serialize the router to a JSON string
25
+ */
26
+ serialize(): string;
27
+ /**
28
+ * Restore a router from a serialized JSON string
29
+ */
30
+ static fromSerialized(json: string): RadixRouter;
31
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Route Handler type (simplified for internal use)
3
+ * In the full framework this will align with GravitoHandler
4
+ */
5
+ export type RouteHandler = Function;
6
+ /**
7
+ * Route Match Result
8
+ */
9
+ export interface RouteMatch {
10
+ handlers: RouteHandler[];
11
+ params: Record<string, string>;
12
+ }
13
+ /**
14
+ * Radix Node Type
15
+ */
16
+ export declare enum NodeType {
17
+ STATIC = 0,
18
+ PARAM = 1,
19
+ WILDCARD = 2
20
+ }
@@ -0,0 +1,73 @@
1
+ /**
2
+ * @fileoverview Photon Type Extensions
3
+ *
4
+ * Type definitions for Photon framework extensions and internal properties.
5
+ * These types help maintain type safety when working with Photon's native APIs.
6
+ *
7
+ * @module @gravito/core/adapters/photon-types
8
+ * @since 2.0.0
9
+ */
10
+ import type { Context } from '@gravito/photon';
11
+ /**
12
+ * Extended Photon Request with additional properties
13
+ */
14
+ export interface PhotonRequestExtended {
15
+ /**
16
+ * Validate request data against a schema
17
+ * This is typically added by validation middleware
18
+ */
19
+ valid<T = unknown>(target: string): T;
20
+ /**
21
+ * Allow dynamic property access for middleware extensions
22
+ */
23
+ [key: string]: unknown;
24
+ }
25
+ /**
26
+ * Extended Photon Context with internal caching
27
+ */
28
+ export interface PhotonContextExtended extends Context {
29
+ /**
30
+ * Internal cache for parsed JSON body to avoid re-parsing
31
+ * @internal
32
+ */
33
+ _cachedJsonBody?: unknown;
34
+ /**
35
+ * Extended request object
36
+ */
37
+ req: Context['req'] & PhotonRequestExtended;
38
+ }
39
+ /**
40
+ * Extended Response with flash message support
41
+ * This is a partial interface since we're adding the method dynamically
42
+ */
43
+ export interface ResponseWithFlash {
44
+ /**
45
+ * Add flash message to session (for redirect responses)
46
+ * @param key - Flash message key
47
+ * @param value - Flash message value
48
+ * @returns The response for chaining
49
+ */
50
+ with(key: string, value: unknown): ResponseWithFlash;
51
+ [key: string]: unknown;
52
+ }
53
+ /**
54
+ * Session interface with flash message support
55
+ */
56
+ export interface SessionWithFlash {
57
+ /**
58
+ * Store a flash message in the session
59
+ */
60
+ flash(key: string, value: unknown): void;
61
+ /**
62
+ * Allow other session properties
63
+ */
64
+ [key: string]: unknown;
65
+ }
66
+ /**
67
+ * Type guard to check if context has cached JSON body
68
+ */
69
+ export declare function hasCachedJsonBody(ctx: Context): ctx is PhotonContextExtended;
70
+ /**
71
+ * Type guard to check if request has valid method
72
+ */
73
+ export declare function hasValidMethod(req: Context['req']): req is Context['req'] & PhotonRequestExtended;
@@ -0,0 +1,196 @@
1
+ /**
2
+ * @fileoverview HTTP Adapter Interface for Gravito Framework
3
+ *
4
+ * This module defines the contract that all HTTP adapters must implement.
5
+ * By programming to this interface, Gravito can swap out the underlying
6
+ * HTTP engine without changing application code.
7
+ *
8
+ * @module @gravito/core/adapters
9
+ * @since 2.0.0
10
+ */
11
+ import type { GravitoContext, GravitoErrorHandler, GravitoHandler, GravitoMiddleware, GravitoNotFoundHandler, GravitoVariables, HttpMethod } from '../http/types';
12
+ /**
13
+ * Configuration options for HTTP adapters
14
+ */
15
+ export interface AdapterConfig {
16
+ /**
17
+ * Base path prefix for all routes
18
+ * @default ''
19
+ */
20
+ basePath?: string;
21
+ /**
22
+ * Whether to enable strict routing (trailing slashes matter)
23
+ * @default false
24
+ */
25
+ strictRouting?: boolean;
26
+ /**
27
+ * Custom options passed to the underlying HTTP engine
28
+ */
29
+ engineOptions?: Record<string, unknown>;
30
+ }
31
+ /**
32
+ * Route definition structure
33
+ */
34
+ export interface RouteDefinition {
35
+ method: HttpMethod;
36
+ path: string;
37
+ handlers: (GravitoHandler | GravitoMiddleware)[];
38
+ name?: string;
39
+ middleware?: GravitoMiddleware[];
40
+ }
41
+ /**
42
+ * HttpAdapter - The core interface for HTTP engine abstraction
43
+ *
44
+ * Any HTTP engine (Photon, Express, Fastify, custom Bun implementation)
45
+ * must implement this interface to be usable with Gravito.
46
+ *
47
+ * @typeParam V - Context variables type
48
+ *
49
+ * @example
50
+ * ```typescript
51
+ * // Using the default Photon adapter
52
+ * import { PhotonAdapter } from '@gravito/core/adapters'
53
+ *
54
+ * const core = new PlanetCore({
55
+ * adapter: new PhotonAdapter()
56
+ * })
57
+ *
58
+ * // Using a custom adapter
59
+ * import { BunNativeAdapter } from '@gravito/adapter-bun'
60
+ *
61
+ * const core = new PlanetCore({
62
+ * adapter: new BunNativeAdapter()
63
+ * })
64
+ * ```
65
+ */
66
+ export interface HttpAdapter<V extends GravitoVariables = GravitoVariables> {
67
+ /**
68
+ * Adapter name for identification
69
+ * @example 'photon', 'bun-native', 'express'
70
+ */
71
+ readonly name: string;
72
+ /**
73
+ * Adapter version
74
+ */
75
+ readonly version: string;
76
+ /**
77
+ * Access the underlying native HTTP engine instance.
78
+ *
79
+ * ⚠️ WARNING: Using this ties your code to a specific adapter.
80
+ *
81
+ * @example
82
+ * ```typescript
83
+ * // For Photon adapter
84
+ * const photonApp = adapter.native as Photon
85
+ *
86
+ * // For custom Bun adapter
87
+ * const bunApp = adapter.native as BunApp
88
+ * ```
89
+ */
90
+ readonly native: unknown;
91
+ /**
92
+ * Register a route with the adapter
93
+ *
94
+ * @param method - HTTP method
95
+ * @param path - Route path (may include parameters like ':id')
96
+ * @param handlers - One or more handlers for this route (handlers or middleware)
97
+ */
98
+ route(method: HttpMethod, path: string, ...handlers: (GravitoHandler<V> | GravitoMiddleware<V>)[]): void;
99
+ /**
100
+ * Register multiple routes at once
101
+ *
102
+ * @param routes - Array of route definitions
103
+ */
104
+ routes(routes: RouteDefinition[]): void;
105
+ /**
106
+ * Register a middleware for a path
107
+ *
108
+ * @param path - Path pattern to match
109
+ * @param middleware - One or more middleware functions
110
+ */
111
+ use(path: string, ...middleware: GravitoMiddleware<V>[]): void;
112
+ /**
113
+ * Register a global middleware (applied to all routes)
114
+ *
115
+ * @param middleware - Middleware function
116
+ */
117
+ useGlobal(...middleware: GravitoMiddleware<V>[]): void;
118
+ /**
119
+ * Mount a sub-adapter at a path
120
+ *
121
+ * @param path - Mount path
122
+ * @param subAdapter - The adapter to mount
123
+ */
124
+ mount(path: string, subAdapter: HttpAdapter<V>): void;
125
+ /**
126
+ * Set the error handler
127
+ *
128
+ * @param handler - Error handler function
129
+ */
130
+ onError(handler: GravitoErrorHandler<V>): void;
131
+ /**
132
+ * Set the not-found handler
133
+ *
134
+ * @param handler - Not-found handler function
135
+ */
136
+ onNotFound(handler: GravitoNotFoundHandler<V>): void;
137
+ /**
138
+ * The main fetch handler for serving requests.
139
+ *
140
+ * This is compatible with `Bun.serve()`, Cloudflare Workers,
141
+ * and other fetch-based runtimes.
142
+ *
143
+ * @param request - Incoming HTTP request
144
+ * @param server - Optional server context (Bun.Server, etc.)
145
+ * @returns HTTP response
146
+ *
147
+ * @example
148
+ * ```typescript
149
+ * // With Bun.serve
150
+ * Bun.serve({
151
+ * port: 3000,
152
+ * fetch: adapter.fetch
153
+ * })
154
+ * ```
155
+ */
156
+ fetch(request: Request, server?: unknown): Response | Promise<Response>;
157
+ /**
158
+ * Predictive Route Warming (JIT Optimization)
159
+ *
160
+ * Simulates requests to specified routes to trigger JIT compilation (FTL)
161
+ * before real traffic arrives.
162
+ *
163
+ * @param paths List of paths to warm up (e.g. ['/api/users', '/health'])
164
+ * @since 2.1.0
165
+ */
166
+ warmup?(paths: string[]): Promise<void>;
167
+ /**
168
+ * Initialize the adapter
169
+ *
170
+ * Called during PlanetCore.boot()
171
+ */
172
+ init?(): void | Promise<void>;
173
+ /**
174
+ * Cleanup resources
175
+ *
176
+ * Called during graceful shutdown
177
+ */
178
+ shutdown?(): void | Promise<void>;
179
+ /**
180
+ * Create a GravitoContext from a raw request.
181
+ *
182
+ * This is used internally for testing and advanced scenarios.
183
+ *
184
+ * @param request - Raw HTTP request
185
+ * @returns Gravito context
186
+ */
187
+ createContext(request: Request): GravitoContext<V>;
188
+ }
189
+ /**
190
+ * Factory function type for creating adapters
191
+ */
192
+ export type AdapterFactory<V extends GravitoVariables = GravitoVariables> = (config?: AdapterConfig) => HttpAdapter<V>;
193
+ /**
194
+ * Check if a value is an HttpAdapter
195
+ */
196
+ export declare function isHttpAdapter(value: unknown): value is HttpAdapter;