@barchart/portfolio-client-js 3.0.0 → 3.3.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.
@@ -0,0 +1,3 @@
1
+ **New Features**
2
+
3
+ * Added ```JwtProvider.forAdmin``` function.
@@ -0,0 +1,3 @@
1
+ **New Features**
2
+
3
+ * Added ```PortfolioGateway.queryPositionsForSymbol``` function.
@@ -0,0 +1,3 @@
1
+ **New Features**
2
+
3
+ * Added ```product``` to the ```PortfolioGateway```.
@@ -0,0 +1,59 @@
1
+ const PortfolioGateway = require('./../../lib/gateway/PortfolioGateway'),
2
+ JwtProvider = require('./../../lib/security/JwtProvider');
3
+
4
+ console.info(`Example: Node.js example script started.`);
5
+
6
+ let portfolioGateway = null;
7
+
8
+ process.on('SIGINT', () => {
9
+ console.info('Example: Processing SIGINT');
10
+
11
+ if (portfolioGateway !== null) {
12
+ portfolioGateway.dispose();
13
+ }
14
+
15
+ process.exit();
16
+ });
17
+
18
+ process.on('unhandledRejection', (error) => {
19
+ console.error('Unhandled Promise Rejection', error);
20
+ });
21
+
22
+ process.on('uncaughtException', (error) => {
23
+ console.error('Unhandled Error', error);
24
+ });
25
+
26
+ const userId = process.argv[2];
27
+ const contextId = process.argv[3];
28
+
29
+ const symbol = process.argv[4];
30
+
31
+ if (typeof(userId) !== 'string' || userId.length === 0) {
32
+ console.error('A user identifier must be specified. Usage example: "node example.js user-123"');
33
+ process.exit();
34
+ }
35
+
36
+ console.info(`Example: Initializing PortfolioGateway, connecting to test environment as [ ${userId} ] [ ${contextId} ].`);
37
+
38
+ PortfolioGateway.forDevelopment(JwtProvider.forDevelopment(userId, contextId))
39
+ .then((gateway) => {
40
+ portfolioGateway = gateway;
41
+
42
+ console.info(`Example: Retrieving portfolio(s) for [ ${userId} ] [ ${contextId} ].`);
43
+
44
+ return portfolioGateway.readPortfolios()
45
+ .then((portfolios) => {
46
+ console.info(`Example: Retrieved [ ${portfolios.length} ] portfolio(s) for [ ${userId} ] [ ${contextId} ].`);
47
+ });
48
+ }).then(() => {
49
+ console.info(`Example: Querying positions(s) for [ ${userId} ] [ ${contextId} ] [ ${symbol} ].`);
50
+
51
+ return portfolioGateway.queryPositionsForSymbol(symbol)
52
+ .then((portfolios) => {
53
+ console.info(`Example: Retrieved [ ${portfolios.length} ] portion(s) for [ ${userId} ] [ ${contextId} ] [ ${symbol} ].`);
54
+ });
55
+ }).then(() => {
56
+ portfolioGateway.dispose();
57
+
58
+ console.info(`Example: Node.js example script completed normally.`);
59
+ });
@@ -34,7 +34,7 @@ module.exports = (() => {
34
34
 
35
35
  const REST_API_SECURE_PROTOCOL = 'https';
36
36
  const REST_API_SECURE_PORT = 443;
37
-
37
+
38
38
  /**
39
39
  * The **central component of the SDK**. It is responsible for connecting to Barchart's
40
40
  * Portfolio Service. It can be used to query, edit, and delete portfolios.
@@ -44,19 +44,20 @@ module.exports = (() => {
44
44
  * @param {String} host - The hostname of the Portfolio web service.
45
45
  * @param {Number} port - The TCP port number of the Portfolio web service.
46
46
  * @param {String} environment - A description of the environment we're connecting to.
47
+ * @param {String=} product - A product code which uses the gateway.
47
48
  * @extends {Disposable}
48
49
  */
49
50
  class PortfolioGateway extends Disposable {
50
- constructor(protocol, host, port, environment) {
51
+ constructor(protocol, host, port, environment, product) {
51
52
  super();
52
53
 
53
54
  this._environment = environment;
54
55
 
55
56
  this._jwtProvider = null;
56
-
57
+
57
58
  this._started = false;
58
59
  this._startPromise = null;
59
-
60
+
60
61
  const requestInterceptor = RequestInterceptor.fromDelegate((options, endpoint) => {
61
62
  return Promise.resolve()
62
63
  .then(() => {
@@ -65,6 +66,10 @@ module.exports = (() => {
65
66
  options.headers = options.headers || {};
66
67
  options.headers.Authorization = `Bearer ${token}`;
67
68
 
69
+ if (is.string(product) && product.length > 0) {
70
+ options.headers['X-Barchart-Product'] = product;
71
+ }
72
+
68
73
  return options;
69
74
  });
70
75
  }).catch((e) => {
@@ -156,7 +161,9 @@ module.exports = (() => {
156
161
  .withVariableParameter('position', 'position', 'position', false);
157
162
  })
158
163
  .withQueryBuilder((qb) => {
159
- qb.withVariableParameter('includePreviousPrice', 'includePreviousPrice', 'includePreviousPrice', true);
164
+ qb.withVariableParameter('open', 'open', 'open', true)
165
+ .withVariableParameter('symbol', 'symbol', 'symbol', true)
166
+ .withVariableParameter('includePreviousPrice', 'includePreviousPrice', 'includePreviousPrice', true);
160
167
  })
161
168
  .withRequestInterceptor(requestInterceptor)
162
169
  .withRequestInterceptor(RequestInterceptor.PLAIN_TEXT_RESPONSE)
@@ -560,7 +567,7 @@ module.exports = (() => {
560
567
  }
561
568
 
562
569
  /**
563
- * Retrieves positions for a user, a user's portfolio, or a single position.
570
+ * Retrieves all positions for a user, a user's portfolio, or a single position.
564
571
  *
565
572
  * @public
566
573
  * @param {String=} portfolio
@@ -581,6 +588,38 @@ module.exports = (() => {
581
588
  });
582
589
  }
583
590
 
591
+ /**
592
+ * Queries positions.
593
+ *
594
+ * @public
595
+ * @param {String} symbol
596
+ * @param {Boolean=} open
597
+ * @param {PositionSchema=} schema
598
+ * @returns {Promise<Position[]>}
599
+ */
600
+ queryPositionsForSymbol(symbol, open, schema) {
601
+ return Promise.resolve()
602
+ .then(() => {
603
+ checkStart.call(this);
604
+
605
+ assert.argumentIsRequired(symbol, 'symbol', String);
606
+ assert.argumentIsOptional(open, 'open', Boolean);
607
+
608
+ const payload = { };
609
+
610
+ payload.portfolio = '*';
611
+ payload.position = '*';
612
+
613
+ payload.symbol = symbol;
614
+
615
+ if (open) {
616
+ payload.open = open;
617
+ }
618
+
619
+ return Gateway.invoke(this._readPositionsEndpoint, payload);
620
+ });
621
+ }
622
+
584
623
  /**
585
624
  * Updates a position.
586
625
  *
@@ -1090,19 +1129,6 @@ module.exports = (() => {
1090
1129
  });
1091
1130
  }
1092
1131
 
1093
- /**
1094
- * Returns current API version of portfolio.
1095
- *
1096
- * @public
1097
- * @returns {Promise<Object>}
1098
- */
1099
- readVersion() {
1100
- return Promise.resolve()
1101
- .then(() => {
1102
- return Gateway.invoke(this._readVersionEndpoint);
1103
- });
1104
- }
1105
-
1106
1132
  /**
1107
1133
  * Generates a URL suitable for downloading a brokerage report (as a PDF).
1108
1134
  *
@@ -1127,6 +1153,33 @@ module.exports = (() => {
1127
1153
  });
1128
1154
  }
1129
1155
 
1156
+ /**
1157
+ * Queries existing positions for a specific symbol.
1158
+ *
1159
+ * @public
1160
+ * @param {String} symbol
1161
+ * @returns {Promise<Array>}
1162
+ */
1163
+ querySymbol(symbol) {
1164
+ return Promise.resolve()
1165
+ .then(() => {
1166
+ return [ ];
1167
+ });
1168
+ }
1169
+
1170
+ /**
1171
+ * Returns current API version of portfolio.
1172
+ *
1173
+ * @public
1174
+ * @returns {Promise<Object>}
1175
+ */
1176
+ readVersion() {
1177
+ return Promise.resolve()
1178
+ .then(() => {
1179
+ return Gateway.invoke(this._readVersionEndpoint);
1180
+ });
1181
+ }
1182
+
1130
1183
  /**
1131
1184
  * Creates and starts a new {@link PortfolioGateway} for use in the public test environment.
1132
1185
  *
package/lib/index.js CHANGED
@@ -1,12 +1,12 @@
1
- const JwtGateway = require('./security/JwtGateway'),
1
+ const JwtProvider = require('./security/JwtProvider'),
2
2
  PortfolioGateway = require('./gateway/PortfolioGateway');
3
3
 
4
4
  module.exports = (() => {
5
5
  'use strict';
6
6
 
7
7
  return {
8
- JwtGateway: JwtGateway,
8
+ JwtProvider: JwtProvider,
9
9
  PortfolioGateway: PortfolioGateway,
10
- version: '3.0.0'
10
+ version: '3.3.0'
11
11
  };
12
- })();
12
+ })();
@@ -136,6 +136,22 @@ module.exports = (() => {
136
136
  return getJwtProviderForImpersonation(Configuration.getJwtImpersonationHost, 'dev', userId, contextId, permissions);
137
137
  }
138
138
 
139
+ /**
140
+ * Builds a {@link JwtProvider} which will generate tokens impersonating the specified
141
+ * user. The "admin" environment is for Barchart use only and access is restricted
142
+ * to Barchart's internal network.
143
+ *
144
+ * @public
145
+ * @static
146
+ * @param {String} userId - The user identifier to impersonate.
147
+ * @param {String} contextId - The context identifier of the user to impersonate.
148
+ * @param {String=} permissions - The desired permission level.
149
+ * @returns {JwtProvider}
150
+ */
151
+ static forAdmin(userId, contextId, permissions) {
152
+ return getJwtProviderForImpersonation(Configuration.getJwtImpersonationHost, 'admin', userId, contextId, permissions);
153
+ }
154
+
139
155
  _onDispose() {
140
156
  this._scheduler.dispose();
141
157
  this._scheduler = null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-client-js",
3
- "version": "3.0.0",
3
+ "version": "3.3.0",
4
4
  "description": "JavaScript library for interfacing with Barchart's Portfolio API",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",
@@ -22,7 +22,7 @@
22
22
  ],
23
23
  "dependencies": {
24
24
  "@barchart/common-js": "^3.15.0",
25
- "@barchart/portfolio-api-common": "^1.4.4"
25
+ "@barchart/portfolio-api-common": "^1.5.0"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@babel/core": "^7.11.1",