Creating the service implementation skeleton

Create a file called population-service.ts under src/services and add the skeleton to it:

import {PopulationService} from "./population-service.intf"; 
import {Country, DataPoint} from "../domain"; 
import { 
    WorldBankApiV2, 
} from "./world-bank-api"; 

export class PopulationServiceImpl implements PopulationService { 
    private readonly countriesApiBaseUrl: string;

    constructor(baseUrl: string) { 
        if (!baseUrl || baseUrl.trim().length === 0) { 
            throw new Error("The base URL must be provided!"); 
        } else if (!baseUrl.toLocaleLowerCase().startsWith('https://') 
&& !baseUrl.toLocaleLowerCase().startsWith('http://')) { throw new Error("The URL looks invalid. It should start
with 'http://' or https://'"); } let cleanBaseUrl = baseUrl.trim(); if (cleanBaseUrl.endsWith('/')) { cleanBaseUrl = cleanBaseUrl.substr(0,
cleanBaseUrl.lastIndexOf('/')); } this.countriesApiBaseUrl =
`${cleanBaseUrl}/${WorldBankApiV2.VERSION}/
${WorldBankApiV2.COUNTRIES_API_PREFIX}`; console.log(`Population service initialized.\nCountries API
URL: [${this.countriesApiBaseUrl}]`); } async getAllCountries(): Promise<Country[]> { // TODO implement throw new Error("Not implemented yet"); } async getCountry(countryCode: string): Promise<Country> { // TODO implement throw new Error("Not implemented yet"); } async getTotalPopulation(country: Country, dateRange: string):
Promise<DataPoint[]> { // TODO implement throw new Error("Not implemented yet"); } async getFemalePopulation(country: Country, dateRange: string):
Promise<DataPoint[]> { // TODO implement throw new Error("Not implemented yet"); } async getMalePopulation(country: Country, dateRange: string):
Promise<DataPoint[]> { // TODO implement throw new Error("Not implemented yet"); } async getAdultFemaleLiteracy(country: Country, dateRange: string):
Promise<DataPoint[]> { // TODO implement throw new Error("Not implemented yet"); } async getAdultMaleLiteracy(country: Country, dateRange: string):
Promise<DataPoint[]> { // TODO implement throw new Error("Not implemented yet"); } async getFemaleSurvivalToAge65(country: Country, dateRange:
string): Promise<DataPoint[]> { // TODO implement throw new Error("Not implemented yet"); } async getLifeExpectancy(country: Country, dateRange: string):
Promise<DataPoint[]> { // TODO implement throw new Error("Not implemented yet"); }
async getMaleSurvivalToAge65(country: Country, dateRange: string):
Promise<DataPoint[]> { // TODO implement throw new Error("Not implemented yet"); } }

In the constructor, we expect to receive the base URL of the World Bank API. We didn't hardcode it to allow defining a different URL as needed (for example a different one per environment). This can be useful, especially for testing purposes. After some basic validations, we store the base URL of the main API.

Now, let's start implementing the different methods together.