@arrowsphere/api-client 3.30.0-rc-jpb.1 → 3.30.0-rc-jpb.3

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.
@@ -4,6 +4,7 @@ export declare enum HttpClientSecurity {
4
4
  TOKEN = "token",
5
5
  API_KEY = "apikey"
6
6
  }
7
+ export declare type Headers = Record<string, string>;
7
8
  export declare abstract class AbstractHttpClient {
8
9
  /**
9
10
  * Base path for HTTP calls
@@ -21,6 +22,10 @@ export declare abstract class AbstractHttpClient {
21
22
  * ArrowSphere API URL
22
23
  */
23
24
  protected url: string;
25
+ /**
26
+ * Defines header information for http requests
27
+ */
28
+ protected headers: Headers;
24
29
  /**
25
30
  * Http Exceptions Handlers
26
31
  */
@@ -34,6 +39,17 @@ export declare abstract class AbstractHttpClient {
34
39
  setToken(token: string): this;
35
40
  setUrl(url: string): this;
36
41
  setSecurity(security: HttpClientSecurity): this;
42
+ /**
43
+ * Warning: can remove useful headers, prefer use mergeHeaders()
44
+ * @param headers
45
+ */
46
+ setHeaders(headers: Record<string, string>): this;
47
+ /**
48
+ * Will merge existing headers with those in parameters.
49
+ * If There is key equality, the header passed as parameter has priority.
50
+ * @param headers
51
+ */
52
+ mergeHeaders(headers: Record<string, string>): this;
37
53
  /**
38
54
  * Allow to register error/exception handler.
39
55
  * Handlers can be developed in another projects as long as they respect the interface HttpExceptionHandler.
@@ -25,6 +25,10 @@ class AbstractHttpClient {
25
25
  * ArrowSphere API URL
26
26
  */
27
27
  this.url = '';
28
+ /**
29
+ * Defines header information for http requests
30
+ */
31
+ this.headers = {};
28
32
  /**
29
33
  * Http Exceptions Handlers
30
34
  */
@@ -48,6 +52,27 @@ class AbstractHttpClient {
48
52
  this.security = security;
49
53
  return this;
50
54
  }
55
+ /**
56
+ * Warning: can remove useful headers, prefer use mergeHeaders()
57
+ * @param headers
58
+ */
59
+ setHeaders(headers) {
60
+ this.headers = headers;
61
+ return this;
62
+ }
63
+ /**
64
+ * Will merge existing headers with those in parameters.
65
+ * If There is key equality, the header passed as parameter has priority.
66
+ * @param headers
67
+ */
68
+ mergeHeaders(headers) {
69
+ const mergedHeaders = {
70
+ ...this.headers,
71
+ ...headers,
72
+ };
73
+ this.setHeaders(mergedHeaders);
74
+ return this;
75
+ }
51
76
  /**
52
77
  * Allow to register error/exception handler.
53
78
  * Handlers can be developed in another projects as long as they respect the interface HttpExceptionHandler.
@@ -5,13 +5,17 @@ import { GetProductsType } from './catalog';
5
5
  import { AbstractHttpClient } from './AbstractHttpClient';
6
6
  export declare type GraphQLResponseTypes = GetProductsType;
7
7
  export declare abstract class AbstractGraphQLClient extends AbstractHttpClient {
8
- protected client: GraphQLClient;
8
+ /**
9
+ * Must not be called directly.
10
+ * Use getClientInstance() to access it.
11
+ * @protected
12
+ */
13
+ protected graphQLClient: GraphQLClient;
9
14
  protected optionsHeader?: Dom.RequestInit['headers'];
10
15
  protected options: Options;
11
- private getClient;
12
- setOptionsHeader(options: Dom.RequestInit['headers']): this;
16
+ private getClientInstance;
13
17
  setOptions(options: Options): this;
14
18
  protected post<GraphQLResponseTypes>(query: string): Promise<GraphQLResponseTypes>;
15
- protected generateUrl(): string;
19
+ private generateUrl;
16
20
  protected stringifyQuery(query: any): string;
17
21
  }
@@ -33,24 +33,21 @@ class AbstractGraphQLClient extends AbstractHttpClient_1.AbstractHttpClient {
33
33
  super(...arguments);
34
34
  this.options = {};
35
35
  }
36
- getClient() {
37
- this.client = new graphql_request_1.GraphQLClient(this.generateUrl());
38
- return this;
39
- }
40
- setOptionsHeader(options) {
41
- this.optionsHeader = options;
42
- return this;
36
+ getClientInstance() {
37
+ var _a;
38
+ return ((_a = this.graphQLClient) !== null && _a !== void 0 ? _a : (this.graphQLClient = new graphql_request_1.GraphQLClient(this.generateUrl())));
43
39
  }
44
40
  setOptions(options) {
45
41
  this.options = options;
46
42
  return this;
47
43
  }
48
44
  async post(query) {
49
- this.getClient().client.setHeaders({
45
+ const headers = {
50
46
  authorization: this.token,
51
- ...this.optionsHeader,
52
- });
53
- return await this.client.request(query);
47
+ ...this.headers,
48
+ };
49
+ this.getClientInstance().setHeaders(headers);
50
+ return await this.getClientInstance().request(query);
54
51
  }
55
52
  generateUrl() {
56
53
  const url = new URL(`${this.options.isAdmin ? path.join('admin', this.basePath) : this.basePath}${this.path}`, this.url);
@@ -1,5 +1,5 @@
1
1
  import { AxiosInstance, AxiosResponse } from 'axios';
2
- import { AbstractHttpClient } from './AbstractHttpClient';
2
+ import { AbstractHttpClient, Headers } from './AbstractHttpClient';
3
3
  /**
4
4
  * Lists of available query parameters for the API call
5
5
  */
@@ -25,7 +25,6 @@ export declare type ParametersWithPaginationType = (Parameters & {
25
25
  [ParameterKeys.PER_PAGE_CAMEL]?: number;
26
26
  });
27
27
  export declare type Parameters = Record<string, string | string[] | number | number[] | boolean | null | undefined>;
28
- export declare type Headers = Record<string, string>;
29
28
  export declare type Payload = Record<string, unknown> | Array<Payload>;
30
29
  export declare type Options = {
31
30
  isAdmin?: boolean;
@@ -59,10 +58,6 @@ export declare abstract class AbstractRestfulClient extends AbstractHttpClient {
59
58
  * Defines whether the pagination options are camel cased or not
60
59
  */
61
60
  protected isCamelPagination: boolean;
62
- /**
63
- * Defines header information for axios call
64
- */
65
- protected headers: Headers;
66
61
  /**
67
62
  * AbstractClient constructor.
68
63
  * @returns AbstractRestfulClient
@@ -91,12 +86,6 @@ export declare abstract class AbstractRestfulClient extends AbstractHttpClient {
91
86
  * @returns AbstractRestfulClient
92
87
  */
93
88
  setPage(page: number): this;
94
- /**
95
- * Sets Header Information
96
- * @param headers - Header axios information
97
- * @returns AbstractRestfulClient
98
- */
99
- setHeaders(headers: Record<string, string>): this;
100
89
  /**
101
90
  * Sends a GET request and returns the response
102
91
  * @param parameters - Query parameters to send
@@ -52,10 +52,6 @@ class AbstractRestfulClient extends AbstractHttpClient_1.AbstractHttpClient {
52
52
  * Defines whether the pagination options are camel cased or not
53
53
  */
54
54
  this.isCamelPagination = false;
55
- /**
56
- * Defines header information for axios call
57
- */
58
- this.headers = {};
59
55
  this.client = axiosSingleton_1.AxiosSingleton.getInstance();
60
56
  this.setApiKey((_a = configuration === null || configuration === void 0 ? void 0 : configuration[ParameterKeys.API_KEY]) !== null && _a !== void 0 ? _a : '');
61
57
  this.setUrl((_b = configuration === null || configuration === void 0 ? void 0 : configuration[ParameterKeys.URL]) !== null && _b !== void 0 ? _b : '');
@@ -95,15 +91,6 @@ class AbstractRestfulClient extends AbstractHttpClient_1.AbstractHttpClient {
95
91
  this.page = page;
96
92
  return this;
97
93
  }
98
- /**
99
- * Sets Header Information
100
- * @param headers - Header axios information
101
- * @returns AbstractRestfulClient
102
- */
103
- setHeaders(headers) {
104
- this.headers = headers;
105
- return this;
106
- }
107
94
  /**
108
95
  * Sends a GET request and returns the response
109
96
  * @param parameters - Query parameters to send
@@ -5,7 +5,11 @@ const catalogGraphQLClient_1 = require("./catalog/catalogGraphQLClient");
5
5
  const abstractGraphQLClient_1 = require("./abstractGraphQLClient");
6
6
  class PublicGraphQLClient extends abstractGraphQLClient_1.AbstractGraphQLClient {
7
7
  getCatalogGraphQLClient() {
8
- return new catalogGraphQLClient_1.CatalogGraphQLClient().setUrl(this.url).setToken(this.token);
8
+ return new catalogGraphQLClient_1.CatalogGraphQLClient()
9
+ .setUrl(this.url)
10
+ .setToken(this.token)
11
+ .setHeaders(this.headers)
12
+ .setToken(this.token);
9
13
  }
10
14
  }
11
15
  exports.PublicGraphQLClient = PublicGraphQLClient;
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "type": "git",
5
5
  "url": "https://github.com/ArrowSphere/nodejs-api-client.git"
6
6
  },
7
- "version": "3.30.0-rc-jpb.1",
7
+ "version": "3.30.0-rc-jpb.3",
8
8
  "description": "Node.js client for ArrowSphere's public API",
9
9
  "main": "build/index.js",
10
10
  "types": "build/index.d.ts",
@@ -16,6 +16,7 @@
16
16
  "lint:fix": "eslint ./{src,tests}/**/*.ts --fix && prettier --write ./{src,tests}/**/*.ts",
17
17
  "prepare": "npm run clean && npm run build",
18
18
  "test": "mocha -r ts-node/register \"tests/**/*.ts\"",
19
+ "test:one": "mocha -r ts-node/register",
19
20
  "test:watch": "mocha --watch -r ts-node/register \"tests/**/*.ts\"",
20
21
  "test:coverage": "nyc --check-coverage npm run test",
21
22
  "test:coverage:report": "nyc report --reporter=text-lcov | coveralls"