npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ngx-core-services

v1.0.30

Published

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

Downloads

6

Readme

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.
}