Example for Creating Angular Services with Signals and Resources for API Interaction

Discussão em 'Angular' iniciado por eric-bam, Dezembro 19, 2024.

  1. eric-bam

    eric-bam Guest

    I'm looking to implement an Angular service that interacts with an enterprise RESTful API using Angular's new signal and resource features. Most of the examples I've found demonstrate making API calls directly within components, but I want to adhere to best practices by encapsulating this logic within a dedicated service.

    Here's the structure of my API responses:

    export interface ApiResponse<T> {
    success: boolean;
    message: string;
    statusCode: number;
    data: T;
    }


    Questions:

    1. What is the ideal structure for an Angular service that leverages signal and resource for API interactions?
    2. How can I implement inputs to these? I've seen plenty of examples of passing in a signal to a resource directly, but I haven't seen how this would work in a service where you could have quite a few resource endpoints. I wouldn't assume you would have a signal for each possible endpoint's inputs, would you?
    3. Is is better to use resource with promises or rxResource with observables? Assume we're not married to either of them.

    I could also just be way off base here with how these are supposed to be used. We're pretty new to Angular, and I just want to make sure we're getting things off on the right foot before we implement hundreds of endpoints in our Angular app the wrong way.

    Any guidance, sample implementations, or references to best practices for setting up such a service would be greatly appreciated!

    Continue reading...

Compartilhe esta Página