attlaz-client 1.21.5 → 1.21.9

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.
@@ -0,0 +1,7 @@
1
+ import { CursorPagination } from '../Model/Pagination/CursorPagination.js';
2
+ import { CollectionResult } from '../Model/Result/CollectionResult.js';
3
+ export declare class LoadAllHelper {
4
+ static loadAll<T extends {
5
+ id: string;
6
+ }>(call: (pagination: CursorPagination) => Promise<CollectionResult<T>>): Promise<T[]>;
7
+ }
@@ -0,0 +1,20 @@
1
+ import { CursorPagination } from '../Model/Pagination/CursorPagination.js';
2
+ export class LoadAllHelper {
3
+ static async loadAll(call) {
4
+ const totalResult = [];
5
+ let hasMore = true;
6
+ let lastRef = null;
7
+ while (hasMore) {
8
+ const firstPagination = new CursorPagination();
9
+ firstPagination.limit = 10;
10
+ firstPagination.startingAfter = lastRef;
11
+ const data = await call(firstPagination);
12
+ for (const record of data.getData()) {
13
+ totalResult.push(record);
14
+ lastRef = record.id;
15
+ }
16
+ hasMore = data.hasMore;
17
+ }
18
+ return totalResult;
19
+ }
20
+ }
@@ -1,11 +1,9 @@
1
1
  import { AxiosError } from 'axios';
2
2
  export declare class ClientError extends Error {
3
3
  httpStatus: number | null;
4
- type: string;
5
- code: string;
6
- param?: string | undefined;
7
- stack?: string;
8
- constructor(code: string, httpErrorCode?: number | null);
4
+ message: string;
5
+ response: unknown;
6
+ constructor(message: string, httpErrorCode?: number | null);
9
7
  static fromError(error: Error | AxiosError): ClientError;
10
8
  static byStatus(statusCode: number, statusText: string): ClientError | null;
11
9
  }
@@ -2,67 +2,41 @@ import { AxiosError } from 'axios';
2
2
  import { HttpStatus } from './HttpStatus.js';
3
3
  export class ClientError extends Error {
4
4
  httpStatus = null;
5
- type;
6
- code;
7
- // public message: string;
8
- param = undefined;
9
- stack;
10
- constructor(code, httpErrorCode = null) {
11
- super(code);
12
- this.code = code;
5
+ message;
6
+ response;
7
+ constructor(message, httpErrorCode = null) {
8
+ super(message);
9
+ this.message = message;
13
10
  this.httpStatus = httpErrorCode;
14
11
  }
15
12
  static fromError(error) {
16
13
  let clientError = new ClientError('Unknown error', HttpStatus.HTTP_INTERNAL_SERVER_ERROR);
17
14
  if (error instanceof AxiosError) {
18
- if (error.code === 'ECONNREFUSED') {
15
+ if (error.code === 'ECONNREFUSED' || error.code === 'ERR_NETWORK') {
19
16
  clientError.httpStatus = HttpStatus.HTTP_UNAVAILABLE;
20
- clientError.code = 'Service not available';
17
+ // clientError.code = 'Service not available';
18
+ clientError.message = 'Service not available';
21
19
  return clientError;
22
20
  }
23
- if (error.status !== undefined) {
21
+ if (error.status !== undefined && error.status !== null) {
24
22
  clientError.httpStatus = error.status;
25
23
  }
26
24
  // console.log('Status', clientError.httpStatus);
27
25
  // if(clientError.httpStatus === HttpStatusCode.Ref)
28
- clientError.message = error.message;
26
+ // clientError.message = error.message;
29
27
  if (error.code !== undefined) {
30
- clientError.code = error.code;
28
+ // clientError.code = error.code;
31
29
  }
32
30
  if (error.response === undefined) {
33
- if (error.code === 'ECONNREFUSED') {
34
- clientError.message = 'Connection refused';
35
- }
36
- else {
37
- console.error('Unable to parse AxiosError response', {
38
- error, status: error.status, code: error.code,
39
- });
40
- }
31
+ console.error('Unknown AxiosError error', {
32
+ error, status: error.status, code: error.code,
33
+ });
41
34
  }
42
35
  else {
43
- // console.debug('Parse response', {error: error, data: error.response.data});
44
- switch (error.response.status) {
45
- case 404:
46
- clientError.code = 'Not found';
47
- break;
48
- default:
49
- }
50
- const errorData = error.response.data;
51
- if (errorData.error !== undefined) {
52
- // console.debug('Error data', errorData);
53
- if (errorData.error.type !== undefined) {
54
- clientError.type = errorData.error.type;
55
- }
56
- if (errorData.error.code !== undefined) {
57
- clientError.code = errorData.error.code;
58
- }
59
- if (errorData.error.message !== undefined) {
60
- clientError.message = errorData.error.message;
61
- }
62
- if (errorData.error.param !== undefined) {
63
- clientError.param = errorData.error.param;
64
- }
65
- }
36
+ const response = error.response;
37
+ // console.log('Response', response);
38
+ clientError.message = response.statusText;
39
+ clientError.response = response;
66
40
  }
67
41
  return clientError;
68
42
  }
@@ -74,7 +48,7 @@ export class ClientError extends Error {
74
48
  // message = error.message;
75
49
  // }
76
50
  //
77
- // console.error(error);
51
+ // console.error(error);
78
52
  const xError = error;
79
53
  if (xError.status !== null && xError.status !== undefined) {
80
54
  const statusCode = xError.status;
@@ -69,11 +69,12 @@ export class OAuthClient {
69
69
  }
70
70
  getAxiosInstance() {
71
71
  if (this.axiosInstance === null) {
72
- this.axiosInstance = axios.create({
72
+ const config = {
73
73
  // httpsAgent: new https.Agent({
74
74
  // rejectUnauthorized: false,
75
75
  // }),
76
- });
76
+ };
77
+ this.axiosInstance = axios.create(config);
77
78
  }
78
79
  return this.axiosInstance;
79
80
  }
@@ -0,0 +1,10 @@
1
+ export declare class ApiError extends Error {
2
+ httpStatus: number | null;
3
+ type: string;
4
+ code: string;
5
+ param?: string | undefined;
6
+ stack?: string;
7
+ message: string;
8
+ response: unknown;
9
+ constructor(message: string, httpErrorCode?: number | null);
10
+ }
@@ -0,0 +1,15 @@
1
+ export class ApiError extends Error {
2
+ httpStatus = null;
3
+ type;
4
+ code;
5
+ // public message: string;
6
+ param = undefined;
7
+ stack;
8
+ message;
9
+ response;
10
+ constructor(message, httpErrorCode = null) {
11
+ super(message);
12
+ this.message = message;
13
+ this.httpStatus = httpErrorCode;
14
+ }
15
+ }
@@ -10,6 +10,7 @@ export declare abstract class Endpoint {
10
10
  private formatKey;
11
11
  requestCollection<T>(action: string | QueryString, parser: (input: any) => T, parameters?: Parameters, method?: string, signWithOauthToken?: boolean): Promise<CollectionResult<T>>;
12
12
  requestObject<T>(action: string | QueryString, parameters: Parameters | undefined, parser: (input: Record<string, any>) => T, method?: string, signWithOauthToken?: boolean): Promise<ObjectResult<T>>;
13
+ private toApiError;
13
14
  parseCollection<T>(rawData: object[], parser: (input: any) => T): T[];
14
15
  private parseObject;
15
16
  }
@@ -7,6 +7,8 @@ import { JsonSerializable } from '../Model/JsonSerializable.js';
7
7
  import { LogStreamId } from '../Model/Log/LogStreamId.js';
8
8
  import { QueryString } from '../Http/Data/QueryString.js';
9
9
  import { HttpStatus } from '../Http/HttpStatus.js';
10
+ import { ApiError } from '../Model/Error/ApiError.js';
11
+ import { ClientError } from '../Http/ClientError.js';
10
12
  export class Endpoint {
11
13
  httpClient;
12
14
  constructor(httpClient) {
@@ -74,7 +76,13 @@ export class Endpoint {
74
76
  if (action instanceof QueryString) {
75
77
  action = action.build();
76
78
  }
77
- const requestResponse = await this.httpClient.request(action, parameters, method, signWithOauthToken);
79
+ let requestResponse;
80
+ try {
81
+ requestResponse = await this.httpClient.request(action, parameters, method, signWithOauthToken);
82
+ }
83
+ catch (error) {
84
+ throw this.toApiError(error);
85
+ }
78
86
  const result = new CollectionResult();
79
87
  /**
80
88
  * Parse errors
@@ -123,11 +131,36 @@ export class Endpoint {
123
131
  result.setData(null);
124
132
  }
125
133
  else {
126
- throw error;
134
+ throw this.toApiError(error);
127
135
  }
128
136
  }
129
137
  return result;
130
138
  }
139
+ toApiError(error) {
140
+ if (error instanceof ClientError) {
141
+ const apiError = new ApiError(error.message, error.httpStatus);
142
+ if (error.response !== null && error.response !== undefined) {
143
+ const response = error.response;
144
+ if (response.data.error !== undefined) {
145
+ // console.debug('Error data', errorData);
146
+ if (response.data.error.type !== undefined) {
147
+ apiError.type = response.data.error.type;
148
+ }
149
+ if (response.data.error.code !== undefined) {
150
+ apiError.code = response.data.error.code;
151
+ }
152
+ if (response.data.error.message !== undefined) {
153
+ apiError.message = response.data.error.message;
154
+ }
155
+ if (response.data.error.param !== undefined) {
156
+ apiError.param = response.data.error.param;
157
+ }
158
+ }
159
+ }
160
+ return apiError;
161
+ }
162
+ return new ApiError('Unknown Error', HttpStatus.HTTP_INTERNAL_SERVER_ERROR);
163
+ }
131
164
  // public async request<T>(action: string, parameters: any | null = null, method: string = 'GET', signWithOauthToken: boolean = true): Promise<DataResult<T>> {
132
165
  // const result: any = await this.httpClient.request(action, parameters, method, signWithOauthToken);
133
166
  //
package/dist/index.d.ts CHANGED
@@ -13,6 +13,10 @@ export { OAuthClientToken } from './Http/OAuthClientToken.js';
13
13
  export { ClientError } from './Http/ClientError.js';
14
14
  export { HttpStatus } from './Http/HttpStatus.js';
15
15
  export { ContentTypeHelper } from './Http/ContentTypeHelper.js';
16
+ /**
17
+ * Helpers
18
+ */
19
+ export { LoadAllHelper } from './Helper/LoadAllHelper.js';
16
20
  /**
17
21
  * Models
18
22
  */
package/dist/index.js CHANGED
@@ -8,6 +8,10 @@ export { OAuthClientToken } from './Http/OAuthClientToken.js';
8
8
  export { ClientError } from './Http/ClientError.js';
9
9
  export { HttpStatus } from './Http/HttpStatus.js';
10
10
  export { ContentTypeHelper } from './Http/ContentTypeHelper.js';
11
+ /**
12
+ * Helpers
13
+ */
14
+ export { LoadAllHelper } from './Helper/LoadAllHelper.js';
11
15
  /**
12
16
  * Models
13
17
  */
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "1.21.4";
1
+ export declare const VERSION = "1.21.6";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = "1.21.4";
1
+ export const VERSION = "1.21.6";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "attlaz-client",
3
- "version": "1.21.5",
3
+ "version": "1.21.9",
4
4
  "description": "Javascript Client to access Attlaz API",
5
5
  "types": "./dist/index.d.ts",
6
6
  "main": "./dist/index.js",