@finatic/client 0.0.142 → 0.9.1

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.
Files changed (64) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/LICENSE +39 -0
  3. package/README.md +54 -425
  4. package/dist/index.d.ts +7329 -1579
  5. package/dist/index.js +8110 -6114
  6. package/dist/index.js.map +1 -1
  7. package/dist/index.mjs +8047 -6085
  8. package/dist/index.mjs.map +1 -1
  9. package/package.json +77 -33
  10. package/dist/types/core/client/ApiClient.d.ts +0 -270
  11. package/dist/types/core/client/FinaticConnect.d.ts +0 -332
  12. package/dist/types/core/portal/PortalUI.d.ts +0 -37
  13. package/dist/types/index.d.ts +0 -12
  14. package/dist/types/lib/logger/index.d.ts +0 -2
  15. package/dist/types/lib/logger/logger.d.ts +0 -4
  16. package/dist/types/lib/logger/logger.types.d.ts +0 -28
  17. package/dist/types/mocks/MockApiClient.d.ts +0 -171
  18. package/dist/types/mocks/MockDataProvider.d.ts +0 -139
  19. package/dist/types/mocks/MockFactory.d.ts +0 -53
  20. package/dist/types/mocks/utils.d.ts +0 -24
  21. package/dist/types/themes/portalPresets.d.ts +0 -9
  22. package/dist/types/types/api/auth.d.ts +0 -93
  23. package/dist/types/types/api/broker.d.ts +0 -421
  24. package/dist/types/types/api/core.d.ts +0 -46
  25. package/dist/types/types/api/errors.d.ts +0 -31
  26. package/dist/types/types/api/orders.d.ts +0 -39
  27. package/dist/types/types/api/portfolio.d.ts +0 -55
  28. package/dist/types/types/common/pagination.d.ts +0 -33
  29. package/dist/types/types/connect.d.ts +0 -58
  30. package/dist/types/types/index.d.ts +0 -13
  31. package/dist/types/types/portal.d.ts +0 -204
  32. package/dist/types/types/ui/theme.d.ts +0 -104
  33. package/dist/types/utils/brokerUtils.d.ts +0 -30
  34. package/dist/types/utils/errors.d.ts +0 -45
  35. package/dist/types/utils/events.d.ts +0 -12
  36. package/dist/types/utils/themeUtils.d.ts +0 -34
  37. package/src/core/client/ApiClient.ts +0 -2004
  38. package/src/core/client/FinaticConnect.ts +0 -1606
  39. package/src/core/portal/PortalUI.ts +0 -335
  40. package/src/index.d.ts +0 -23
  41. package/src/index.ts +0 -100
  42. package/src/lib/logger/index.ts +0 -3
  43. package/src/lib/logger/logger.ts +0 -332
  44. package/src/lib/logger/logger.types.ts +0 -34
  45. package/src/mocks/MockApiClient.ts +0 -1058
  46. package/src/mocks/MockDataProvider.ts +0 -986
  47. package/src/mocks/MockFactory.ts +0 -97
  48. package/src/mocks/utils.ts +0 -133
  49. package/src/themes/portalPresets.ts +0 -1307
  50. package/src/types/api/auth.ts +0 -112
  51. package/src/types/api/broker.ts +0 -461
  52. package/src/types/api/core.ts +0 -53
  53. package/src/types/api/errors.ts +0 -35
  54. package/src/types/api/orders.ts +0 -45
  55. package/src/types/api/portfolio.ts +0 -59
  56. package/src/types/common/pagination.ts +0 -164
  57. package/src/types/connect.ts +0 -56
  58. package/src/types/index.ts +0 -25
  59. package/src/types/portal.ts +0 -214
  60. package/src/types/ui/theme.ts +0 -105
  61. package/src/utils/brokerUtils.ts +0 -104
  62. package/src/utils/errors.ts +0 -104
  63. package/src/utils/events.ts +0 -66
  64. package/src/utils/themeUtils.ts +0 -165
@@ -1,332 +0,0 @@
1
- import { Logger, LoggerExtra, LoggerMetadata, LoggerOptions, LogLevel, LogVerbosity } from './logger.types';
2
-
3
- const LOG_LEVEL_ORDER: Record<Exclude<LogLevel, 'silent'>, number> = {
4
- error: 0,
5
- warn: 1,
6
- info: 2,
7
- debug: 3,
8
- };
9
-
10
- const LEVEL_TO_CONSOLE: Record<Exclude<LogLevel, 'silent'>, keyof Console> = {
11
- error: 'error',
12
- warn: 'warn',
13
- info: 'info',
14
- debug: 'debug',
15
- };
16
-
17
- const DEFAULT_LOGGER_NAME = 'FinaticLogger';
18
-
19
- const parseLogLevel = (value: unknown, fallback: LogLevel): LogLevel => {
20
- if (typeof value !== 'string') {
21
- return fallback;
22
- }
23
-
24
- const normalized = value.toLowerCase().trim() as LogLevel;
25
- if (normalized === 'silent' || normalized === 'error' || normalized === 'warn' || normalized === 'info' || normalized === 'debug') {
26
- return normalized;
27
- }
28
-
29
- return fallback;
30
- };
31
-
32
- const parseVerbosity = (value: unknown, fallback: LogVerbosity): LogVerbosity => {
33
- if (typeof value !== 'string' && typeof value !== 'number') {
34
- return fallback;
35
- }
36
-
37
- const numeric = typeof value === 'number' ? value : Number.parseInt(value, 10);
38
- if (Number.isNaN(numeric)) {
39
- return fallback;
40
- }
41
-
42
- if (numeric <= 0) {
43
- return 0;
44
- }
45
-
46
- if (numeric >= 3) {
47
- return 3;
48
- }
49
-
50
- return numeric as LogVerbosity;
51
- };
52
-
53
- const resolveEnv = (key: string): string | undefined => {
54
- try {
55
- if (typeof process !== 'undefined' && process.env && typeof process.env[key] === 'string') {
56
- return process.env[key];
57
- }
58
- } catch {
59
- // ignore
60
- }
61
-
62
- try {
63
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
64
- const metaEnv = typeof import.meta !== 'undefined' ? (import.meta as any).env : undefined;
65
- if (metaEnv && typeof metaEnv[key] === 'string') {
66
- return metaEnv[key];
67
- }
68
- } catch {
69
- // ignore
70
- }
71
-
72
- try {
73
- if (typeof globalThis !== 'undefined') {
74
- const value = (globalThis as Record<string, unknown>)[key];
75
- if (typeof value === 'string') {
76
- return value;
77
- }
78
- }
79
- } catch {
80
- // ignore
81
- }
82
-
83
- return undefined;
84
- };
85
-
86
- const resolveDefaultLogLevel = (explicitLevel?: LogLevel): LogLevel => {
87
- if (explicitLevel) {
88
- return explicitLevel;
89
- }
90
-
91
- const envLevel =
92
- resolveEnv('FINATIC_LOG_LEVEL') ||
93
- resolveEnv('VITE_FINATIC_LOG_LEVEL') ||
94
- resolveEnv('NEXT_PUBLIC_FINATIC_LOG_LEVEL') ||
95
- resolveEnv('NEXT_FINATIC_LOG_LEVEL') ||
96
- resolveEnv('REACT_APP_FINATIC_LOG_LEVEL') ||
97
- resolveEnv('NUXT_PUBLIC_FINATIC_LOG_LEVEL') ||
98
- resolveEnv('NX_FINATIC_LOG_LEVEL');
99
-
100
- if (envLevel) {
101
- return parseLogLevel(envLevel, 'silent');
102
- }
103
-
104
- return 'silent';
105
- };
106
-
107
- const resolveVerbosity = (): LogVerbosity => {
108
- const envVerbosity =
109
- resolveEnv('FINATIC_LOG_VERBOSITY') ||
110
- resolveEnv('VITE_FINATIC_LOG_VERBOSITY') ||
111
- resolveEnv('NEXT_PUBLIC_FINATIC_LOG_VERBOSITY') ||
112
- resolveEnv('NEXT_FINATIC_LOG_VERBOSITY') ||
113
- resolveEnv('REACT_APP_FINATIC_LOG_VERBOSITY') ||
114
- resolveEnv('NUXT_PUBLIC_FINATIC_LOG_VERBOSITY') ||
115
- resolveEnv('NX_FINATIC_LOG_VERBOSITY');
116
-
117
- if (envVerbosity) {
118
- return parseVerbosity(envVerbosity, 1);
119
- }
120
-
121
- return 1;
122
- };
123
-
124
- const resolveBaseMetadata = (): LoggerMetadata => {
125
- const base: LoggerMetadata = {
126
- timestamp: new Date().toISOString(),
127
- };
128
-
129
- try {
130
- if (typeof globalThis !== 'undefined') {
131
- if (typeof (globalThis as Window).location !== 'undefined') {
132
- base.host = (globalThis as Window).location.hostname;
133
- }
134
- if (typeof (globalThis as Window).navigator !== 'undefined') {
135
- base.user_agent = (globalThis as Window).navigator.userAgent;
136
- }
137
- }
138
- } catch {
139
- // ignore
140
- }
141
-
142
- try {
143
- if (typeof process !== 'undefined') {
144
- base.pid = process.pid;
145
- }
146
- } catch {
147
- // ignore
148
- }
149
-
150
- return base;
151
- };
152
-
153
- const normalizeError = (error: unknown): Record<string, unknown> | undefined => {
154
- if (!error) {
155
- return undefined;
156
- }
157
-
158
- if (error instanceof Error) {
159
- return {
160
- type: error.name,
161
- message: error.message,
162
- stacktrace: error.stack,
163
- };
164
- }
165
-
166
- if (typeof error === 'object') {
167
- return { ...(error as Record<string, unknown>) };
168
- }
169
-
170
- return {
171
- type: 'Error',
172
- message: String(error),
173
- };
174
- };
175
-
176
- const shouldLog = (requestedLevel: Exclude<LogLevel, 'silent'>, currentLevel: LogLevel): boolean => {
177
- if (currentLevel === 'silent') {
178
- return false;
179
- }
180
-
181
- const currentOrder = LOG_LEVEL_ORDER[currentLevel];
182
- const requestedOrder = LOG_LEVEL_ORDER[requestedLevel];
183
-
184
- return requestedOrder <= currentOrder;
185
- };
186
-
187
- const buildPayload = (
188
- name: string,
189
- level: Exclude<LogLevel, 'silent'>,
190
- message: string,
191
- defaultMetadata: LoggerMetadata | undefined,
192
- extra: LoggerExtra | undefined,
193
- verbosity: LogVerbosity,
194
- ): Record<string, unknown> => {
195
- const payload: Record<string, unknown> = {
196
- timestamp: new Date().toISOString(),
197
- message,
198
- };
199
-
200
- if (verbosity >= 1 && extra) {
201
- if (extra.module) {
202
- payload.module = extra.module;
203
- }
204
- if (extra.function) {
205
- payload.function = extra.function;
206
- }
207
- }
208
-
209
- if (verbosity >= 2) {
210
- if (extra?.duration_ms !== undefined) {
211
- payload.duration_ms = extra.duration_ms;
212
- }
213
- if (extra?.event) {
214
- payload.event = extra.event;
215
- }
216
- if (extra?.error) {
217
- payload.error = normalizeError(extra.error);
218
- }
219
- }
220
-
221
- if (verbosity >= 3) {
222
- payload.level = level.toUpperCase();
223
- payload.name = name;
224
- const baseMetadata = resolveBaseMetadata();
225
- const mergedMetadata: LoggerMetadata = {
226
- ...baseMetadata,
227
- ...(defaultMetadata || {}),
228
- ...(extra?.metadata || {}),
229
- };
230
- if (Object.keys(mergedMetadata).length > 0) {
231
- payload.metadata = mergedMetadata;
232
- }
233
- }
234
-
235
- const restKeys = ['module', 'function', 'event', 'duration_ms', 'error', 'metadata'];
236
- if (extra) {
237
- Object.entries(extra).forEach(([key, value]) => {
238
- if (!restKeys.includes(key) && value !== undefined) {
239
- payload[key] = value;
240
- }
241
- });
242
- }
243
-
244
- return payload;
245
- };
246
-
247
- const consoleWrite = (
248
- consoleLevel: keyof Console,
249
- name: string,
250
- level: Exclude<LogLevel, 'silent'>,
251
- message: string,
252
- payload: Record<string, unknown>,
253
- ): void => {
254
- if (typeof console === 'undefined' || typeof console[consoleLevel] !== 'function') {
255
- return;
256
- }
257
-
258
- const prefix = `${level.toUpperCase()}: ${name} || ${message}`;
259
- console[consoleLevel](prefix, payload);
260
- };
261
-
262
- export const setupLogger = (nameOrOptions: string | LoggerOptions, level?: LogLevel, defaultMetadata?: LoggerMetadata): Logger => {
263
- const options: LoggerOptions =
264
- typeof nameOrOptions === 'string'
265
- ? {
266
- name: nameOrOptions,
267
- level,
268
- defaultMetadata,
269
- }
270
- : nameOrOptions;
271
-
272
- const loggerName = options.name || DEFAULT_LOGGER_NAME;
273
- let currentLevel: LogLevel = resolveDefaultLogLevel(options.level);
274
- const loggerDefaultMetadata = options.defaultMetadata;
275
- const verbosity = resolveVerbosity();
276
-
277
- const log = (requestedLevel: Exclude<LogLevel, 'silent'>, message: string, extra?: LoggerExtra) => {
278
- if (!shouldLog(requestedLevel, currentLevel)) {
279
- return;
280
- }
281
-
282
- const payload = buildPayload(loggerName, requestedLevel, message, loggerDefaultMetadata, extra, verbosity);
283
- consoleWrite(LEVEL_TO_CONSOLE[requestedLevel], loggerName, requestedLevel, message, payload);
284
- };
285
-
286
- return {
287
- getLevel: () => currentLevel,
288
- setLevel: (nextLevel: LogLevel) => {
289
- currentLevel = nextLevel;
290
- },
291
- debug: (message: string, extra?: LoggerExtra) => log('debug', message, extra),
292
- info: (message: string, extra?: LoggerExtra) => log('info', message, extra),
293
- warn: (message: string, extra?: LoggerExtra) => log('warn', message, extra),
294
- error: (message: string, extra?: LoggerExtra) => log('error', message, extra),
295
- exception: (message: string, error: unknown, extra?: LoggerExtra) => {
296
- log('error', message, {
297
- ...extra,
298
- error,
299
- event: extra?.event || 'exception',
300
- });
301
- },
302
- };
303
- };
304
-
305
- export const buildLoggerExtra = (metadata: LoggerMetadata): LoggerExtra => ({
306
- metadata,
307
- });
308
-
309
- export const logStartEnd =
310
- (logger: Logger) =>
311
- <Args extends unknown[], ReturnType>(fn: (...args: Args) => ReturnType | Promise<ReturnType>) =>
312
- async (...args: Args): Promise<ReturnType> => {
313
- const start = Date.now();
314
- const functionName = fn.name || 'anonymous';
315
- logger.debug('START', { module: 'logStartEnd', function: functionName, event: 'start' });
316
-
317
- try {
318
- const result = await fn(...args);
319
- const duration = Date.now() - start;
320
- logger.info('END', { module: 'logStartEnd', function: functionName, event: 'end', duration_ms: duration });
321
- return result;
322
- } catch (error) {
323
- const duration = Date.now() - start;
324
- logger.exception('EXCEPTION', error, {
325
- module: 'logStartEnd',
326
- function: functionName,
327
- duration_ms: duration,
328
- });
329
- throw error;
330
- }
331
- };
332
-
@@ -1,34 +0,0 @@
1
- export type LogLevel = 'silent' | 'error' | 'warn' | 'info' | 'debug';
2
-
3
- export interface LoggerMetadata {
4
- [key: string]: unknown;
5
- }
6
-
7
- export interface LoggerExtra {
8
- metadata?: LoggerMetadata;
9
- module?: string;
10
- function?: string;
11
- event?: string;
12
- duration_ms?: number;
13
- error?: unknown;
14
- [key: string]: unknown;
15
- }
16
-
17
- export interface LoggerOptions {
18
- name: string;
19
- level?: LogLevel;
20
- defaultMetadata?: LoggerMetadata;
21
- }
22
-
23
- export interface Logger {
24
- getLevel: () => LogLevel;
25
- setLevel: (level: LogLevel) => void;
26
- debug: (message: string, extra?: LoggerExtra) => void;
27
- info: (message: string, extra?: LoggerExtra) => void;
28
- warn: (message: string, extra?: LoggerExtra) => void;
29
- error: (message: string, extra?: LoggerExtra) => void;
30
- exception: (message: string, error: unknown, extra?: LoggerExtra) => void;
31
- }
32
-
33
- export type LogVerbosity = 0 | 1 | 2 | 3;
34
-