📘 API Documentation: Request and Response

This section provides an in-depth look at the API methods used in the Angular-based frontend application. These methods interact with external APIs to handle data for orders, payments, staff, and brand profiles. The application leverages RxJS operators like map and catchError to process API responses and handle errors effectively.


🛠️ API Methods Overview

Service Method Description
HelperService getLocations() Fetches all available locations from the API.
addTracking(body) Submits tracking information for an order.
OrdersService getOrders(pageInfo) Retrieves a list of orders with optional pagination.
fulfillOrder(order) Sends a request to fulfill an order.
BrandProfileService updateBrand(data) Updates the details of a brand profile.

🔗 Example API Request and Response

Here are examples of how API methods are structured and processed using RxJS operators in Angular services.

1. Fetching Locations

getLocations() {
  return this.http.get(`${environment.apiUrl}/locations`);
}
  • Request Type: GET
  • Endpoint: /locations
  • Response:
    [
      {
        "id": "1",
        "name": "New York"
      },
      {
        "id": "2",
        "name": "Los Angeles"
      }
    ]
    

2. Adding Tracking Information

addTracking(body: any) {
  return this.http.post<any>(`${environment.apiUrl}/add-tracking`, body)
    .pipe(
      map(response => {
        return response;
      }),
      catchError(err => {
        return JSON.parse(err.message);
      })
    );
}
  • Request Type: POST
  • Endpoint: /add-tracking
  • Request Payload:
    {
      "orderId": "12345",
      "trackingNumber": "1Z999AA10123456784",
      "shippingService": "UPS"
    }
    
  • Response:
    {
      "status": "success",
      "message": "Tracking information added successfully."
    }
    

3. Updating Brand Profile

updateBrand(data: Object) {
  return this.http.post<any>(`${environment.apiUrl}/public/brand/update-details`, { ...data, accountId: "test" })
    .pipe(map(response => {
      return response;
    }));
}
  • Request Type: POST
  • Endpoint: /public/brand/update-details
  • Request Payload:
    {
      "brandName": "BrandX",
      "email": "[email protected]",
      "contact": "1234567890",
      "website": "https://brandx.com"
    }
    
  • Response:
    {
      "status": "success",
      "message": "Brand details updated successfully."
    }
    

🌟 Key RxJS Operators

Operator Description
map Transforms the response data before passing it to the subscriber.
catchError Handles errors gracefully by catching them and allowing custom error handling logic.

Example Usage of RxJS Operators

addBusiness(body: any) {
  return this.http.post<any>(`${environment.apiUrl}/stripe/create-account`, body)
    .pipe(
      map(response => {
        return response;
      }),
      catchError(err => {
        return JSON.parse(err.message);
      })
    );
}

💡 Error Handling

The catchError operator is used to handle API errors. It allows the application to gracefully manage issues like network errors or invalid responses. For example:

catchError(err => {
  console.error('Error occurred:', err);
  return throwError(() => new Error('Failed to process the request.'));
})

🎨 Color-Coded Highlights

  • Endpoints: /locations, /add-tracking, /public/brand/update-details
  • Operators: map, catchError
  • Request Types: GET, POST

🚀 Emojis for Better Visuals

  • 📘 Documentation
  • 🔗 API Endpoints
  • 🛠️ Methods
  • 🌟 Features
  • 💡 Tips

This documentation ensures a clear understanding of the API methods, their usage, and the underlying RxJS features for efficient error handling and data processing.