From d82b4eb11d46a9c59c6e18b397ffaef322a89ea4 Mon Sep 17 00:00:00 2001 From: Rinat Arsaev <11846445+A77AY@users.noreply.github.com> Date: Fri, 21 Apr 2023 12:14:45 +0400 Subject: [PATCH] CM-42: Add Config & Error services (#6) --- .github/workflows/pr.yaml | 8 ++++ README.md | 32 +++++++++++++++ projects/ng-core/package.json | 2 +- projects/ng-core/src/lib/index.ts | 1 + .../src/lib/services/config/config.service.ts | 14 +++++++ .../ng-core/src/lib/services/config/index.ts | 1 + .../src/lib/services/error/error.module.ts | 9 +++++ .../ng-core/src/lib/services/error/index.ts | 3 ++ .../error/notification-error.service.ts | 39 +++++++++++++++++++ .../lib/services/error/types/error-service.ts | 3 ++ .../lib/services/error/utils/handle-error.ts | 16 ++++++++ projects/ng-core/src/lib/services/index.ts | 2 + 12 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 projects/ng-core/src/lib/services/config/config.service.ts create mode 100644 projects/ng-core/src/lib/services/config/index.ts create mode 100644 projects/ng-core/src/lib/services/error/error.module.ts create mode 100644 projects/ng-core/src/lib/services/error/index.ts create mode 100644 projects/ng-core/src/lib/services/error/notification-error.service.ts create mode 100644 projects/ng-core/src/lib/services/error/types/error-service.ts create mode 100644 projects/ng-core/src/lib/services/error/utils/handle-error.ts create mode 100644 projects/ng-core/src/lib/services/index.ts diff --git a/.github/workflows/pr.yaml b/.github/workflows/pr.yaml index 4c3402d..11eacd1 100644 --- a/.github/workflows/pr.yaml +++ b/.github/workflows/pr.yaml @@ -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 }} diff --git a/README.md b/README.md index 9d2d675..dfc74c6 100644 --- a/README.md +++ b/README.md @@ -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)_ diff --git a/projects/ng-core/package.json b/projects/ng-core/package.json index 6e54ec9..37ae1a9 100644 --- a/projects/ng-core/package.json +++ b/projects/ng-core/package.json @@ -1,6 +1,6 @@ { "name": "@vality/ng-core", - "version": "0.1.0", + "version": "0.2.0", "publishConfig": { "access": "public" }, diff --git a/projects/ng-core/src/lib/index.ts b/projects/ng-core/src/lib/index.ts index 07635cb..80e3bea 100644 --- a/projects/ng-core/src/lib/index.ts +++ b/projects/ng-core/src/lib/index.ts @@ -1 +1,2 @@ export * from './components'; +export * from './services'; diff --git a/projects/ng-core/src/lib/services/config/config.service.ts b/projects/ng-core/src/lib/services/config/config.service.ts new file mode 100644 index 0000000..308e06b --- /dev/null +++ b/projects/ng-core/src/lib/services/config/config.service.ts @@ -0,0 +1,14 @@ +import { HttpClient } from '@angular/common/http'; +import { Injectable } from '@angular/core'; +import { lastValueFrom } from 'rxjs'; + +@Injectable() +export class ConfigServiceSuperclass { + config!: T; + + constructor(private http: HttpClient) {} + + async init({ configUrl }: { configUrl: string }): Promise { + this.config = await lastValueFrom(this.http.get(configUrl)); + } +} diff --git a/projects/ng-core/src/lib/services/config/index.ts b/projects/ng-core/src/lib/services/config/index.ts new file mode 100644 index 0000000..9039225 --- /dev/null +++ b/projects/ng-core/src/lib/services/config/index.ts @@ -0,0 +1 @@ +export * from './config.service'; diff --git a/projects/ng-core/src/lib/services/error/error.module.ts b/projects/ng-core/src/lib/services/error/error.module.ts new file mode 100644 index 0000000..36932f1 --- /dev/null +++ b/projects/ng-core/src/lib/services/error/error.module.ts @@ -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 {} diff --git a/projects/ng-core/src/lib/services/error/index.ts b/projects/ng-core/src/lib/services/error/index.ts new file mode 100644 index 0000000..81c89f9 --- /dev/null +++ b/projects/ng-core/src/lib/services/error/index.ts @@ -0,0 +1,3 @@ +export * from './notification-error.service'; +export * from './utils/handle-error'; +export * from './error.module'; diff --git a/projects/ng-core/src/lib/services/error/notification-error.service.ts b/projects/ng-core/src/lib/services/error/notification-error.service.ts new file mode 100644 index 0000000..01bac99 --- /dev/null +++ b/projects/ng-core/src/lib/services/error/notification-error.service.ts @@ -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, + }); + }; +} diff --git a/projects/ng-core/src/lib/services/error/types/error-service.ts b/projects/ng-core/src/lib/services/error/types/error-service.ts new file mode 100644 index 0000000..615a368 --- /dev/null +++ b/projects/ng-core/src/lib/services/error/types/error-service.ts @@ -0,0 +1,3 @@ +export interface ErrorService { + error: (error: unknown, message?: string) => void; +} diff --git a/projects/ng-core/src/lib/services/error/utils/handle-error.ts b/projects/ng-core/src/lib/services/error/utils/handle-error.ts new file mode 100644 index 0000000..10ca278 --- /dev/null +++ b/projects/ng-core/src/lib/services/error/utils/handle-error.ts @@ -0,0 +1,16 @@ +import { Observable, EMPTY } from 'rxjs'; +import { catchError } from 'rxjs/operators'; + +export function handleError( + errorHandler: (error: unknown, message?: string) => void, + message?: string, + result: Observable = EMPTY +) { + return (source: Observable): Observable => + source.pipe( + catchError((err) => { + errorHandler(err, message); + return result; + }) + ); +} diff --git a/projects/ng-core/src/lib/services/index.ts b/projects/ng-core/src/lib/services/index.ts new file mode 100644 index 0000000..3423d2b --- /dev/null +++ b/projects/ng-core/src/lib/services/index.ts @@ -0,0 +1,2 @@ +export * from './config'; +export * from './error';