feat(registry-envelope-integration): update execute(), call, subscribe, env to return ResponseEnvelope
- OperationRegistry.execute() now returns Promise<ResponseEnvelope<TOutput>> - Applies shared result pipeline: detect → wrap → normalize → validate - Uses KindGuard.IsUnknown() to check if Value.Cast should be applied - PendingRequestMap.call() returns Promise<ResponseEnvelope> - PendingRequestMap.respond() validates envelope via isResponseEnvelope() - CallHandler captures handler result, wraps, normalizes, validates, publishes - CallEventSchema call.responded.output changed to ResponseEnvelopeSchema - subscribe() yields ResponseEnvelope with isResponseEnvelope() passthrough - OperationEnv inner functions return Promise<ResponseEnvelope> - Tests updated for all new return types and behaviors - 171 tests passing, build and lint clean
This commit is contained in:
49
src/call.ts
49
src/call.ts
@@ -1,10 +1,12 @@
|
||||
import { Type, type Static } from "@alkdev/typebox";
|
||||
import { Type, type Static, KindGuard } from "@alkdev/typebox";
|
||||
import { Value } from "@alkdev/typebox/value";
|
||||
import { createPubSub, type PubSub } from "@alkdev/pubsub";
|
||||
import { getLogger } from "@logtape/logtape";
|
||||
import { OperationRegistry } from "./registry.js";
|
||||
import { CallError, InfrastructureErrorCode, mapError } from "./error.js";
|
||||
import { validateOrThrow } from "./validation.js";
|
||||
import { validateOrThrow, collectErrors, formatValueErrors } from "./validation.js";
|
||||
import type { Identity, OperationContext, AccessControl, OperationSpec } from "./types.js";
|
||||
import { ResponseEnvelopeSchema, isResponseEnvelope, localEnvelope, type ResponseEnvelope } from "./response-envelope.js";
|
||||
|
||||
const logger = getLogger("operations:call");
|
||||
|
||||
@@ -23,7 +25,7 @@ export const CallEventSchema = {
|
||||
}),
|
||||
"call.responded": Type.Object({
|
||||
requestId: Type.String(),
|
||||
output: Type.Unknown(),
|
||||
output: ResponseEnvelopeSchema,
|
||||
}),
|
||||
"call.aborted": Type.Object({
|
||||
requestId: Type.String(),
|
||||
@@ -52,7 +54,7 @@ type CallPubSubMap = {
|
||||
};
|
||||
|
||||
interface PendingRequest {
|
||||
resolve: (value: unknown) => void;
|
||||
resolve: (value: ResponseEnvelope) => void;
|
||||
reject: (reason: unknown) => void;
|
||||
deadline?: number;
|
||||
timer?: ReturnType<typeof setTimeout>;
|
||||
@@ -121,7 +123,7 @@ export class PendingRequestMap {
|
||||
operationId: string,
|
||||
input: unknown,
|
||||
options?: { parentRequestId?: string; deadline?: number; identity?: Identity },
|
||||
): Promise<unknown> {
|
||||
): Promise<ResponseEnvelope> {
|
||||
const requestId = crypto.randomUUID();
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
@@ -148,7 +150,10 @@ export class PendingRequestMap {
|
||||
});
|
||||
}
|
||||
|
||||
respond(requestId: string, output: unknown): void {
|
||||
respond(requestId: string, output: ResponseEnvelope): void {
|
||||
if (!isResponseEnvelope(output)) {
|
||||
throw new Error(`PendingRequestMap.respond() requires a ResponseEnvelope, got: ${typeof output}`);
|
||||
}
|
||||
this.pubsub.publish("call.responded", "", {
|
||||
requestId,
|
||||
output,
|
||||
@@ -180,11 +185,16 @@ export class PendingRequestMap {
|
||||
}
|
||||
|
||||
export function buildCallHandler(config: CallHandlerConfig): CallHandler {
|
||||
const { registry } = config;
|
||||
const { registry, eventTarget } = config;
|
||||
|
||||
return async (event: CallRequestedEvent): Promise<void> => {
|
||||
const { requestId, operationId, input, identity } = event;
|
||||
|
||||
let callMap: PendingRequestMap | undefined;
|
||||
if (eventTarget) {
|
||||
callMap = new PendingRequestMap(eventTarget);
|
||||
}
|
||||
|
||||
try {
|
||||
const spec = registry.getSpec(operationId);
|
||||
|
||||
@@ -223,10 +233,33 @@ export function buildCallHandler(config: CallHandlerConfig): CallHandler {
|
||||
|
||||
validateOrThrow(spec.inputSchema, input, `Input validation for ${operationId}`);
|
||||
|
||||
await handler(input, context);
|
||||
const result = await handler(input, context);
|
||||
|
||||
let envelope: ResponseEnvelope;
|
||||
if (isResponseEnvelope(result)) {
|
||||
envelope = result as ResponseEnvelope;
|
||||
} else {
|
||||
envelope = localEnvelope(result, operationId);
|
||||
}
|
||||
|
||||
if (!KindGuard.IsUnknown(spec.outputSchema)) {
|
||||
envelope.data = Value.Cast(spec.outputSchema, envelope.data);
|
||||
}
|
||||
|
||||
const errors = collectErrors(spec.outputSchema, envelope.data);
|
||||
if (errors.length > 0) {
|
||||
logger.warn(`Output validation failed for ${operationId}:\n${formatValueErrors(errors)}`);
|
||||
}
|
||||
|
||||
if (callMap) {
|
||||
callMap.respond(requestId, envelope);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
const callError = mapError(error);
|
||||
if (callMap) {
|
||||
callMap.emitError(requestId, callError.code, callError.message, callError.details);
|
||||
}
|
||||
throw callError;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import { OperationType } from "./types.js";
|
||||
import type { OperationContext, OperationEnv, Identity } from "./types.js";
|
||||
import type { OperationRegistry } from "./registry.js";
|
||||
import type { ResponseEnvelope } from "./response-envelope.js";
|
||||
import { getLogger } from "@logtape/logtape";
|
||||
|
||||
const logger = getLogger("operations:env");
|
||||
|
||||
export interface CallMap {
|
||||
call(operationId: string, input: unknown, options?: { parentRequestId?: string; deadline?: number; identity?: Identity }): Promise<unknown>;
|
||||
call(operationId: string, input: unknown, options?: { parentRequestId?: string; deadline?: number; identity?: Identity }): Promise<ResponseEnvelope>;
|
||||
}
|
||||
|
||||
export interface EnvOptions {
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import type { OperationContext, OperationSpec, OperationHandler, SubscriptionHandler } from "./types.js";
|
||||
import { getLogger } from "@logtape/logtape";
|
||||
import { Value } from "@alkdev/typebox/value";
|
||||
import { KindGuard } from "@alkdev/typebox";
|
||||
import { assertIsSchema, validateOrThrow, collectErrors, formatValueErrors } from "./validation.js";
|
||||
import { isResponseEnvelope, localEnvelope, type ResponseEnvelope } from "./response-envelope.js";
|
||||
|
||||
const logger = getLogger("operations:registry");
|
||||
|
||||
@@ -81,7 +83,7 @@ export class OperationRegistry {
|
||||
operationId: string,
|
||||
input: TInput,
|
||||
context: OperationContext,
|
||||
): Promise<TOutput> {
|
||||
): Promise<ResponseEnvelope<TOutput>> {
|
||||
const spec = this.specs.get(operationId);
|
||||
if (!spec) {
|
||||
throw new Error(`Operation not found: ${operationId}`);
|
||||
@@ -94,13 +96,24 @@ export class OperationRegistry {
|
||||
|
||||
validateOrThrow(spec.inputSchema, input, `Input validation failed for ${operationId}`);
|
||||
|
||||
const result = await handler(input, context) as TOutput;
|
||||
const result = await handler(input, context);
|
||||
|
||||
const errors = collectErrors(spec.outputSchema, result);
|
||||
let envelope: ResponseEnvelope<TOutput>;
|
||||
if (isResponseEnvelope(result)) {
|
||||
envelope = result as ResponseEnvelope<TOutput>;
|
||||
} else {
|
||||
envelope = localEnvelope(result as TOutput, operationId);
|
||||
}
|
||||
|
||||
if (!KindGuard.IsUnknown(spec.outputSchema)) {
|
||||
envelope.data = Value.Cast(spec.outputSchema, envelope.data) as TOutput;
|
||||
}
|
||||
|
||||
const errors = collectErrors(spec.outputSchema, envelope.data);
|
||||
if (errors.length > 0) {
|
||||
logger.warn(`Output validation failed for ${operationId}:\n${formatValueErrors(errors)}`);
|
||||
}
|
||||
|
||||
return result;
|
||||
return envelope;
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,13 @@
|
||||
import type { OperationContext } from "./types.js";
|
||||
import { OperationRegistry } from "./registry.js";
|
||||
import { isResponseEnvelope, localEnvelope, type ResponseEnvelope } from "./response-envelope.js";
|
||||
|
||||
export async function* subscribe(
|
||||
registry: OperationRegistry,
|
||||
operationId: string,
|
||||
input: unknown,
|
||||
context: OperationContext,
|
||||
): AsyncGenerator<unknown, void, unknown> {
|
||||
): AsyncGenerator<ResponseEnvelope, void, unknown> {
|
||||
const spec = registry.getSpec(operationId);
|
||||
|
||||
if (!spec) {
|
||||
@@ -23,7 +24,11 @@ export async function* subscribe(
|
||||
|
||||
try {
|
||||
for await (const value of generator) {
|
||||
yield value;
|
||||
if (isResponseEnvelope(value)) {
|
||||
yield value;
|
||||
} else {
|
||||
yield localEnvelope(value, operationId);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
if (generator.return) {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Type, type Static, type TSchema } from "@alkdev/typebox";
|
||||
import type { ResponseEnvelope } from "./response-envelope.js";
|
||||
|
||||
export enum OperationType {
|
||||
QUERY = "query",
|
||||
@@ -12,7 +13,7 @@ export interface Identity {
|
||||
resources?: Record<string, string[]>
|
||||
}
|
||||
|
||||
export type OperationEnv = Record<string, Record<string, (input: unknown) => Promise<unknown>>>
|
||||
export type OperationEnv = Record<string, Record<string, (input: unknown) => Promise<ResponseEnvelope>>>
|
||||
|
||||
export const OperationContextSchema = Type.Object({
|
||||
metadata: Type.Optional(Type.Record(Type.String(), Type.Unknown())),
|
||||
|
||||
Reference in New Issue
Block a user