1.0.30 • Published 4 years ago

ngx-core-services v1.0.30

Weekly downloads
235
License
-
Repository
-
Last release
4 years ago

NgxCore

⚡ Try it on Stackblitz: https://stackblitz.com/edit/ngx-core-services

Getting started

Installation:

Install via npm package manager

npm i ngx-core-services

Dependencies:

 @auth0/angular-jwt
 jwt-decode

Usage:

Module:

import {
  NgxCoreModule,
  JwtInterceptorService,
  ErrorInterceptorService,
  AuthService,
  StorageService,
  ApiService,
  RefreshTokenInterceptor
} from "ngx-core-services";
imports: [
...
	// Authentication server, Api server, Project name
	NgxCoreModule.forRoot({
		authHost:  'test', //environment.host,
		apiHost:  'test',//environment.host,
		project:  'test'//environment.name,
		refreshType: 0 // refresh token refresh type, if 0 it refreshes access token auto, else navigates to route ''.
	}),
...
],
providers: [
...
	providers: [
    {
		provide: LocationStrategy,
		useClass: HashLocationStrategy
	},
	{ 	provide: HTTP_INTERCEPTORS,
		useClass: JwtInterceptorService, // Intercept http requests and add jwt to the header
		multi: true
	},
    {
		provide: HTTP_INTERCEPTORS,
		useClass: ErrorInterceptorService, // Checks for http errors
		multi: true
	},
    {
		provide: HTTP_INTERCEPTORS,
		useClass: RefreshTokenInterceptor, // Refresh token interceptor
		multi: true
	},
    AuthService,
    StorageService,
    ApiService,
  ],
...
]

NgxCoreModule:

export interface NgxCoreConfig {
  authHost?: string; // API url for the authentication server
  apiHost?: string; // API url for the API server
  project?: string; // Project name
  refreshType?: number; // Refresh token refresh type
}

ApiService:

get(url: string, data?: any) // HTTP GET request
put(url: string, data?: any) // HTTP PUT request
post(url: string, data?: any) // HTTP POST request
delete(url: string, data?: any) // HTTP DELETE request

StorageService:

Cookies:

setCookie((name: string), (val: string)); // Set cookie
getCookie((name: string)); // Returns with a cookie associated with the given name
deleteCookie((name: string)); // Deletes a cookie by name
clearCookies(); // Clears cookies

LocalStorage:

setStorage((name: string), (val: string)); // Set local storage
getStorage((name: string)); // Returns with a storage value associated with the given name
deleteStorage((name: string)); // Deletes a storage by name
clearStorage(); // Clears the storage

AuthService:

public  canActivate(route: ActivatedRouteSnapshot):boolean // Route guard
public  isAuthenticated(): boolean // Has token and isvalid
public  getToken() // Returns the saved access_token
public  login(url: string, data) // User login, if data['refreshType'] is given then sets refreshType (concats url to authHost)
public  getRole() // Get user role from access_token identity
public  getUsername() // Get username from access_token identity
public  getName() // Get name from access_token identity
public  getUserData() // Get user data from access_token identity
public  change(data: any) // Data change subject
public  logout(url: string, data?) // User logout, cookie and storage clear.

JwtInterceptorService:

intercept((request: HttpRequest<any>), (next: HttpHandler)); // sets the access_token or refresh_token

ErrorInterceptorService:

intercept((request: HttpRequest<any>), (next: HttpHandler)); // throws error if something bad happens

RefreshTokenInterceptorService:

intercept((request: HttpRequest<any>), (next: HttpHandler)); // sets the refresh token if access_token has expired

AppState

setAppState(); // Sets localstorage key, and loggedIn
getAppState(); // Returns app state
getAppStateByKey(key: string); // Returns app state by key
changeAppState(obj: any); // Changes app state e.g.: obj={'loggedIn': true}

Example:

App Component:
import { AppStateService } from "ngx-core-services";
...
constructor(private appStateService: AppStateService){
}
ngOnInit(): void {
  this.appStateService.setAppState();
}

ViewState

getViewState(); // Sets the viewState - only use it in the view constructor!
setViewState(obj: any); // Returns the viewState object
getViewStateByKey(key: string); // Returns data that maches the given parameter.
getView(); // Returns the current view.
changeViewState(obj: any, history?); // Changes the view state. Sets the goven data and history, emits the current change.

Example:

View1:
import { ViewStateService } from "ngx-core-services";
...
constructor(private viewStateService: ViewStateService) {
  this.viewStateService.setViewState({view: 'View1'});
}
Component1:
test = 0;
constructor(private viewStateService: ViewStateService) {
    this.viewStateService.datachange.subscribe(data => {
        this.test = data.get('test');
        // write your code
    });
}
...
btnTest_Click() {
    this.test++;
    this.viewStateService.changeViewState({ test: this.test, test: 'test' }, 'btnTest_Click'); // data object and history, you don't need to change the view's name, only give the data and the history.
}
1.0.30

4 years ago

1.0.29

4 years ago

1.0.28

4 years ago

1.0.27

4 years ago

1.0.22

4 years ago

1.0.26

4 years ago

1.0.25

4 years ago

1.0.24

4 years ago

1.0.23

4 years ago

1.0.19

4 years ago

1.0.21

4 years ago

1.0.20

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.9

4 years ago

1.0.10

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.2.12

4 years ago

0.2.11

4 years ago

0.2.10

4 years ago

0.2.9

4 years ago

0.2.8

4 years ago

0.2.7

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.3

4 years ago

0.2.4

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.0.12

4 years ago

0.0.13

4 years ago

0.0.10

4 years ago

0.0.11

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.3

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago