@barchart/portfolio-client-js 1.1.2

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,9 @@
1
+ const JwtGateway = require('@barchart/tgam-jwt-js/lib/JwtGateway');
2
+
3
+ module.exports = (() => {
4
+ 'use strict';
5
+
6
+ window.Barchart = window.Barchart || { };
7
+ window.Barchart.Jwt = window.Barchart.Jwt || { };
8
+ window.Barchart.Jwt.JwtProvider = JwtGateway;
9
+ })();
package/gulpfile.js ADDED
@@ -0,0 +1,148 @@
1
+ const gulp = require('gulp');
2
+
3
+ const browserify = require('browserify'),
4
+ buffer = require('vinyl-buffer'),
5
+ bump = require('gulp-bump'),
6
+ exec = require('child_process').exec,
7
+ git = require('gulp-git'),
8
+ gitStatus = require('git-get-status'),
9
+ glob = require('glob'),
10
+ jasmine = require('gulp-jasmine'),
11
+ jshint = require('gulp-jshint'),
12
+ replace = require('gulp-replace'),
13
+ runSequence = require('run-sequence'),
14
+ source = require('vinyl-source-stream'),
15
+ util = require('gulp-util');
16
+
17
+ const fs = require('fs');
18
+
19
+ function getVersionFromPackage() {
20
+ return JSON.parse(fs.readFileSync('./package.json', 'utf8')).version;
21
+ }
22
+
23
+ gulp.task('ensure-clean-working-directory', () => {
24
+ gitStatus(function(err, status) {
25
+ if (err, !status.clean) {
26
+ throw new Error('Unable to proceed, your working directory is not clean.');
27
+ }
28
+ });
29
+ });
30
+
31
+ gulp.task('bump-version', () => {
32
+ return gulp.src([ './package.json' ])
33
+ .pipe(bump({ type: 'patch' }).on('error', util.log))
34
+ .pipe(gulp.dest('./'));
35
+ });
36
+
37
+ gulp.task('embed-version', function () {
38
+ var version = getVersionFromPackage();
39
+
40
+ return gulp.src(['./lib/index.js'])
41
+ .pipe(replace(/(version:\s*')([0-9]+\.[0-9]+\.[0-9]+)(')/g, '$1' + version + '$3'))
42
+ .pipe(gulp.dest('./lib/'));
43
+ });
44
+
45
+ gulp.task('document', function (cb) {
46
+ exec('jsdoc . -c jsdoc.json -r -d docs', (error, stdout, stderr) => {
47
+ console.log(stdout);
48
+ console.log(stderr);
49
+
50
+ cb();
51
+ });
52
+ });
53
+
54
+ gulp.task('commit-changes', () => {
55
+ return gulp.src([ './', './test/', './package.json', './lib/index.js', './example/example.js', './test/SpecRunner.js' ])
56
+ .pipe(git.add())
57
+ .pipe(git.commit('Release. Bump version number'));
58
+ });
59
+
60
+ gulp.task('push-changes', (cb) => {
61
+ git.push('origin', 'master', cb);
62
+ });
63
+
64
+ gulp.task('create-tag', (cb) => {
65
+ const version = getVersionFromPackage();
66
+
67
+ git.tag(version, 'Release ' + version, function (error) {
68
+ if (error) {
69
+ return cb(error);
70
+ }
71
+
72
+ git.push('origin', 'master', { args: '--tags' }, cb);
73
+ });
74
+ });
75
+
76
+ gulp.task('build-example-bundle', function() {
77
+ return browserify([ './example/example.shim.js', './lib/index.js' ], { standalone: 'Barchart.Portfolio' })
78
+ .bundle()
79
+ .pipe(source('example.js'))
80
+ .pipe(buffer())
81
+ .pipe(gulp.dest('./example'));
82
+ });
83
+
84
+ gulp.task('build-test-bundle', () => {
85
+ return browserify({ entries: glob.sync('test/specs/**/*.js') })
86
+ .bundle()
87
+ .pipe(source('SpecRunner.js'))
88
+ .pipe(buffer())
89
+ .pipe(gulp.dest('test'));
90
+ });
91
+
92
+ gulp.task('execute-browser-tests', () => {
93
+ return gulp.src('test/SpecRunner.js')
94
+ .pipe(jasmine());
95
+ });
96
+
97
+ gulp.task('execute-node-tests', () => {
98
+ return gulp.src(['test/specs/**/*.js'])
99
+ .pipe(jasmine());
100
+ });
101
+
102
+ gulp.task('execute-tests', (cb) => {
103
+ runSequence(
104
+ 'build-test-bundle',
105
+ 'execute-browser-tests',
106
+ 'execute-node-tests',
107
+
108
+ function (error) {
109
+ if (error) {
110
+ console.log(error.message);
111
+ }
112
+
113
+ cb(error);
114
+ });
115
+ });
116
+
117
+ gulp.task('release', (cb) => {
118
+ runSequence(
119
+ 'ensure-clean-working-directory',
120
+ 'execute-tests',
121
+ 'document',
122
+ 'bump-version',
123
+ 'embed-version',
124
+ 'build-example-bundle',
125
+ 'commit-changes',
126
+ 'push-changes',
127
+ 'create-tag',
128
+
129
+ function (error) {
130
+ if (error) {
131
+ console.log(error.message);
132
+ } else {
133
+ console.log('Release complete');
134
+ }
135
+
136
+ cb(error);
137
+ });
138
+ });
139
+
140
+ gulp.task('lint', () => {
141
+ return gulp.src([ './**/*.js', './test/specs/**/*.js', '!./node_modules/**', '!./docs/**', '!./test/SpecRunner.js', '!./example/example.js' ])
142
+ .pipe(jshint({'esversion': 6}))
143
+ .pipe(jshint.reporter('default'));
144
+ });
145
+
146
+ gulp.task('test', [ 'execute-tests' ]);
147
+
148
+ gulp.task('default', [ 'lint' ]);
package/jsdoc.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "source": {
3
+ "include": [ "." ],
4
+ "exclude": [ "./node_modules", "./test" ],
5
+ "includePattern": ".+\\.js(doc|x)?$",
6
+ "excludePattern": "(^|\\/|\\\\)_"
7
+ }
8
+ }
@@ -0,0 +1,42 @@
1
+ module.exports = (() => {
2
+ 'use strict';
3
+
4
+ /**
5
+ * Static configuration data.
6
+ *
7
+ * @public
8
+ */
9
+ class Configuration {
10
+ constructor() {
11
+
12
+ }
13
+
14
+ /**
15
+ * The host of the development system.
16
+ *
17
+ * @public
18
+ * @static
19
+ * @returns {String}
20
+ */
21
+ static get developmentHost() {
22
+ return 'g4zerhpif5.execute-api.us-east-1.amazonaws.com/dev';
23
+ }
24
+
25
+ /**
26
+ * The host of the production system.
27
+ *
28
+ * @public
29
+ * @static
30
+ * @returns {String}
31
+ */
32
+ static get productionHost() {
33
+ return 'g4zerhpif5.execute-api.us-east-1.amazonaws.com/prod';
34
+ }
35
+
36
+ toString() {
37
+ return '[Configuration]';
38
+ }
39
+ }
40
+
41
+ return Configuration;
42
+ })();
@@ -0,0 +1,292 @@
1
+ const assert = require('@barchart/common-js/lang/assert'),
2
+ Disposable = require('@barchart/common-js/lang/Disposable'),
3
+ Enum = require('@barchart/common-js/lang/Enum'),
4
+ is = require('@barchart/common-js/lang/is');
5
+
6
+ const TransactionSchema = require('@barchart/portfolio-api-common/lib/serialization/TransactionSchema');
7
+
8
+ const EndpointBuilder = require('@barchart/common-js/api/http/builders/EndpointBuilder'),
9
+ Gateway = require('@barchart/common-js/api/http/Gateway'),
10
+ ProtocolType = require('@barchart/common-js/api/http/definitions/ProtocolType'),
11
+ ErrorInterceptor = require('@barchart/common-js/api/http/interceptors/ErrorInterceptor'),
12
+ RequestInterceptor = require('@barchart/common-js/api/http/interceptors/RequestInterceptor'),
13
+ ResponseInterceptor = require('@barchart/common-js/api/http/interceptors/ResponseInterceptor'),
14
+ VerbType = require('@barchart/common-js/api/http/definitions/VerbType');
15
+
16
+ const Configuration = require('./../common/Configuration');
17
+
18
+ module.exports = (() => {
19
+ 'use strict';
20
+
21
+ /**
22
+ * Web service gateway for invoking the Portfolio API.
23
+ *
24
+ * @public
25
+ * @param {String} protocol - The protocol to use (either HTTP or HTTPS).
26
+ * @param {String} host - The host name of the Portfolio web service.
27
+ * @param {Number} port - The TCP port number of the Portfolio web service.
28
+ * @param {RequestInterceptor=} requestInterceptor - A request interceptor used with each request (typically used to inject JWT tokens).
29
+ * @extends {Disposable}
30
+ */
31
+ class PortfolioGateway extends Disposable {
32
+ constructor(protocol, host, port, requestInterceptor) {
33
+ super();
34
+
35
+ this._started = false;
36
+ this._startPromise = null;
37
+
38
+ const protocolType = Enum.fromCode(ProtocolType, protocol.toUpperCase());
39
+
40
+ let requestInterceptorToUse;
41
+
42
+ if (requestInterceptor) {
43
+ requestInterceptorToUse = requestInterceptor;
44
+ } else {
45
+ requestInterceptorToUse = RequestInterceptor.EMPTY;
46
+ }
47
+
48
+ this._readPortfoliosEndpoint = EndpointBuilder.for('read-portfolios', 'read portfolios')
49
+ .withVerb(VerbType.GET)
50
+ .withProtocol(protocolType)
51
+ .withHost(host)
52
+ .withPort(port)
53
+ .withPathBuilder((pb) => {
54
+ pb.withLiteralParameter('portfolios', 'portfolios')
55
+ .withVariableParameter('portfolio', 'portfolio', 'portfolio', false);
56
+ })
57
+ .withRequestInterceptor(requestInterceptorToUse)
58
+ .withResponseInterceptor(responseInterceptorForPortfolioDeserialization)
59
+ .withErrorInterceptor(ErrorInterceptor.GENERAL)
60
+ .endpoint;
61
+
62
+ this._readPositionsEndpoint = EndpointBuilder.for('read-positions', 'read positions')
63
+ .withVerb(VerbType.GET)
64
+ .withProtocol(protocolType)
65
+ .withHost(host)
66
+ .withPort(port)
67
+ .withPathBuilder((pb) => {
68
+ pb.withLiteralParameter('portfolios', 'portfolios')
69
+ .withVariableParameter('portfolio', 'portfolio', 'portfolio', false)
70
+ .withLiteralParameter('positions', 'positions')
71
+ .withVariableParameter('position', 'position', 'position', false);
72
+ })
73
+ .withRequestInterceptor(requestInterceptorToUse)
74
+ .withResponseInterceptor(responseInterceptorForPositionDeserialization)
75
+ .withErrorInterceptor(ErrorInterceptor.GENERAL)
76
+ .endpoint;
77
+
78
+ this._readTransactionsEndpoint = EndpointBuilder.for('read-transactions', 'read transactions')
79
+ .withVerb(VerbType.GET)
80
+ .withProtocol(protocolType)
81
+ .withHost(host)
82
+ .withPort(port)
83
+ .withPathBuilder((pb) => {
84
+ pb.withLiteralParameter('portfolios', 'portfolios')
85
+ .withVariableParameter('portfolio', 'portfolio', 'portfolio', false)
86
+ .withLiteralParameter('positions', 'positions')
87
+ .withVariableParameter('position', 'position', 'position', false)
88
+ .withLiteralParameter('transactions', 'transactions');
89
+ })
90
+ .withQueryBuilder((qb) => {
91
+ qb.withLiteralParameter('mode', 'mode', 'text');
92
+ })
93
+ .withRequestInterceptor(RequestInterceptor.PLAIN_TEXT_RESPONSE)
94
+ .withRequestInterceptor(requestInterceptorToUse)
95
+ .withResponseInterceptor(responseInterceptorForTransactionDeserialization)
96
+ .withErrorInterceptor(ErrorInterceptor.GENERAL)
97
+ .endpoint;
98
+
99
+ this._readTransactionsReportEndpoint = EndpointBuilder.for('read-transactions', 'read transactions')
100
+ .withVerb(VerbType.GET)
101
+ .withProtocol(protocolType)
102
+ .withHost(host)
103
+ .withPort(port)
104
+ .withPathBuilder((pb) => {
105
+ pb.withLiteralParameter('portfolios', 'portfolios')
106
+ .withVariableParameter('portfolio', 'portfolio', 'portfolio', false)
107
+ .withLiteralParameter('positions', 'positions')
108
+ .withVariableParameter('position', 'position', 'position', true)
109
+ .withLiteralParameter('transactions', 'transactions')
110
+ .withLiteralParameter('formatted', 'formatted');
111
+ })
112
+ .withRequestInterceptor(requestInterceptorToUse)
113
+ .withResponseInterceptor(ResponseInterceptor.DATA)
114
+ .withErrorInterceptor(ErrorInterceptor.GENERAL)
115
+ .endpoint;
116
+ }
117
+
118
+ /**
119
+ * Initializes the connection to the remote server and returns a promise
120
+ * containing the current instance.
121
+ *
122
+ * @public
123
+ * @returns {Promise.<PortfolioGateway>}
124
+ */
125
+ start() {
126
+ return Promise.resolve()
127
+ .then(() => {
128
+ if (this._startPromise === null) {
129
+ this._startPromise = Promise.resolve()
130
+ .then(() => {
131
+ this._started = true;
132
+
133
+ return this;
134
+ }).catch((e) => {
135
+ this._startPromise = null;
136
+
137
+ throw e;
138
+ });
139
+ }
140
+
141
+ return this._startPromise;
142
+ });
143
+ }
144
+
145
+ /**
146
+ * Reads all portfolios for a user, or a single portfolio.
147
+ *
148
+ * @public
149
+ * @param {String=} portfolio
150
+ * @return {Promise.<Portfolio[]>}
151
+ */
152
+ readPortfolios(portfolio) {
153
+ return Promise.resolve()
154
+ .then(() => {
155
+ checkStart.call(this);
156
+
157
+ assert.argumentIsOptional(portfolio, 'portfolio', String);
158
+
159
+ return Gateway.invoke(this._readPortfoliosEndpoint, { portfolio: portfolio || '*' });
160
+ });
161
+ }
162
+
163
+ /**
164
+ * Retrieves positions for a user, a user's portfolio, or a single position.
165
+ *
166
+ * @public
167
+ * @param {String=} portfolio
168
+ * @param {String=} position
169
+ * @returns {Promise.<Position[]>}
170
+ */
171
+ readPositions(portfolio, position) {
172
+ return Promise.resolve()
173
+ .then(() => {
174
+ checkStart.call(this);
175
+
176
+ assert.argumentIsOptional(portfolio, 'portfolio', String);
177
+ assert.argumentIsOptional(position, 'position', String);
178
+
179
+ return Gateway.invoke(this._readPositionsEndpoint, { portfolio: portfolio, position: position || '*' });
180
+ });
181
+ }
182
+
183
+ /**
184
+ * Retrieves transactions for a portfolio, or a single position.
185
+ *
186
+ * @public
187
+ * @param {String} portfolio
188
+ * @param {String=} position
189
+ * @returns {Promise.<Transaction[]>}
190
+ */
191
+ readTransactions(portfolio, position) {
192
+ return Promise.resolve()
193
+ .then(() => {
194
+ checkStart.call(this);
195
+
196
+ assert.argumentIsRequired(portfolio, 'portfolio', String);
197
+ assert.argumentIsOptional(position, 'position', String);
198
+
199
+ return Gateway.invoke(this._readTransactionsEndpoint, { portfolio: portfolio, position: position || '*' });
200
+ });
201
+ }
202
+
203
+ getTransactionsFormatted(portfolio, position) {
204
+ return Promise.resolve()
205
+ .then(() => {
206
+ checkStart.call(this);
207
+
208
+ assert.argumentIsRequired(portfolio, 'portfolio', String);
209
+ assert.argumentIsOptional(position, 'position', String);
210
+
211
+ return Gateway.invoke(this._readTransactionsReportEndpoint, { portfolio: portfolio, position: position || '*' });
212
+ });
213
+ }
214
+
215
+ /**
216
+ * Creates and starts a new {@link PortfolioGateway} for use in the development environment.
217
+ *
218
+ * @public
219
+ * @static
220
+ * @param {RequestInterceptor=|Promise.<RequestInterceptor>=} requestInterceptor - A request interceptor used with each request (typically used to inject JWT tokens).
221
+ * @returns {Promise.<PortfolioGateway>}
222
+ */
223
+ static forDevelopment(requestInterceptor) {
224
+ return Promise.resolve(requestInterceptor)
225
+ .then((requestInterceptor) => {
226
+ assert.argumentIsOptional(requestInterceptor, 'requestInterceptor', RequestInterceptor, 'RequestInterceptor');
227
+
228
+ return start(new PortfolioGateway('https', Configuration.developmentHost, 443, requestInterceptor));
229
+ });
230
+ }
231
+
232
+ /**
233
+ * Creates and starts a new {@link PortfolioGateway} for use in the production environment.
234
+ *
235
+ * @public
236
+ * @static
237
+ * @param {RequestInterceptor=|Promise.<RequestInterceptor>=} requestInterceptor - A request interceptor used with each request (typically used to inject JWT tokens).
238
+ * @returns {Promise.<PortfolioGateway>}
239
+ */
240
+ static forProduction(requestInterceptor) {
241
+ return Promise.resolve(requestInterceptor)
242
+ .then((requestInterceptor) => {
243
+ assert.argumentIsOptional(requestInterceptor, 'requestInterceptor', RequestInterceptor, 'RequestInterceptor');
244
+
245
+ return start(new PortfolioGateway('https', Configuration.productionHost, 443, requestInterceptor));
246
+ });
247
+ }
248
+
249
+ _onDispose() {
250
+ return;
251
+ }
252
+
253
+ toString() {
254
+ return '[PortfolioGateway]';
255
+ }
256
+ }
257
+
258
+ const responseInterceptorForPortfolioDeserialization = ResponseInterceptor.fromDelegate((response, ignored) => {
259
+ return response.data;
260
+ });
261
+
262
+ const responseInterceptorForPositionDeserialization = ResponseInterceptor.fromDelegate((response, ignored) => {
263
+ return response.data;
264
+ });
265
+
266
+ const responseInterceptorForTransactionDeserialization = ResponseInterceptor.fromDelegate((response, ignored) => {
267
+ debugger;
268
+
269
+ const reviver = TransactionSchema.COMPLETE.schema.getReviver();
270
+
271
+ return JSON.parse(response.data, reviver);
272
+ });
273
+
274
+ function start(gateway) {
275
+ return gateway.start()
276
+ .then(() => {
277
+ return gateway;
278
+ });
279
+ }
280
+
281
+ function checkStart() {
282
+ if (this.getIsDisposed()) {
283
+ throw new Error('Unable to use gateway, the gateway has been disposed.');
284
+ }
285
+
286
+ if (!this._started) {
287
+ throw new Error('Unable to use gateway, the gateway has not started.');
288
+ }
289
+ }
290
+
291
+ return PortfolioGateway;
292
+ })();