-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
57 changed files
with
216,880 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
name: Build | ||
|
||
on: [push] | ||
|
||
jobs: | ||
lint: | ||
name: Build | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-node@v1 | ||
with: | ||
node-version: 12 | ||
- run: npm ci | ||
- run: npm run build:lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
name: Lint | ||
|
||
on: [push] | ||
|
||
jobs: | ||
lint: | ||
name: Lint | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-node@v1 | ||
with: | ||
node-version: 12 | ||
- run: npm ci | ||
- run: npm run lint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
name: Publish Package | ||
|
||
on: | ||
push: | ||
tags: | ||
- 'v*' | ||
|
||
jobs: | ||
publish-npm: | ||
name: Publish to NPM | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-node@v1 | ||
with: | ||
node-version: 12 | ||
registry-url: https://registry.npmjs.org/ | ||
- run: npm ci | ||
- run: npm run build:lib | ||
- run: npm run publish:lib | ||
env: | ||
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}} | ||
|
||
publish-gpr: | ||
name: Publish to GitHub Packages | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-node@v1 | ||
with: | ||
node-version: 12 | ||
registry-url: https://npm.pkg.github.com/ | ||
scope: '@SachinShekhar' | ||
- run: npm ci | ||
- run: npm run build:lib | ||
- run: npm run publish:lib | ||
env: | ||
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Changelogs | ||
|
||
Check library's [GitHub Releases page](https://github.com/SachinShekhar/ss-ngrx-router-store/releases) for detailed changelogs. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright 2020 Sachin Shekhar | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,182 @@ | ||
Simple Serialized NgRx Router Store | ||
# Simple Serialized NgRx Router Store | ||
|
||
`ss-ngrx-router-store` package is a library which serializes Angular router snapshot and puts it into NgRx store. It also searches entire route tree to bring route-specific attributes at one place (no need to think about children, firstChild, parent, root etc). The result is very clean ([check demo](https://sachinshekhar.github.io/ss-ngrx-router-store)). It is useful to newbies and experts alike. | ||
|
||
![Demo](./demo.png) | ||
|
||
## Installation | ||
|
||
1. Make sure that you've installed and setup `@angular/router` and `@ngrx/store`. | ||
|
||
2. ``` | ||
npm install --save ss-ngrx-router-store @ngrx/router-store | ||
``` | ||
|
||
3. Import `SSNgRxRouterStoreModule` in `app.module.ts`: | ||
|
||
```typescript | ||
|
||
import { BrowserModule } from '@angular/platform-browser'; | ||
import { NgModule } from '@angular/core'; | ||
import { RouterModule, Routes } from '@angular/router'; | ||
import { StoreModule } from '@ngrx/store'; | ||
import { StoreDevtoolsModule } from '@ngrx/store-devtools'; | ||
import { SSNgRxRouterStoreModule } from 'ss-ngrx-router-store'; | ||
|
||
.... | ||
.... | ||
|
||
@NgModule({ | ||
declarations: [AppComponent], | ||
imports: [ | ||
BrowserModule, | ||
RouterModule.forRoot(routes), | ||
StoreModule.forRoot(reducers, { metaReducers }), | ||
StoreDevtoolsModule.instrument({ | ||
maxAge: 25, | ||
logOnly: environment.production | ||
}), | ||
SSNgRxRouterStoreModule | ||
], | ||
providers: [], | ||
bootstrap: [AppComponent] | ||
}) | ||
export class AppModule {} | ||
|
||
``` | ||
|
||
No need to install anything from `@ngrx/router-store`. Also, keep in mind that import orders matter. | ||
|
||
4. Done! You can see `router` state in `Redux DevTools` browser extensions. | ||
|
||
### For Advanced Users | ||
|
||
`SSNgRxRouterStoreModule` uses default settings of `@ngrx/router-store`. If you want to override default settings, you can setup `@ngrx/router-store` yourself and use serializer provided by `ss-ngrx-router-store`. For example: | ||
|
||
```typescript | ||
import { SSRouterSerializer } from 'ss-ngrx-router-store'; | ||
|
||
@NgModule({ | ||
imports: [ | ||
StoreRouterConnectingModule.forRoot({ | ||
stateKey: 'clean-router', | ||
serializer: SSRouterSerializer, | ||
navigationActionTiming: NavigationActionTiming.PostActivation | ||
}) | ||
] | ||
}) | ||
``` | ||
|
||
## Usage | ||
|
||
`ss-ngrx-router-store` provides lots of selectors which you can use in your app. | ||
|
||
```typescript | ||
import { Component, OnInit } from '@angular/core'; | ||
import { Store } from '@ngrx/store'; | ||
import { Observable } from 'rxjs'; | ||
import { getParams } from 'ss-ngrx-router-store'; | ||
|
||
@Component({ | ||
template: ` | ||
<p> | ||
{{ (params$ | async).chatId }} | ||
</p> | ||
`, | ||
styles: [] | ||
}) | ||
export class YourComponent implements OnInit { | ||
constructor(private store: Store<any>) {} | ||
|
||
params$: Observable<{ chatId: string }>; | ||
|
||
ngOnInit(): void { | ||
this.routerState$ = this.store.select<{ chatId: string }>(getParams); | ||
} | ||
} | ||
``` | ||
|
||
### List of Selectors | ||
|
||
| Selectors | Usage | | ||
| --------------- | ------------------------------------------------------------------- | | ||
| getRouterState | Select partial state of NgRx Router Store | | ||
| getRouteMap | Select map containing URL, Params, Query Params, Fragment & Data | | ||
| getNavigationId | Select Navigation Id which automatically increments upon navigation | | ||
| getURL | Select current URL visible in the address bar (without hostname) | | ||
| getParams | Select Params (including Matrix Params) from all routes | | ||
| getQueryParams | Select global Query Params | | ||
| getFragment | Select global Fragment | | ||
| getData | Select static data from all routes | | ||
|
||
## Limitations | ||
|
||
If keys of multiple params are same, only child route param will be serialized because of limitation of `Typescript` / `Javascript`. e.g. If your path is | ||
|
||
``` | ||
path: 'folder/:id/mail/:id' | ||
``` | ||
|
||
and you visit `folder/inbox/mail/122458656`, your router state will only register | ||
|
||
``` | ||
params: { | ||
id: 122458656 | ||
} | ||
``` | ||
|
||
not `id: 'inbox'`. | ||
|
||
**Solution**: Be creative with key names and avoid duplicates. e.g. If your path is | ||
|
||
``` | ||
path: 'folder/:folderId/mail/:mailId | ||
``` | ||
|
||
and you visit `folder/inbox/mail/122458656`, your router state will properly register | ||
|
||
``` | ||
params: { | ||
folderId: 'inbox', | ||
mailId: 122458656 | ||
} | ||
``` | ||
|
||
. This limitation is also applicable to static `data` because it is also route-specific. | ||
|
||
Query Params and Fragment don't have such limitations because they are global to every route in the tree. | ||
|
||
## Interfaces | ||
|
||
`ss-ngrx-router-store` also exposes interfaces used by serialized state. You can use them to write type-safe codes. | ||
|
||
`SimpleSerializedRouterState` | ||
|
||
```typescript | ||
{ | ||
url: string; | ||
params: Params; | ||
queryParams: Params; | ||
fragment: string; | ||
data: Data; | ||
} | ||
``` | ||
|
||
(`Params` and `Data` are from `@angular/router`) | ||
|
||
`SSRouterPartialState` | ||
|
||
```typescript | ||
{ | ||
state: SimpleSerializedRouterState; | ||
navigationId: number; | ||
} | ||
``` | ||
|
||
## Component | ||
|
||
```html | ||
<ss-ngrx-router-store-state></ss-ngrx-router-store-state> | ||
``` | ||
|
||
will reactively print entire serialized router state on any page. You can use it for debugging purposes in case you are away from your dev environment and you don't have access to `Redux Devtools`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>Simple Serialized Ngrx Router Store</title> | ||
<base href="/" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<link rel="icon" type="image/x-icon" href="favicon.ico" /> | ||
</head> | ||
<body> | ||
<app-root></app-root> | ||
<script src="runtime-es2015.bf83fefcacda138fd53d.js" type="module"></script><script src="runtime-es5.bf83fefcacda138fd53d.js" nomodule defer></script><script src="polyfills-es5.8f3195ff1eee99ac5d2c.js" nomodule defer></script><script src="polyfills-es2015.2534ca07cb11e92b71b6.js" type="module"></script><script src="styles-es2015.5cbc75b609d01d0f6f0f.js" type="module"></script><script src="styles-es5.5cbc75b609d01d0f6f0f.js" nomodule defer></script><script src="vendor-es2015.2006f4633ea7ccaec7f5.js" type="module"></script><script src="vendor-es5.2006f4633ea7ccaec7f5.js" nomodule defer></script><script src="main-es2015.259f646c903c3bb009c6.js" type="module"></script><script src="main-es5.259f646c903c3bb009c6.js" nomodule defer></script></body> | ||
</html> |
Oops, something went wrong.