@tstdl/base 0.90.4 → 0.90.6

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,8 @@
1
- import { SecretRequirementsError } from '../authentication/index.js';
2
- import { HttpError, HttpErrorReason } from '../http/http.error.js';
1
+ import type { SecretRequirementsError } from '../authentication/index.js';
2
+ import type { HttpError } from '../http/http.error.js';
3
+ import { HttpErrorReason } from '../http/http.error.js';
3
4
  import type { Localization, LocalizeItem } from '../text/localization.service.js';
4
- import { Enumeration } from '../types.js';
5
+ import type { Enumeration } from '../types.js';
5
6
  import type { ApiError } from './api.error.js';
6
7
  import type { BadRequestError } from './bad-request.error.js';
7
8
  import type { CustomError, CustomErrorStatic } from './custom.error.js';
@@ -1,6 +1,6 @@
1
1
  import { HttpErrorReason } from '../http/http.error.js';
2
2
  import { enumerationLocalization, getLocalizationKeys } from '../text/localization.service.js';
3
- import { isDefined } from '../utils/type-guards.js';
3
+ import { isDefined, isNotNull } from '../utils/type-guards.js';
4
4
  export const errorsLocalizationKeys = getLocalizationKeys();
5
5
  export const germanTstdlErrorsLocalization = {
6
6
  language: { code: 'de', name: 'Deutsch' },
@@ -8,7 +8,7 @@ export const germanTstdlErrorsLocalization = {
8
8
  errors: {
9
9
  ApiError: {
10
10
  header: 'API Fehler',
11
- message: getErrorMessage
11
+ message: 'Bitte versuchen Sie es später noch einmal.'
12
12
  },
13
13
  BadRequestError: {
14
14
  header: 'Ungültige Anfrage',
@@ -20,11 +20,11 @@ export const germanTstdlErrorsLocalization = {
20
20
  },
21
21
  InvalidCredentialsError: {
22
22
  header: 'Ungültige Zugangsdaten',
23
- message: getErrorMessage
23
+ message: 'Überprüfen Sie Ihre Eingaben'
24
24
  },
25
25
  InvalidTokenError: {
26
26
  header: 'Anmeldung abgelaufen oder ungültig',
27
- message: getErrorMessage
27
+ message: 'Bitte melden Sie sich erneut an'
28
28
  },
29
29
  MaxBytesExceededError: {
30
30
  header: 'Daten größer als erlaubt',
@@ -58,7 +58,7 @@ export const germanTstdlErrorsLocalization = {
58
58
  header: (error) => ((isDefined(error.response) && error.response.statusCode != 0)
59
59
  ? `Http Fehler - ${error.response.statusCode.toString()}`
60
60
  : 'Http Fehler'),
61
- message: (error, context) => context.localizationService.localizeOnce({ enum: HttpErrorReason, value: error.reason })
61
+ message: getHttpErrorMessage
62
62
  },
63
63
  SecretRequirementsError: {
64
64
  header: 'Passwortanforderungen nicht erfüllt',
@@ -70,8 +70,9 @@ export const germanTstdlErrorsLocalization = {
70
70
  enumerationLocalization(HttpErrorReason, {
71
71
  [HttpErrorReason.Unknown]: 'Unbekannt',
72
72
  [HttpErrorReason.Cancelled]: 'Anfrage abgebrochen',
73
+ [HttpErrorReason.Network]: 'Netzwerkfehler',
73
74
  [HttpErrorReason.InvalidRequest]: 'Ungültige Anfrage',
74
- [HttpErrorReason.Non200StatusCode]: 'Antwort enthielt einen Fehler',
75
+ [HttpErrorReason.StatusCode]: 'Antwort enthielt einen Fehler',
75
76
  [HttpErrorReason.ErrorResponse]: 'Antwort enthielt einen Fehler',
76
77
  [HttpErrorReason.ResponseError]: 'Fehler beim Empfang der Antwort',
77
78
  [HttpErrorReason.Timeout]: 'Zeitüberschreitung'
@@ -84,7 +85,7 @@ export const englishTstdlErrorsLocalization = {
84
85
  errors: {
85
86
  ApiError: {
86
87
  header: 'API error',
87
- message: getErrorMessage
88
+ message: 'Please try again later'
88
89
  },
89
90
  BadRequestError: {
90
91
  header: 'Bad Request',
@@ -96,11 +97,11 @@ export const englishTstdlErrorsLocalization = {
96
97
  },
97
98
  InvalidCredentialsError: {
98
99
  header: 'Invalid credentials',
99
- message: getErrorMessage
100
+ message: 'Check your inputs'
100
101
  },
101
102
  InvalidTokenError: {
102
103
  header: 'Login expired or invalid',
103
- message: getErrorMessage
104
+ message: 'Please log in again'
104
105
  },
105
106
  MaxBytesExceededError: {
106
107
  header: 'Data larger than allowed',
@@ -134,7 +135,7 @@ export const englishTstdlErrorsLocalization = {
134
135
  header: (error) => ((isDefined(error.response) && error.response.statusCode != 0)
135
136
  ? `Http error - ${error.response.statusCode.toString()}`
136
137
  : 'Http error'),
137
- message: (error, context) => context.localizationService.localizeOnce({ enum: HttpErrorReason, value: error.reason })
138
+ message: getHttpErrorMessage
138
139
  },
139
140
  SecretRequirementsError: {
140
141
  header: 'Secret requirements not met',
@@ -146,14 +147,20 @@ export const englishTstdlErrorsLocalization = {
146
147
  enumerationLocalization(HttpErrorReason, {
147
148
  [HttpErrorReason.Unknown]: 'Unknown',
148
149
  [HttpErrorReason.Cancelled]: 'Request cancelled',
150
+ [HttpErrorReason.Network]: 'Network error',
149
151
  [HttpErrorReason.InvalidRequest]: 'Invalid request',
150
- [HttpErrorReason.Non200StatusCode]: 'Response contained an error',
152
+ [HttpErrorReason.StatusCode]: 'Response contained an error',
151
153
  [HttpErrorReason.ErrorResponse]: 'Response contained an error',
152
154
  [HttpErrorReason.ResponseError]: 'Error while receiving the response',
153
155
  [HttpErrorReason.Timeout]: 'Zeitüberschreitung'
154
156
  })
155
157
  ]
156
158
  };
159
+ function getHttpErrorMessage(error, context) {
160
+ return (isDefined(error.response) && (isNotNull(error.response.statusMessage)))
161
+ ? error.response.statusMessage
162
+ : context.localizationService.localizeOnce({ enum: HttpErrorReason, value: error.reason });
163
+ }
157
164
  function getErrorMessage(error) {
158
- return `${error.message}`;
165
+ return error.message.replace(/\.$/u, '');
159
166
  }
@@ -66,9 +66,7 @@ let UndiciHttpClientAdapter = class UndiciHttpClientAdapter extends HttpClientAd
66
66
  }
67
67
  catch (error) {
68
68
  if (error instanceof undiciErrors.UndiciError) {
69
- const reason = ((error instanceof undiciErrors.BodyTimeoutError) || (error instanceof undiciErrors.HeadersTimeoutError)) ? HttpErrorReason.Timeout
70
- : (error instanceof undiciErrors.RequestAbortedError) ? HttpErrorReason.Cancelled
71
- : HttpErrorReason.Unknown;
69
+ const reason = getHttpErrorReason(error);
72
70
  throw new HttpError(reason, httpClientRequest, undefined, undefined, error);
73
71
  }
74
72
  throw error;
@@ -88,3 +86,32 @@ export function configureUndiciHttpClientAdapter(options = {}) {
88
86
  Injector.register(HttpClientAdapter, { useToken: UndiciHttpClientAdapter });
89
87
  }
90
88
  }
89
+ function getHttpErrorReason(error) {
90
+ switch (error.code) {
91
+ case 'UND_ERR_CONNECT_TIMEOUT':
92
+ case 'UND_ERR_HEADERS_TIMEOUT':
93
+ case 'UND_ERR_BODY_TIMEOUT':
94
+ return HttpErrorReason.Timeout;
95
+ case 'UND_ERR_RESPONSE_STATUS_CODE':
96
+ return HttpErrorReason.StatusCode;
97
+ case 'UND_ERR_HEADERS_OVERFLOW':
98
+ case 'UND_ERR_RES_CONTENT_LENGTH_MISMATCH':
99
+ return HttpErrorReason.ResponseError;
100
+ case 'UND_ERR_REQ_CONTENT_LENGTH_MISMATCH':
101
+ case 'UND_ERR_INVALID_ARG':
102
+ case 'UND_ERR_INVALID_RETURN_VALUE':
103
+ return HttpErrorReason.InvalidRequest;
104
+ case 'UND_ERR_ABORTED':
105
+ case 'UND_ERR_DESTROYED':
106
+ case 'UND_ERR_CLOSED':
107
+ return HttpErrorReason.Cancelled;
108
+ case 'UND_ERR_SOCKET':
109
+ return HttpErrorReason.Network;
110
+ case 'UND_ERR_INFO':
111
+ case 'UND_ERR_NOT_SUPPORTED':
112
+ case 'UND_ERR_BPL_MISSING_UPSTREAM':
113
+ case 'UND_ERR_RES_EXCEEDED_MAX_SIZE':
114
+ default:
115
+ return HttpErrorReason.Unknown;
116
+ }
117
+ }
@@ -11,7 +11,7 @@ export type HttpClientResponseObject = TypedOmit<HttpClientResponse, 'hasBody' |
11
11
  export type HttpClientResponseOptions = {
12
12
  request: HttpClientRequest;
13
13
  statusCode: number;
14
- statusMessage: string;
14
+ statusMessage: string | null;
15
15
  headers: HttpHeadersObject | HttpHeaders;
16
16
  body: HttpBody | HttpBodySource;
17
17
  closeHandler: () => void;
@@ -20,7 +20,7 @@ export declare class HttpClientResponse {
20
20
  private readonly closeHandler;
21
21
  readonly request: HttpClientRequest;
22
22
  readonly statusCode: number;
23
- readonly statusMessage: string;
23
+ readonly statusMessage: string | null;
24
24
  readonly headers: HttpHeaders;
25
25
  readonly body: HttpBody;
26
26
  get hasBody(): boolean;
@@ -1,3 +1,4 @@
1
+ import { isString } from '../../utils/type-guards.js';
1
2
  import { HttpBody } from '../http-body.js';
2
3
  import { HttpHeaders } from '../http-headers.js';
3
4
  export class HttpClientResponse {
@@ -13,7 +14,7 @@ export class HttpClientResponse {
13
14
  constructor(options) {
14
15
  this.request = options.request;
15
16
  this.statusCode = options.statusCode;
16
- this.statusMessage = options.statusMessage;
17
+ this.statusMessage = (isString(options.statusMessage) && (options.statusMessage.length > 0)) ? options.statusMessage : null;
17
18
  this.headers = new HttpHeaders(options.headers);
18
19
  this.body = (options.body instanceof HttpBody) ? options.body : new HttpBody(options.body, this.headers);
19
20
  this.closeHandler = options.closeHandler;
@@ -240,7 +240,7 @@ async function errorMiddleware(request, next) {
240
240
  try {
241
241
  const response = await next(request);
242
242
  if (request.throwOnNon200 && ((response.statusCode < 200) || (response.statusCode >= 400))) {
243
- const httpError = await HttpError.create(HttpErrorReason.Non200StatusCode, request, response, `Status code ${response.statusCode}.`);
243
+ const httpError = await HttpError.create(HttpErrorReason.StatusCode, request, response, `Status code ${response.statusCode}.`);
244
244
  throw httpError;
245
245
  }
246
246
  return response;
@@ -5,9 +5,12 @@ import type { HttpClientRequest, HttpClientRequestObject, HttpClientResponse, Ht
5
5
  export declare enum HttpErrorReason {
6
6
  Unknown = "Unknown",
7
7
  Cancelled = "Cancelled",
8
+ Network = "Network",
8
9
  InvalidRequest = "InvalidRequest",
9
- Non200StatusCode = "Non200StatusCode",
10
+ StatusCode = "StatusCode",
11
+ /** Valid http response containing an error */
10
12
  ErrorResponse = "ErrorResponse",
13
+ /** Invalid http response */
11
14
  ResponseError = "ResponseError",
12
15
  Timeout = "Timeout"
13
16
  }
@@ -5,9 +5,12 @@ export var HttpErrorReason;
5
5
  (function (HttpErrorReason) {
6
6
  HttpErrorReason["Unknown"] = "Unknown";
7
7
  HttpErrorReason["Cancelled"] = "Cancelled";
8
+ HttpErrorReason["Network"] = "Network";
8
9
  HttpErrorReason["InvalidRequest"] = "InvalidRequest";
9
- HttpErrorReason["Non200StatusCode"] = "Non200StatusCode";
10
+ HttpErrorReason["StatusCode"] = "StatusCode";
11
+ /** Valid http response containing an error */
10
12
  HttpErrorReason["ErrorResponse"] = "ErrorResponse";
13
+ /** Invalid http response */
11
14
  HttpErrorReason["ResponseError"] = "ResponseError";
12
15
  HttpErrorReason["Timeout"] = "Timeout";
13
16
  })(HttpErrorReason || (HttpErrorReason = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tstdl/base",
3
- "version": "0.90.4",
3
+ "version": "0.90.6",
4
4
  "author": "Patrick Hein",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -149,7 +149,7 @@
149
149
  "playwright": "^1.38",
150
150
  "preact": "^10.18",
151
151
  "preact-render-to-string": "^6.2",
152
- "undici": "^5.25",
152
+ "undici": "^5.26",
153
153
  "urlpattern-polyfill": "^9.0"
154
154
  },
155
155
  "peerDependenciesMeta": {
@@ -1,5 +1,6 @@
1
1
  import type { PromiseRejectFunction, PromiseResolveFunction } from './types.js';
2
2
  export declare class CustomPromise<T> extends Promise<T> {
3
+ static readonly [Symbol.species]: PromiseConstructor;
3
4
  protected readonly resolve: PromiseResolveFunction<T>;
4
5
  protected readonly reject: PromiseRejectFunction;
5
6
  constructor();
@@ -1,4 +1,5 @@
1
1
  export class CustomPromise extends Promise {
2
+ static [Symbol.species] = Promise;
2
3
  resolve;
3
4
  reject;
4
5
  constructor() {