Creating a Service
In this lesson, we'll create a service to make a request to the Enzoic API.
We'll cover the following...
The validator we’re creating will need to make a request to the Enzoic API. The API is expecting the password hash data. We’ll talk about hashes when we need to send the password.
First things first, we need to create a service. We have the option of performing the request in the validator. However, we should outsource the request in a service for reusability. It also allows us to practice writing services and to work with observables.
Here’s how the flow of data will look like:
The password will be passed down from the validator to the service. The service will make the request to the API, and the API will respond if the password is compromised. We’ll then give the response to the validator.
Generating a service
Let’s create the service. Run the following command:
ng generate service enzoic
This will generate two files: enzoic.service.ts
and enzoic.service.spec.ts
. We’re calling the service Enzoic because it’s the name of the service to which we’ll be making an API request.
Making a request
Before we can make a request, we’ll need to register the HttpClientModule
. We’ll update the app.module.ts
file to register the module.