Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Effective Angular

You're reading from   Effective Angular Develop applications of any size by effectively using Angular with Nx, RxJS, NgRx, and Cypress

Arrow left icon
Product type Paperback
Published in Aug 2024
Publisher Packt
ISBN-13 9781805125532
Length 400 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Roberto Heckers Roberto Heckers
Author Profile Icon Roberto Heckers
Roberto Heckers
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

Preface 1. Part 1:Angular Basics and Setting Up Scalable Nx Workspaces FREE CHAPTER
2. Chapter 1: Scalable Front-End Architecture for Angular Applications 3. Chapter 2: Powerful Angular Features 4. Chapter 3: Enhancing Your Applications with Directives, Pipes, and Animations 5. Chapter 4: Building Forms Like a Pro 6. Part 2:Handling Application State and Writing Cleaner, More Scalable Code
7. Chapter 5: Creating Dynamic Angular Components 8. Chapter 6: Applying Code Conventions and Design Patterns in Angular 9. Chapter 7: Mastering Reactive Programming in Angular 10. Chapter 8: Handling Application State with Grace 11. Part 3:Getting Ready for Production with Automated Tests, Performance, Security, and Accessibility
12. Chapter 9: Enhancing the Performance and Security of Angular Applications 13. Chapter 10: Internationalization, Localization, and Accessibility of Angular Applications 14. Chapter 11: Testing Angular Applications 15. Chapter 12: Deploying Angular Applications 16. Index 17. Other Books You May Enjoy

Building a generic HTTP service containing a model adapter

To build your generic HTTP service with a model adapter, start by creating a new library named generic-http of type data-access in the domain shared. In this library, create a file named generic-http.service.ts and a file named model-adapter.interface.ts.

Inside the model-adapter interface file, add this interface:

export interface ModelAdapter<T, S> {
  fromDto(dto: T): S;
  toDto(model: S): T;
}

We use generic types so we can make our model adapter type-safe. The T and S are placeholders for the data transfer object (DTO) and frontend model we will provide to the adapter. After creating the interface, start with the generic HTTP service.

The generic HTTP service will need a property for the API URL and the default HTTP headers, and the service needs to inject the HTTP client.

@Injectable({
  providedIn: 'root'
})
export abstract class GenericHttpService<T, S&gt...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime