@tstdl/base 0.90.0 → 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,7 +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';
15
- import type { UnsupportedMediaTypeError } from './unsupported-media-type.error.js';
18
+ type ErrorLocalizationEntry<T extends CustomError> = {
19
+ header: LocalizeItem<T>;
20
+ message?: LocalizeItem<T>;
21
+ };
22
+ export type ErrorsLocalizationEntries<T extends CustomErrorStatic[]> = {
23
+ [ErrorName in T[number]['errorName']]: ErrorLocalizationEntry<InstanceType<Extract<T[number], {
24
+ errorName: ErrorName;
25
+ }>>>;
26
+ };
27
+ export type ErrorsLocalization<T extends CustomErrorStatic[] = CustomErrorStatic[], Enums extends Enumeration[] = Enumeration[]> = Localization<{
28
+ errors: ErrorsLocalizationEntries<T>;
29
+ }, Enums>;
16
30
  type TstdlErrors = [
17
31
  typeof ApiError,
18
32
  typeof BadRequestError,
@@ -26,19 +40,12 @@ type TstdlErrors = [
26
40
  typeof NotSupportedError,
27
41
  typeof TimeoutError,
28
42
  typeof UnauthorizedError,
29
- typeof UnsupportedMediaTypeError
43
+ typeof HttpError,
44
+ typeof SecretRequirementsError
30
45
  ];
31
- export type ErrorsLocalizationEntries<T extends CustomErrorStatic[]> = {
32
- [I in Extract<keyof T, number> as TstdlErrors[I]['errorName']]: {
33
- header: LocalizeItem<InstanceType<T[I]>>;
34
- message?: LocalizeItem<InstanceType<T[I]>>;
35
- };
36
- };
37
- export type ErrorsLocalization<T extends CustomErrorStatic[] = CustomErrorStatic[]> = Localization<{
38
- errors: ErrorsLocalizationEntries<T>;
39
- }>;
40
46
  export declare const errorsLocalizationKeys: import("../text/localization.service.js").ProxyLocalizationKeys<{
41
- errors: ErrorsLocalizationEntries<CustomErrorStatic<import("./custom.error.js").CustomError>[]>;
47
+ errors: ErrorsLocalizationEntries<CustomErrorStatic<CustomError>[]>;
42
48
  }>;
43
- export declare const germanTstdlErrorsLocalization: ErrorsLocalization<TstdlErrors>;
49
+ export declare const germanTstdlErrorsLocalization: ErrorsLocalization<TstdlErrors, [typeof HttpErrorReason]>;
50
+ export declare const englishTstdlErrorsLocalization: ErrorsLocalization<TstdlErrors>;
44
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' },
@@ -21,7 +23,7 @@ export const germanTstdlErrorsLocalization = {
21
23
  message: getErrorMessage
22
24
  },
23
25
  InvalidTokenError: {
24
- header: 'Anmeldung ungültig oder abgelaufen',
26
+ header: 'Anmeldung abgelaufen oder ungültig',
25
27
  message: getErrorMessage
26
28
  },
27
29
  MaxBytesExceededError: {
@@ -29,7 +31,7 @@ export const germanTstdlErrorsLocalization = {
29
31
  message: getErrorMessage
30
32
  },
31
33
  MethodNotAllowedError: {
32
- header: 'Ungültige Anfrage',
34
+ header: 'Anfrage nicht erlaubt',
33
35
  message: getErrorMessage
34
36
  },
35
37
  NotFoundError: {
@@ -52,13 +54,105 @@ export const germanTstdlErrorsLocalization = {
52
54
  header: 'Unautorisiert',
53
55
  message: getErrorMessage
54
56
  },
55
- UnsupportedMediaTypeError: {
56
- header: '',
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
66
+ }
67
+ }
68
+ },
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
+ ]
80
+ };
81
+ export const englishTstdlErrorsLocalization = {
82
+ language: { code: 'en', name: 'English' },
83
+ keys: {
84
+ errors: {
85
+ ApiError: {
86
+ header: 'API error',
87
+ message: getErrorMessage
88
+ },
89
+ BadRequestError: {
90
+ header: 'Bad Request',
91
+ message: getErrorMessage
92
+ },
93
+ ForbiddenError: {
94
+ header: 'Access forbidden',
95
+ message: getErrorMessage
96
+ },
97
+ InvalidCredentialsError: {
98
+ header: 'Invalid credentials',
99
+ message: getErrorMessage
100
+ },
101
+ InvalidTokenError: {
102
+ header: 'Login expired or invalid',
103
+ message: getErrorMessage
104
+ },
105
+ MaxBytesExceededError: {
106
+ header: 'Data larger than allowed',
107
+ message: getErrorMessage
108
+ },
109
+ MethodNotAllowedError: {
110
+ header: 'Request not allowed',
111
+ message: getErrorMessage
112
+ },
113
+ NotFoundError: {
114
+ header: 'Not found',
115
+ message: getErrorMessage
116
+ },
117
+ NotImplementedError: {
118
+ header: 'Function not implemented',
119
+ message: getErrorMessage
120
+ },
121
+ NotSupportedError: {
122
+ header: 'Not supported',
123
+ message: getErrorMessage
124
+ },
125
+ TimeoutError: {
126
+ header: 'Timeout',
127
+ message: getErrorMessage
128
+ },
129
+ UnauthorizedError: {
130
+ header: 'Unauthorized',
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',
57
141
  message: getErrorMessage
58
142
  }
59
143
  }
60
144
  },
61
- 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
+ ]
62
156
  };
63
157
  function getErrorMessage(error) {
64
158
  return `${error.message}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tstdl/base",
3
- "version": "0.90.0",
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 : {});