110 lines
3.2 KiB
TypeScript
110 lines
3.2 KiB
TypeScript
import {
|
|
createReferenceResource,
|
|
type ReferenceResource,
|
|
} from "../domain/reference-resource.ts";
|
|
import {
|
|
mappingFailure,
|
|
mappingSuccess,
|
|
type InstalledBoundaryMapper,
|
|
type MappingResult,
|
|
} from "../../../contracts/boundary-mapper.ts";
|
|
|
|
export type ReferenceResourceView = Readonly<{
|
|
resourceId: string;
|
|
title: string;
|
|
createdAt: string | null;
|
|
optimistic?: boolean;
|
|
}>;
|
|
|
|
export function mapReferenceResourcePayload(
|
|
value: unknown,
|
|
): MappingResult<ReferenceResource> {
|
|
if (!value || typeof value !== "object") {
|
|
return mappingFailure("MAPPING_INVARIANT_REJECTED");
|
|
}
|
|
const dto = value as Record<string, unknown>;
|
|
if (typeof dto.id !== "string" || typeof dto.name !== "string") {
|
|
return mappingFailure("MAPPING_INVARIANT_REJECTED");
|
|
}
|
|
try {
|
|
return mappingSuccess(
|
|
createReferenceResource({
|
|
id: dto.id,
|
|
displayName: dto.name,
|
|
createdAt: typeof dto.createdAt === "string" ? dto.createdAt : null,
|
|
}),
|
|
);
|
|
} catch {
|
|
return mappingFailure("MAPPING_INVARIANT_REJECTED");
|
|
}
|
|
}
|
|
|
|
export function mapReferenceResourceListPayload(
|
|
payload: unknown,
|
|
): MappingResult<readonly ReferenceResource[]> {
|
|
if (!Array.isArray(payload)) {
|
|
return mappingFailure("MAPPING_INVARIANT_REJECTED");
|
|
}
|
|
if (payload.length > 100) {
|
|
return mappingFailure("OUTPUT_LIMIT_EXCEEDED");
|
|
}
|
|
const output: ReferenceResource[] = [];
|
|
for (const item of payload) {
|
|
const mapped = mapReferenceResourcePayload(item);
|
|
if (!mapped.ok) return mapped;
|
|
output.push(mapped.value);
|
|
}
|
|
return mappingSuccess(Object.freeze(output));
|
|
}
|
|
|
|
/**
|
|
* Registry-facing compatibility projection. Feature adapters should prefer the
|
|
* operation-specific mapper functions above so their result type is exact.
|
|
*/
|
|
export function mapReferenceOperation(
|
|
operationId: string,
|
|
payload: unknown,
|
|
): MappingResult<ReferenceResource | readonly ReferenceResource[]> {
|
|
if (operationId === "LIST_REFERENCE_RESOURCES") {
|
|
return mapReferenceResourceListPayload(payload);
|
|
}
|
|
if (
|
|
operationId === "CREATE_REFERENCE_RESOURCE" ||
|
|
operationId === "GET_REFERENCE_RESOURCE"
|
|
) {
|
|
return mapReferenceResourcePayload(payload);
|
|
}
|
|
return mappingFailure("MAPPING_INVARIANT_REJECTED");
|
|
}
|
|
|
|
export const REFERENCE_BOUNDARY_MAPPERS = Object.freeze({
|
|
ReferenceResourceListMapper: Object.freeze({
|
|
mapperId: "ReferenceResourceListMapper",
|
|
mapperVersion: 1,
|
|
inputSchemaId: "ReferenceResourceListPayload",
|
|
outputContractId: "ReferenceResourceList",
|
|
owner: "feature-frontend-reference-feature-vertical-slice",
|
|
maxOutputItems: 100,
|
|
map: mapReferenceResourceListPayload,
|
|
}),
|
|
ReferenceResourceMapper: Object.freeze({
|
|
mapperId: "ReferenceResourceMapper",
|
|
mapperVersion: 1,
|
|
inputSchemaId: "ReferenceResourcePayload",
|
|
outputContractId: "ReferenceResource",
|
|
owner: "feature-frontend-reference-feature-vertical-slice",
|
|
maxOutputItems: 1,
|
|
map: mapReferenceResourcePayload,
|
|
}),
|
|
} satisfies Readonly<Record<string, InstalledBoundaryMapper>>);
|
|
|
|
export function toReferenceView(
|
|
resource: ReferenceResource,
|
|
): ReferenceResourceView {
|
|
return Object.freeze({
|
|
resourceId: resource.id,
|
|
title: resource.displayName,
|
|
createdAt: resource.createdAt,
|
|
});
|
|
}
|