otc-trading-desk 1.0.1-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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License Copyright (c) 2021
2
+
3
+ Permission is hereby granted, free
4
+ of charge, to any person obtaining a copy of this software and associated
5
+ documentation files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use, copy, modify, merge,
7
+ publish, distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to the
9
+ following conditions:
10
+
11
+ The above copyright notice and this permission notice
12
+ (including the next paragraph) shall be included in all copies or substantial
13
+ portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
16
+ ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
18
+ EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # otc-trading-desk
2
+
3
+ CQL utility parserTrading desk react hooks
4
+
5
+ ## Features
6
+
7
+ - ES6 syntax
8
+ - React 16.8 hooks
9
+
10
+ ## Install
11
+
12
+ ```sh
13
+ yarn add otc-trading-desk
14
+ // or
15
+ npm i otc-trading-desk
16
+ ```
17
+
18
+ ### Requirements
19
+
20
+ - Node.js `v12.x` or later
21
+ - React 16.8
22
+
23
+
24
+ ### Usage
25
+
26
+ ```js
27
+ import { convertToAST } from 'otc-trading-desk';
28
+
29
+ const ast = convertToAST('Select * from table_name');
30
+
31
+ ast.walk(node => {
32
+ // do your stuff.
33
+ })
34
+ ```
package/hooks/index.js ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ exports.__esModule = true;
17
+ __exportStar(require("./useFetchOtcTradingDeskIndications"), exports);
18
+ __exportStar(require("./useFetchOtcTradingDeskCustomerAccounts"), exports);
19
+ __exportStar(require("./useOtcTradingDeskAccountsMap"), exports);
package/hooks/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./useFetchOtcTradingDeskIndications";
2
+ export * from "./useFetchOtcTradingDeskCustomerAccounts";
3
+ export * from "./useOtcTradingDeskAccountsMap";
@@ -0,0 +1,117 @@
1
+ "use strict";
2
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
3
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
4
+ if (ar || !(i in from)) {
5
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
6
+ ar[i] = from[i];
7
+ }
8
+ }
9
+ return to.concat(ar || Array.prototype.slice.call(from));
10
+ };
11
+ exports.__esModule = true;
12
+ exports.usePollOtcTradingDeskCustomerAccounts = exports.useClearOtcTradingDeskCustomerAccounts = exports.useRefreshOtcTradingDeskCustomerAccounts = exports.useFetchOtcTradingDeskCustomerAccounts = exports.getFetchOtcTradingDeskCustomerAccountsUrl = exports.validateOtcTradingDeskCustomerAccountsResult = exports.getOtcTradingDeskCustomerAccountsResultErrors = void 0;
13
+ /* eslint-disable */
14
+ // This file is copied from otc-trading-desk/hooks/useFetchOtcTradingDeskCustomerAccounts.ts
15
+ // and modified to include an array of userIds encoded into the URL.
16
+ // The reason for doing this is to reduce eng lift to removing MobX, and this is only done
17
+ // because otc-trading-desk is a legacy app that hasn't been modified in years.
18
+ // If otc-trading-desk is to be a first-class citizen in our repo down the line, this should
19
+ // be refactored and we should fix the API layer to accept userIds without encoded [0] and [1] indexes.
20
+ var React = require("react");
21
+ var FetchProvider_1 = require("platform/contexts/network-primitives/FetchProvider/FetchProvider");
22
+ var AuthTokenContext_1 = require("platform/contexts/environment/AuthTokenContext");
23
+ var ApiOtcCustomerAccount_1 = require("platform/apiClient/generated/types/ApiOtcCustomerAccount");
24
+ var getOtcTradingDeskCustomerAccountsResultErrors = function (values) {
25
+ if (!Array.isArray(values)) {
26
+ return [{ fieldName: '', error: 'Type mismatch: data should be an array' }];
27
+ }
28
+ return values.reduce(function (combinedErrors, value, index) {
29
+ var errors = (0, ApiOtcCustomerAccount_1.getApiOtcCustomerAccountErrors)(value);
30
+ if (errors.length) {
31
+ return __spreadArray(__spreadArray([], combinedErrors, true), [
32
+ {
33
+ fieldName: "[".concat(index, "]"),
34
+ errors: errors
35
+ },
36
+ ], false);
37
+ }
38
+ else {
39
+ return combinedErrors;
40
+ }
41
+ }, []);
42
+ };
43
+ exports.getOtcTradingDeskCustomerAccountsResultErrors = getOtcTradingDeskCustomerAccountsResultErrors;
44
+ var validateOtcTradingDeskCustomerAccountsResult = function (value) {
45
+ var errors = (0, exports.getOtcTradingDeskCustomerAccountsResultErrors)(value);
46
+ if (errors.length) {
47
+ console.error("API Error: OtcTradingDeskCustomerAccountsResult data does not match contract.", errors);
48
+ }
49
+ };
50
+ exports.validateOtcTradingDeskCustomerAccountsResult = validateOtcTradingDeskCustomerAccountsResult;
51
+ var getFetchOtcTradingDeskCustomerAccountsUrl = function (params) {
52
+ var searchParams = new URLSearchParams();
53
+ if (params.userIds !== null && params.userIds !== undefined) {
54
+ var val = params.userIds;
55
+ Array.isArray(val) &&
56
+ val.forEach(function (_, i) { return searchParams.append("user_ids[".concat(i, "]"), _); });
57
+ }
58
+ var url = "/otc-trading-desk/customers/accounts?".concat(searchParams);
59
+ return url;
60
+ };
61
+ exports.getFetchOtcTradingDeskCustomerAccountsUrl = getFetchOtcTradingDeskCustomerAccountsUrl;
62
+ /**
63
+ * Fetch Gets an OTC customer's account balances
64
+ * @Url /otc-trading-desk/customers/accounts
65
+ * @Auth Required
66
+ */
67
+ var useFetchOtcTradingDeskCustomerAccounts = function (params) {
68
+ var authId = (0, AuthTokenContext_1.useAuthTokens)().authId;
69
+ if (!authId) {
70
+ throw new Error('Attempted to fetch secure API endpoint without auth tokens.');
71
+ }
72
+ var url = (0, exports.getFetchOtcTradingDeskCustomerAccountsUrl)(params);
73
+ var data = (0, FetchProvider_1.useFetch)(url)[0];
74
+ if (process.env.NODE_ENV !== 'production') {
75
+ (0, exports.validateOtcTradingDeskCustomerAccountsResult)(data);
76
+ }
77
+ return data;
78
+ };
79
+ exports.useFetchOtcTradingDeskCustomerAccounts = useFetchOtcTradingDeskCustomerAccounts;
80
+ /**
81
+ * Refresh Gets an OTC customer's account balances without throwing to suspense
82
+ * @Url /otc-trading-desk/customers/accounts
83
+ * @Auth Required
84
+ */
85
+ var useRefreshOtcTradingDeskCustomerAccounts = function (params) {
86
+ var authId = (0, AuthTokenContext_1.useAuthTokens)().authId;
87
+ if (!authId) {
88
+ throw new Error('Attempted to fetch secure API endpoint without auth tokens.');
89
+ }
90
+ var url = (0, exports.getFetchOtcTradingDeskCustomerAccountsUrl)(params);
91
+ var fetchAndCacheItem = (0, FetchProvider_1.useFetchCacheDispatchers)().fetchAndCacheItem;
92
+ var refresh = React.useCallback(function () { return fetchAndCacheItem(url, true); }, [fetchAndCacheItem, url]);
93
+ return refresh;
94
+ };
95
+ exports.useRefreshOtcTradingDeskCustomerAccounts = useRefreshOtcTradingDeskCustomerAccounts;
96
+ /**
97
+ * Remove Gets an OTC customer's account balances from cache without refetching. Subscribed components may refresh and throw to suspense.
98
+ * @Url /otc-trading-desk/customers/accounts
99
+ */
100
+ var useClearOtcTradingDeskCustomerAccounts = function (params) {
101
+ var url = (0, exports.getFetchOtcTradingDeskCustomerAccountsUrl)(params);
102
+ var clearItem = (0, FetchProvider_1.useFetchCacheDispatchers)().clearItem;
103
+ var clear = React.useCallback(function () { return clearItem(url); }, [url, clearItem]);
104
+ return clear;
105
+ };
106
+ exports.useClearOtcTradingDeskCustomerAccounts = useClearOtcTradingDeskCustomerAccounts;
107
+ /**
108
+ * Starts polling Gets an OTC customer's account balances
109
+ * @Url /otc-trading-desk/customers/accounts
110
+ * @maxAgeMs The maximum time since the resource was last fetched before it is refreshed.
111
+ */
112
+ var usePollOtcTradingDeskCustomerAccounts = function (params, maxAgeMs) {
113
+ var url = (0, exports.getFetchOtcTradingDeskCustomerAccountsUrl)(params);
114
+ (0, FetchProvider_1.useFetchPolling)(url, maxAgeMs);
115
+ };
116
+ exports.usePollOtcTradingDeskCustomerAccounts = usePollOtcTradingDeskCustomerAccounts;
117
+ exports["default"] = exports.useFetchOtcTradingDeskCustomerAccounts;
@@ -0,0 +1,155 @@
1
+ /* eslint-disable */
2
+ // This file is copied from otc-trading-desk/hooks/useFetchOtcTradingDeskCustomerAccounts.ts
3
+ // and modified to include an array of userIds encoded into the URL.
4
+ // The reason for doing this is to reduce eng lift to removing MobX, and this is only done
5
+ // because otc-trading-desk is a legacy app that hasn't been modified in years.
6
+ // If otc-trading-desk is to be a first-class citizen in our repo down the line, this should
7
+ // be refactored and we should fix the API layer to accept userIds without encoded [0] and [1] indexes.
8
+ import * as React from 'react';
9
+ import {
10
+ useFetch,
11
+ useFetchPolling,
12
+ useFetchCacheDispatchers,
13
+ } from 'platform/contexts/network-primitives/FetchProvider/FetchProvider';
14
+ import { useAuthTokens } from 'platform/contexts/environment/AuthTokenContext';
15
+ import ApiOtcCustomerAccount, {
16
+ getApiOtcCustomerAccountErrors,
17
+ } from 'platform/apiClient/generated/types/ApiOtcCustomerAccount';
18
+
19
+ export type OtcTradingDeskCustomerAccountsArgs = {
20
+ /**
21
+ * Fetch customer account(s).
22
+ */
23
+ userIds?: string[];
24
+ };
25
+
26
+ export type OtcTradingDeskCustomerAccountsResult = ApiOtcCustomerAccount[];
27
+
28
+ export const getOtcTradingDeskCustomerAccountsResultErrors = (
29
+ values: any
30
+ ): { fieldName: string; error: string }[] => {
31
+ if (!Array.isArray(values)) {
32
+ return [{ fieldName: '', error: 'Type mismatch: data should be an array' }];
33
+ }
34
+
35
+ return values.reduce((combinedErrors, value, index) => {
36
+ const errors = getApiOtcCustomerAccountErrors(value);
37
+ if (errors.length) {
38
+ return [
39
+ ...combinedErrors,
40
+ {
41
+ fieldName: `[${index}]`,
42
+ errors,
43
+ },
44
+ ];
45
+ } else {
46
+ return combinedErrors;
47
+ }
48
+ }, []);
49
+ };
50
+
51
+ export const validateOtcTradingDeskCustomerAccountsResult = (value: any) => {
52
+ const errors = getOtcTradingDeskCustomerAccountsResultErrors(value);
53
+ if (errors.length) {
54
+ console.error(
55
+ `API Error: OtcTradingDeskCustomerAccountsResult data does not match contract.`,
56
+ errors
57
+ );
58
+ }
59
+ };
60
+
61
+ export const getFetchOtcTradingDeskCustomerAccountsUrl = (
62
+ params: OtcTradingDeskCustomerAccountsArgs
63
+ ) => {
64
+ const searchParams = new URLSearchParams();
65
+ if (params.userIds !== null && params.userIds !== undefined) {
66
+ const val = params.userIds;
67
+ Array.isArray(val) &&
68
+ val.forEach((_, i) => searchParams.append(`user_ids[${i}]`, _));
69
+ }
70
+ const url = `/otc-trading-desk/customers/accounts?${searchParams}`;
71
+ return url;
72
+ };
73
+
74
+ /**
75
+ * Fetch Gets an OTC customer's account balances
76
+ * @Url /otc-trading-desk/customers/accounts
77
+ * @Auth Required
78
+ */
79
+ export const useFetchOtcTradingDeskCustomerAccounts = (
80
+ params: OtcTradingDeskCustomerAccountsArgs
81
+ ) => {
82
+ const { authId } = useAuthTokens();
83
+ if (!authId) {
84
+ throw new Error(
85
+ 'Attempted to fetch secure API endpoint without auth tokens.'
86
+ );
87
+ }
88
+
89
+ const url = getFetchOtcTradingDeskCustomerAccountsUrl(params);
90
+ const data = useFetch<OtcTradingDeskCustomerAccountsResult>(url)[0];
91
+
92
+ if (process.env.NODE_ENV !== 'production') {
93
+ validateOtcTradingDeskCustomerAccountsResult(data);
94
+ }
95
+
96
+ return data;
97
+ };
98
+
99
+ /**
100
+ * Refresh Gets an OTC customer's account balances without throwing to suspense
101
+ * @Url /otc-trading-desk/customers/accounts
102
+ * @Auth Required
103
+ */
104
+ export const useRefreshOtcTradingDeskCustomerAccounts = (
105
+ params: OtcTradingDeskCustomerAccountsArgs
106
+ ) => {
107
+ const { authId } = useAuthTokens();
108
+ if (!authId) {
109
+ throw new Error(
110
+ 'Attempted to fetch secure API endpoint without auth tokens.'
111
+ );
112
+ }
113
+
114
+ const url = getFetchOtcTradingDeskCustomerAccountsUrl(params);
115
+
116
+ const { fetchAndCacheItem } = useFetchCacheDispatchers();
117
+ const refresh = React.useCallback(
118
+ () => fetchAndCacheItem<OtcTradingDeskCustomerAccountsResult>(url, true),
119
+ [fetchAndCacheItem, url]
120
+ );
121
+
122
+ return refresh;
123
+ };
124
+
125
+ /**
126
+ * Remove Gets an OTC customer's account balances from cache without refetching. Subscribed components may refresh and throw to suspense.
127
+ * @Url /otc-trading-desk/customers/accounts
128
+ */
129
+ export const useClearOtcTradingDeskCustomerAccounts = (
130
+ params: OtcTradingDeskCustomerAccountsArgs
131
+ ) => {
132
+ const url = getFetchOtcTradingDeskCustomerAccountsUrl(params);
133
+
134
+ const { clearItem } = useFetchCacheDispatchers();
135
+
136
+ const clear = React.useCallback(() => clearItem(url), [url, clearItem]);
137
+
138
+ return clear;
139
+ };
140
+
141
+ /**
142
+ * Starts polling Gets an OTC customer's account balances
143
+ * @Url /otc-trading-desk/customers/accounts
144
+ * @maxAgeMs The maximum time since the resource was last fetched before it is refreshed.
145
+ */
146
+ export const usePollOtcTradingDeskCustomerAccounts = (
147
+ params: OtcTradingDeskCustomerAccountsArgs,
148
+ maxAgeMs: number
149
+ ) => {
150
+ const url = getFetchOtcTradingDeskCustomerAccountsUrl(params);
151
+
152
+ useFetchPolling(url, maxAgeMs);
153
+ };
154
+
155
+ export default useFetchOtcTradingDeskCustomerAccounts;
@@ -0,0 +1,142 @@
1
+ "use strict";
2
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
3
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
4
+ if (ar || !(i in from)) {
5
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
6
+ ar[i] = from[i];
7
+ }
8
+ }
9
+ return to.concat(ar || Array.prototype.slice.call(from));
10
+ };
11
+ exports.__esModule = true;
12
+ exports.usePollOtcTradingDeskIndications = exports.useClearOtcTradingDeskIndications = exports.useRefreshOtcTradingDeskIndications = exports.useFetchOtcTradingDeskIndications = exports.getFetchOtcTradingDeskIndicationsUrl = exports.validateOtcTradingDeskIndicationsResult = exports.getOtcTradingDeskIndicationsResultErrors = void 0;
13
+ /* eslint-disable */
14
+ // This file is copied from otc-trading-desk/hooks/useFetchOtcTradingDeskIndications.ts
15
+ // and modified to include an array of statuses encoded into the URL.
16
+ // The reason for doing this is to reduce eng lift to removing MobX, and this is only done
17
+ // because otc-trading-desk is a legacy app that hasn't been modified in years.
18
+ // If otc-trading-desk is to be a first-class citizen in our repo down the line, this should
19
+ // be refactored and we should fix the API layer to accept statuses without encoded [0] and [1] indexes.
20
+ var React = require("react");
21
+ var FetchProvider_1 = require("platform/contexts/network-primitives/FetchProvider/FetchProvider");
22
+ var AuthTokenContext_1 = require("platform/contexts/environment/AuthTokenContext");
23
+ var ApiOtcIndication_1 = require("platform/apiClient/generated/types/ApiOtcIndication");
24
+ var getOtcTradingDeskIndicationsResultErrors = function (values) {
25
+ if (!Array.isArray(values)) {
26
+ return [{ fieldName: '', error: 'Type mismatch: data should be an array' }];
27
+ }
28
+ return values.reduce(function (combinedErrors, value, index) {
29
+ var errors = (0, ApiOtcIndication_1.getApiOtcIndicationErrors)(value);
30
+ if (errors.length) {
31
+ return __spreadArray(__spreadArray([], combinedErrors, true), [
32
+ {
33
+ fieldName: "[".concat(index, "]"),
34
+ errors: errors
35
+ },
36
+ ], false);
37
+ }
38
+ else {
39
+ return combinedErrors;
40
+ }
41
+ }, []);
42
+ };
43
+ exports.getOtcTradingDeskIndicationsResultErrors = getOtcTradingDeskIndicationsResultErrors;
44
+ var validateOtcTradingDeskIndicationsResult = function (value) {
45
+ var errors = (0, exports.getOtcTradingDeskIndicationsResultErrors)(value);
46
+ if (errors.length) {
47
+ console.error("API Error: OtcTradingDeskIndicationsResult data does not match contract.", errors);
48
+ }
49
+ };
50
+ exports.validateOtcTradingDeskIndicationsResult = validateOtcTradingDeskIndicationsResult;
51
+ var getFetchOtcTradingDeskIndicationsUrl = function (params) {
52
+ var searchParams = new URLSearchParams();
53
+ if (params.userIds !== null && params.userIds !== undefined) {
54
+ var val = params.userIds;
55
+ Array.isArray(val) && val.forEach(function (_) { return searchParams.append('user_ids', _); });
56
+ }
57
+ if (params.profileIds !== null && params.profileIds !== undefined) {
58
+ var val = params.profileIds;
59
+ Array.isArray(val) && val.forEach(function (_) { return searchParams.append('profile_ids', _); });
60
+ }
61
+ if (params.searchSubstring !== null && params.searchSubstring !== undefined) {
62
+ var val = params.searchSubstring;
63
+ searchParams.append('search_substring', "".concat(val));
64
+ }
65
+ if (params.side !== null && params.side !== undefined) {
66
+ var val = params.side;
67
+ searchParams.append('side', "".concat(val));
68
+ }
69
+ if (params.limit !== null && params.limit !== undefined) {
70
+ var val = params.limit;
71
+ searchParams.append('limit', "".concat(val));
72
+ }
73
+ if (params.offset !== null && params.offset !== undefined) {
74
+ var val = params.offset;
75
+ searchParams.append('offset', "".concat(val));
76
+ }
77
+ if (params.statuses !== null && params.statuses !== undefined) {
78
+ var val = params.statuses;
79
+ // NOTE: IF CODEGEN NEEDS TO BE UPDATED - this line is the only difference from otc-trading-desk/hooks/useFetchOtcTradingDeskIndications.ts.
80
+ Array.isArray(val) &&
81
+ val.forEach(function (_, i) { return searchParams.append("statuses[".concat(i, "]"), _); });
82
+ }
83
+ var url = "/otc-trading-desk/indications?".concat(searchParams);
84
+ return url;
85
+ };
86
+ exports.getFetchOtcTradingDeskIndicationsUrl = getFetchOtcTradingDeskIndicationsUrl;
87
+ /**
88
+ * Fetch Gets all OTC trading desk orders
89
+ * @Url /otc-trading-desk/indications
90
+ * @Auth Required
91
+ */
92
+ var useFetchOtcTradingDeskIndications = function (params) {
93
+ var authId = (0, AuthTokenContext_1.useAuthTokens)().authId;
94
+ if (!authId) {
95
+ throw new Error('Attempted to fetch secure API endpoint without auth tokens.');
96
+ }
97
+ var url = (0, exports.getFetchOtcTradingDeskIndicationsUrl)(params);
98
+ var data = (0, FetchProvider_1.useFetch)(url)[0];
99
+ if (process.env.NODE_ENV !== 'production') {
100
+ (0, exports.validateOtcTradingDeskIndicationsResult)(data);
101
+ }
102
+ return data;
103
+ };
104
+ exports.useFetchOtcTradingDeskIndications = useFetchOtcTradingDeskIndications;
105
+ /**
106
+ * Refresh Gets all OTC trading desk orders without throwing to suspense
107
+ * @Url /otc-trading-desk/indications
108
+ * @Auth Required
109
+ */
110
+ var useRefreshOtcTradingDeskIndications = function (params) {
111
+ var authId = (0, AuthTokenContext_1.useAuthTokens)().authId;
112
+ if (!authId) {
113
+ throw new Error('Attempted to fetch secure API endpoint without auth tokens.');
114
+ }
115
+ var url = (0, exports.getFetchOtcTradingDeskIndicationsUrl)(params);
116
+ var fetchAndCacheItem = (0, FetchProvider_1.useFetchCacheDispatchers)().fetchAndCacheItem;
117
+ var refresh = React.useCallback(function () { return fetchAndCacheItem(url, true); }, [fetchAndCacheItem, url]);
118
+ return refresh;
119
+ };
120
+ exports.useRefreshOtcTradingDeskIndications = useRefreshOtcTradingDeskIndications;
121
+ /**
122
+ * Remove Gets all OTC trading desk orders from cache without refetching. Subscribed components may refresh and throw to suspense.
123
+ * @Url /otc-trading-desk/indications
124
+ */
125
+ var useClearOtcTradingDeskIndications = function (params) {
126
+ var url = (0, exports.getFetchOtcTradingDeskIndicationsUrl)(params);
127
+ var clearItem = (0, FetchProvider_1.useFetchCacheDispatchers)().clearItem;
128
+ var clear = React.useCallback(function () { return clearItem(url); }, [url, clearItem]);
129
+ return clear;
130
+ };
131
+ exports.useClearOtcTradingDeskIndications = useClearOtcTradingDeskIndications;
132
+ /**
133
+ * Starts polling Gets all OTC trading desk orders
134
+ * @Url /otc-trading-desk/indications
135
+ * @maxAgeMs The maximum time since the resource was last fetched before it is refreshed.
136
+ */
137
+ var usePollOtcTradingDeskIndications = function (params, maxAgeMs) {
138
+ var url = (0, exports.getFetchOtcTradingDeskIndicationsUrl)(params);
139
+ (0, FetchProvider_1.useFetchPolling)(url, maxAgeMs);
140
+ };
141
+ exports.usePollOtcTradingDeskIndications = usePollOtcTradingDeskIndications;
142
+ exports["default"] = exports.useFetchOtcTradingDeskIndications;
@@ -0,0 +1,216 @@
1
+ /* eslint-disable */
2
+ // This file is copied from otc-trading-desk/hooks/useFetchOtcTradingDeskIndications.ts
3
+ // and modified to include an array of statuses encoded into the URL.
4
+ // The reason for doing this is to reduce eng lift to removing MobX, and this is only done
5
+ // because otc-trading-desk is a legacy app that hasn't been modified in years.
6
+ // If otc-trading-desk is to be a first-class citizen in our repo down the line, this should
7
+ // be refactored and we should fix the API layer to accept statuses without encoded [0] and [1] indexes.
8
+ import * as React from 'react';
9
+ import {
10
+ useFetch,
11
+ useFetchPolling,
12
+ useFetchCacheDispatchers,
13
+ } from 'platform/contexts/network-primitives/FetchProvider/FetchProvider';
14
+ import { useAuthTokens } from 'platform/contexts/environment/AuthTokenContext';
15
+ import ApiOtcIndication, {
16
+ getApiOtcIndicationErrors,
17
+ } from 'platform/apiClient/generated/types/ApiOtcIndication';
18
+
19
+ export type OtcTradingDeskIndicationsArgs = {
20
+ /**
21
+ * No description provided
22
+ */
23
+ userIds?: string[];
24
+
25
+ /**
26
+ * No description provided
27
+ */
28
+ profileIds?: string[];
29
+
30
+ /**
31
+ * No description provided
32
+ */
33
+ searchSubstring?: string;
34
+
35
+ /**
36
+ * No description provided
37
+ */
38
+ side?: 'buy' | 'sell';
39
+
40
+ /**
41
+ * No description provided
42
+ */
43
+ limit?: number;
44
+
45
+ /**
46
+ * No description provided
47
+ */
48
+ offset?: number;
49
+
50
+ /**
51
+ * No description provided
52
+ */
53
+ statuses?: ('received' | 'progress' | 'canceled' | 'done' | 'expired')[];
54
+ };
55
+
56
+ export type OtcTradingDeskIndicationsResult = ApiOtcIndication[];
57
+
58
+ export const getOtcTradingDeskIndicationsResultErrors = (
59
+ values: any
60
+ ): { fieldName: string; error: string }[] => {
61
+ if (!Array.isArray(values)) {
62
+ return [{ fieldName: '', error: 'Type mismatch: data should be an array' }];
63
+ }
64
+
65
+ return values.reduce((combinedErrors, value, index) => {
66
+ const errors = getApiOtcIndicationErrors(value);
67
+ if (errors.length) {
68
+ return [
69
+ ...combinedErrors,
70
+ {
71
+ fieldName: `[${index}]`,
72
+ errors,
73
+ },
74
+ ];
75
+ } else {
76
+ return combinedErrors;
77
+ }
78
+ }, []);
79
+ };
80
+
81
+ export const validateOtcTradingDeskIndicationsResult = (value: any) => {
82
+ const errors = getOtcTradingDeskIndicationsResultErrors(value);
83
+ if (errors.length) {
84
+ console.error(
85
+ `API Error: OtcTradingDeskIndicationsResult data does not match contract.`,
86
+ errors
87
+ );
88
+ }
89
+ };
90
+
91
+ export const getFetchOtcTradingDeskIndicationsUrl = (
92
+ params: OtcTradingDeskIndicationsArgs
93
+ ) => {
94
+ const searchParams = new URLSearchParams();
95
+ if (params.userIds !== null && params.userIds !== undefined) {
96
+ const val = params.userIds;
97
+ Array.isArray(val) && val.forEach(_ => searchParams.append('user_ids', _));
98
+ }
99
+
100
+ if (params.profileIds !== null && params.profileIds !== undefined) {
101
+ const val = params.profileIds;
102
+ Array.isArray(val) && val.forEach(_ => searchParams.append('profile_ids', _));
103
+ }
104
+
105
+ if (params.searchSubstring !== null && params.searchSubstring !== undefined) {
106
+ const val = params.searchSubstring;
107
+ searchParams.append('search_substring', `${val}`);
108
+ }
109
+
110
+ if (params.side !== null && params.side !== undefined) {
111
+ const val = params.side;
112
+ searchParams.append('side', `${val}`);
113
+ }
114
+
115
+ if (params.limit !== null && params.limit !== undefined) {
116
+ const val = params.limit;
117
+ searchParams.append('limit', `${val}`);
118
+ }
119
+
120
+ if (params.offset !== null && params.offset !== undefined) {
121
+ const val = params.offset;
122
+ searchParams.append('offset', `${val}`);
123
+ }
124
+
125
+ if (params.statuses !== null && params.statuses !== undefined) {
126
+ const val = params.statuses;
127
+ // NOTE: IF CODEGEN NEEDS TO BE UPDATED - this line is the only difference from otc-trading-desk/hooks/useFetchOtcTradingDeskIndications.ts.
128
+ Array.isArray(val) &&
129
+ val.forEach((_, i) => searchParams.append(`statuses[${i}]`, _));
130
+ }
131
+ const url = `/otc-trading-desk/indications?${searchParams}`;
132
+ return url;
133
+ };
134
+
135
+ /**
136
+ * Fetch Gets all OTC trading desk orders
137
+ * @Url /otc-trading-desk/indications
138
+ * @Auth Required
139
+ */
140
+ export const useFetchOtcTradingDeskIndications = (
141
+ params: OtcTradingDeskIndicationsArgs
142
+ ) => {
143
+ const { authId } = useAuthTokens();
144
+ if (!authId) {
145
+ throw new Error(
146
+ 'Attempted to fetch secure API endpoint without auth tokens.'
147
+ );
148
+ }
149
+
150
+ const url = getFetchOtcTradingDeskIndicationsUrl(params);
151
+ const data = useFetch<OtcTradingDeskIndicationsResult>(url)[0];
152
+
153
+ if (process.env.NODE_ENV !== 'production') {
154
+ validateOtcTradingDeskIndicationsResult(data);
155
+ }
156
+
157
+ return data;
158
+ };
159
+
160
+ /**
161
+ * Refresh Gets all OTC trading desk orders without throwing to suspense
162
+ * @Url /otc-trading-desk/indications
163
+ * @Auth Required
164
+ */
165
+ export const useRefreshOtcTradingDeskIndications = (
166
+ params: OtcTradingDeskIndicationsArgs
167
+ ) => {
168
+ const { authId } = useAuthTokens();
169
+ if (!authId) {
170
+ throw new Error(
171
+ 'Attempted to fetch secure API endpoint without auth tokens.'
172
+ );
173
+ }
174
+
175
+ const url = getFetchOtcTradingDeskIndicationsUrl(params);
176
+
177
+ const { fetchAndCacheItem } = useFetchCacheDispatchers();
178
+ const refresh = React.useCallback(
179
+ () => fetchAndCacheItem<OtcTradingDeskIndicationsResult>(url, true),
180
+ [fetchAndCacheItem, url]
181
+ );
182
+
183
+ return refresh;
184
+ };
185
+
186
+ /**
187
+ * Remove Gets all OTC trading desk orders from cache without refetching. Subscribed components may refresh and throw to suspense.
188
+ * @Url /otc-trading-desk/indications
189
+ */
190
+ export const useClearOtcTradingDeskIndications = (
191
+ params: OtcTradingDeskIndicationsArgs
192
+ ) => {
193
+ const url = getFetchOtcTradingDeskIndicationsUrl(params);
194
+
195
+ const { clearItem } = useFetchCacheDispatchers();
196
+
197
+ const clear = React.useCallback(() => clearItem(url), [url, clearItem]);
198
+
199
+ return clear;
200
+ };
201
+
202
+ /**
203
+ * Starts polling Gets all OTC trading desk orders
204
+ * @Url /otc-trading-desk/indications
205
+ * @maxAgeMs The maximum time since the resource was last fetched before it is refreshed.
206
+ */
207
+ export const usePollOtcTradingDeskIndications = (
208
+ params: OtcTradingDeskIndicationsArgs,
209
+ maxAgeMs: number
210
+ ) => {
211
+ const url = getFetchOtcTradingDeskIndicationsUrl(params);
212
+
213
+ useFetchPolling(url, maxAgeMs);
214
+ };
215
+
216
+ export default useFetchOtcTradingDeskIndications;
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
14
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
15
+ if (ar || !(i in from)) {
16
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
17
+ ar[i] = from[i];
18
+ }
19
+ }
20
+ return to.concat(ar || Array.prototype.slice.call(from));
21
+ };
22
+ exports.__esModule = true;
23
+ exports.useRefreshOtcTradingDeskAccountsMap = exports.useOtcTradingDeskAccountsMap = void 0;
24
+ var react_1 = require("react");
25
+ var withError_1 = require("platform/hooks/core/withError");
26
+ var useFetchOtcTradingDeskCustomerAccounts_1 = require("otc-trading-desk/hooks/useFetchOtcTradingDeskCustomerAccounts");
27
+ var accountsMap = {};
28
+ var addToAccountsMap = function (accounts) {
29
+ var newAccountsMap = {};
30
+ accounts.forEach(function (account) {
31
+ if (newAccountsMap[account.userId]) {
32
+ newAccountsMap[account.userId] = __spreadArray(__spreadArray([], newAccountsMap[account.userId], true), [
33
+ account,
34
+ ], false);
35
+ }
36
+ else {
37
+ newAccountsMap[account.userId] = [account];
38
+ }
39
+ });
40
+ var updatedMap = __assign(__assign({}, accountsMap), newAccountsMap);
41
+ accountsMap = updatedMap;
42
+ return updatedMap;
43
+ };
44
+ var useOtcTradingDeskAccountsMap = function (userIds, isTradingDesk) {
45
+ var userIdsWithFallback = userIds && userIds.length ? userIds : [''];
46
+ var newAccounts = null;
47
+ if (isTradingDesk) {
48
+ newAccounts = (0, withError_1["default"])(function () {
49
+ // eslint-disable-next-line react-hooks/rules-of-hooks
50
+ return (0, useFetchOtcTradingDeskCustomerAccounts_1.useFetchOtcTradingDeskCustomerAccounts)({ userIds: userIdsWithFallback });
51
+ })[0];
52
+ }
53
+ return (0, react_1.useMemo)(function () {
54
+ if (!newAccounts)
55
+ return {};
56
+ var updatedMap = addToAccountsMap(newAccounts);
57
+ return updatedMap;
58
+ }, [newAccounts]);
59
+ };
60
+ exports.useOtcTradingDeskAccountsMap = useOtcTradingDeskAccountsMap;
61
+ var useRefreshOtcTradingDeskAccountsMap = function (userIds) {
62
+ var refreshAccounts = (0, useFetchOtcTradingDeskCustomerAccounts_1.useRefreshOtcTradingDeskCustomerAccounts)({ userIds: userIds });
63
+ return function () {
64
+ if (!userIds || !userIds.length)
65
+ return Promise.resolve({});
66
+ return refreshAccounts().then(function (response) {
67
+ if (!(response instanceof Error)) {
68
+ var updatedMap = addToAccountsMap(response.body);
69
+ return updatedMap;
70
+ }
71
+ return {};
72
+ });
73
+ };
74
+ };
75
+ exports.useRefreshOtcTradingDeskAccountsMap = useRefreshOtcTradingDeskAccountsMap;
@@ -0,0 +1,62 @@
1
+ import { useMemo } from 'react';
2
+ import withError from 'platform/hooks/core/withError';
3
+ import ApiOtcCustomerAccount from 'platform/apiClient/generated/types/ApiOtcCustomerAccount';
4
+ import {
5
+ useFetchOtcTradingDeskCustomerAccounts,
6
+ useRefreshOtcTradingDeskCustomerAccounts,
7
+ } from 'otc-trading-desk/hooks/useFetchOtcTradingDeskCustomerAccounts';
8
+
9
+ let accountsMap: Record<string, readonly ApiOtcCustomerAccount[]> = {};
10
+
11
+ const addToAccountsMap = (accounts: readonly ApiOtcCustomerAccount[]) => {
12
+ const newAccountsMap: Record<string, readonly ApiOtcCustomerAccount[]> = {};
13
+ accounts.forEach(account => {
14
+ if (newAccountsMap[account.userId]) {
15
+ newAccountsMap[account.userId] = [
16
+ ...newAccountsMap[account.userId],
17
+ account,
18
+ ];
19
+ } else {
20
+ newAccountsMap[account.userId] = [account];
21
+ }
22
+ });
23
+ const updatedMap = { ...accountsMap, ...newAccountsMap };
24
+ accountsMap = updatedMap;
25
+ return updatedMap;
26
+ };
27
+
28
+ export const useOtcTradingDeskAccountsMap = (
29
+ userIds: string[],
30
+ isTradingDesk: boolean
31
+ ) => {
32
+ const userIdsWithFallback = userIds && userIds.length ? userIds : [''];
33
+ let newAccounts: readonly ApiOtcCustomerAccount[] | null = null;
34
+ if (isTradingDesk) {
35
+ [newAccounts] = withError(() =>
36
+ // eslint-disable-next-line react-hooks/rules-of-hooks
37
+ useFetchOtcTradingDeskCustomerAccounts({ userIds: userIdsWithFallback })
38
+ );
39
+ }
40
+ return useMemo(() => {
41
+ if (!newAccounts) return {};
42
+ const updatedMap = addToAccountsMap(newAccounts);
43
+ return updatedMap;
44
+ }, [newAccounts]);
45
+ };
46
+
47
+ export const useRefreshOtcTradingDeskAccountsMap = (userIds: string[]) => {
48
+ const refreshAccounts = useRefreshOtcTradingDeskCustomerAccounts({ userIds });
49
+ return () => {
50
+ if (!userIds || !userIds.length)
51
+ return Promise.resolve(
52
+ {} as Record<string, readonly ApiOtcCustomerAccount[]>
53
+ );
54
+ return refreshAccounts().then(response => {
55
+ if (!(response instanceof Error)) {
56
+ const updatedMap = addToAccountsMap(response.body);
57
+ return updatedMap;
58
+ }
59
+ return {};
60
+ });
61
+ };
62
+ };
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "otc-trading-desk",
3
+ "version": "1.0.1-1",
4
+ "private": false,
5
+ "description": "Trading desk react hooks",
6
+ "license": "MIT",
7
+ "author": "hcnbs",
8
+ "repository": "https://www.github.com/hcnbs/otc-trading-desk",
9
+ "main": "dist/hooks/index.js",
10
+ "scripts": {
11
+ "build": "tsc",
12
+ "test": "jest"
13
+ },
14
+ "dependencies": {
15
+ "react": "^16.8.6"
16
+ },
17
+ "devDependencies": {
18
+ "jest": "28.1.3",
19
+ "typescript": "^4.7.2"
20
+ },
21
+ "keywords": ["react hooks", "trading desk"],
22
+ "publishConfig": {
23
+ "access": "public"
24
+ }
25
+ }