attlaz-client 1.13.51 → 1.13.53

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.
@@ -2,7 +2,7 @@ export declare class ClientError implements Error {
2
2
  code: number | null;
3
3
  name: string;
4
4
  message: string;
5
- stack: string | undefined;
5
+ stack?: string;
6
6
  constructor(name: string, code?: number | null);
7
7
  static fromError(error: Error | any): ClientError;
8
8
  static byStatus(statusCode: number, statusText: string): ClientError | null;
@@ -12,7 +12,8 @@ export declare class HttpClient {
12
12
  static HTTP_UNAVAILABLE: number;
13
13
  static HTTP_TIMEOUT: number;
14
14
  static request(request: HttpClientRequest): Promise<HttpClientResponse>;
15
- private static getContentType;
15
+ private static popsicleRequest;
16
+ private static isJson;
16
17
  static formatContentType(input: string): {
17
18
  type: string;
18
19
  options: string | null;
@@ -13,6 +13,18 @@ export class HttpClient {
13
13
  static HTTP_UNAVAILABLE = 503;
14
14
  static HTTP_TIMEOUT = 504;
15
15
  static async request(request) {
16
+ const response = await HttpClient.popsicleRequest(request);
17
+ // const response: HttpClientResponse = await HttpClient.superAgentRequest(request);
18
+ const error = ClientError.byStatus(response.status, response.statusText);
19
+ if (error !== null && error !== undefined) {
20
+ if (response.body !== null && response.body !== undefined && response.body.error !== null && response.body.error !== undefined && response.body.error.message !== null && response.body.error.message !== undefined) {
21
+ error.message = response.body.error.message;
22
+ }
23
+ throw error;
24
+ }
25
+ return response;
26
+ }
27
+ static async popsicleRequest(request) {
16
28
  const rawRequest = {
17
29
  url: request.getFullUrl(),
18
30
  method: request.method,
@@ -20,46 +32,71 @@ export class HttpClient {
20
32
  omitDefaultHeaders: true,
21
33
  body: request.body,
22
34
  };
23
- const response = await fetch(request.getFullUrl(), rawRequest);
24
- const httpResponse = new HttpClientResponse(response.status, response.statusText);
25
- const contentType = this.getContentType(response);
26
- const rawData = await response.text();
27
- if (contentType.type === 'application/json') {
35
+ const rawResponse = await fetch(request.getFullUrl(), rawRequest);
36
+ const httpResponse = new HttpClientResponse(rawResponse.status, rawResponse.statusText);
37
+ for (const header of rawResponse.headers.keys()) {
38
+ const headerValue = rawResponse.headers.get(header);
39
+ httpResponse.headers[header] = headerValue === null ? '' : headerValue;
40
+ }
41
+ httpResponse.body = await rawResponse.text();
42
+ if (HttpClient.isJson(httpResponse)) {
28
43
  let jsonData = null;
29
- if (rawData !== '') {
44
+ if (httpResponse.body !== '') {
30
45
  try {
31
- jsonData = JSON.parse(rawData);
46
+ jsonData = JSON.parse(httpResponse.body);
32
47
  }
33
48
  catch (e) {
34
- console.error('Unable to parse response data to JSON', { statusText: httpResponse.statusText, error: e });
49
+ // console.error(, {statusText: httpResponse.statusText, error: e});
50
+ throw new Error('Unable to parse response data to JSON');
35
51
  }
36
52
  }
37
53
  httpResponse.body = jsonData;
38
54
  }
39
55
  else {
40
- httpResponse.body = rawData;
41
- }
42
- for (const header of response.headers.keys()) {
43
- const headerValue = response.headers.get(header);
44
- httpResponse.headers[header] = headerValue === null ? '' : headerValue;
45
- }
46
- const error = ClientError.byStatus(response.status, response.statusText);
47
- if (error !== null && error !== undefined) {
48
- if (httpResponse.body !== null && httpResponse.body !== undefined && httpResponse.body.error !== null && httpResponse.body.error !== undefined && httpResponse.body.error.message !== null && httpResponse.body.error.message !== undefined) {
49
- error.message = httpResponse.body.error.message;
50
- }
51
- throw error;
56
+ // response.body = rawData;
52
57
  }
53
58
  return httpResponse;
54
59
  }
55
- static getContentType(response) {
56
- if (response.headers.has('Content-Type')) {
57
- const rawContentType = response.headers.get('Content-Type');
58
- if (rawContentType !== null) {
59
- return this.formatContentType(rawContentType);
60
- }
61
- }
62
- return { type: 'application/json', options: null };
60
+ // private static async superAgentRequest(request: HttpClientRequest): Promise<HttpClientResponse> {
61
+ //
62
+ //
63
+ // let x: SuperAgent.Request | null = null;
64
+ // if (request.method === HttpClientRequest.GET) {
65
+ // x = SuperAgent.get(request.getFullUrl());
66
+ // } else if (request.method === HttpClientRequest.POST) {
67
+ // x = SuperAgent.post(request.getFullUrl());
68
+ //
69
+ // } else if (request.method === HttpClientRequest.PUT) {
70
+ // x = SuperAgent.put(request.getFullUrl());
71
+ // } else {
72
+ // throw new Error('Unknown request method `' + request.method + '`');
73
+ // }
74
+ //
75
+ //
76
+ // if (x === null) {
77
+ // throw new Error('Hm');
78
+ // }
79
+ //
80
+ //
81
+ // for (const key in request.headers) {
82
+ // if (Object.prototype.hasOwnProperty.call(request.headers, key)) {
83
+ // console.log('Set ' + key + ': ' + request.headers[key]);
84
+ // x.set(key, request.headers[key] as string);
85
+ // }
86
+ // }
87
+ //
88
+ //
89
+ // const rawResponse: SuperAgent.Response = await x.send(request.body);
90
+ //
91
+ //
92
+ // const response: HttpClientResponse = new HttpClientResponse(rawResponse.statusCode, '');
93
+ // response.body = rawResponse.body;
94
+ // // TODO: append/validate headers
95
+ // response.headers = rawResponse.headers;
96
+ // return response;
97
+ // }
98
+ static isJson(response) {
99
+ return response.getContentType() === 'application/json';
63
100
  }
64
101
  static formatContentType(input) {
65
102
  const d = input.split(';');
@@ -9,6 +9,7 @@ export class HttpClientResponse {
9
9
  this.statusText = statusText;
10
10
  }
11
11
  getContentType() {
12
+ // TODO: content-type or Content-Type or both...?
12
13
  let contentType = this.headers['content-type'];
13
14
  if (contentType === null || contentType === undefined) {
14
15
  return null;
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "1.13.50";
1
+ export declare const VERSION = "1.13.51";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = "1.13.50";
1
+ export const VERSION = "1.13.51";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "attlaz-client",
3
- "version": "1.13.51",
3
+ "version": "1.13.53",
4
4
  "description": "Javascript Client to access Attlaz API",
5
5
  "types": "./dist/index.d.ts",
6
6
  "main": "./dist/index.js",
@@ -47,20 +47,20 @@
47
47
  "devDependencies": {
48
48
  "@types/jest": "^29.5.2",
49
49
  "@types/node": "^20.9.0",
50
- "dotenv": "^16.3.1",
51
- "jest": "^29.5.0",
52
- "rimraf": "^5.0.1",
53
- "rollup-plugin-commonjs": "^10.1.0",
54
- "rollup-plugin-node-resolve": "^5.2.0",
55
- "rollup-plugin-typescript": "^1.0.1",
56
50
  "@typescript-eslint/eslint-plugin": "^7.1.0",
57
51
  "@typescript-eslint/parser": "^7.1.0",
52
+ "dotenv": "^16.3.1",
58
53
  "eslint": "^8.42.0",
59
54
  "eslint-config-airbnb-base": "^15.0.0",
60
55
  "eslint-plugin-import": "^2.27.5",
61
56
  "eslint-plugin-jsdoc": "^48.2.0",
62
57
  "eslint-plugin-prefer-arrow": "^1.2.3",
63
58
  "eslint-plugin-promise": "^6.1.1",
59
+ "jest": "^29.5.0",
60
+ "rimraf": "^5.0.1",
61
+ "rollup-plugin-commonjs": "^10.1.0",
62
+ "rollup-plugin-node-resolve": "^5.2.0",
63
+ "rollup-plugin-typescript": "^1.0.1",
64
64
  "ts-jest": "^29.1.0",
65
65
  "typescript": "^5.1.3"
66
66
  },