@xss-shared-library/https
This library provides reusable HTTP services and utilities for Angular applications. It includes features like token-based authentication, URL configuration, and HTTP interceptors to streamline API communication.
Features
- Token-Based Authentication: Includes services for managing authentication tokens and refreshing them.
- HTTP Interceptors: Automatically adds authentication headers and handles errors globally.
- URL Configuration: Dynamically configure API and OData URLs.
- Reusable HTTP Clients: Provides base HTTP clients for common API operations.
- Integration with Other Libraries: Works seamlessly with
@xss-shared-library/uicomponent
and @xss-shared-library/translations
.
Installation
To install the library, run the following command:
npm install @xss-shared-library/https
Usage
Import the Module
Add the XSSHttpServiceModule
to your Angular application's module:
import { XSSHttpServiceModule } from '@xss-shared-library/https';
@NgModule({
imports: [
XSSHttpServiceModule
]
})
export class AppModule { }
Configuration: appsettings.json
To use this library, ensure you have an appsettings.json
file in the assets
folder of your Angular project. This file should include the necessary configuration for API URLs and token validation.
Example appsettings.json
:
{
"App": {
"Api": "https://api.example.com",
"EnableTokenValidation": true
}
}
Api
: The base URL for your API.
EnableTokenValidation
: Set to true
to enable token validation using the AuthService
.
Example: Using the AuthService
The AuthService
provides methods for managing user authentication, such as checking tokens and logging out.
Check Token:
If EnableTokenValidation
is set to true
in appsettings.json
, you can use the AuthService
to validate the token. If the token is invalid, the system will automatically redirect to the /login
path.
import { AuthService } from '@xss-shared-library/https';
constructor(private authService: AuthService) {
this.authService.checkToken();
}
Log Out:
this.authService.logout();
Example: Using the BaseHttpClients
The BaseHttpClients
class provides reusable methods for making HTTP requests.
Perform a GET Request:
import { BaseHttpClients } from '@xss-shared-library/https';
constructor(private httpClient: BaseHttpClients) {
this.httpClient.getCheckToken().subscribe(response => {
console.log('Token is valid:', response);
});
}
Perform a POST Request:
this.httpClient.postLogOut().subscribe(response => {
console.log('Logged out successfully:', response);
});
Contributing
Contributions are welcome! If you find a bug or have a feature request, please open an issue or submit a pull request.
License
This library is licensed under the MIT License. See the LICENSE
file for more details.
Further Help
For more help with the Angular CLI, use ng help
or check out the Angular CLI Overview and Command Reference page.