Defining the contract

As discussed earlier, we will isolate our services in a dedicated folder. The src/services folder already exists; we'll use it now.

Within that folder, create a new TypeScript file called population-service.intf.ts.

The name ends with the .intf.ts suffix to clearly indicate that the file contains an interface definition. Usually, you should define a single concept per file (that is, class, interface, type, and so on). The folder structure and file naming conventions help navigate around the code easily.

In this file, define the interface of the service as follows:

import {Country, DataPoint} from "../domain"; 

export interface PopulationService { 
    getAllCountries(): Promise<Country[]>; 
    getCountry(countryCode: string): Promise<Country>; 
    getTotalPopulation(country: Country, dateRange: string): 
Promise<DataPoint[]>; getMalePopulation(country: Country, dateRange: string):
Promise<DataPoint[]>; getFemalePopulation(country: Country, dateRange: string):
Promise<DataPoint[]>; getLifeExpectancy(country: Country, dateRange: string):
Promise<DataPoint[]>; getAdultMaleLiteracy(country: Country, dateRange: string):
Promise<DataPoint[]>; getAdultFemaleLiteracy(country: Country, dateRange: string):
Promise<DataPoint[]>; getMaleSurvivalToAge65(country: Country, dateRange: string):
Promise<DataPoint[]>; getFemaleSurvivalToAge65(country: Country, dateRange: string):
Promise<DataPoint[]>; }
If you want to impose more type safety in this interface, then you can change the return types to Readonly<DataPoint[]>, just as we did in the previous chapter.

Also, notice how we've imported the Country and DataPoint classes from our domain module; we can import those this way thanks to the barrel that we created before.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.168.56