tigeropen 0.4.0 → 0.5.7
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.
- package/README.md +82 -2
- package/dist/cjs/auth/authentication.js +74 -0
- package/dist/cjs/auth/index.js +24 -0
- package/dist/cjs/auth/oauth2/client-registration.js +44 -0
- package/dist/cjs/auth/oauth2/device-authorization.js +40 -0
- package/dist/cjs/auth/oauth2/errors.js +64 -0
- package/dist/cjs/auth/oauth2/http-utils.js +112 -0
- package/dist/cjs/auth/oauth2/index.js +42 -0
- package/dist/cjs/auth/oauth2/loopback-receiver.js +215 -0
- package/dist/cjs/auth/oauth2/metadata.js +34 -0
- package/dist/cjs/auth/oauth2/oauth2-authentication.js +74 -0
- package/dist/cjs/auth/oauth2/session-manager.js +762 -0
- package/dist/cjs/auth/oauth2/token-store.js +252 -0
- package/dist/cjs/auth/oauth2/token.js +76 -0
- package/dist/cjs/client/api-response.js +28 -7
- package/dist/cjs/client/case-convert.js +4 -3
- package/dist/cjs/client/http-client.js +124 -8
- package/dist/cjs/client/token-methods.js +42 -0
- package/dist/cjs/config/client-config.js +115 -16
- package/dist/cjs/config/config-parser.js +19 -9
- package/dist/cjs/config/index.js +4 -1
- package/dist/cjs/config/token-manager.js +54 -4
- package/dist/cjs/index.js +53 -2
- package/dist/cjs/model/enums.js +18 -1
- package/dist/cjs/model/order-helpers.js +149 -0
- package/dist/cjs/push/index.js +1 -0
- package/dist/cjs/push/pb/Request.js +12 -1
- package/dist/cjs/push/proto-message.js +5 -1
- package/dist/cjs/push/push-client.js +69 -25
- package/dist/cjs/push/tick-util.js +172 -0
- package/dist/cjs/quote/quote-client.js +300 -70
- package/dist/cjs/trade/trade-client.js +186 -16
- package/dist/cjs/version.js +4 -0
- package/dist/esm/auth/authentication.js +68 -0
- package/dist/esm/auth/index.js +3 -0
- package/dist/esm/auth/oauth2/client-registration.js +40 -0
- package/dist/esm/auth/oauth2/device-authorization.js +36 -0
- package/dist/esm/auth/oauth2/errors.js +59 -0
- package/dist/esm/auth/oauth2/http-utils.js +105 -0
- package/dist/esm/auth/oauth2/index.js +11 -0
- package/dist/esm/auth/oauth2/loopback-receiver.js +178 -0
- package/dist/esm/auth/oauth2/metadata.js +30 -0
- package/dist/esm/auth/oauth2/oauth2-authentication.js +70 -0
- package/dist/esm/auth/oauth2/session-manager.js +725 -0
- package/dist/esm/auth/oauth2/token-store.js +214 -0
- package/dist/esm/auth/oauth2/token.js +71 -0
- package/dist/esm/client/api-request.js +1 -1
- package/dist/esm/client/api-response.js +29 -8
- package/dist/esm/client/case-convert.js +4 -3
- package/dist/esm/client/http-client.js +128 -12
- package/dist/esm/client/index.js +6 -6
- package/dist/esm/client/token-methods.js +37 -0
- package/dist/esm/config/client-config.js +118 -20
- package/dist/esm/config/config-parser.js +19 -9
- package/dist/esm/config/index.js +4 -3
- package/dist/esm/config/token-manager.js +55 -5
- package/dist/esm/index.js +15 -1
- package/dist/esm/logger/index.js +1 -1
- package/dist/esm/model/contract-helpers.js +1 -1
- package/dist/esm/model/enums.js +17 -0
- package/dist/esm/model/order-helpers.js +142 -1
- package/dist/esm/model/order-status.js +1 -1
- package/dist/esm/package.json +3 -0
- package/dist/esm/push/index.js +6 -5
- package/dist/esm/push/pb/PushData.js +11 -11
- package/dist/esm/push/pb/QuoteBasicData.js +1 -1
- package/dist/esm/push/pb/Request.js +12 -1
- package/dist/esm/push/pb/Response.js +1 -1
- package/dist/esm/push/pb/index.js +17 -17
- package/dist/esm/push/proto-message.js +8 -4
- package/dist/esm/push/push-client.js +76 -32
- package/dist/esm/push/tick-util.js +165 -0
- package/dist/esm/quote/index.js +1 -1
- package/dist/esm/quote/quote-client.js +269 -72
- package/dist/esm/signer/index.js +2 -2
- package/dist/esm/trade/index.js +1 -1
- package/dist/esm/trade/trade-client.js +156 -19
- package/dist/esm/version.js +1 -0
- package/dist/types/auth/authentication.d.ts +98 -0
- package/dist/types/auth/index.d.ts +4 -0
- package/dist/types/auth/oauth2/client-registration.d.ts +32 -0
- package/dist/types/auth/oauth2/device-authorization.d.ts +40 -0
- package/dist/types/auth/oauth2/errors.d.ts +46 -0
- package/dist/types/auth/oauth2/http-utils.d.ts +21 -0
- package/dist/types/auth/oauth2/index.d.ts +16 -0
- package/dist/types/auth/oauth2/loopback-receiver.d.ts +29 -0
- package/dist/types/auth/oauth2/metadata.d.ts +32 -0
- package/dist/types/auth/oauth2/oauth2-authentication.d.ts +31 -0
- package/dist/types/auth/oauth2/session-manager.d.ts +241 -0
- package/dist/types/auth/oauth2/token-store.d.ts +49 -0
- package/dist/types/auth/oauth2/token.d.ts +56 -0
- package/dist/types/client/api-response.d.ts +6 -4
- package/dist/types/client/case-convert.d.ts +3 -1
- package/dist/types/client/http-client.d.ts +32 -1
- package/dist/types/client/token-methods.d.ts +34 -0
- package/dist/types/config/client-config.d.ts +22 -1
- package/dist/types/config/index.d.ts +3 -1
- package/dist/types/config/token-manager.d.ts +27 -3
- package/dist/types/index.d.ts +22 -1
- package/dist/types/model/enums.d.ts +18 -2
- package/dist/types/model/order-helpers.d.ts +52 -1
- package/dist/types/model/order.d.ts +127 -10
- package/dist/types/model/quote-requests.d.ts +27 -15
- package/dist/types/model/quote.d.ts +151 -24
- package/dist/types/model/trade-requests.d.ts +123 -4
- package/dist/types/model/trade.d.ts +121 -17
- package/dist/types/push/callbacks.d.ts +3 -7
- package/dist/types/push/index.d.ts +2 -1
- package/dist/types/push/pb/Request.d.ts +1 -0
- package/dist/types/push/proto-message.d.ts +4 -1
- package/dist/types/push/push-client.d.ts +38 -3
- package/dist/types/push/tick-util.d.ts +30 -0
- package/dist/types/quote/quote-client.d.ts +101 -36
- package/dist/types/trade/trade-client.d.ts +69 -14
- package/dist/types/version.d.ts +1 -0
- package/package.json +9 -4
|
@@ -8,11 +8,12 @@
|
|
|
8
8
|
*
|
|
9
9
|
* Priority: environment variables > code options (incl. config file) > defaults
|
|
10
10
|
*/
|
|
11
|
-
import { readFileSync, existsSync } from 'fs';
|
|
12
|
-
import { join } from 'path';
|
|
11
|
+
import { readFileSync, existsSync, statSync } from 'fs';
|
|
12
|
+
import { join, dirname } from 'path';
|
|
13
13
|
import { homedir, networkInterfaces } from 'os';
|
|
14
|
-
import { parsePropertiesString } from './config-parser';
|
|
15
|
-
import { queryDomains, resolveDynamicServerUrl, resolveDynamicQuoteServerUrl } from './domain';
|
|
14
|
+
import { parsePropertiesString } from './config-parser.js';
|
|
15
|
+
import { queryDomains, resolveDynamicServerUrl, resolveDynamicQuoteServerUrl } from './domain.js';
|
|
16
|
+
import { TokenManager } from './token-manager.js';
|
|
16
17
|
/** Default config file name */
|
|
17
18
|
const CONFIG_FILE_NAME = 'tiger_openapi_config.properties';
|
|
18
19
|
/** Defaults */
|
|
@@ -21,11 +22,20 @@ const DEFAULT_TIMEOUT = 15;
|
|
|
21
22
|
const DEFAULT_SERVER_URL = 'https://openapi.tigerfintech.com/gateway';
|
|
22
23
|
/** Tiger public key for response signature verification (Base64-encoded) */
|
|
23
24
|
const TIGER_PUBLIC_KEY = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDNF3G8SoEcCZh2rshUbayDgLLrj6rKgzNMxDL2HSnKcB0+GPOsndqSv+a4IBu9+I3fyBp5hkyMMG2+AXugd9pMpy6VxJxlNjhX1MYbNTZJUT4nudki4uh+LMOkIBHOceGNXjgB+cXqmlUnjlqha/HgboeHSnSgpM3dKSJQlIOsDwIDAQAB';
|
|
25
|
+
/** Sandbox / QA tiger public key for response signature verification */
|
|
26
|
+
export const SANDBOX_TIGER_PUBLIC_KEY = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCbm21i11hgAENGd3/f280PSe4g9YGkS3TEXBYMidihTvHHf' +
|
|
27
|
+
'+tJ0PYD0o3PruI0hl3qhEjHTAxb75T5YD3SGK4IBhHn/Rk6mhqlGgI+bBrBVYaXixmHfRo75RpUUuWACyeqQk' +
|
|
28
|
+
'ZckgR0McxuW9xRMIa2cXZOoL1E4SL4lXKGhKoWbwIDAQAB';
|
|
24
29
|
/** Environment variable names */
|
|
25
30
|
const ENV_TIGER_ID = 'TIGEROPEN_TIGER_ID';
|
|
26
31
|
const ENV_PRIVATE_KEY = 'TIGEROPEN_PRIVATE_KEY';
|
|
27
32
|
const ENV_ACCOUNT = 'TIGEROPEN_ACCOUNT';
|
|
33
|
+
const ENV_SECRET_KEY = 'TIGEROPEN_SECRET_KEY';
|
|
28
34
|
const ENV_TOKEN = 'TIGEROPEN_TOKEN';
|
|
35
|
+
const ENV_TOKEN_FILE = 'TIGEROPEN_TOKEN_FILE';
|
|
36
|
+
const ENV_TIGER_PUBLIC_KEY = 'TIGEROPEN_TIGER_PUBLIC_KEY';
|
|
37
|
+
const ENV_SERVER_URL = 'TIGEROPEN_SERVER_URL';
|
|
38
|
+
const ENV_QUOTE_SERVER_URL = 'TIGEROPEN_QUOTE_SERVER_URL';
|
|
29
39
|
/**
|
|
30
40
|
* Load and parse a Java-style properties file.
|
|
31
41
|
*
|
|
@@ -98,12 +108,32 @@ export function createClientConfig(options) {
|
|
|
98
108
|
const opts = options ?? {};
|
|
99
109
|
// Load properties: explicit path > auto-discovery
|
|
100
110
|
let fileProps = {};
|
|
111
|
+
let configFileDir;
|
|
101
112
|
if (opts.propertiesFilePath) {
|
|
102
|
-
|
|
113
|
+
// Support both directory path and full file path
|
|
114
|
+
let filePath = opts.propertiesFilePath;
|
|
115
|
+
if (existsSync(filePath) && statSync(filePath).isDirectory()) {
|
|
116
|
+
configFileDir = filePath;
|
|
117
|
+
filePath = join(filePath, CONFIG_FILE_NAME);
|
|
118
|
+
}
|
|
119
|
+
else if (!existsSync(filePath) && !filePath.endsWith('.properties')) {
|
|
120
|
+
// Path doesn't exist as-is; try treating it as a directory
|
|
121
|
+
const asDir = join(filePath, CONFIG_FILE_NAME);
|
|
122
|
+
if (existsSync(asDir)) {
|
|
123
|
+
configFileDir = filePath;
|
|
124
|
+
filePath = asDir;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
// It's a file path: derive directory from it
|
|
129
|
+
configFileDir = dirname(filePath);
|
|
130
|
+
}
|
|
131
|
+
fileProps = loadPropertiesFile(filePath);
|
|
103
132
|
}
|
|
104
133
|
else {
|
|
105
134
|
const discovered = discoverConfigFile();
|
|
106
135
|
if (discovered) {
|
|
136
|
+
configFileDir = dirname(discovered);
|
|
107
137
|
fileProps = loadPropertiesFile(discovered);
|
|
108
138
|
}
|
|
109
139
|
}
|
|
@@ -111,15 +141,20 @@ export function createClientConfig(options) {
|
|
|
111
141
|
let tigerId = opts.tigerId || fileProps['tiger_id'] || '';
|
|
112
142
|
let privateKey = opts.privateKey || resolvePrivateKey(fileProps) || '';
|
|
113
143
|
let account = opts.account || fileProps['account'] || '';
|
|
144
|
+
let secretKey = opts.secretKey || fileProps['secret_key'] || undefined;
|
|
114
145
|
let token = opts.token || fileProps['token'];
|
|
115
146
|
const license = opts.license || fileProps['license'];
|
|
116
147
|
const language = opts.language || fileProps['language'] || DEFAULT_LANGUAGE;
|
|
117
148
|
const timezone = opts.timezone || fileProps['timezone'];
|
|
149
|
+
// tiger_public_key from properties file (e.g. for QA/sandbox environments)
|
|
150
|
+
let tigerPublicKey = opts.tigerPublicKey || fileProps['tiger_public_key'] || TIGER_PUBLIC_KEY;
|
|
118
151
|
// Environment variables override everything
|
|
119
152
|
const envTigerId = process.env[ENV_TIGER_ID];
|
|
120
153
|
const envPrivateKey = process.env[ENV_PRIVATE_KEY];
|
|
121
154
|
const envAccount = process.env[ENV_ACCOUNT];
|
|
155
|
+
const envSecretKey = process.env[ENV_SECRET_KEY];
|
|
122
156
|
const envToken = process.env[ENV_TOKEN];
|
|
157
|
+
const envTigerPublicKey = process.env[ENV_TIGER_PUBLIC_KEY];
|
|
123
158
|
if (envTigerId) {
|
|
124
159
|
tigerId = envTigerId;
|
|
125
160
|
}
|
|
@@ -129,9 +164,47 @@ export function createClientConfig(options) {
|
|
|
129
164
|
if (envAccount) {
|
|
130
165
|
account = envAccount;
|
|
131
166
|
}
|
|
167
|
+
if (envSecretKey) {
|
|
168
|
+
secretKey = envSecretKey;
|
|
169
|
+
}
|
|
132
170
|
if (envToken) {
|
|
133
171
|
token = envToken;
|
|
134
172
|
}
|
|
173
|
+
if (envTigerPublicKey) {
|
|
174
|
+
tigerPublicKey = envTigerPublicKey;
|
|
175
|
+
}
|
|
176
|
+
// Token loading priority: env TIGEROPEN_TOKEN > tokenLoader > token file (TIGEROPEN_TOKEN_FILE or default)
|
|
177
|
+
if (!token) {
|
|
178
|
+
if (opts.tokenLoader) {
|
|
179
|
+
// tokenLoader is async-capable; we do a best-effort synchronous call here.
|
|
180
|
+
// If the loader returns a Promise, the token will be set later via HttpClient.
|
|
181
|
+
try {
|
|
182
|
+
const result = opts.tokenLoader();
|
|
183
|
+
if (result instanceof Promise) {
|
|
184
|
+
// Cannot await in sync constructor; HttpClient.startTokenAutoRefresh() will
|
|
185
|
+
// resolve this Promise and sync the token after construction.
|
|
186
|
+
}
|
|
187
|
+
else if (result) {
|
|
188
|
+
token = result;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
catch {
|
|
192
|
+
// loader failed; leave token undefined
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
// Load from token file: TIGEROPEN_TOKEN_FILE env var > config dir > default
|
|
197
|
+
const tokenFilePath = process.env[ENV_TOKEN_FILE]
|
|
198
|
+
|| (configFileDir ? join(configFileDir, 'tiger_openapi_token.properties') : 'tiger_openapi_token.properties');
|
|
199
|
+
const tm = new TokenManager({ filePath: tokenFilePath });
|
|
200
|
+
try {
|
|
201
|
+
token = tm.loadTokenSync();
|
|
202
|
+
}
|
|
203
|
+
catch {
|
|
204
|
+
// file not found or no token field; leave token undefined
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
135
208
|
// Validate required fields
|
|
136
209
|
if (!tigerId) {
|
|
137
210
|
throw new Error(`tigerId is required. Set it via options.tigerId, a config file, or the ${ENV_TIGER_ID} environment variable.`);
|
|
@@ -139,47 +212,72 @@ export function createClientConfig(options) {
|
|
|
139
212
|
if (!privateKey) {
|
|
140
213
|
throw new Error(`privateKey is required. Set it via options.privateKey, a config file, or the ${ENV_PRIVATE_KEY} environment variable.`);
|
|
141
214
|
}
|
|
142
|
-
// Resolve server URL: explicit > dynamic domain > default
|
|
215
|
+
// Resolve server URL: env var > explicit opts > file > dynamic domain > default
|
|
143
216
|
const enableDynamicDomain = opts.enableDynamicDomain ?? true;
|
|
144
217
|
let serverUrl;
|
|
145
218
|
let quoteServerUrl;
|
|
146
|
-
|
|
219
|
+
// Query dynamic domains once (may spawn a child process); reuse for both URLs.
|
|
220
|
+
let domainConf = {};
|
|
221
|
+
const envServerUrl = process.env[ENV_SERVER_URL];
|
|
222
|
+
const envQuoteServerUrl = process.env[ENV_QUOTE_SERVER_URL];
|
|
223
|
+
const needServerUrl = !envServerUrl && !opts.serverUrl && !fileProps['server_url'];
|
|
224
|
+
const needQuoteServerUrl = !envQuoteServerUrl && !opts.quoteServerUrl && !fileProps['quote_server_url'];
|
|
225
|
+
if (enableDynamicDomain && (needServerUrl || needQuoteServerUrl)) {
|
|
226
|
+
domainConf = queryDomains(license);
|
|
227
|
+
}
|
|
228
|
+
if (envServerUrl) {
|
|
229
|
+
serverUrl = envServerUrl;
|
|
230
|
+
}
|
|
231
|
+
else if (opts.serverUrl) {
|
|
147
232
|
serverUrl = opts.serverUrl;
|
|
148
233
|
}
|
|
234
|
+
else if (fileProps['server_url']) {
|
|
235
|
+
serverUrl = fileProps['server_url'];
|
|
236
|
+
}
|
|
149
237
|
else {
|
|
150
|
-
|
|
151
|
-
if (enableDynamicDomain) {
|
|
152
|
-
const domainConf = queryDomains(license);
|
|
153
|
-
dynamicUrl = resolveDynamicServerUrl(domainConf, license);
|
|
154
|
-
}
|
|
238
|
+
const dynamicUrl = resolveDynamicServerUrl(domainConf, license);
|
|
155
239
|
serverUrl = dynamicUrl || DEFAULT_SERVER_URL;
|
|
156
240
|
}
|
|
157
|
-
// Resolve quote server URL: explicit > dynamic domain > serverUrl fallback
|
|
158
|
-
if (
|
|
241
|
+
// Resolve quote server URL: env var > explicit opts > file > dynamic domain > serverUrl fallback
|
|
242
|
+
if (envQuoteServerUrl) {
|
|
243
|
+
quoteServerUrl = envQuoteServerUrl;
|
|
244
|
+
}
|
|
245
|
+
else if (opts.quoteServerUrl) {
|
|
159
246
|
quoteServerUrl = opts.quoteServerUrl;
|
|
160
247
|
}
|
|
248
|
+
else if (fileProps['quote_server_url']) {
|
|
249
|
+
quoteServerUrl = fileProps['quote_server_url'];
|
|
250
|
+
}
|
|
161
251
|
else {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
252
|
+
// Only use dynamic domain if serverUrl itself came from dynamic resolution (not pinned by env/opts/file).
|
|
253
|
+
// When serverUrl is pinned, prefer it as the quote fallback to avoid mixing environments.
|
|
254
|
+
const serverUrlPinned = !!(envServerUrl || opts.serverUrl || fileProps['server_url']);
|
|
255
|
+
if (!serverUrlPinned) {
|
|
256
|
+
const dynamicQuoteUrl = resolveDynamicQuoteServerUrl(domainConf, license);
|
|
257
|
+
quoteServerUrl = dynamicQuoteUrl || serverUrl;
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
quoteServerUrl = serverUrl;
|
|
166
261
|
}
|
|
167
|
-
quoteServerUrl = dynamicQuoteUrl || serverUrl;
|
|
168
262
|
}
|
|
169
263
|
return {
|
|
170
264
|
tigerId,
|
|
171
265
|
privateKey,
|
|
172
266
|
account,
|
|
267
|
+
secretKey: secretKey || undefined,
|
|
173
268
|
license,
|
|
174
269
|
language,
|
|
175
270
|
timezone,
|
|
176
271
|
timeout: opts.timeout ?? DEFAULT_TIMEOUT,
|
|
177
272
|
token,
|
|
178
273
|
tokenRefreshDuration: opts.tokenRefreshDuration,
|
|
274
|
+
tokenCheckInterval: opts.tokenCheckInterval,
|
|
275
|
+
tokenLoader: opts.tokenLoader,
|
|
276
|
+
tokenWriter: opts.tokenWriter,
|
|
179
277
|
serverUrl,
|
|
180
278
|
quoteServerUrl,
|
|
181
279
|
deviceId: detectDeviceId(),
|
|
182
|
-
tigerPublicKey
|
|
280
|
+
tigerPublicKey,
|
|
183
281
|
};
|
|
184
282
|
}
|
|
185
283
|
/**
|
|
@@ -33,34 +33,34 @@ export function parsePropertiesString(content) {
|
|
|
33
33
|
let continuation = false;
|
|
34
34
|
for (const rawLine of lines) {
|
|
35
35
|
if (continuation) {
|
|
36
|
-
// 续行:去除前导空格后拼接
|
|
37
36
|
const trimmedLeft = rawLine.replace(/^[ \t]+/, '');
|
|
38
|
-
if (trimmedLeft.
|
|
37
|
+
if (trimmedLeft === '' || trimmedLeft.startsWith('#') || trimmedLeft.startsWith('!')) {
|
|
38
|
+
continuation = false;
|
|
39
|
+
}
|
|
40
|
+
else if (endsWithContinuation(trimmedLeft)) {
|
|
39
41
|
currentLine += trimmedLeft.slice(0, -1);
|
|
40
42
|
continue;
|
|
41
43
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
+
else {
|
|
45
|
+
currentLine += trimmedLeft;
|
|
46
|
+
continuation = false;
|
|
47
|
+
}
|
|
44
48
|
}
|
|
45
49
|
else {
|
|
46
50
|
const trimmed = rawLine.trim();
|
|
47
|
-
// 跳过空行
|
|
48
51
|
if (trimmed === '') {
|
|
49
52
|
continue;
|
|
50
53
|
}
|
|
51
|
-
// 跳过注释行
|
|
52
54
|
if (trimmed.startsWith('#') || trimmed.startsWith('!')) {
|
|
53
55
|
continue;
|
|
54
56
|
}
|
|
55
|
-
|
|
56
|
-
if (trimmed.endsWith('\\')) {
|
|
57
|
+
if (endsWithContinuation(trimmed)) {
|
|
57
58
|
currentLine = trimmed.slice(0, -1);
|
|
58
59
|
continuation = true;
|
|
59
60
|
continue;
|
|
60
61
|
}
|
|
61
62
|
currentLine = trimmed;
|
|
62
63
|
}
|
|
63
|
-
// 解析键值对
|
|
64
64
|
const result = parseKeyValue(currentLine);
|
|
65
65
|
if (result) {
|
|
66
66
|
props[result[0]] = result[1];
|
|
@@ -76,6 +76,16 @@ export function parsePropertiesString(content) {
|
|
|
76
76
|
}
|
|
77
77
|
return props;
|
|
78
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* 判断一行是否以续行标记结尾(奇数个末尾反斜杠)。
|
|
81
|
+
*/
|
|
82
|
+
function endsWithContinuation(line) {
|
|
83
|
+
let count = 0;
|
|
84
|
+
for (let i = line.length - 1; i >= 0 && line[i] === '\\'; i--) {
|
|
85
|
+
count++;
|
|
86
|
+
}
|
|
87
|
+
return count % 2 === 1;
|
|
88
|
+
}
|
|
79
89
|
/**
|
|
80
90
|
* 解析单行键值对,支持 = 和 : 分隔符。
|
|
81
91
|
* 值中可以包含 = 或 :,只按第一个分隔符拆分。
|
package/dist/esm/config/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 配置层导出
|
|
3
3
|
*/
|
|
4
|
-
export { parsePropertiesFile, parsePropertiesString } from './config-parser';
|
|
5
|
-
export { createClientConfig, loadPropertiesFile } from './client-config';
|
|
6
|
-
export {
|
|
4
|
+
export { parsePropertiesFile, parsePropertiesString } from './config-parser.js';
|
|
5
|
+
export { createClientConfig, loadPropertiesFile, SANDBOX_TIGER_PUBLIC_KEY } from './client-config.js';
|
|
6
|
+
export { TokenManager } from './token-manager.js';
|
|
7
|
+
export { queryDomains, resolveDynamicServerUrl, resolveDynamicQuoteServerUrl } from './domain.js';
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import { readFileSync, writeFileSync, mkdirSync } from 'fs';
|
|
8
8
|
import { dirname } from 'path';
|
|
9
|
-
import { parsePropertiesString } from './config-parser';
|
|
9
|
+
import { parsePropertiesString } from './config-parser.js';
|
|
10
10
|
/** 默认 Token 文件名 */
|
|
11
11
|
const DEFAULT_TOKEN_FILE = 'tiger_openapi_token.properties';
|
|
12
12
|
/** 默认刷新间隔(毫秒):24 小时 */
|
|
@@ -18,6 +18,7 @@ export class TokenManager {
|
|
|
18
18
|
constructor(options) {
|
|
19
19
|
this.token = '';
|
|
20
20
|
this.timer = null;
|
|
21
|
+
this.fileEnabled = options?.filePath !== undefined;
|
|
21
22
|
this.filePath = options?.filePath ?? DEFAULT_TOKEN_FILE;
|
|
22
23
|
this.refreshInterval = options?.refreshInterval ?? DEFAULT_REFRESH_INTERVAL;
|
|
23
24
|
let dur = options?.refreshDuration ?? 0;
|
|
@@ -25,9 +26,39 @@ export class TokenManager {
|
|
|
25
26
|
dur = 30;
|
|
26
27
|
}
|
|
27
28
|
this.refreshDuration = dur;
|
|
29
|
+
this.tokenLoader = options?.tokenLoader;
|
|
30
|
+
this.tokenWriter = options?.tokenWriter;
|
|
28
31
|
}
|
|
29
|
-
/**
|
|
30
|
-
|
|
32
|
+
/**
|
|
33
|
+
* 从 properties 文件(或自定义 tokenLoader)加载 Token。
|
|
34
|
+
* 若设置了 tokenLoader,优先调用;否则从 properties 文件读取。
|
|
35
|
+
*/
|
|
36
|
+
async loadToken() {
|
|
37
|
+
if (this.tokenLoader) {
|
|
38
|
+
const token = await this.tokenLoader();
|
|
39
|
+
if (!token) {
|
|
40
|
+
throw new Error('自定义 tokenLoader 返回空值');
|
|
41
|
+
}
|
|
42
|
+
this.token = token;
|
|
43
|
+
return token;
|
|
44
|
+
}
|
|
45
|
+
const content = readFileSync(this.filePath, 'utf-8');
|
|
46
|
+
const props = parsePropertiesString(content);
|
|
47
|
+
const token = props['token'];
|
|
48
|
+
if (!token) {
|
|
49
|
+
throw new Error('Token 文件中未找到 token 字段');
|
|
50
|
+
}
|
|
51
|
+
this.token = token;
|
|
52
|
+
return token;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* 同步版文件加载(仅用于无 tokenLoader 时的兼容入口)。
|
|
56
|
+
* 建议优先使用 loadToken()(异步)。
|
|
57
|
+
*/
|
|
58
|
+
loadTokenSync() {
|
|
59
|
+
if (this.tokenLoader) {
|
|
60
|
+
throw new Error('tokenLoader 是异步函数,请使用 loadToken()');
|
|
61
|
+
}
|
|
31
62
|
const content = readFileSync(this.filePath, 'utf-8');
|
|
32
63
|
const props = parsePropertiesString(content);
|
|
33
64
|
const token = props['token'];
|
|
@@ -41,10 +72,25 @@ export class TokenManager {
|
|
|
41
72
|
getToken() {
|
|
42
73
|
return this.token;
|
|
43
74
|
}
|
|
44
|
-
/**
|
|
75
|
+
/**
|
|
76
|
+
* 设置 Token;若启用了文件持久化(显式传入 filePath),则同步写文件。
|
|
77
|
+
* 成功后触发 tokenWriter 回调(如有)。
|
|
78
|
+
*/
|
|
45
79
|
setToken(token) {
|
|
46
80
|
this.token = token;
|
|
47
|
-
this.
|
|
81
|
+
if (this.fileEnabled) {
|
|
82
|
+
this.saveTokenToFile(token);
|
|
83
|
+
}
|
|
84
|
+
if (this.tokenWriter) {
|
|
85
|
+
this.tokenWriter(token);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* 仅更新内存中的 token,不写文件,不触发 tokenWriter 回调。
|
|
90
|
+
* 用于多个组件共享 token 时的内部同步。
|
|
91
|
+
*/
|
|
92
|
+
syncToken(token) {
|
|
93
|
+
this.token = token;
|
|
48
94
|
}
|
|
49
95
|
/** 将 Token 保存到 properties 文件 */
|
|
50
96
|
saveTokenToFile(token) {
|
|
@@ -101,6 +147,10 @@ export class TokenManager {
|
|
|
101
147
|
// 刷新失败时静默跳过
|
|
102
148
|
}
|
|
103
149
|
}, this.refreshInterval);
|
|
150
|
+
// 防止 timer 阻止 Node.js 进程退出
|
|
151
|
+
if (this.timer.unref) {
|
|
152
|
+
this.timer.unref();
|
|
153
|
+
}
|
|
104
154
|
}
|
|
105
155
|
/** 停止后台刷新 */
|
|
106
156
|
stopAutoRefresh() {
|
package/dist/esm/index.js
CHANGED
|
@@ -15,4 +15,18 @@
|
|
|
15
15
|
* @packageDocumentation
|
|
16
16
|
*/
|
|
17
17
|
/** SDK 版本号 */
|
|
18
|
-
export
|
|
18
|
+
export { SDK_VERSION as VERSION } from './version.js';
|
|
19
|
+
// Config
|
|
20
|
+
export { createClientConfig, loadPropertiesFile, SANDBOX_TIGER_PUBLIC_KEY } from './config/index.js';
|
|
21
|
+
export { TokenManager } from './config/index.js';
|
|
22
|
+
// Transport
|
|
23
|
+
export { HttpClient } from './client/index.js';
|
|
24
|
+
export { TigerError, classifyErrorCode } from './client/index.js';
|
|
25
|
+
// Clients
|
|
26
|
+
export { QuoteClient, parseOptionIdentifier } from './quote/index.js';
|
|
27
|
+
export { TradeClient } from './trade/index.js';
|
|
28
|
+
export { PushClient, ConnectionState, SubjectType } from './push/index.js';
|
|
29
|
+
export { marketOrder, limitOrder, stopOrder, stopLimitOrder } from './model/order-helpers.js';
|
|
30
|
+
export { normalizeOrderStatus } from './model/order-status.js';
|
|
31
|
+
// Enums
|
|
32
|
+
export { OrderStatus, SecurityType, OrderType, TimeInForce, Market, Currency, QuoteRight, SegmentType, OrderSortBy, CorporateActionType, IndustryLevel, SortDirection, OptionAnalysisPeriod, FinancialReportPeriod, Language, License, BarPeriod, orderStatusCode, } from './model/enums.js';
|
package/dist/esm/logger/index.js
CHANGED
package/dist/esm/model/enums.js
CHANGED
|
@@ -47,7 +47,20 @@ export var OrderType;
|
|
|
47
47
|
OrderType["TWAP"] = "TWAP";
|
|
48
48
|
OrderType["VWAP"] = "VWAP";
|
|
49
49
|
OrderType["OCA"] = "OCA";
|
|
50
|
+
OrderType["ICEBERG"] = "ICEBERG";
|
|
50
51
|
})(OrderType || (OrderType = {}));
|
|
52
|
+
/** 冰山单价格类型 */
|
|
53
|
+
export var PriceType;
|
|
54
|
+
(function (PriceType) {
|
|
55
|
+
/** 限价 */
|
|
56
|
+
PriceType["LIMIT_PRICE"] = "LIMIT_PRICE";
|
|
57
|
+
/** 卖一价 */
|
|
58
|
+
PriceType["ASK_PRICE"] = "ASK_PRICE";
|
|
59
|
+
/** 买一价 */
|
|
60
|
+
PriceType["BID_PRICE"] = "BID_PRICE";
|
|
61
|
+
/** 最新价 */
|
|
62
|
+
PriceType["LATEST_PRICE"] = "LATEST_PRICE";
|
|
63
|
+
})(PriceType || (PriceType = {}));
|
|
51
64
|
/**
|
|
52
65
|
* 订单状态枚举。对齐 Java SDK OrderStatus 定义。
|
|
53
66
|
*
|
|
@@ -133,6 +146,7 @@ export var TimeInForce;
|
|
|
133
146
|
(function (TimeInForce) {
|
|
134
147
|
TimeInForce["DAY"] = "DAY";
|
|
135
148
|
TimeInForce["GTC"] = "GTC";
|
|
149
|
+
TimeInForce["GTD"] = "GTD";
|
|
136
150
|
TimeInForce["OPG"] = "OPG";
|
|
137
151
|
})(TimeInForce || (TimeInForce = {}));
|
|
138
152
|
/** 订单排序字段 */
|
|
@@ -155,6 +169,9 @@ export var CorporateActionType;
|
|
|
155
169
|
CorporateActionType["Split"] = "split";
|
|
156
170
|
CorporateActionType["Dividend"] = "dividend";
|
|
157
171
|
CorporateActionType["Earning"] = "earning";
|
|
172
|
+
CorporateActionType["SymbolChange"] = "symbol_change";
|
|
173
|
+
CorporateActionType["Delisting"] = "delisting";
|
|
174
|
+
CorporateActionType["IPO"] = "ipo";
|
|
158
175
|
})(CorporateActionType || (CorporateActionType = {}));
|
|
159
176
|
/** 行业级别 (1~4 级) */
|
|
160
177
|
export var IndustryLevel;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { OrderType, TimeInForce } from './enums';
|
|
1
|
+
import { OrderType, TimeInForce, PriceType } from './enums.js';
|
|
2
2
|
/** Market order */
|
|
3
3
|
export function marketOrder(account, symbol, secType, action, quantity) {
|
|
4
4
|
return {
|
|
@@ -87,7 +87,148 @@ export function algoOrder(account, symbol, secType, action, quantity, limitPrice
|
|
|
87
87
|
outsideRth: false,
|
|
88
88
|
};
|
|
89
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* Iceberg order. `displaySize` is required; all other parameters are optional.
|
|
92
|
+
* Pass `priceType` as empty string to use the server default (LIMIT_PRICE).
|
|
93
|
+
* Pass `startTime` / `endTime` as 0 to omit the time window.
|
|
94
|
+
*/
|
|
95
|
+
export function icebergOrder(account, symbol, secType, action, quantity, limitPrice, displaySize, minDisplaySize, checkIntervals, priceType, startTime, endTime) {
|
|
96
|
+
const req = {
|
|
97
|
+
account, symbol, secType, action,
|
|
98
|
+
orderType: OrderType.ICEBERG,
|
|
99
|
+
totalQuantity: quantity,
|
|
100
|
+
limitPrice,
|
|
101
|
+
timeInForce: TimeInForce.DAY,
|
|
102
|
+
outsideRth: false,
|
|
103
|
+
displaySize,
|
|
104
|
+
priceType: priceType || PriceType.LIMIT_PRICE,
|
|
105
|
+
};
|
|
106
|
+
if (minDisplaySize !== undefined && minDisplaySize > 0)
|
|
107
|
+
req.minDisplaySize = minDisplaySize;
|
|
108
|
+
if (checkIntervals !== undefined && checkIntervals > 0)
|
|
109
|
+
req.checkIntervals = checkIntervals;
|
|
110
|
+
if (startTime && startTime > 0)
|
|
111
|
+
req.startTime = startTime;
|
|
112
|
+
if (endTime && endTime > 0)
|
|
113
|
+
req.endTime = endTime;
|
|
114
|
+
return req;
|
|
115
|
+
}
|
|
90
116
|
/** Attached profit / loss leg */
|
|
91
117
|
export function orderLeg(legType, price, timeInForce) {
|
|
92
118
|
return { legType, price, timeInForce };
|
|
93
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* Market order by cash amount (instead of quantity).
|
|
122
|
+
* Server uses `isQuantityByAmount=true` + `amount` field.
|
|
123
|
+
*/
|
|
124
|
+
export function marketOrderByAmount(account, symbol, secType, action, amount) {
|
|
125
|
+
return {
|
|
126
|
+
account, symbol, secType, action,
|
|
127
|
+
orderType: OrderType.MKT,
|
|
128
|
+
totalQuantity: 0,
|
|
129
|
+
cashAmount: amount,
|
|
130
|
+
timeInForce: TimeInForce.DAY,
|
|
131
|
+
outsideRth: false,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Limit order by cash amount (instead of quantity).
|
|
136
|
+
*/
|
|
137
|
+
export function limitOrderByAmount(account, symbol, secType, action, amount, limitPrice) {
|
|
138
|
+
return {
|
|
139
|
+
account, symbol, secType, action,
|
|
140
|
+
orderType: OrderType.LMT,
|
|
141
|
+
totalQuantity: 0,
|
|
142
|
+
cashAmount: amount,
|
|
143
|
+
limitPrice,
|
|
144
|
+
timeInForce: TimeInForce.DAY,
|
|
145
|
+
outsideRth: false,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Trailing stop order using a fixed price offset (auxPrice) instead of a percentage.
|
|
150
|
+
* Use `trailOrder()` for percentage-based trailing stops.
|
|
151
|
+
*/
|
|
152
|
+
export function trailOrderByPrice(account, symbol, secType, action, quantity, auxPrice) {
|
|
153
|
+
return {
|
|
154
|
+
account, symbol, secType, action,
|
|
155
|
+
orderType: OrderType.TRAIL,
|
|
156
|
+
totalQuantity: quantity,
|
|
157
|
+
auxPrice,
|
|
158
|
+
timeInForce: TimeInForce.DAY,
|
|
159
|
+
outsideRth: false,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Limit order with attached profit/loss legs (bracket order).
|
|
164
|
+
* At most 2 legs are supported.
|
|
165
|
+
*
|
|
166
|
+
* @param orderLegs - Array of `OrderLegRequest` (use `orderLeg()` helper)
|
|
167
|
+
*/
|
|
168
|
+
export function limitOrderWithLegs(account, symbol, secType, action, quantity, limitPrice, orderLegs) {
|
|
169
|
+
if (orderLegs.length === 0) {
|
|
170
|
+
throw new Error('At least 1 order leg is required for a bracket order');
|
|
171
|
+
}
|
|
172
|
+
if (orderLegs.length > 2) {
|
|
173
|
+
throw new Error('At most 2 order legs are supported');
|
|
174
|
+
}
|
|
175
|
+
return {
|
|
176
|
+
account, symbol, secType, action,
|
|
177
|
+
orderType: OrderType.LMT,
|
|
178
|
+
totalQuantity: quantity,
|
|
179
|
+
limitPrice,
|
|
180
|
+
orderLegs,
|
|
181
|
+
timeInForce: TimeInForce.DAY,
|
|
182
|
+
outsideRth: false,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Multi-leg (combo/MLEG) order for options combos.
|
|
187
|
+
*
|
|
188
|
+
* @param contractLegs - Array of `ContractLegRequest` (use `contractLeg()` helper)
|
|
189
|
+
* @param comboType - e.g. `'MLEG'`
|
|
190
|
+
*/
|
|
191
|
+
export function comboOrder(account, action, quantity, orderType, contractLegs, comboType = 'MLEG', limitPrice, auxPrice, trailingPercent) {
|
|
192
|
+
return {
|
|
193
|
+
account,
|
|
194
|
+
symbol: '', // not used for combo orders
|
|
195
|
+
secType: 'MLEG',
|
|
196
|
+
action,
|
|
197
|
+
orderType,
|
|
198
|
+
totalQuantity: quantity,
|
|
199
|
+
limitPrice,
|
|
200
|
+
auxPrice,
|
|
201
|
+
trailingPercent,
|
|
202
|
+
contractLegs,
|
|
203
|
+
comboType,
|
|
204
|
+
timeInForce: TimeInForce.DAY,
|
|
205
|
+
outsideRth: false,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* OCA (One-Cancels-All) order.
|
|
210
|
+
* Server expects `orderType: 'OCA'` and an `ocaOrders` list.
|
|
211
|
+
*
|
|
212
|
+
* @param ocaOrders - The contained orders (use any order helper to build each)
|
|
213
|
+
*/
|
|
214
|
+
export function ocaOrder(account, symbol, secType, action, quantity, ocaOrders) {
|
|
215
|
+
return {
|
|
216
|
+
account, symbol, secType, action,
|
|
217
|
+
orderType: OrderType.OCA,
|
|
218
|
+
totalQuantity: quantity,
|
|
219
|
+
ocaOrders,
|
|
220
|
+
timeInForce: TimeInForce.DAY,
|
|
221
|
+
outsideRth: false,
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Build a single leg for a multi-leg (MLEG / combo) order.
|
|
226
|
+
*
|
|
227
|
+
* @param symbol - Underlying symbol
|
|
228
|
+
* @param secType - Security type (e.g. `'OPT'`)
|
|
229
|
+
* @param action - `'BUY'` or `'SELL'`
|
|
230
|
+
* @param ratio - Number of contracts for this leg
|
|
231
|
+
*/
|
|
232
|
+
export function contractLeg(symbol, secType, action, ratio, expiry, strike, right) {
|
|
233
|
+
return { symbol, secType, action, ratio, expiry, strike, right };
|
|
234
|
+
}
|
package/dist/esm/push/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Push module exports
|
|
3
3
|
*/
|
|
4
|
-
export { SubjectType } from './push-message';
|
|
5
|
-
export * from './callbacks';
|
|
6
|
-
export * from './varint';
|
|
7
|
-
export * from './
|
|
8
|
-
export
|
|
4
|
+
export { SubjectType } from './push-message.js';
|
|
5
|
+
export * from './callbacks.js';
|
|
6
|
+
export * from './varint.js';
|
|
7
|
+
export * from './tick-util.js';
|
|
8
|
+
export * from './proto-message.js';
|
|
9
|
+
export { PushClient, ConnectionState } from './push-client.js';
|