@supacloud/elysia 0.17.0 → 0.19.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/dist/index.d.ts CHANGED
@@ -1,18 +1,38 @@
1
- import { Elysia } from "elysia";
1
+ import { Elysia, type AnyElysia, type InferContext, type StatusMap } from "elysia";
2
+ import type { CommandRuntimeAudit, CommandRuntimeAuthorizer, CommandRuntimeGovernance, CommandRuntimeInvocation, CommandRuntimeMiddleware } from "@supacloud/app";
2
3
  import { type CommandPreview } from "@supacloud/contracts";
4
+ import { type HttpPolicyRegistry } from "./http-policy";
5
+ export { HttpPolicyConfigurationError } from "./http-policy";
6
+ export type { HttpPolicy, HttpPolicyContext, HttpPolicyResponseContext, HttpPolicyDeclaration, HttpPolicyRegistry } from "./http-policy";
7
+ export { createHttpPolicySuite } from "./http-policy-suite";
8
+ export type { HttpPolicySuiteOptions, BuiltinHttpPolicyDeclaration } from "./http-policy-suite";
9
+ export { createMemoryHttpRateLimitStore, createMemoryHttpCacheStore } from "./http-policy-stores";
10
+ export type { HttpRateLimitStore, HttpRateLimitResult, HttpCacheStore, HttpCacheEntry } from "./http-policy-stores";
11
+ export { createPostgresHttpPolicyStores, HTTP_POLICY_STORE_SQL } from "./http-policy-postgres";
12
+ export type { HttpPolicyDatabase } from "./http-policy-postgres";
13
+ export { createHttpTelemetry } from "./http-telemetry";
14
+ export type { HttpTelemetryEvent, HttpTelemetryObserver } from "./http-telemetry";
3
15
  import { type ExecutionObserver } from "./execution";
4
16
  import { type ApplicationDocumentationOptions } from "./documentation";
5
17
  export type { ExecutionEvent, ExecutionObserver } from "./execution";
18
+ export { bindCompiledCommand } from "./command-binding";
19
+ export type { CompiledCommandBinding, CompiledCommandBindingOptions, CompiledCommandCallContext, } from "./command-binding";
6
20
  export { createSchemaDecoder, assertResponseStatusDeclared, defineElysiaRoute, defineJsonContract, defineRouteContract, registerElysiaRoute, responseSchemaForStatus, responseStatusDeclared, responseStatusOf, SchemaContractError, toElysiaRouteSchema, } from "./schema_contract";
7
21
  export type { ElysiaRouteContext, ElysiaRouteDefinition, ElysiaRouteHandler, ElysiaRouteSchema, ResponseMapSelector, RouteContractSchemas, SchemaDecoderOptions, SchemaNormalizeMode, } from "./schema_contract";
8
22
  export { createPersistentCommandAdapter, type PersistentCommandHandler } from "./persistent-command";
9
23
  export { createDocumentationPlugin } from "./documentation";
10
24
  export type { ApplicationDocumentationOptions, DocumentationSource, GraphqlDocumentationOptions, OpenApiDocumentationOptions, } from "./documentation";
11
25
  export interface CompiledRoute {
26
+ parse?: "none";
27
+ allowDeleteBody?: true;
28
+ /** Compiler-emitted route title, exposed as the OpenAPI operation summary. */
29
+ title?: string;
12
30
  method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS";
13
31
  path: string;
14
32
  /** Method name on the controller instance. */
15
33
  handler: string;
34
+ /** Static metadata preserved by the compiler; HTTP policies are adapter-owned. */
35
+ data?: Record<string, unknown>;
16
36
  /** TypeBox schema; validation is enabled only when the field is present. */
17
37
  body?: unknown;
18
38
  params?: unknown;
@@ -116,19 +136,8 @@ export interface SupaCloudRequestContext {
116
136
  identity: TrustedRequestIdentity;
117
137
  idempotencyKey?: string;
118
138
  }
119
- export interface CommandInvocation {
120
- command: CompiledCommand;
121
- input: {
122
- body: unknown;
123
- params: Record<string, unknown>;
124
- query: Record<string, unknown>;
125
- };
126
- request: Request;
127
- requestContext: unknown;
128
- scope?: Record<string, unknown>;
129
- services: Record<string, unknown>;
130
- }
131
- export type CommandExecutor = (invocation: CommandInvocation, next: () => unknown | Promise<unknown>) => unknown | Promise<unknown>;
139
+ export type CommandInvocation = CommandRuntimeInvocation<CompiledCommand>;
140
+ export type CommandExecutor = CommandRuntimeMiddleware<CommandInvocation>;
132
141
  export { assertFeatureTransition } from "./feature";
133
142
  export type { FeatureTransitionSpec } from "./feature";
134
143
  export interface ApplicationAspectContext {
@@ -149,28 +158,48 @@ export type ApplicationAspect = (context: ApplicationAspectContext, next: () =>
149
158
  */
150
159
  export declare function composeAspects(...aspects: (ApplicationAspect | undefined | null)[]): ApplicationAspect;
151
160
  export type ApplicationAspectPipeline = (context: ApplicationAspectContext, next: () => unknown | Promise<unknown>, observe?: (stage: string, run: () => unknown | Promise<unknown>) => unknown | Promise<unknown>) => unknown | Promise<unknown>;
152
- export type CommandAuthorizer = (invocation: CommandInvocation) => void | Promise<void>;
153
- export type CommandMiddleware = (invocation: CommandInvocation, next: () => unknown | Promise<unknown>) => unknown | Promise<unknown>;
154
- export interface CommandAudit {
155
- succeeded(invocation: CommandInvocation, result: unknown): void | Promise<void>;
156
- failed(invocation: CommandInvocation, error: unknown): void | Promise<void>;
161
+ export type CommandAuthorizer = CommandRuntimeAuthorizer<CommandInvocation>;
162
+ export type CommandMiddleware = CommandRuntimeMiddleware<CommandInvocation>;
163
+ export type CommandAudit = CommandRuntimeAudit<CommandInvocation>;
164
+ export type CommandGovernance = CommandRuntimeGovernance<CommandInvocation>;
165
+ export interface CommandAuthorizationRequest {
166
+ readonly principal: {
167
+ readonly kind: "user" | "service";
168
+ readonly issuer: string;
169
+ readonly subject: string;
170
+ };
171
+ readonly applicationId: string;
172
+ readonly domain: {
173
+ readonly type: string;
174
+ readonly id: string;
175
+ };
157
176
  }
158
- export interface CommandGovernance {
159
- /** Application-owned adapters: a single RPC owns all declared persistence. */
160
- rpc?: Record<string, {
161
- capabilities: {
162
- audit?: boolean;
163
- transaction?: boolean;
164
- idempotency?: boolean;
165
- boundary?: "database" | "external";
166
- };
167
- execute: CommandMiddleware;
168
- }>;
169
- authorize: CommandAuthorizer;
170
- idempotency?: CommandMiddleware;
171
- transaction?: CommandMiddleware;
172
- audit?: CommandAudit;
177
+ export interface CommandAuthorizationContext {
178
+ readonly applicationId: string;
179
+ readonly permissions: readonly string[];
180
+ readonly permissionCatalogVersion?: string;
181
+ readonly permissionCatalogDigest?: string;
173
182
  }
183
+ export interface CommandAuthorizationCatalog {
184
+ readonly version: string;
185
+ readonly digest?: string;
186
+ }
187
+ export interface CommandAuthorizationAdapterOptions {
188
+ readonly applicationId: string;
189
+ readonly issuer: string;
190
+ readonly domain: (invocation: CommandInvocation) => {
191
+ readonly type: string;
192
+ readonly id: string;
193
+ };
194
+ readonly resolve: (request: CommandAuthorizationRequest) => Promise<CommandAuthorizationContext>;
195
+ readonly catalog?: CommandAuthorizationCatalog;
196
+ }
197
+ /**
198
+ * Adapt a SupAuth-style resolver to the standard command governance port.
199
+ * The resolver remains the authorization source of truth; this adapter only
200
+ * maps trusted request identity and normalizes public failure statuses.
201
+ */
202
+ export declare function createCommandAuthorizationAdapter(options: CommandAuthorizationAdapterOptions): CommandAuthorizer;
174
203
  /**
175
204
  * Compose multiple CommandExecutors into a single onion-style pipeline.
176
205
  * Outer executors run first before calling `next()`, and complete last.
@@ -207,7 +236,73 @@ export interface ErrorContext {
207
236
  frameworkCode: string | number | undefined;
208
237
  }
209
238
  export type ErrorMapper = (error: unknown, context: ErrorContext) => Response | undefined | Promise<Response | undefined>;
210
- export interface ApplicationOptions {
239
+ declare function mountHttp<const Http extends AnyElysia = Elysia>(http?: Http, name?: string, normalize?: boolean): Elysia<"", {
240
+ decorator: {} & Http["~Singleton"]["decorator"];
241
+ store: ({} & Http["~Singleton"]["store"] extends infer T_1 ? { [K_1 in keyof T_1]: T_1[K_1]; } : never) extends infer T ? { [K in keyof T]: T[K]; } : never;
242
+ derive: {} & Http["~Singleton"]["derive"];
243
+ resolve: {} & Http["~Singleton"]["resolve"];
244
+ }, {
245
+ typebox: {};
246
+ error: {};
247
+ } & Http["~Definitions"], {
248
+ schema: {};
249
+ standaloneSchema: {};
250
+ macro: {};
251
+ macroFn: {};
252
+ parser: {};
253
+ response: {};
254
+ } & Http["~Metadata"], {} & Http["~Routes"], {
255
+ derive: {};
256
+ resolve: {};
257
+ schema: {};
258
+ standaloneSchema: {};
259
+ response: {};
260
+ }, {
261
+ derive: {};
262
+ resolve: {};
263
+ schema: {};
264
+ standaloneSchema: {};
265
+ response: {};
266
+ } & {
267
+ derive: {} & ({
268
+ derive: {};
269
+ resolve: {};
270
+ schema: {};
271
+ standaloneSchema: {};
272
+ response: {};
273
+ } & Http["~Ephemeral"])["derive"];
274
+ resolve: {} & ({
275
+ derive: {};
276
+ resolve: {};
277
+ schema: {};
278
+ standaloneSchema: {};
279
+ response: {};
280
+ } & Http["~Ephemeral"])["resolve"];
281
+ schema: import("elysia").MergeSchema<({
282
+ derive: {};
283
+ resolve: {};
284
+ schema: {};
285
+ standaloneSchema: {};
286
+ response: {};
287
+ } & Http["~Ephemeral"])["schema"], {}, "">;
288
+ standaloneSchema: ({
289
+ derive: {};
290
+ resolve: {};
291
+ schema: {};
292
+ standaloneSchema: {};
293
+ response: {};
294
+ } & Http["~Ephemeral"])["standaloneSchema"] & {};
295
+ response: ({
296
+ derive: {};
297
+ resolve: {};
298
+ schema: {};
299
+ standaloneSchema: {};
300
+ response: {};
301
+ } & Http["~Ephemeral"])["response"] & {};
302
+ }>;
303
+ export type ApplicationHttpContext<Http extends AnyElysia = Elysia> = Omit<InferContext<ReturnType<typeof mountHttp<Http>>>, "body" | "params" | "query" | "headers" | "cookie"> & Pick<HttpContext, "body" | "params" | "query" | "headers" | "cookie">;
304
+ export type HttpRequestContextFactory<Http extends AnyElysia = Elysia, Value = unknown> = (request: Request, context: ApplicationHttpContext<Http>) => Value | Promise<Value>;
305
+ export interface ApplicationOptions<Http extends AnyElysia = Elysia, RequestContext = unknown> {
211
306
  name?: string;
212
307
  /** false rejects extra schema properties instead of silently removing them. */
213
308
  normalize?: boolean;
@@ -215,8 +310,12 @@ export interface ApplicationOptions {
215
310
  modules?: CompiledModule[];
216
311
  /** Platform-level dependencies (db client etc.), passed to createServices. */
217
312
  deps?: Record<string, unknown>;
218
- /** Builds the per-request context object. Defaults to { requestId, request }. */
219
- requestContext?: RequestContextFactory;
313
+ /** Native plugin. Use scoped/global derive/resolve hooks to extend compiled routes. */
314
+ http?: Http;
315
+ /** Startup-compiled policies selected by route.data.httpPolicies, in declaration order. */
316
+ httpPolicies?: HttpPolicyRegistry<Http>;
317
+ /** Runs after validation and HTTP resolvers, with the validated native context. */
318
+ requestContext?: HttpRequestContextFactory<Http, RequestContext>;
220
319
  /** Enforces permission/audit/idempotency policy for command-bound routes. */
221
320
  commandGovernance?: CommandGovernance;
222
321
  /** Optional custom or composed executor. When provided alongside commandGovernance, it wraps or composes with governance. */
@@ -276,15 +375,91 @@ export declare function previewCompiledCommand<Input>(options: {
276
375
  }): Promise<CommandPreview>;
277
376
  export declare function requireTrustedIdentity(requestContext: unknown): Required<Pick<TrustedRequestIdentity, "subject" | "accessToken">> & TrustedRequestIdentity;
278
377
  export declare function requireIdempotencyKey(invocation: CommandInvocation): string;
378
+ interface HttpContext {
379
+ body: unknown;
380
+ params: Record<string, unknown>;
381
+ query: Record<string, unknown>;
382
+ headers?: Record<string, unknown>;
383
+ cookie?: Record<string, unknown>;
384
+ request: Request;
385
+ set: {
386
+ status?: number | keyof StatusMap;
387
+ };
388
+ scope?: Record<string, unknown>;
389
+ requestContext?: unknown;
390
+ }
279
391
  /**
280
392
  * Adapt a single compiled module into an Elysia plugin.
281
393
  *
282
- * The plugin decorates the context with `services`; when the module defines
283
- * `createRequestScope`, a fresh request scope is resolved per request and
284
- * exposed on the `scope` context key. Request-scoped controller instances are
285
- * looked up on `scope`, everything else on `services`.
394
+ * The plugin resolves module-local `services`; when the module defines
395
+ * `createRequestScope`, a fresh scope is created inside the governed handler
396
+ * and passed to the controller input as `scope`. Request-scoped controllers
397
+ * are looked up on that scope, everything else on `services`.
286
398
  */
287
- export declare function createModulePlugin(compiled: CompiledModule, services: Record<string, unknown>, ctxFactory?: RequestContextFactory, options?: Pick<ApplicationOptions, "commandGovernance" | "commandExecutor" | "errorMapper" | "onExecution" | "normalize">, imported?: Record<string, Record<string, unknown>>): Elysia;
399
+ export declare function createModulePlugin<const Services extends Record<string, unknown>, const Http extends AnyElysia = Elysia>(compiled: CompiledModule, services: Services, ctxFactory?: HttpRequestContextFactory<Http>, options?: Pick<ApplicationOptions<Http>, "http" | "httpPolicies" | "commandGovernance" | "commandExecutor" | "errorMapper" | "onExecution" | "normalize">, imported?: Record<string, Record<string, unknown>>): Elysia<"", {
400
+ decorator: {} & Http["~Singleton"]["decorator"];
401
+ store: ({} & Http["~Singleton"]["store"] extends infer T_1 ? { [K_1 in keyof T_1]: T_1[K_1]; } : never) extends infer T ? { [K in keyof T]: T[K]; } : never;
402
+ derive: {} & Http["~Singleton"]["derive"];
403
+ resolve: {} & Http["~Singleton"]["resolve"];
404
+ }, {
405
+ typebox: {};
406
+ error: {};
407
+ } & Http["~Definitions"], {
408
+ schema: {};
409
+ standaloneSchema: {};
410
+ macro: {};
411
+ macroFn: {};
412
+ parser: {};
413
+ response: {};
414
+ } & Http["~Metadata"], {} & Http["~Routes"], {
415
+ derive: {};
416
+ resolve: {};
417
+ schema: {};
418
+ standaloneSchema: {};
419
+ response: {};
420
+ }, {
421
+ derive: {} & ({
422
+ derive: {};
423
+ resolve: {};
424
+ schema: {};
425
+ standaloneSchema: {};
426
+ response: {};
427
+ } & Http["~Ephemeral"])["derive"];
428
+ resolve: ({
429
+ derive: {};
430
+ resolve: {};
431
+ schema: {};
432
+ standaloneSchema: {};
433
+ response: {};
434
+ } & Http["~Ephemeral"])["resolve"] & {
435
+ readonly services: Services;
436
+ readonly requestContext: unknown;
437
+ };
438
+ schema: {} & import("elysia").MergeSchema<({
439
+ derive: {};
440
+ resolve: {};
441
+ schema: {};
442
+ standaloneSchema: {};
443
+ response: {};
444
+ } & Http["~Ephemeral"])["schema"], {}, "">;
445
+ standaloneSchema: {} & ({
446
+ derive: {};
447
+ resolve: {};
448
+ schema: {};
449
+ standaloneSchema: {};
450
+ response: {};
451
+ } & Http["~Ephemeral"])["standaloneSchema"];
452
+ response: import("elysia/types").UnionResponseStatus<{} & ({
453
+ derive: {};
454
+ resolve: {};
455
+ schema: {};
456
+ standaloneSchema: {};
457
+ response: {};
458
+ } & Http["~Ephemeral"])["response"], import("elysia").ExtractErrorFromHandle<{
459
+ readonly services: Services;
460
+ readonly requestContext: unknown;
461
+ }>>;
462
+ }>;
288
463
  /**
289
464
  * Execute one compiler-emitted Job descriptor.
290
465
  *
@@ -301,9 +476,135 @@ export declare function defaultErrorResponse(error: unknown, frameworkCode?: str
301
476
  * `createServices` receives `deps` plus the services of all previously
302
477
  * created modules, keyed by module name.
303
478
  */
304
- export declare function createApplication(options: ApplicationOptions): Elysia;
479
+ export declare function createApplication<const Http extends AnyElysia = Elysia>(options: ApplicationOptions<Http>): Elysia<"", {
480
+ decorator: {} & Http["~Singleton"]["decorator"];
481
+ store: ({} & Http["~Singleton"]["store"] extends infer T_1 ? { [K_1 in keyof T_1]: T_1[K_1]; } : never) extends infer T ? { [K in keyof T]: T[K]; } : never;
482
+ derive: {} & Http["~Singleton"]["derive"];
483
+ resolve: {} & Http["~Singleton"]["resolve"];
484
+ }, {
485
+ typebox: {};
486
+ error: {};
487
+ } & Http["~Definitions"], {
488
+ schema: {};
489
+ standaloneSchema: {};
490
+ macro: {};
491
+ macroFn: {};
492
+ parser: {};
493
+ response: {};
494
+ } & Http["~Metadata"], {} & Http["~Routes"], {
495
+ derive: {};
496
+ resolve: {};
497
+ schema: {};
498
+ standaloneSchema: {};
499
+ response: {};
500
+ }, {
501
+ derive: {};
502
+ resolve: {};
503
+ schema: {};
504
+ standaloneSchema: {};
505
+ response: {};
506
+ } & {
507
+ derive: {} & ({
508
+ derive: {};
509
+ resolve: {};
510
+ schema: {};
511
+ standaloneSchema: {};
512
+ response: {};
513
+ } & Http["~Ephemeral"])["derive"];
514
+ resolve: {} & ({
515
+ derive: {};
516
+ resolve: {};
517
+ schema: {};
518
+ standaloneSchema: {};
519
+ response: {};
520
+ } & Http["~Ephemeral"])["resolve"];
521
+ schema: import("elysia").MergeSchema<({
522
+ derive: {};
523
+ resolve: {};
524
+ schema: {};
525
+ standaloneSchema: {};
526
+ response: {};
527
+ } & Http["~Ephemeral"])["schema"], {}, "">;
528
+ standaloneSchema: ({
529
+ derive: {};
530
+ resolve: {};
531
+ schema: {};
532
+ standaloneSchema: {};
533
+ response: {};
534
+ } & Http["~Ephemeral"])["standaloneSchema"] & {};
535
+ response: ({
536
+ derive: {};
537
+ resolve: {};
538
+ schema: {};
539
+ standaloneSchema: {};
540
+ response: {};
541
+ } & Http["~Ephemeral"])["response"] & {};
542
+ }>;
305
543
  /** Semantic alias of createApplication for readable tests. */
306
- export declare function createTestApp(options: ApplicationOptions): Elysia;
544
+ export declare function createTestApp<const Http extends AnyElysia = Elysia>(options: ApplicationOptions<Http>): Elysia<"", {
545
+ decorator: {} & Http["~Singleton"]["decorator"];
546
+ store: ({} & Http["~Singleton"]["store"] extends infer T_1 ? { [K_1 in keyof T_1]: T_1[K_1]; } : never) extends infer T ? { [K in keyof T]: T[K]; } : never;
547
+ derive: {} & Http["~Singleton"]["derive"];
548
+ resolve: {} & Http["~Singleton"]["resolve"];
549
+ }, {
550
+ typebox: {};
551
+ error: {};
552
+ } & Http["~Definitions"], {
553
+ schema: {};
554
+ standaloneSchema: {};
555
+ macro: {};
556
+ macroFn: {};
557
+ parser: {};
558
+ response: {};
559
+ } & Http["~Metadata"], {} & Http["~Routes"], {
560
+ derive: {};
561
+ resolve: {};
562
+ schema: {};
563
+ standaloneSchema: {};
564
+ response: {};
565
+ }, {
566
+ derive: {};
567
+ resolve: {};
568
+ schema: {};
569
+ standaloneSchema: {};
570
+ response: {};
571
+ } & {
572
+ derive: {} & ({
573
+ derive: {};
574
+ resolve: {};
575
+ schema: {};
576
+ standaloneSchema: {};
577
+ response: {};
578
+ } & Http["~Ephemeral"])["derive"];
579
+ resolve: {} & ({
580
+ derive: {};
581
+ resolve: {};
582
+ schema: {};
583
+ standaloneSchema: {};
584
+ response: {};
585
+ } & Http["~Ephemeral"])["resolve"];
586
+ schema: import("elysia").MergeSchema<({
587
+ derive: {};
588
+ resolve: {};
589
+ schema: {};
590
+ standaloneSchema: {};
591
+ response: {};
592
+ } & Http["~Ephemeral"])["schema"], {}, "">;
593
+ standaloneSchema: ({
594
+ derive: {};
595
+ resolve: {};
596
+ schema: {};
597
+ standaloneSchema: {};
598
+ response: {};
599
+ } & Http["~Ephemeral"])["standaloneSchema"] & {};
600
+ response: ({
601
+ derive: {};
602
+ resolve: {};
603
+ schema: {};
604
+ standaloneSchema: {};
605
+ response: {};
606
+ } & Http["~Ephemeral"])["response"] & {};
607
+ }>;
307
608
  export { createMemorySandbox, } from "./memory";
308
609
  export type { HandleLike as MemoryHandleLike, MemoryDatabase, MemorySandbox, MemorySandboxOptions, MemoryStorage, MemoryStorageObject, } from "./memory";
309
610
  export { createMemoryPolicy } from "./memory_policy";