import { createFailure } from "../../../../src/contracts/errors.ts"; import type { Result } from "../../../../src/contracts/result.ts"; import type {} from "../../../../src/application/ports/in/application-api.ts"; import { createLocalDraft, type LocalDraft } from "../domain/local-draft.ts"; export type LocalDraftRecord = Readonly<{ draft: LocalDraft; revision: number; }>; export type SaveLocalDraftCommand = Readonly<{ draft: LocalDraft; /** Only unambiguous while draft ids are never reused (see LocalDraft.draftId). */ expectedRevision: number | null; idempotencyKey: string; }>; export type RemoveLocalDraftCommand = Readonly<{ draftId: string; expectedRevision: number; idempotencyKey: string; }>; export type LocalDraftStore = Readonly<{ save( command: SaveLocalDraftCommand, signal?: AbortSignal, ): Promise>>; find( draftId: string, signal?: AbortSignal, ): Promise>; remove( command: RemoveLocalDraftCommand, signal?: AbortSignal, ): Promise>; }>; export type LocalDraftFeatureInput = Readonly<{ saveDraft( command: SaveLocalDraftCommand, context?: Readonly<{ signal?: AbortSignal }>, ): Promise>>; findDraft( draftId: string, context?: Readonly<{ signal?: AbortSignal }>, ): Promise>; removeDraft( command: RemoveLocalDraftCommand, context?: Readonly<{ signal?: AbortSignal }>, ): Promise>; }>; declare module "../../../../src/application/ports/in/application-api.ts" { interface ApplicationFeatureInputs { "local-draft": LocalDraftFeatureInput; } } export function createLocalDraftFeatureInput( store: LocalDraftStore, ): LocalDraftFeatureInput { return Object.freeze({ // 09-06. The domain rules (trimmed non-blank id and title, bounded // lengths) are applied here; the store never sees an unchecked draft. async saveDraft(command, context) { let draft: LocalDraft; try { draft = createLocalDraft(command.draft); } catch { return Object.freeze({ ok: false as const, error: createFailure("VALIDATION_REJECTED", "LOCAL_DRAFT_SAVE", 0), }); } return store.save(Object.freeze({ ...command, draft }), context?.signal); }, findDraft(draftId, context) { return store.find(draftId, context?.signal); }, removeDraft(command, context) { return store.remove(command, context?.signal); }, }); }