dl-commons 0.0.1-testing.2 → 0.0.1-testing.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dl-commons",
3
3
  "type": "commonjs",
4
- "version": "0.0.1-testing.2",
4
+ "version": "0.0.1-testing.3",
5
5
  "description": "distance learning commons",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -6,6 +6,7 @@ import { DL_SESSIONS__MONGO__CS } from './models/consts/env-vars.const';
6
6
  import { Session, SessionSchema } from './models/schemas/session.schema';
7
7
  import { HttpModule } from '@nestjs/axios';
8
8
  import { ISession } from './models/interfaces/session.interface';
9
+ import { DictionariesService } from './services/dictionaries.service';
9
10
 
10
11
  declare global {
11
12
  namespace Express {
@@ -29,7 +30,7 @@ declare global {
29
30
  MongooseModule.forFeature([{ name: Session.name, schema: SessionSchema }], 'sessions-store'),
30
31
  ],
31
32
  controllers: [],
32
- providers: [SessionsService],
33
- exports: [SessionsService],
33
+ providers: [SessionsService, DictionariesService],
34
+ exports: [SessionsService, DictionariesService],
34
35
  })
35
36
  export class DLCommonsModule {}
@@ -0,0 +1,57 @@
1
+ import { HttpService } from '@nestjs/axios';
2
+ import { Injectable } from '@nestjs/common';
3
+ import { ConfigService } from '@nestjs/config';
4
+ import { RESTResponseBody, RESTError } from '../models/dtos/wrappers/rest-wrapper.class';
5
+ import { firstValueFrom } from 'rxjs';
6
+ import { IDictionary } from '../models/interfaces/dictionary.interface';
7
+
8
+ @Injectable()
9
+ export class DictionariesService {
10
+ //#region Private Members
11
+ private _baseUrl: string;
12
+ //#endregion
13
+
14
+ //#region Constructor
15
+ constructor(private readonly _httpService: HttpService, private readonly _configService: ConfigService) {
16
+ this._baseUrl = this._configService.getOrThrow('DL_DICTIONARIES__REST__BASE_URL');
17
+ }
18
+ //#endregion
19
+
20
+ //#region Public API
21
+ public async fetchById(id: string) {
22
+ try {
23
+ const endpoint = `${this._baseUrl}/v1/dictionaries/by-id/${id}`;
24
+ const response$ = this._httpService.get<RESTResponseBody<IDictionary>>(endpoint);
25
+ const response = await firstValueFrom(response$);
26
+
27
+ if (response.data.errors) {
28
+ throw new RESTError('dictionaries_api_error', response.data.errors);
29
+ }
30
+ if (!response.data || !response.data.result) {
31
+ throw new RESTError('dictionaries_api_error', 'empty response');
32
+ }
33
+ return response.data.result.data;
34
+ } catch (e) {
35
+ throw RESTError.createFromCatch(e, { keyword: 'unknown', debug: e });
36
+ }
37
+ }
38
+
39
+ public async fetchByKeyword(keyword: string) {
40
+ try {
41
+ const endpoint = `${this._baseUrl}/v1/dictionaries/by-keyword/${keyword}`;
42
+ const response$ = this._httpService.get<RESTResponseBody<IDictionary>>(endpoint);
43
+ const response = await firstValueFrom(response$);
44
+
45
+ if (response.data.errors) {
46
+ throw new RESTError('dictionaries_api_error', response.data.errors);
47
+ }
48
+ if (!response.data || !response.data.result) {
49
+ throw new RESTError('dictionaries_api_error', 'empty response');
50
+ }
51
+ return response.data.result.data;
52
+ } catch (e) {
53
+ throw RESTError.createFromCatch(e, { keyword: 'unknown', debug: e });
54
+ }
55
+ }
56
+ //#endregion
57
+ }