-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
3994673
commit ad967b8
Showing
5 changed files
with
49 additions
and
2 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
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,35 @@ | ||
import { BackendConfig } from '../../shared/config/BackendConfig.js'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { HttpService } from '@nestjs/axios'; | ||
import { UserParams } from '../ui-backend/api/user.params.js'; | ||
import { catchError, firstValueFrom, map } from 'rxjs'; | ||
import { AxiosError, AxiosResponse } from 'axios'; | ||
import { Injectable, UnauthorizedException } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class BackendService { | ||
private backendConfig: BackendConfig; | ||
|
||
private backendServerBasePath: string; | ||
|
||
public constructor(configService: ConfigService, private httpService: HttpService) { | ||
this.backendConfig = configService.getOrThrow<BackendConfig>('BACKEND'); | ||
this.backendServerBasePath = `http://${this.backendConfig.SERVER}:${this.backendConfig.PORT}`; | ||
} | ||
|
||
public login(userParams: UserParams): Promise<string> { | ||
return firstValueFrom( | ||
this.httpService | ||
.post(`${this.backendServerBasePath}/api/login`, userParams) | ||
.pipe( | ||
catchError((e: AxiosError) => { | ||
if ((e.status == 404)) { | ||
throw new UnauthorizedException(); | ||
} | ||
throw e; | ||
}), | ||
) | ||
.pipe(map((v: AxiosResponse<string>) => v.data)), | ||
); | ||
} | ||
} |
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
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,11 @@ | ||
import {IsInt, IsNotEmpty, IsString, Min} from 'class-validator'; | ||
|
||
export class BackendConfig { | ||
@IsString() | ||
@IsNotEmpty() | ||
public readonly SERVER!: string; | ||
|
||
@IsInt() | ||
@Min(1) | ||
public readonly PORT!: number; | ||
} |
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