import { Injectable } from '@angular/core'; import { httpResource } from '@angular/common/http'; import { Result, ok, err } from '@shared/kernel/fp'; import { BrpAddressDto } from '@registratie/contracts/brp-address.dto'; /** * Infrastructure adapter for the BRP address lookup, reached only through our own * ("BFF-lite") endpoint — the anti-corruption boundary. In this POC the endpoint * is a static mock; pointing at a real backend touches only this file + the DTO. */ @Injectable({ providedIn: 'root' }) export class BrpAdapter { // Typed as the DTO for ergonomics, but the value is untrusted JSON until // parseBrpAddress validates it. adresResource() { return httpResource(() => 'mock/brp-address.json'); } } /** Trust-boundary parse: validate the untrusted response shape. "Geen adres" is a valid outcome (gevonden: false), not a malformed response. ponytail: hand-written; reach for a schema lib once the contract count grows. */ export function parseBrpAddress(json: unknown): Result { if (typeof json !== 'object' || json === null) return err('brp-address: not an object'); const dto = json as Partial; if (typeof dto.gevonden !== 'boolean') return err('brp-address: missing/invalid gevonden'); if (dto.gevonden) { const a = dto.adres; if (!a || typeof a.straat !== 'string' || typeof a.postcode !== 'string' || typeof a.woonplaats !== 'string') { return err('brp-address: missing/invalid adres'); } } return ok({ gevonden: dto.gevonden, adres: dto.adres }); }