import { toResourceViewModel } from "../../application/view-models/resource-view-model.js"; import { queryKeys } from "../../contracts/query-keys.js"; /** * @param {{ * http: { execute(operationId: string, input?: Record): Promise< * {ok: true, value: unknown} | {ok: false, error: import("../../contracts/errors.js").ApiFailure} * > }, * cache: import("../../application/ports/query-cache-port.js").QueryCachePort * }} ports */ export function createSampleFacade(ports) { return Object.freeze({ async listResources(filters = {}) { const key = queryKeys.resource.list(filters); const result = await ports.http.execute("LIST_SAMPLE_RESOURCES", { routeId: "SAMPLE_RESOURCE_LIST", }); if (!result.ok) return result; const models = /** @type {Array} */ ( result.value ); const cached = ports.cache.write(key, models); if (!cached.ok) return cached; return { ok: /** @type {true} */ (true), value: models.map((model) => toResourceViewModel(model)), }; }, /** @param {{ name: string }} command */ async createResource(command) { const result = await ports.http.execute("CREATE_SAMPLE_RESOURCE", { body: command, routeId: "SAMPLE_RESOURCE_LIST", }); if (!result.ok) return result; const invalidated = await ports.cache.invalidate(queryKeys.resource.all()); if (!invalidated.ok) return invalidated; return { ok: /** @type {true} */ (true), value: toResourceViewModel( /** @type {import("../../domain/models/resource.js").Resource} */ ( result.value ), ), }; }, }); }