Skip to content

Commit

Permalink
Merge branch 'release/1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
SachinShekhar committed Feb 11, 2020
2 parents ebc765d + 0ac2b50 commit 42a8f22
Show file tree
Hide file tree
Showing 57 changed files with 216,880 additions and 32 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/build.yml
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
15 changes: 15 additions & 0 deletions .github/workflows/lint.yml
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
38 changes: 38 additions & 0 deletions .github/workflows/publish.yml
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}}
Empty file added .nojekyll
Empty file.
3 changes: 3 additions & 0 deletions CHANGELOG.md
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.
7 changes: 7 additions & 0 deletions LICENSE
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.
183 changes: 182 additions & 1 deletion README.md
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`.
40 changes: 39 additions & 1 deletion angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/ss-ngrx-router-store-library",
"outputPath": "docs",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
Expand Down Expand Up @@ -107,6 +107,44 @@
}
}
}
},
"ss-ngrx-router-store": {
"projectType": "library",
"root": "projects/ss-ngrx-router-store",
"sourceRoot": "projects/ss-ngrx-router-store/src",
"prefix": "ss",
"architect": {
"build": {
"builder": "@angular-devkit/build-ng-packagr:build",
"options": {
"tsConfig": "projects/ss-ngrx-router-store/tsconfig.lib.json",
"project": "projects/ss-ngrx-router-store/ng-package.json"
},
"configurations": {
"production": {
"tsConfig": "projects/ss-ngrx-router-store/tsconfig.lib.prod.json"
}
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "projects/ss-ngrx-router-store/src/test.ts",
"tsConfig": "projects/ss-ngrx-router-store/tsconfig.spec.json",
"karmaConfig": "projects/ss-ngrx-router-store/karma.conf.js"
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"projects/ss-ngrx-router-store/tsconfig.lib.json",
"projects/ss-ngrx-router-store/tsconfig.spec.json"
],
"exclude": ["**/node_modules/**"]
}
}
}
}
},
"defaultProject": "ss-ngrx-router-store-library",
Expand Down
Binary file added demo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file added docs/.nojekyll
Empty file.
Binary file added docs/favicon.ico
Binary file not shown.
13 changes: 13 additions & 0 deletions docs/index.html
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>

0 comments on commit 42a8f22

Please sign in to comment.