@barchart/portfolio-client-js 3.2.0 → 3.4.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.
@@ -1,3 +1,3 @@
1
1
  **New Features**
2
2
 
3
- * Added ability to query positions by symbol. See the ```PortfolioGateway.``` function.
3
+ * Added ```PortfolioGateway.queryPositionsForSymbol``` function.
@@ -0,0 +1,3 @@
1
+ **New Features**
2
+
3
+ * Added optional ```product``` argument to the ```PortfolioGateway``` constructor.
@@ -0,0 +1,3 @@
1
+ **New Features**
2
+
3
+ * Added ```PortfolioGateway.readMarketValue``` function.
@@ -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 code for the product which is using 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) => {
@@ -419,6 +424,22 @@ module.exports = (() => {
419
424
  .withErrorInterceptor(ErrorInterceptor.GENERAL)
420
425
  .endpoint;
421
426
 
427
+ this._readMarketValueEndpoint = EndpointBuilder.for('read-market-value', 'read market value')
428
+ .withVerb(VerbType.POST)
429
+ .withProtocol(protocolType)
430
+ .withHost(host)
431
+ .withPort(port)
432
+ .withPathBuilder((pb) => {
433
+ pb.withLiteralParameter('version', 'v1');
434
+ })
435
+ .withQueryBuilder((qb) => {
436
+ qb.withVariableParameter('symbol', 'symbol', 'symbol', false, x => x.symbol);
437
+ })
438
+ .withRequestInterceptor(requestInterceptor)
439
+ .withResponseInterceptor(responseInterceptorForMarketValues)
440
+ .withErrorInterceptor(ErrorInterceptor.GENERAL)
441
+ .endpoint;
442
+
422
443
  this._brokerageReportUrlGenerator = (user, portfolio, frame, end) => {
423
444
  return `https://${Configuration.getBrokerageHost(host)}/reports/portfolios/${portfolio}/frames/${frame.code}/date/${end.format()}/${user}`;
424
445
  };
@@ -1124,6 +1145,25 @@ module.exports = (() => {
1124
1145
  });
1125
1146
  }
1126
1147
 
1148
+ /**
1149
+ * Returns all market value for a symbol.
1150
+ *
1151
+ * @public
1152
+ * @param {String} symbol
1153
+ */
1154
+ readMarketValue(symbol) {
1155
+ return Promise.resolve()
1156
+ .then(() => {
1157
+ assert.argumentIsRequired(symbol, 'symbol', String);
1158
+
1159
+ const payload = { };
1160
+
1161
+ payload.symbol = symbol;
1162
+
1163
+ return Gateway.invoke(this._readMarketValueEndpoint, payload);
1164
+ });
1165
+ }
1166
+
1127
1167
  /**
1128
1168
  * Generates a URL suitable for downloading a brokerage report (as a PDF).
1129
1169
  *
@@ -1181,14 +1221,16 @@ module.exports = (() => {
1181
1221
  * @public
1182
1222
  * @static
1183
1223
  * @param {JwtProvider} jwtProvider
1224
+ * @param {String=} product
1184
1225
  * @returns {Promise<PortfolioGateway>}
1185
1226
  */
1186
- static forTest(jwtProvider) {
1227
+ static forTest(jwtProvider, product) {
1187
1228
  return Promise.resolve()
1188
1229
  .then(() => {
1189
1230
  assert.argumentIsRequired(jwtProvider, 'jwtProvider', JwtProvider, 'JwtProvider');
1231
+ assert.argumentIsOptional(product, 'product', String);
1190
1232
 
1191
- return start(new PortfolioGateway(REST_API_SECURE_PROTOCOL, Configuration.testHost, REST_API_SECURE_PORT, 'test'), jwtProvider);
1233
+ return start(new PortfolioGateway(REST_API_SECURE_PROTOCOL, Configuration.testHost, REST_API_SECURE_PORT, 'test', product), jwtProvider);
1192
1234
  });
1193
1235
  }
1194
1236
 
@@ -1198,14 +1240,16 @@ module.exports = (() => {
1198
1240
  * @public
1199
1241
  * @static
1200
1242
  * @param {JwtProvider} jwtProvider
1243
+ * @param {String=} product
1201
1244
  * @returns {Promise<PortfolioGateway>}
1202
1245
  */
1203
- static forDevelopment(jwtProvider) {
1246
+ static forDevelopment(jwtProvider, product) {
1204
1247
  return Promise.resolve()
1205
1248
  .then(() => {
1206
1249
  assert.argumentIsRequired(jwtProvider, 'jwtProvider', JwtProvider, 'JwtProvider');
1250
+ assert.argumentIsOptional(product, 'product', String);
1207
1251
 
1208
- return start(new PortfolioGateway(REST_API_SECURE_PROTOCOL, Configuration.developmentHost, REST_API_SECURE_PORT, 'development'), jwtProvider);
1252
+ return start(new PortfolioGateway(REST_API_SECURE_PROTOCOL, Configuration.developmentHost, REST_API_SECURE_PORT, 'development', product), jwtProvider);
1209
1253
  });
1210
1254
  }
1211
1255
 
@@ -1215,14 +1259,16 @@ module.exports = (() => {
1215
1259
  * @public
1216
1260
  * @static
1217
1261
  * @param {JwtProvider} jwtProvider
1262
+ * @param {String=} product
1218
1263
  * @returns {Promise<PortfolioGateway>}
1219
1264
  */
1220
- static forStaging(jwtProvider) {
1265
+ static forStaging(jwtProvider, product) {
1221
1266
  return Promise.resolve()
1222
1267
  .then(() => {
1223
1268
  assert.argumentIsRequired(jwtProvider, 'jwtProvider', JwtProvider, 'JwtProvider');
1269
+ assert.argumentIsOptional(product, 'product', String);
1224
1270
 
1225
- return start(new PortfolioGateway(REST_API_SECURE_PROTOCOL, Configuration.stagingHost, REST_API_SECURE_PORT, 'staging'), jwtProvider);
1271
+ return start(new PortfolioGateway(REST_API_SECURE_PROTOCOL, Configuration.stagingHost, REST_API_SECURE_PORT, 'staging', product), jwtProvider);
1226
1272
  });
1227
1273
  }
1228
1274
 
@@ -1232,14 +1278,16 @@ module.exports = (() => {
1232
1278
  * @public
1233
1279
  * @static
1234
1280
  * @param {JwtProvider} jwtProvider
1281
+ * @param {String=} product
1235
1282
  * @returns {Promise<PortfolioGateway>}
1236
1283
  */
1237
- static forDemo(jwtProvider) {
1284
+ static forDemo(jwtProvider, product) {
1238
1285
  return Promise.resolve()
1239
1286
  .then(() => {
1240
1287
  assert.argumentIsRequired(jwtProvider, 'jwtProvider', JwtProvider, 'JwtProvider');
1288
+ assert.argumentIsOptional(product, 'product', String);
1241
1289
 
1242
- return start(new PortfolioGateway(REST_API_SECURE_PROTOCOL, Configuration.demoHost, REST_API_SECURE_PORT, 'demo'), jwtProvider);
1290
+ return start(new PortfolioGateway(REST_API_SECURE_PROTOCOL, Configuration.demoHost, REST_API_SECURE_PORT, 'demo', product), jwtProvider);
1243
1291
  });
1244
1292
  }
1245
1293
 
@@ -1249,14 +1297,16 @@ module.exports = (() => {
1249
1297
  * @public
1250
1298
  * @static
1251
1299
  * @param {JwtProvider} jwtProvider
1300
+ * @param {String=} product
1252
1301
  * @returns {Promise<PortfolioGateway>}
1253
1302
  */
1254
- static forProduction(jwtProvider) {
1303
+ static forProduction(jwtProvider, product) {
1255
1304
  return Promise.resolve()
1256
1305
  .then(() => {
1257
1306
  assert.argumentIsRequired(jwtProvider, 'jwtProvider', JwtProvider, 'JwtProvider');
1307
+ assert.argumentIsOptional(product, 'product', String);
1258
1308
 
1259
- return start(new PortfolioGateway(REST_API_SECURE_PROTOCOL, Configuration.productionHost, REST_API_SECURE_PORT, 'production'), jwtProvider);
1309
+ return start(new PortfolioGateway(REST_API_SECURE_PROTOCOL, Configuration.productionHost, REST_API_SECURE_PORT, 'production', product), jwtProvider);
1260
1310
  });
1261
1311
  }
1262
1312
 
@@ -1266,14 +1316,16 @@ module.exports = (() => {
1266
1316
  * @public
1267
1317
  * @static
1268
1318
  * @param {JwtProvider} jwtProvider
1319
+ * @param {String=} product
1269
1320
  * @returns {Promise<PortfolioGateway>}
1270
1321
  */
1271
- static forAdmin(jwtProvider) {
1322
+ static forAdmin(jwtProvider, product) {
1272
1323
  return Promise.resolve()
1273
1324
  .then(() => {
1274
1325
  assert.argumentIsRequired(jwtProvider, 'jwtProvider', JwtProvider, 'JwtProvider');
1326
+ assert.argumentIsOptional(product, 'product', String);
1275
1327
 
1276
- return start(new PortfolioGateway(REST_API_SECURE_PROTOCOL, Configuration.adminHost, REST_API_SECURE_PORT, 'admin'), jwtProvider);
1328
+ return start(new PortfolioGateway(REST_API_SECURE_PROTOCOL, Configuration.adminHost, REST_API_SECURE_PORT, 'admin', product), jwtProvider);
1277
1329
  });
1278
1330
  }
1279
1331
 
@@ -1411,6 +1463,14 @@ module.exports = (() => {
1411
1463
  }
1412
1464
  });
1413
1465
 
1466
+ const responseInterceptorForMarketValues = ResponseInterceptor.fromDelegate((response, ignored) => {
1467
+ try {
1468
+ return JSON.parse(response.data);
1469
+ } catch (e) {
1470
+ console.error('Error deserializing data', e);
1471
+ }
1472
+ });
1473
+
1414
1474
  function start(gateway, jwtProvider) {
1415
1475
  return gateway.connect(jwtProvider)
1416
1476
  .then(() => {
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.2.0'
10
+ version: '3.4.0'
11
11
  };
12
- })();
12
+ })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-client-js",
3
- "version": "3.2.0",
3
+ "version": "3.4.0",
4
4
  "description": "JavaScript library for interfacing with Barchart's Portfolio API",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",