forked from icastillejogomez/degiro-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeGiro.ts
370 lines (319 loc) · 11.7 KB
/
DeGiro.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Import modules
import * as async from 'async'
// Import interfaces
import { DeGiroClassInterface } from './interfaces'
// Import types
import {
DeGiroSettupType,
LoginResponseType,
AccountConfigType,
AccountDataType,
CashFoundType,
SearchProductResultType,
GetPorfolioConfigType,
SearchProductOptionsType,
OrderType,
CreateOrderResultType,
IsLoginOptionsType,
GetOrdersConfigType,
GetOrdersResultType,
GetAccountStateOptionsType,
AccountReportsType,
AccountInfoType,
GetHistoricalOrdersOptionsType,
HistoricalOrdersType,
FavouriteProductType,
StockType,
GetNewsOptionsType,
NewsType,
WebUserSettingType,
ConfigDictionaryType,
i18nMessagesType,
WebSettingsType,
GetPopularStocksConfigType,
GetTransactionsOptionsType,
TransactionType,
} from './types'
// Import requests
import {
loginRequest,
getAccountConfigRequest,
getAccountDataRequest,
getPortfolioRequest,
getProductsByIdsRequest,
searchProductRequest,
createOrderRequest,
executeOrderRequest,
deleteOrderRequest,
logoutRequest,
getOrdersRequest,
getAccountStateRequest,
getConfigDictionaryRequest,
getAccountInfoRequest,
getWebi18nMessagesRequest,
getNewsRequest,
getWebSettingsRequest,
getWebUserSettingsRequest,
getAccountReportsRequest,
getCashFundstRequest,
getPopularStocksRequest,
getTransactionsRequest,
} from './api'
import { runInThisContext } from 'vm'
/**
* @class DeGiro
* @description Main class of DeGiro Unofficial API.
*/
export class DeGiro implements DeGiroClassInterface {
/* Properties */
private readonly username: string | undefined
private readonly pwd: string | undefined
private readonly oneTimePassword: string | undefined
private jsessionId: string | undefined
private accountConfig: AccountConfigType | undefined
private accountData: AccountDataType | undefined
/* Constructor and generator function */
constructor(params: DeGiroSettupType = {}) {
let { username, pwd, oneTimePassword, jsessionId } = params
username = username || process.env['DEGIRO_USER']
pwd = pwd || process.env['DEGIRO_PWD']
oneTimePassword = oneTimePassword || process.env['DEGIRO_OTP']
jsessionId = jsessionId || process.env['DEGIRO_JSESSIONID']
if (!username && !jsessionId) throw new Error('DeGiro api needs an username to access')
if (!pwd && !jsessionId) throw new Error('DeGiro api needs an password to access')
this.username = username
this.pwd = pwd
this.oneTimePassword = oneTimePassword
this.jsessionId = jsessionId
}
static create(params: DeGiroSettupType): DeGiro {
return new DeGiro(params)
}
/* Session methods */
login(): Promise<AccountDataType> {
if (this.jsessionId) return this.loginWithJSESSIONID(this.jsessionId)
return new Promise((resolve, reject) => {
loginRequest({
username: this.username as string,
pwd: this.pwd as string,
oneTimePassword: this.oneTimePassword,
})
.then((loginResponse: LoginResponseType) => {
if (!loginResponse.sessionId) reject('Login response have not a sessionId field')
else return this.getAccountConfig(loginResponse.sessionId)
})
.then(() => this.getAccountData())
.then(resolve)
.catch(reject)
})
}
logout(): Promise<void> {
return new Promise((resolve, reject) => {
if (!this.accountData || !this.accountConfig) {
return reject('You must log in first')
}
logoutRequest(this.accountData, this.accountConfig)
.then(() => {
delete this.accountData
delete this.accountConfig
resolve()
})
.catch(reject)
})
}
isLogin(options?: IsLoginOptionsType): boolean | Promise<boolean> {
if (!options || !options.secure) return this.hasSessionId() && !!this.accountData
return new Promise((resolve) => {
this.getAccountConfig()
.then(() => resolve(true))
.catch(() => resolve(false))
})
}
private hasSessionId = (): boolean => !!this.accountConfig && !!this.accountConfig.data && !!this.accountConfig.data.sessionId
private loginWithJSESSIONID(jsessionId: string): Promise<AccountDataType> {
return new Promise((resolve, reject) => {
this.getAccountConfig(jsessionId)
.then(() => this.getAccountData())
.then((accountData) => {
this.jsessionId = undefined // Remove the jsessionId to prevent reuse
resolve(accountData)
})
.catch(reject)
})
}
getJSESSIONID = () => this.hasSessionId() ? (<AccountConfigType>this.accountConfig).data.sessionId : undefined
/* Account methods */
getAccountConfig(sessionId?: string): Promise<AccountConfigType> {
return new Promise((resolve, reject) => {
if (!sessionId && !this.hasSessionId()) {
return reject('You must log in first or provide a JSESSIONID')
}
getAccountConfigRequest(sessionId || (<AccountConfigType>this.accountConfig).data.sessionId)
.then((accountConfig: AccountConfigType) => {
this.accountConfig = accountConfig
resolve(accountConfig)
})
.catch(reject)
})
}
getAccountData(): Promise<AccountDataType> {
return new Promise((resolve, reject) => {
if (!this.hasSessionId()) {
return reject('You must log in first')
}
getAccountDataRequest(<AccountConfigType>this.accountConfig)
.then((accountData: AccountDataType) => {
this.accountData = accountData
resolve(accountData)
})
.catch(reject)
})
}
getAccountState(options: GetAccountStateOptionsType): Promise<any[]> {
if (!this.hasSessionId()) {
return Promise.reject('You must log in first')
}
return getAccountStateRequest(<AccountDataType>this.accountData, <AccountConfigType>this.accountConfig, options)
}
getAccountReports(): Promise<AccountReportsType> {
if (!this.hasSessionId()) {
return Promise.reject('You must log in first')
}
return getAccountReportsRequest(<AccountDataType>this.accountData, <AccountConfigType>this.accountConfig)
}
getAccountInfo(): Promise<AccountInfoType> {
if (!this.hasSessionId()) {
return Promise.reject('You must log in first')
}
return getAccountInfoRequest(<AccountDataType>this.accountData, <AccountConfigType>this.accountConfig)
}
/* Search methods */
searchProduct(options: SearchProductOptionsType): Promise<SearchProductResultType[]> {
if (!this.hasSessionId()) {
return Promise.reject('You must log in first')
}
return searchProductRequest(options, <AccountDataType>this.accountData, <AccountConfigType>this.accountConfig)
}
/* Cash Funds methods */
getCashFunds(): Promise<CashFoundType[]> {
if (!this.hasSessionId()) {
return Promise.reject('You must log in first')
}
return getCashFundstRequest(<AccountDataType>this.accountData, <AccountConfigType>this.accountConfig)
}
/* Porfolio methods */
getPortfolio(config: GetPorfolioConfigType): Promise<any[]> {
return new Promise((resolve, reject) => {
if (!this.hasSessionId()) {
return reject('You must log in first')
}
getPortfolioRequest(<AccountDataType>this.accountData, <AccountConfigType>this.accountConfig, config)
.then(portfolio => this.completePortfolioDetails(portfolio, config.getProductDetails || false))
.then(resolve)
.catch(reject)
})
}
private completePortfolioDetails(portfolio: any[], getProductDetails: boolean): Promise<any[]> {
if (!getProductDetails) return Promise.resolve(portfolio)
return new Promise((resolve, reject) => {
async.map(portfolio, (position, next) => {
if (position.positionType !== 'PRODUCT') return next(null, position)
this.getProductsByIds([(position.id)])
.then((product) => {
position.productData = product[position.id]
next(null, position)
})
.catch(error => next(error))
// tslint:disable-next-line: align
}, (error, portfolio) => {
if (error) return reject(error)
resolve(<any[]>portfolio)
})
})
}
/* Stocks methods */
getFavouriteProducts(): Promise<FavouriteProductType[]> {
return new Promise((resolve, reject) => {
reject('Method not implemented')
})
}
getPopularStocks(config: GetPopularStocksConfigType = {}): Promise<StockType[]> {
if (!this.hasSessionId()) {
return Promise.reject('You must log in first')
}
return getPopularStocksRequest(<AccountDataType>this.accountData, <AccountConfigType>this.accountConfig, config)
}
/* Orders methods */
getOrders(config: GetOrdersConfigType): Promise<GetOrdersResultType> {
if (!this.hasSessionId()) {
return Promise.reject('You must log in first')
}
return getOrdersRequest(<AccountDataType>this.accountData, <AccountConfigType>this.accountConfig, config)
}
getHistoricalOrders(options: GetHistoricalOrdersOptionsType): Promise<HistoricalOrdersType> {
return new Promise((resolve, reject) => {
reject('Method not implemented')
})
}
createOrder(order: OrderType): Promise<CreateOrderResultType> {
if (!this.hasSessionId()) {
return Promise.reject('You must log in first')
}
return createOrderRequest(order, <AccountDataType>this.accountData, <AccountConfigType>this.accountConfig)
}
executeOrder(order: OrderType, executeId: String): Promise<String> {
if (!this.hasSessionId()) {
return Promise.reject('You must log in first')
}
return executeOrderRequest(order, executeId, <AccountDataType>this.accountData, <AccountConfigType>this.accountConfig)
}
deleteOrder(orderId: String): Promise<void> {
if (!this.hasSessionId()) {
return Promise.reject('You must log in first')
}
return deleteOrderRequest(orderId, <AccountDataType>this.accountData, <AccountConfigType>this.accountConfig)
}
getTransactions(options: GetTransactionsOptionsType): Promise<TransactionType[]> {
if (!this.hasSessionId()) {
return Promise.reject('You must log in first');
}
return getTransactionsRequest(<AccountDataType>this.accountData, <AccountConfigType>this.accountConfig, options);
}
/* Miscellaneous methods */
getProductsByIds(ids: string[]): Promise<any[]> {
if (!this.hasSessionId()) {
return Promise.reject('You must log in first')
}
return getProductsByIdsRequest(ids, <AccountDataType>this.accountData, <AccountConfigType>this.accountConfig)
}
getNews(options: GetNewsOptionsType): Promise<NewsType> {
if (!this.hasSessionId()) {
return Promise.reject('You must log in first')
}
return getNewsRequest(options, <AccountDataType>this.accountData, <AccountConfigType>this.accountConfig)
}
getWebi18nMessages(lang: string = 'es_ES'): Promise<i18nMessagesType> {
if (!this.hasSessionId()) {
return Promise.reject('You must log in first')
}
return getWebi18nMessagesRequest(lang, <AccountDataType>this.accountData, <AccountConfigType>this.accountConfig)
}
getWebSettings(): Promise<WebSettingsType> {
if (!this.hasSessionId()) {
return Promise.reject('You must log in first')
}
return getWebSettingsRequest(<AccountDataType>this.accountData, <AccountConfigType>this.accountConfig)
}
getWebUserSettings(): Promise<WebUserSettingType> {
if (!this.hasSessionId()) {
return Promise.reject('You must log in first')
}
return getWebUserSettingsRequest(<AccountDataType>this.accountData, <AccountConfigType>this.accountConfig)
}
getConfigDictionary(): Promise<ConfigDictionaryType> {
if (!this.hasSessionId()) {
return Promise.reject('You must log in first')
}
return getConfigDictionaryRequest(<AccountDataType>this.accountData, <AccountConfigType>this.accountConfig)
}
}