@tstdl/base 0.90.1 → 0.90.2

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.
@@ -1,7 +1,10 @@
1
+ import { SecretRequirementsError } from '../authentication/index.js';
2
+ import { HttpError, HttpErrorReason } from '../http/http.error.js';
1
3
  import type { Localization, LocalizeItem } from '../text/localization.service.js';
4
+ import { Enumeration } from '../types.js';
2
5
  import type { ApiError } from './api.error.js';
3
6
  import type { BadRequestError } from './bad-request.error.js';
4
- import type { CustomErrorStatic } from './custom.error.js';
7
+ import type { CustomError, CustomErrorStatic } from './custom.error.js';
5
8
  import type { ForbiddenError } from './forbidden.error.js';
6
9
  import type { InvalidCredentialsError } from './invalid-credentials.error.js';
7
10
  import type { InvalidTokenError } from './invalid-token.error.js';
@@ -12,15 +15,18 @@ import type { NotImplementedError } from './not-implemented.error.js';
12
15
  import type { NotSupportedError } from './not-supported.error.js';
13
16
  import type { TimeoutError } from './timeout.error.js';
14
17
  import type { UnauthorizedError } from './unauthorized.error.js';
18
+ type ErrorLocalizationEntry<T extends CustomError> = {
19
+ header: LocalizeItem<T>;
20
+ message?: LocalizeItem<T>;
21
+ };
15
22
  export type ErrorsLocalizationEntries<T extends CustomErrorStatic[]> = {
16
- [I in Extract<keyof T, number> as T[I]['errorName']]: {
17
- header: LocalizeItem<InstanceType<T[I]>>;
18
- message?: LocalizeItem<InstanceType<T[I]>>;
19
- };
23
+ [ErrorName in T[number]['errorName']]: ErrorLocalizationEntry<InstanceType<Extract<T[number], {
24
+ errorName: ErrorName;
25
+ }>>>;
20
26
  };
21
- export type ErrorsLocalization<T extends CustomErrorStatic[] = CustomErrorStatic[]> = Localization<{
27
+ export type ErrorsLocalization<T extends CustomErrorStatic[] = CustomErrorStatic[], Enums extends Enumeration[] = Enumeration[]> = Localization<{
22
28
  errors: ErrorsLocalizationEntries<T>;
23
- }>;
29
+ }, Enums>;
24
30
  type TstdlErrors = [
25
31
  typeof ApiError,
26
32
  typeof BadRequestError,
@@ -33,11 +39,13 @@ type TstdlErrors = [
33
39
  typeof NotImplementedError,
34
40
  typeof NotSupportedError,
35
41
  typeof TimeoutError,
36
- typeof UnauthorizedError
42
+ typeof UnauthorizedError,
43
+ typeof HttpError,
44
+ typeof SecretRequirementsError
37
45
  ];
38
46
  export declare const errorsLocalizationKeys: import("../text/localization.service.js").ProxyLocalizationKeys<{
39
- errors: ErrorsLocalizationEntries<CustomErrorStatic<import("./custom.error.js").CustomError>[]>;
47
+ errors: ErrorsLocalizationEntries<CustomErrorStatic<CustomError>[]>;
40
48
  }>;
41
- export declare const germanTstdlErrorsLocalization: ErrorsLocalization<TstdlErrors>;
49
+ export declare const germanTstdlErrorsLocalization: ErrorsLocalization<TstdlErrors, [typeof HttpErrorReason]>;
42
50
  export declare const englishTstdlErrorsLocalization: ErrorsLocalization<TstdlErrors>;
43
51
  export {};
@@ -1,4 +1,6 @@
1
- import { getLocalizationKeys } from '../text/localization.service.js';
1
+ import { HttpErrorReason } from '../http/http.error.js';
2
+ import { enumerationLocalization, getLocalizationKeys } from '../text/localization.service.js';
3
+ import { isDefined } from '../utils/type-guards.js';
2
4
  export const errorsLocalizationKeys = getLocalizationKeys();
3
5
  export const germanTstdlErrorsLocalization = {
4
6
  language: { code: 'de', name: 'Deutsch' },
@@ -51,10 +53,30 @@ export const germanTstdlErrorsLocalization = {
51
53
  UnauthorizedError: {
52
54
  header: 'Unautorisiert',
53
55
  message: getErrorMessage
56
+ },
57
+ HttpError: {
58
+ header: (error) => (isDefined(error.response)
59
+ ? `HTTP Fehler ${error.response.statusCode.toString()}`
60
+ : 'HTTP Fehler'),
61
+ message: (error, context) => context.localizationService.localizeOnce({ enum: HttpErrorReason, value: error.reason })
62
+ },
63
+ SecretRequirementsError: {
64
+ header: 'Passwortanforderungen nicht erfüllt',
65
+ message: getErrorMessage
54
66
  }
55
67
  }
56
68
  },
57
- enums: []
69
+ enums: [
70
+ enumerationLocalization(HttpErrorReason, {
71
+ [HttpErrorReason.Unknown]: 'Unbekannt',
72
+ [HttpErrorReason.Cancelled]: 'Anfrage abgebrochen',
73
+ [HttpErrorReason.InvalidRequest]: 'Ungültige Anfrage',
74
+ [HttpErrorReason.Non200StatusCode]: 'Antwort enthielt einen Fehler',
75
+ [HttpErrorReason.ErrorResponse]: 'Antwort enthielt einen Fehler',
76
+ [HttpErrorReason.ResponseError]: 'Fehler beim Empfang der Antwort',
77
+ [HttpErrorReason.Timeout]: 'Zeitüberschreitung'
78
+ })
79
+ ]
58
80
  };
59
81
  export const englishTstdlErrorsLocalization = {
60
82
  language: { code: 'en', name: 'English' },
@@ -107,10 +129,30 @@ export const englishTstdlErrorsLocalization = {
107
129
  UnauthorizedError: {
108
130
  header: 'Unauthorized',
109
131
  message: getErrorMessage
132
+ },
133
+ HttpError: {
134
+ header: (error) => (isDefined(error.response)
135
+ ? `HTTP error ${error.response.statusCode.toString()}`
136
+ : 'HTTP error'),
137
+ message: (error, context) => context.localizationService.localizeOnce({ enum: HttpErrorReason, value: error.reason })
138
+ },
139
+ SecretRequirementsError: {
140
+ header: 'Secret requirements not met',
141
+ message: getErrorMessage
110
142
  }
111
143
  }
112
144
  },
113
- enums: []
145
+ enums: [
146
+ enumerationLocalization(HttpErrorReason, {
147
+ [HttpErrorReason.Unknown]: 'Unknown',
148
+ [HttpErrorReason.Cancelled]: 'Request cancelled',
149
+ [HttpErrorReason.InvalidRequest]: 'Invalid request',
150
+ [HttpErrorReason.Non200StatusCode]: 'Response contained an error',
151
+ [HttpErrorReason.ErrorResponse]: 'Response contained an error',
152
+ [HttpErrorReason.ResponseError]: 'Error while receiving the response',
153
+ [HttpErrorReason.Timeout]: 'Zeitüberschreitung'
154
+ })
155
+ ]
114
156
  };
115
157
  function getErrorMessage(error) {
116
158
  return `${error.message}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tstdl/base",
3
- "version": "0.90.1",
3
+ "version": "0.90.2",
4
4
  "author": "Patrick Hein",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -7,7 +7,10 @@ export type Language = {
7
7
  code: string;
8
8
  name: string;
9
9
  };
10
- export type LocalizeFunction<Parameters = void> = (parameters: Parameters) => string;
10
+ export type LocalizeFunctionContext = {
11
+ localizationService: LocalizationService;
12
+ };
13
+ export type LocalizeFunction<Parameters = void> = (parameters: Parameters, context: LocalizeFunctionContext) => string;
11
14
  export type LocalizeItem<Parameters = void> = string | LocalizeFunction<Parameters>;
12
15
  type LocalizationTemplate = {
13
16
  [key: string]: LocalizeItem<any> | LocalizationTemplate;
@@ -163,7 +163,7 @@ let LocalizationService = class LocalizationService {
163
163
  return `__${key}__`;
164
164
  }
165
165
  if (isFunction(templateOrFunction)) {
166
- return templateOrFunction(parameters);
166
+ return templateOrFunction(parameters, { localizationService: this });
167
167
  }
168
168
  const template = templateOrFunction;
169
169
  const templateParameters = ((isNotNull(parameters) && isObject(parameters)) ? parameters : {});