File

libs/upp-base/src/modules/http.ts

Description

The httpService class provides methods for making HTTP requests and monitoring internet connectivity across multiple platforms (browser and Cordova). It also leverages platform-specific plugins for network state management and performs HTTP requests accordingly, using Angular's HttpClient for web and Ionic's HTTP plugin for mobile.

This service includes platform-dependent methods for GET, POST, HEAD, and connectivity checks, managing the different behaviors required by web browsers and mobile applications.

Additionally, httpService keeps track of the online/offline status and offers an observable that emits the status whenever it changes. This is useful for components and other services that need to respond to connectivity changes.

  • For browsers, it uses Angular's HttpClient.
  • For mobile (Cordova), it uses the Ionic HTTP plugin.
  • Implements OnDestroy to handle cleanup and avoid memory leaks.
Example :
Usage in an Angular component to monitor connectivity:
Example :
constructor(private httpService: httpService) {
  this.httpService.OnOnline?.subscribe(status => {
    console.log("Online status: ", status);
  });
}

this.httpService.get('https://api.example.com/data').subscribe(data => {
  console.log(data);
});

Index

Methods
Accessors

Constructor

constructor(platform: platformService, ionic: HTTP, network: Network, angular: HttpClient)

HTTP service constructor.

Parameters :
Name Type Optional Description
platform platformService No

Platform service used to detect if the environment is Cordova.

ionic HTTP No

Ionic HTTP service used for requests on mobile platforms.

network Network No

Network service used to monitor the network connection status.

angular HttpClient No

Angular HTTP client used for requests in browsers.

Methods

check
check()

Checks the server connection status by making a GET request to a predefined URL.

Returns : any

Observable that emits connection status.

get
get(url: string, withCredentials, headers, responseType: HttpResponseTypes | null, timeout: number | null)

Performs an HTTP GET request.

Parameters :
Name Type Optional Default value Description
url string No

URL of the request.

withCredentials No true

Indicates if credentials should be included.

headers No httpService.headers

Headers for the request.

responseType HttpResponseTypes | null No null

Expected response type.

timeout number | null No null

Timeout duration for the request in milliseconds.

Returns : any

Observable with the HTTP GET response data.

head
head(url: string, headers: any)

Performs an HTTP HEAD request.

Parameters :
Name Type Optional Default value Description
url string No

URL of the request.

headers any No httpService.headers

Headers for the request.

Returns : any

Observable with the HTTP HEAD response data.

ngOnDestroy
ngOnDestroy()

Cleanup method called on service destruction.

Returns : void
post
post(url: string, data: any, headers, responseType: HttpResponseTypes | null, serializer: HttpSerializer | null, timeout: number | null)

Performs an HTTP POST request.

Parameters :
Name Type Optional Default value Description
url string No

URL of the request.

data any No

Data to send in the request body.

headers No httpService.headers

Headers for the request.

responseType HttpResponseTypes | null No null

Expected response type.

serializer HttpSerializer | null No null

Serializer type for the data payload.

timeout number | null No null

Timeout duration for the request in milliseconds.

Returns : any

Observable with the HTTP POST response data.

Accessors

IsOnline
getIsOnline()

Returns a boolean indicating whether the device is online.

Returns : boolean
OnOnline
getOnOnline()

Observable that emits the internet connection status whenever it changes.

Returns : Observable<boolean>
headers
getheaders()

/

results matching ""

    No results matching ""