CM-42: Add Config & Error services (#6)

This commit is contained in:
Rinat Arsaev 2023-04-21 12:14:45 +04:00 committed by GitHub
parent 892390c5cc
commit d82b4eb11d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 129 additions and 1 deletions

View File

@ -16,3 +16,11 @@ jobs:
run: npm run format:check
- name: Build
run: npm run build
- name: Version up
working-directory: ./dist/ng-core
run: npm version prerelease --preid pr-${{github.event.number}}-${GITHUB_SHA::7} --no-git-tag-version
- name: Publish
working-directory: ./dist/ng-core
run: npm publish --tag pr
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

View File

@ -1,3 +1,35 @@
# Angular Libraries
- [Core](/projects/ng-core)
## 💻 Development with locally built/runnable library
1. Link the library
```sh
npm link ../ng-libs/dist/ng-core
```
1. Start Library
```sh
# cd ../ng-core
npm start
```
1. Start your app
```sh
# cd ../your-app
npm start
```
### 📦 Preparing a Pull Request in your application along with the library
To do this, you can use the version published in the NPM (with the `pr` tag) from your PR to the library:
```sh
npm i --save-exact @vality/ng-core@pr
```
_The latest version with the tag can also be [viewed in NPM](https://www.npmjs.com/package/@vality/ng-core?activeTab=versions)_

View File

@ -1,6 +1,6 @@
{
"name": "@vality/ng-core",
"version": "0.1.0",
"version": "0.2.0",
"publishConfig": {
"access": "public"
},

View File

@ -1 +1,2 @@
export * from './components';
export * from './services';

View File

@ -0,0 +1,14 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { lastValueFrom } from 'rxjs';
@Injectable()
export class ConfigServiceSuperclass<T> {
config!: T;
constructor(private http: HttpClient) {}
async init({ configUrl }: { configUrl: string }): Promise<void> {
this.config = await lastValueFrom(this.http.get<T>(configUrl));
}
}

View File

@ -0,0 +1 @@
export * from './config.service';

View File

@ -0,0 +1,9 @@
import { NgModule } from '@angular/core';
import { NotificationErrorService } from './notification-error.service';
import { MatSnackBarModule } from '@angular/material/snack-bar';
@NgModule({
imports: [MatSnackBarModule],
providers: [NotificationErrorService],
})
export class ErrorModule {}

View File

@ -0,0 +1,3 @@
export * from './notification-error.service';
export * from './utils/handle-error';
export * from './error.module';

View File

@ -0,0 +1,39 @@
import { Injectable } from '@angular/core';
import { MatSnackBar } from '@angular/material/snack-bar';
import { ErrorService } from './types/error-service';
const DEFAULT_DURATION_MS = 6000;
@Injectable({
providedIn: 'root',
})
export class NotificationErrorService implements ErrorService {
constructor(private snackBar: MatSnackBar) {}
error = (error: unknown, clientMessage?: string): void => {
const name = String((error as { name: unknown })?.name ?? '');
const message = String((error as { message: unknown })?.message ?? '');
const result = clientMessage || message || name || 'Unknown error';
const other =
error && typeof error === 'object'
? Object.fromEntries(
Object.entries(error).filter(([k, v]) => k !== 'name' && k !== 'message' && v)
)
: {};
console.warn(
[
`Caught error: ${clientMessage || 'Unknown error'}.`,
name && `Name: ${name}.`,
message && `Message: ${message}.`,
Object.keys(other) && JSON.stringify(other, null, 2),
]
.filter(Boolean)
.join('\n')
);
this.snackBar.open(result, 'OK', {
duration: DEFAULT_DURATION_MS,
});
};
}

View File

@ -0,0 +1,3 @@
export interface ErrorService {
error: (error: unknown, message?: string) => void;
}

View File

@ -0,0 +1,16 @@
import { Observable, EMPTY } from 'rxjs';
import { catchError } from 'rxjs/operators';
export function handleError<T>(
errorHandler: (error: unknown, message?: string) => void,
message?: string,
result: Observable<T> = EMPTY
) {
return (source: Observable<T>): Observable<T> =>
source.pipe(
catchError((err) => {
errorHandler(err, message);
return result;
})
);
}

View File

@ -0,0 +1,2 @@
export * from './config';
export * from './error';