@barchart/portfolio-api-common 7.1.1 → 7.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.
@@ -171,10 +171,13 @@ module.exports = (() => {
171
171
  };
172
172
 
173
173
  const buySellFormatter = (t, f) => {
174
+ const ambiguous = is.object(t.snaptrade) && is.boolean(t.snaptrade.ambiguous) && t.snaptrade.ambiguous;
175
+
174
176
  f.boughtSold = t.quantity;
175
177
  f.price = t.trade.price;
176
178
  f.fee = t.fee;
177
179
  f.total = t.amount;
180
+ f.ambiguous = ambiguous;
178
181
 
179
182
  if (t.description) {
180
183
  f.description = t.description;
@@ -183,6 +186,7 @@ module.exports = (() => {
183
186
  f.raw.total = getRawForDecimal(f.total);
184
187
  f.raw.price = getRawForDecimal(f.price);
185
188
  f.raw.boughtSold = getRawForDecimal(f.boughtSold);
189
+ f.raw.ambiguous = ambiguous;
186
190
  };
187
191
 
188
192
  const dividendFormatter = (t, f) => {
@@ -60,16 +60,44 @@ module.exports = (() => {
60
60
  return Promise.reject(`Instrument lookup for [ ${symbol} ] failed, the instrument does not exist`);
61
61
  }
62
62
 
63
- const instrument = result.instrument;
63
+ normalizeInstrument(result.instrument);
64
64
 
65
- if (instrument.symbolType === 18) {
66
- const match = instrument.name.match(regex.crypto.token) || null;
65
+ return result;
66
+ });
67
+ }
68
+
69
+ /**
70
+ * Returns a promise for metadata for multiple instruments.
71
+ *
72
+ * @public
73
+ * @async
74
+ * @param {String[]} symbols
75
+ * @returns {Promise<Object>}
76
+ */
77
+ async getInstruments(symbols) {
78
+ assert.argumentIsArray(symbols, 'symbols', String);
79
+ assert.argumentIsValid(symbols.length, 'symbols.length', x => x > 0, 'is greater than zero');
67
80
 
68
- if (match !== null) {
69
- instrument.name = match[1];
70
- instrument.currency = 'USD';
71
- instrument.symbolType = 999;
72
- }
81
+ const symbolList = symbols.join(',');
82
+
83
+ return promise.timeout(Gateway.invoke(instrumentsLookupEndpoint, { symbols: symbolList }), this._waitInMilliseconds, 'instrument lookup')
84
+ .catch((e) => {
85
+ let message;
86
+
87
+ if (is.string(e) && e === 'timeout') {
88
+ message = `Instrument lookup for [ ${symbolList} ] failed due to timed out`;
89
+ } else {
90
+ message = `Instrument lookup for [ ${symbolList} ] failed due to an unspecified error`;
91
+ }
92
+
93
+ return Promise.reject(message);
94
+ }).then((result) => {
95
+ if (result.instruments === null) {
96
+ return Promise.reject(`Instrument lookup for [ ${symbolList} ] failed, no instruments were returned`);
97
+ }
98
+
99
+ if (is.array(result.instruments)) {
100
+ result.instruments.forEach(normalizeInstrument);
73
101
  }
74
102
 
75
103
  return result;
@@ -94,5 +122,32 @@ module.exports = (() => {
94
122
  .withErrorInterceptor(ErrorInterceptor.GENERAL)
95
123
  .endpoint;
96
124
 
125
+ const instrumentsLookupEndpoint = EndpointBuilder.for('query-instruments', 'query instruments')
126
+ .withVerb(VerbType.GET)
127
+ .withProtocol(ProtocolType.HTTPS)
128
+ .withHost('instruments-prod.aws.barchart.com')
129
+ .withPort(443)
130
+ .withPathBuilder((pb) => {
131
+ pb.withLiteralParameter('instruments', 'instruments');
132
+ })
133
+ .withQueryBuilder((qb) => {
134
+ qb.withVariableParameter('symbols', 'symbols', 'symbols');
135
+ })
136
+ .withResponseInterceptor(ResponseInterceptor.DATA)
137
+ .withErrorInterceptor(ErrorInterceptor.GENERAL)
138
+ .endpoint;
139
+
140
+ function normalizeInstrument(instrument) {
141
+ if (instrument && instrument.symbolType === 18) {
142
+ const match = instrument.name.match(regex.crypto.token) || null;
143
+
144
+ if (match !== null) {
145
+ instrument.name = match[1];
146
+ instrument.currency = 'USD';
147
+ instrument.symbolType = 999;
148
+ }
149
+ }
150
+ }
151
+
97
152
  return InstrumentProvider;
98
- })();
153
+ })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barchart/portfolio-api-common",
3
- "version": "7.1.1",
3
+ "version": "7.3.0",
4
4
  "description": "Common JavaScript code used by Barchart's Portfolio Service",
5
5
  "author": {
6
6
  "name": "Bryan Ingle",