@finatic/client 0.0.139 → 0.0.141
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 +335 -446
- package/dist/index.d.ts +272 -515
- package/dist/index.js +531 -449
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +532 -449
- package/dist/index.mjs.map +1 -1
- package/dist/types/core/client/ApiClient.d.ts +81 -27
- package/dist/types/core/client/FinaticConnect.d.ts +53 -103
- package/dist/types/index.d.ts +1 -2
- package/dist/types/mocks/MockApiClient.d.ts +2 -4
- package/dist/types/mocks/utils.d.ts +0 -5
- package/dist/types/types/api/auth.d.ts +12 -30
- package/dist/types/types/api/broker.d.ts +117 -1
- package/package.json +7 -3
- package/src/core/client/ApiClient.ts +1978 -0
- package/src/core/client/FinaticConnect.ts +1557 -0
- package/src/core/portal/PortalUI.ts +300 -0
- package/src/index.d.ts +23 -0
- package/src/index.ts +99 -0
- package/src/mocks/MockApiClient.ts +1032 -0
- package/src/mocks/MockDataProvider.ts +986 -0
- package/src/mocks/MockFactory.ts +97 -0
- package/src/mocks/utils.ts +133 -0
- package/src/themes/portalPresets.ts +1307 -0
- package/src/types/api/auth.ts +112 -0
- package/src/types/api/broker.ts +461 -0
- package/src/types/api/core.ts +53 -0
- package/src/types/api/errors.ts +35 -0
- package/src/types/api/orders.ts +45 -0
- package/src/types/api/portfolio.ts +59 -0
- package/src/types/common/pagination.ts +138 -0
- package/src/types/connect.ts +56 -0
- package/src/types/index.ts +25 -0
- package/src/types/portal.ts +214 -0
- package/src/types/ui/theme.ts +105 -0
- package/src/utils/brokerUtils.ts +85 -0
- package/src/utils/errors.ts +104 -0
- package/src/utils/events.ts +54 -0
- package/src/utils/themeUtils.ts +146 -0
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { ApiClient } from '../core/client/ApiClient';
|
|
2
|
+
import { MockApiClient } from './MockApiClient';
|
|
3
|
+
import { MockConfig } from './MockDataProvider';
|
|
4
|
+
import { shouldUseMocks, shouldMockApiOnly, getMockConfig } from './utils';
|
|
5
|
+
import { DeviceInfo } from '../types';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Factory class for creating API clients (real or mock)
|
|
9
|
+
*/
|
|
10
|
+
export class MockFactory {
|
|
11
|
+
/**
|
|
12
|
+
* Create an API client based on environment configuration
|
|
13
|
+
* @param baseUrl - The base URL for the API
|
|
14
|
+
* @param deviceInfo - Optional device information
|
|
15
|
+
* @param mockConfig - Optional mock configuration (only used if mocks are enabled)
|
|
16
|
+
* @returns ApiClient or MockApiClient instance
|
|
17
|
+
*/
|
|
18
|
+
static createApiClient(
|
|
19
|
+
baseUrl: string,
|
|
20
|
+
deviceInfo?: DeviceInfo,
|
|
21
|
+
mockConfig?: MockConfig
|
|
22
|
+
): ApiClient | MockApiClient {
|
|
23
|
+
const useMocks = shouldUseMocks();
|
|
24
|
+
const mockApiOnly = shouldMockApiOnly();
|
|
25
|
+
|
|
26
|
+
if (useMocks || mockApiOnly) {
|
|
27
|
+
// Merge environment config with provided config
|
|
28
|
+
const envConfig = getMockConfig();
|
|
29
|
+
const finalConfig: MockConfig = {
|
|
30
|
+
delay: mockConfig?.delay || envConfig.delay,
|
|
31
|
+
scenario: mockConfig?.scenario || 'success',
|
|
32
|
+
customData: mockConfig?.customData || {},
|
|
33
|
+
mockApiOnly: mockApiOnly, // Pass this flag to the mock client
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
return new MockApiClient(baseUrl, deviceInfo, finalConfig);
|
|
37
|
+
} else {
|
|
38
|
+
return new ApiClient(baseUrl, deviceInfo);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Force create a mock API client regardless of environment settings
|
|
44
|
+
* @param baseUrl - The base URL for the API
|
|
45
|
+
* @param deviceInfo - Optional device information
|
|
46
|
+
* @param mockConfig - Optional mock configuration
|
|
47
|
+
* @returns MockApiClient instance
|
|
48
|
+
*/
|
|
49
|
+
static createMockApiClient(
|
|
50
|
+
baseUrl: string,
|
|
51
|
+
deviceInfo?: DeviceInfo,
|
|
52
|
+
mockConfig?: MockConfig
|
|
53
|
+
): MockApiClient {
|
|
54
|
+
return new MockApiClient(baseUrl, deviceInfo, mockConfig);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Force create a real API client regardless of environment settings
|
|
59
|
+
* @param baseUrl - The base URL for the API
|
|
60
|
+
* @param deviceInfo - Optional device information
|
|
61
|
+
* @returns ApiClient instance
|
|
62
|
+
*/
|
|
63
|
+
static createRealApiClient(baseUrl: string, deviceInfo?: DeviceInfo): ApiClient {
|
|
64
|
+
return new ApiClient(baseUrl, deviceInfo);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Check if mocks are currently enabled
|
|
69
|
+
* @returns boolean indicating if mocks are enabled
|
|
70
|
+
*/
|
|
71
|
+
static isMockMode(): boolean {
|
|
72
|
+
return shouldUseMocks();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Get current mock configuration
|
|
77
|
+
* @returns Mock configuration object
|
|
78
|
+
*/
|
|
79
|
+
static getMockConfig(): { enabled: boolean; delay?: number } {
|
|
80
|
+
return getMockConfig();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Convenience function to create an API client
|
|
86
|
+
* @param baseUrl - The base URL for the API
|
|
87
|
+
* @param deviceInfo - Optional device information
|
|
88
|
+
* @param mockConfig - Optional mock configuration
|
|
89
|
+
* @returns ApiClient or MockApiClient instance
|
|
90
|
+
*/
|
|
91
|
+
export function createMockApiClient(
|
|
92
|
+
baseUrl: string,
|
|
93
|
+
deviceInfo?: DeviceInfo,
|
|
94
|
+
mockConfig?: MockConfig
|
|
95
|
+
): ApiClient | MockApiClient {
|
|
96
|
+
return MockFactory.createApiClient(baseUrl, deviceInfo, mockConfig);
|
|
97
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions for mock system environment detection
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
// Type declarations for Node.js environment
|
|
6
|
+
// Note: process is already declared globally in Node.js types
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Check if mocks should be used based on environment variables
|
|
10
|
+
* Supports both browser and Node.js environments
|
|
11
|
+
*/
|
|
12
|
+
export function shouldUseMocks(): boolean {
|
|
13
|
+
// Check Node.js environment
|
|
14
|
+
if (typeof process !== 'undefined' && process?.env) {
|
|
15
|
+
return (
|
|
16
|
+
process.env.FINATIC_USE_MOCKS === 'true' ||
|
|
17
|
+
process.env.NEXT_PUBLIC_FINATIC_USE_MOCKS === 'true'
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Check browser environment
|
|
22
|
+
if (typeof window !== 'undefined') {
|
|
23
|
+
// Check global variable
|
|
24
|
+
if ((window as any).FINATIC_USE_MOCKS === 'true') {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Check Next.js public environment variables
|
|
29
|
+
if ((window as any).NEXT_PUBLIC_FINATIC_USE_MOCKS === 'true') {
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Check localStorage
|
|
34
|
+
try {
|
|
35
|
+
return localStorage.getItem('FINATIC_USE_MOCKS') === 'true';
|
|
36
|
+
} catch (error) {
|
|
37
|
+
// localStorage might not be available
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Set mock mode in the current environment
|
|
47
|
+
*/
|
|
48
|
+
export function setMockMode(enabled: boolean): void {
|
|
49
|
+
// Set in Node.js environment
|
|
50
|
+
if (typeof process !== 'undefined' && process?.env) {
|
|
51
|
+
process.env.FINATIC_USE_MOCKS = enabled ? 'true' : 'false';
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Set in browser environment
|
|
55
|
+
if (typeof window !== 'undefined') {
|
|
56
|
+
(window as any).FINATIC_USE_MOCKS = enabled ? 'true' : 'false';
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
if (enabled) {
|
|
60
|
+
localStorage.setItem('FINATIC_USE_MOCKS', 'true');
|
|
61
|
+
} else {
|
|
62
|
+
localStorage.removeItem('FINATIC_USE_MOCKS');
|
|
63
|
+
}
|
|
64
|
+
} catch (error) {
|
|
65
|
+
// localStorage might not be available
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Check if only API should be mocked (but portal should use real URL)
|
|
72
|
+
*/
|
|
73
|
+
export function shouldMockApiOnly(): boolean {
|
|
74
|
+
// Check Node.js environment
|
|
75
|
+
if (typeof process !== 'undefined' && process?.env) {
|
|
76
|
+
return (
|
|
77
|
+
process.env.FINATIC_MOCK_API_ONLY === 'true' ||
|
|
78
|
+
process.env.NEXT_PUBLIC_FINATIC_MOCK_API_ONLY === 'true'
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Check browser environment
|
|
83
|
+
if (typeof window !== 'undefined') {
|
|
84
|
+
// Check global variable
|
|
85
|
+
if ((window as any).FINATIC_MOCK_API_ONLY === 'true') {
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Check Next.js public environment variables
|
|
90
|
+
if ((window as any).NEXT_PUBLIC_FINATIC_MOCK_API_ONLY === 'true') {
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Check localStorage
|
|
95
|
+
try {
|
|
96
|
+
return localStorage.getItem('FINATIC_MOCK_API_ONLY') === 'true';
|
|
97
|
+
} catch (error) {
|
|
98
|
+
// localStorage might not be available
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Get mock configuration from environment
|
|
108
|
+
*/
|
|
109
|
+
export function getMockConfig(): { enabled: boolean; delay?: number; mockApiOnly?: boolean } {
|
|
110
|
+
const enabled = shouldUseMocks();
|
|
111
|
+
const mockApiOnly = shouldMockApiOnly();
|
|
112
|
+
|
|
113
|
+
let delay: number | undefined;
|
|
114
|
+
|
|
115
|
+
// Check for custom delay in Node.js
|
|
116
|
+
if (typeof process !== 'undefined' && process?.env?.FINATIC_MOCK_DELAY) {
|
|
117
|
+
delay = parseInt(process.env.FINATIC_MOCK_DELAY, 10);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Check for custom delay in browser
|
|
121
|
+
if (typeof window !== 'undefined') {
|
|
122
|
+
try {
|
|
123
|
+
const storedDelay = localStorage.getItem('FINATIC_MOCK_DELAY');
|
|
124
|
+
if (storedDelay) {
|
|
125
|
+
delay = parseInt(storedDelay, 10);
|
|
126
|
+
}
|
|
127
|
+
} catch (error) {
|
|
128
|
+
// localStorage might not be available
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return { enabled, delay, mockApiOnly };
|
|
133
|
+
}
|