Service
Port — the contract + DI token
import { Result } from '@soapjs/soap';
export abstract class WeatherService {
static readonly Token = 'WeatherService';
abstract getByCity(city: string): Promise<Result<Weather>>;
}Adapter — the implementation
import { Result, Failure } from '@soapjs/soap';
export class OpenWeatherService implements WeatherService {
constructor(private readonly apiKey: string) {}
async getByCity(city: string): Promise<Result<Weather>> {
try {
const res = await fetch(`https://api.example.com/weather?q=${city}&key=${this.apiKey}`);
if (!res.ok) return Result.withFailure(Failure.fromError(new Error(`Weather API ${res.status}`)));
const data = await res.json();
return Result.withSuccess(new Weather(data.city, data.tempC));
} catch (error) {
return Result.withFailure(Failure.fromError(error));
}
}
}Wire it in the composition root
Service vs Repository
Repository
Service
Last updated