attlaz-client 1.6.11 → 1.7.0

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.
@@ -8,7 +8,10 @@ export declare class HttpClient {
8
8
  static HTTP_NOT_ALLOWED: number;
9
9
  static HTTP_UNPROCESSABLE_ENTITY: number;
10
10
  static HTTP_INTERNAL_SERVER_ERROR: number;
11
+ static HTTP_BAD_GATEWAY: number;
11
12
  static HTTP_UNAVAILABLE: number;
12
13
  static get(url: string, requestData?: any): Promise<any>;
13
14
  static request2(request: HttpClientRequest): Promise<HttpClientResponse>;
15
+ private static getContentType;
16
+ private static formatContentType;
14
17
  }
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.HttpClient = void 0;
4
4
  const popsicle_1 = require("popsicle");
5
5
  const Utils_1 = require("../Utils");
6
- // import {ClientError} from '..';
7
6
  const HttpClientResponse_1 = require("./HttpClientResponse");
8
7
  const ClientError_1 = require("../Model/Error/ClientError");
8
+ const version_1 = require("../version");
9
9
  class HttpClient {
10
10
  static async get(url, requestData = {}) {
11
11
  if (Utils_1.Utils.isNullOrUndefined(requestData)) {
@@ -21,6 +21,7 @@ class HttpClient {
21
21
  return await response.json();
22
22
  }
23
23
  static async request2(request) {
24
+ request.headers['User-Agent'] = 'Attlaz Http/' + version_1.VERSION;
24
25
  const rawRequest = {
25
26
  url: request.getFullUrl(),
26
27
  method: request.method,
@@ -28,31 +29,27 @@ class HttpClient {
28
29
  omitDefaultHeaders: true,
29
30
  body: request.body
30
31
  };
31
- // TODO: set user agent
32
- // rawRequest.negotiateHttpVersion = NegotiateHttpVersion.HTTP1_ONLY;
33
32
  const response = await (0, popsicle_1.fetch)(request.getFullUrl(), rawRequest);
34
33
  const httpResponse = new HttpClientResponse_1.HttpClientResponse(response.status, response.statusText);
35
- // httpResponse.status = response.status;
36
- // httpResponse.statusText = response.statusText;
37
- // TODO: should we get JSON immediately?
38
- // response.json
39
- // httpResponse.body = await response.text();
34
+ let contentType = this.getContentType(response);
40
35
  const rawData = await response.text();
41
- let jsonData = null;
42
- try {
43
- jsonData = JSON.parse(rawData);
36
+ if (contentType.type === 'application/json') {
37
+ let jsonData = null;
38
+ try {
39
+ jsonData = JSON.parse(rawData);
40
+ }
41
+ catch (e) {
42
+ console.log(httpResponse.statusText);
43
+ console.error('Unable to parse response data to JSON');
44
+ }
45
+ httpResponse.body = jsonData;
44
46
  }
45
- catch (e) {
46
- console.log(httpResponse.statusText);
47
- console.error('Unable to parse response data to JSON');
47
+ else {
48
+ httpResponse.body = rawData;
48
49
  }
49
- httpResponse.body = jsonData;
50
50
  const error = ClientError_1.ClientError.byStatus(response.status, response.statusText);
51
- if (!Utils_1.Utils.isNullOrUndefined(error)) {
52
- // const body: string = await response.text();
53
- // console.log('Text' + body);
54
- //
55
- // console.log(response);
51
+ if (error !== null && error !== undefined) {
52
+ error.body = httpResponse.body;
56
53
  throw error;
57
54
  }
58
55
  // //TODO: validate status
@@ -67,10 +64,28 @@ class HttpClient {
67
64
  // throw error;
68
65
  // }
69
66
  //
70
- // // TODO: make it possible to do request without parsing the json
71
- // return await response.json();
72
67
  return httpResponse;
73
68
  }
69
+ static getContentType(response) {
70
+ if (response.headers.has('Content-Type')) {
71
+ const rawContentType = response.headers.get('Content-Type');
72
+ if (rawContentType !== null) {
73
+ return this.formatContentType(rawContentType);
74
+ }
75
+ }
76
+ return { type: 'application/json', options: null };
77
+ }
78
+ static formatContentType(input) {
79
+ const d = input.split(';');
80
+ if (d.length === 1) {
81
+ return { type: d[0], options: null };
82
+ }
83
+ else if (d.length === 2) {
84
+ return { type: d[0], options: d[1].trim() };
85
+ }
86
+ console.error('Unable to parse content type "' + input + '"');
87
+ return { type: input, options: null };
88
+ }
74
89
  }
75
90
  exports.HttpClient = HttpClient;
76
91
  HttpClient.HTTP_BAD_REQUEST = 400;
@@ -80,4 +95,5 @@ HttpClient.HTTP_NOTFOUND = 404;
80
95
  HttpClient.HTTP_NOT_ALLOWED = 405;
81
96
  HttpClient.HTTP_UNPROCESSABLE_ENTITY = 422;
82
97
  HttpClient.HTTP_INTERNAL_SERVER_ERROR = 500;
98
+ HttpClient.HTTP_BAD_GATEWAY = 502;
83
99
  HttpClient.HTTP_UNAVAILABLE = 503;
@@ -84,7 +84,7 @@ class OAuthClient {
84
84
  }
85
85
  const requestData = this.createRequestData(action, parameters, method, signWithOauthToken);
86
86
  if (this.debug) {
87
- console.debug('[Client] Request: ' + requestData.getFullUrl());
87
+ console.debug('[Client] REQ: ' + requestData.method.toUpperCase() + ' ' + requestData.getFullUrl());
88
88
  }
89
89
  try {
90
90
  const response = await HttpClient_1.HttpClient.request2(requestData);
@@ -3,6 +3,7 @@ export declare class ClientError implements Error {
3
3
  name: string;
4
4
  code: number | null;
5
5
  stack: string;
6
+ body: string | null;
6
7
  constructor(message: string, code?: number | null);
7
8
  static fromError(error: Error | any): ClientError;
8
9
  static byStatus(statusCode: number, statusText: string): ClientError | null;
@@ -4,6 +4,7 @@ exports.ClientError = void 0;
4
4
  const HttpClient_1 = require("../../Http/HttpClient");
5
5
  class ClientError {
6
6
  constructor(message, code = null) {
7
+ this.body = null;
7
8
  this.message = message;
8
9
  this.code = code;
9
10
  }
@@ -66,6 +67,9 @@ class ClientError {
66
67
  case 500:
67
68
  return new ClientError('Internal Server Error', HttpClient_1.HttpClient.HTTP_INTERNAL_SERVER_ERROR);
68
69
  break;
70
+ case 502:
71
+ return new ClientError('Bad gateway', HttpClient_1.HttpClient.HTTP_BAD_GATEWAY);
72
+ break;
69
73
  case 503:
70
74
  return new ClientError('Service not available', HttpClient_1.HttpClient.HTTP_UNAVAILABLE);
71
75
  break;
@@ -50,8 +50,8 @@ class AdapterEndpoint extends Endpoint_1.Endpoint {
50
50
  }
51
51
  async getConnections(projectId) {
52
52
  try {
53
- let url = '/connections';
54
- const result = await this.request(url, { projectId }, 'GET');
53
+ let url = '/project/' + projectId + '/connections';
54
+ const result = await this.request(url, null, 'GET');
55
55
  result.setData(this.parseCollection(result, AdapterConnection_1.AdapterConnection.parse));
56
56
  return result;
57
57
  }
@@ -78,10 +78,8 @@ class AdapterEndpoint extends Endpoint_1.Endpoint {
78
78
  }
79
79
  async saveConnection(projectId, adapter) {
80
80
  try {
81
- let url = '/connections';
82
- const data = adapter;
83
- data.projectId = projectId;
84
- const result = await this.request(url, data, 'POST');
81
+ let url = '/project/' + projectId + '/connections';
82
+ const result = await this.request(url, adapter, 'POST');
85
83
  result.setData(AdapterConnection_1.AdapterConnection.parse(result.getData()));
86
84
  return result;
87
85
  }
@@ -0,0 +1 @@
1
+ export declare const VERSION = "1.7.0";
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VERSION = void 0;
4
+ exports.VERSION = "1.7.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "attlaz-client",
3
- "version": "1.6.11",
3
+ "version": "1.7.0",
4
4
  "description": "Javascript Client to access Attlaz API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -14,6 +14,7 @@
14
14
  "author": "Stijn Duynslaeger <stijn@attlaz.com> (stijn@attlaz.com)",
15
15
  "license": "MIT",
16
16
  "scripts": {
17
+ "prebuild": "node -p \"'export const VERSION = ' + JSON.stringify(require('./package.json').version) + ';'\" > src/version.ts",
17
18
  "prepare": "npm run build",
18
19
  "build": "npm run clean && tsc",
19
20
  "clean": "rimraf dist",