mirror of
https://github.com/valitydev/ng-libs.git
synced 2024-11-06 00:35:21 +00:00
CM-42: Add Config & Error services (#6)
This commit is contained in:
parent
892390c5cc
commit
d82b4eb11d
8
.github/workflows/pr.yaml
vendored
8
.github/workflows/pr.yaml
vendored
@ -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 }}
|
||||
|
32
README.md
32
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)_
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@vality/ng-core",
|
||||
"version": "0.1.0",
|
||||
"version": "0.2.0",
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
|
@ -1 +1,2 @@
|
||||
export * from './components';
|
||||
export * from './services';
|
||||
|
14
projects/ng-core/src/lib/services/config/config.service.ts
Normal file
14
projects/ng-core/src/lib/services/config/config.service.ts
Normal 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));
|
||||
}
|
||||
}
|
1
projects/ng-core/src/lib/services/config/index.ts
Normal file
1
projects/ng-core/src/lib/services/config/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './config.service';
|
9
projects/ng-core/src/lib/services/error/error.module.ts
Normal file
9
projects/ng-core/src/lib/services/error/error.module.ts
Normal 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 {}
|
3
projects/ng-core/src/lib/services/error/index.ts
Normal file
3
projects/ng-core/src/lib/services/error/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export * from './notification-error.service';
|
||||
export * from './utils/handle-error';
|
||||
export * from './error.module';
|
@ -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,
|
||||
});
|
||||
};
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
export interface ErrorService {
|
||||
error: (error: unknown, message?: string) => void;
|
||||
}
|
@ -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;
|
||||
})
|
||||
);
|
||||
}
|
2
projects/ng-core/src/lib/services/index.ts
Normal file
2
projects/ng-core/src/lib/services/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from './config';
|
||||
export * from './error';
|
Loading…
Reference in New Issue
Block a user