refactor: 도메인이 100개 1000개씩 늘어나도 유지보수 쉬운 구조로 변경
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
import {
|
||||
defineAppError,
|
||||
type AppError,
|
||||
} from "../../../platform/errors/app-error.ts";
|
||||
import type { ApiFailure } from "../../../contracts/errors.ts";
|
||||
|
||||
/**
|
||||
* Spec §17–§19. The reference feature's own domain errors.
|
||||
*
|
||||
* This union is local to the feature. It satisfies the platform `AppError`
|
||||
* envelope structurally; nothing central enumerates it, so adding or removing
|
||||
* this feature never edits a shared error type.
|
||||
*/
|
||||
export type ReferenceError =
|
||||
| (AppError & {
|
||||
readonly kind: "reference.not-found";
|
||||
readonly category: "domain";
|
||||
readonly severity: "warning";
|
||||
readonly retryability: "never";
|
||||
})
|
||||
| (AppError & {
|
||||
readonly kind: "reference.conflict";
|
||||
readonly category: "domain";
|
||||
readonly severity: "warning";
|
||||
readonly retryability: "never";
|
||||
})
|
||||
| (AppError & {
|
||||
readonly kind: "reference.rejected";
|
||||
readonly category: "validation";
|
||||
readonly severity: "warning";
|
||||
readonly retryability: "never";
|
||||
});
|
||||
|
||||
export const REFERENCE_ERRORS = Object.freeze({
|
||||
NOT_FOUND: defineAppError({
|
||||
kind: "reference.not-found",
|
||||
category: "domain",
|
||||
severity: "warning",
|
||||
retryability: "never",
|
||||
}),
|
||||
CONFLICT: defineAppError({
|
||||
kind: "reference.conflict",
|
||||
category: "domain",
|
||||
severity: "warning",
|
||||
retryability: "never",
|
||||
}),
|
||||
REJECTED: defineAppError({
|
||||
kind: "reference.rejected",
|
||||
category: "validation",
|
||||
severity: "warning",
|
||||
retryability: "never",
|
||||
}),
|
||||
} satisfies Record<string, ReferenceError>);
|
||||
|
||||
/**
|
||||
* Interprets a transport-normalized failure in this feature's own vocabulary.
|
||||
* Anything that is not a reference domain outcome stays `null`: the platform
|
||||
* error mapping, not this feature, owns transport/contract failures.
|
||||
*/
|
||||
export function toReferenceError(failure: ApiFailure): ReferenceError | null {
|
||||
switch (failure.kind) {
|
||||
case "NOT_FOUND":
|
||||
return REFERENCE_ERRORS.NOT_FOUND;
|
||||
case "CONFLICT":
|
||||
return REFERENCE_ERRORS.CONFLICT;
|
||||
case "VALIDATION_REJECTED":
|
||||
return REFERENCE_ERRORS.REJECTED;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
import type { Result } from "../../../contracts/result.ts";
|
||||
import type {} from "../../../application/ports/in/application-api.ts";
|
||||
import {
|
||||
toReferenceView,
|
||||
type ReferenceResourceView,
|
||||
} from "../contracts/reference-mapper.ts";
|
||||
import type { ReferenceResource } from "../domain/reference-resource.ts";
|
||||
import type { MutationIntent } from "../../../contracts/mutation-intent.ts";
|
||||
import type { ExecutionContext } from "../../../platform/execution/execution-context.ts";
|
||||
import type { TelemetryPort } from "../../../platform/telemetry/telemetry-port.ts";
|
||||
import type { ApiFailure } from "../../../contracts/errors.ts";
|
||||
import { REFERENCE_FEATURE_ID } from "../contracts/reference-feature-contract.ts";
|
||||
|
||||
/**
|
||||
* Spec §10. Every call may join a caller-owned interaction so several
|
||||
* operations share one request/retry/concurrency budget and one deadline.
|
||||
*/
|
||||
export type ReferenceCallContext = Readonly<{
|
||||
signal?: AbortSignal;
|
||||
execution?: ExecutionContext;
|
||||
}>;
|
||||
|
||||
export type ReferenceListFilters = Readonly<{
|
||||
cursor?: string;
|
||||
limit: number;
|
||||
tags?: readonly string[];
|
||||
}>;
|
||||
|
||||
export type ReferenceResult<Value> = Result<Value>;
|
||||
|
||||
export type ReferenceCreateCommand = Readonly<{
|
||||
name: string;
|
||||
note?: string;
|
||||
}>;
|
||||
|
||||
export type ReferenceFeatureInput = Readonly<{
|
||||
listResources(
|
||||
filters: ReferenceListFilters,
|
||||
context?: ReferenceCallContext,
|
||||
): Promise<ReferenceResult<readonly ReferenceResourceView[]>>;
|
||||
createResource(
|
||||
command: ReferenceCreateCommand,
|
||||
context?: ReferenceCallContext & Readonly<{ intent?: MutationIntent }>,
|
||||
): Promise<ReferenceResult<ReferenceResourceView>>;
|
||||
getResource(
|
||||
resourceId: string,
|
||||
context?: ReferenceCallContext,
|
||||
): Promise<ReferenceResult<ReferenceResourceView>>;
|
||||
}>;
|
||||
|
||||
declare module "../../../application/ports/in/application-api.ts" {
|
||||
interface ApplicationFeatureInputs {
|
||||
"reference-feature": ReferenceFeatureInput;
|
||||
}
|
||||
}
|
||||
|
||||
export type ReferenceGateway = Readonly<{
|
||||
list(
|
||||
filters: ReferenceListFilters,
|
||||
context?: ReferenceCallContext,
|
||||
): Promise<ReferenceResult<readonly ReferenceResource[]>>;
|
||||
create(
|
||||
command: ReferenceCreateCommand,
|
||||
context?: ReferenceCallContext & Readonly<{ intent?: MutationIntent }>,
|
||||
): Promise<ReferenceResult<ReferenceResource>>;
|
||||
get(
|
||||
resourceId: string,
|
||||
context?: ReferenceCallContext,
|
||||
): Promise<ReferenceResult<ReferenceResource>>;
|
||||
}>;
|
||||
|
||||
/**
|
||||
* Spec §24. Feature-owned telemetry payload. The platform envelope carries it;
|
||||
* no central union lists it. Only the bounded failure kind leaves the feature.
|
||||
*/
|
||||
export type ReferenceLoadFailedPayload = Readonly<{
|
||||
operation: "list" | "detail";
|
||||
reason: ApiFailure["kind"];
|
||||
}>;
|
||||
|
||||
function reportLoadFailure(
|
||||
telemetry: TelemetryPort | undefined,
|
||||
operation: ReferenceLoadFailedPayload["operation"],
|
||||
failure: ApiFailure,
|
||||
context: ReferenceCallContext | undefined,
|
||||
): void {
|
||||
if (!telemetry || failure.kind === "REQUEST_ABORTED") return;
|
||||
const payload: ReferenceLoadFailedPayload = { operation, reason: failure.kind };
|
||||
try {
|
||||
// Fire-and-forget: telemetry never changes the business result.
|
||||
void Promise.resolve(
|
||||
telemetry.emit({
|
||||
kind: "reference.load.failed",
|
||||
severity: "warning",
|
||||
featureId: REFERENCE_FEATURE_ID,
|
||||
...(context?.execution === undefined
|
||||
? {}
|
||||
: { interactionId: context.execution.interaction.id }),
|
||||
payload,
|
||||
}),
|
||||
).catch(() => undefined);
|
||||
} catch {
|
||||
// A throwing port is contained at the call site (spec §26).
|
||||
}
|
||||
}
|
||||
|
||||
export function createReferenceFeatureInput(
|
||||
gateway: ReferenceGateway,
|
||||
observability: Readonly<{ telemetry?: TelemetryPort }> = {},
|
||||
): ReferenceFeatureInput {
|
||||
const { telemetry } = observability;
|
||||
return Object.freeze({
|
||||
async listResources(filters, context) {
|
||||
const result = await gateway.list(filters, context);
|
||||
if (!result.ok) reportLoadFailure(telemetry, "list", result.error, context);
|
||||
return result.ok
|
||||
? {
|
||||
ok: true as const,
|
||||
value: result.value.map((resource) => toReferenceView(resource)),
|
||||
}
|
||||
: result;
|
||||
},
|
||||
async createResource(command, context) {
|
||||
const result = await gateway.create(command, context);
|
||||
return result.ok
|
||||
? { ok: true as const, value: toReferenceView(result.value) }
|
||||
: result;
|
||||
},
|
||||
async getResource(resourceId, context) {
|
||||
const result = await gateway.get(resourceId, context);
|
||||
if (!result.ok) reportLoadFailure(telemetry, "detail", result.error, context);
|
||||
return result.ok
|
||||
? { ok: true as const, value: toReferenceView(result.value) }
|
||||
: result;
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user