@trade-it/core 0.1.0 → 0.3.0

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/CHANGELOG.md ADDED
@@ -0,0 +1,22 @@
1
+ # Changelog
2
+
3
+ All notable changes to `@trade-it/core` will be documented in this file.
4
+
5
+ ## 0.2.0 - 2026-07-27
6
+
7
+ ### Added
8
+
9
+ - Typed `takeProfit` and `stopLoss` trade configuration
10
+ - `TradeItExitTrigger`, `TriggerUnit`, and current Trade Link query parameters
11
+ - `OrderDirection.Even` support
12
+
13
+ ### Fixed
14
+
15
+ - Serialize multi-leg order direction instead of silently dropping it
16
+ - Remove generated type and runtime dependencies on internal Zod schemas
17
+
18
+ ## 0.1.0 - 2026-02-25
19
+
20
+ ### Added
21
+
22
+ - Initial embedded SDK types, event parsing, and launch URL resolution
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Trade It, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -7,6 +7,7 @@ This package powers the Trade It SDK surface shared across framework wrappers, i
7
7
  - shared types
8
8
  - trade URL resolution
9
9
  - event parsing helpers
10
+ - attached-exit and multi-leg direction serialization
10
11
 
11
12
  Most integrators should use [`@trade-it/react`](https://www.npmjs.com/package/@trade-it/react) directly.
12
13
 
@@ -27,4 +28,4 @@ Integration guides and reference docs:
27
28
  ## Notes
28
29
 
29
30
  - This package is intended for framework wrappers or advanced integrations.
30
- - Trade It session URLs should still be requested from your server.
31
+ - Request Trade It session URLs from your server immediately before launch; they expire after 30 minutes.
package/dist/index.cjs CHANGED
@@ -20,6 +20,7 @@ let OrderType = /* @__PURE__ */ function(OrderType$1) {
20
20
  let OrderDirection = /* @__PURE__ */ function(OrderDirection$1) {
21
21
  OrderDirection$1["Debit"] = "debit";
22
22
  OrderDirection$1["Credit"] = "credit";
23
+ OrderDirection$1["Even"] = "even";
23
24
  return OrderDirection$1;
24
25
  }({});
25
26
  let TimeInForce = /* @__PURE__ */ function(TimeInForce$1) {
@@ -34,6 +35,14 @@ let TradeUnit = /* @__PURE__ */ function(TradeUnit$1) {
34
35
  TradeUnit$1["Shares"] = "shares";
35
36
  return TradeUnit$1;
36
37
  }({});
38
+ /** How an exit trigger (take-profit / stop-loss) value is expressed */
39
+ let TriggerUnit = /* @__PURE__ */ function(TriggerUnit$1) {
40
+ /** An absolute price level */
41
+ TriggerUnit$1["Price"] = "$";
42
+ /** A percentage offset from the entry price */
43
+ TriggerUnit$1["Percent"] = "%";
44
+ return TriggerUnit$1;
45
+ }({});
37
46
  let LegType = /* @__PURE__ */ function(LegType$1) {
38
47
  LegType$1["Option"] = "option";
39
48
  LegType$1["Equity"] = "equity";
@@ -46,70 +55,87 @@ let TradeType = /* @__PURE__ */ function(TradeType$1) {
46
55
  }({});
47
56
 
48
57
  //#endregion
49
- //#region ../../lib/types/src/types/trade-link.types.ts
50
- /**
51
- * Short query parameter keys for trade link URLs
52
- */
58
+ //#region src/events.ts
59
+ function isTradeItEvent(data) {
60
+ if (!data || typeof data !== "object") return false;
61
+ const message = data;
62
+ return message.source === "tradeit" && message.version === 1 && typeof message.type === "string" && message.type.startsWith("tradeit.");
63
+ }
64
+ function getTradeItErrorMessage(event) {
65
+ const message = event.payload?.message;
66
+ return typeof message === "string" ? message : "Trade It embedded experience returned an error event.";
67
+ }
68
+
69
+ //#endregion
70
+ //#region src/types.ts
71
+ /** Compact query keys accepted by Trade It trade URLs. */
53
72
  let TradeLinkParam = /* @__PURE__ */ function(TradeLinkParam$1) {
54
73
  TradeLinkParam$1["TradeType"] = "tt";
55
74
  TradeLinkParam$1["Action"] = "t";
75
+ TradeLinkParam$1["Direction"] = "dir";
56
76
  TradeLinkParam$1["OrderType"] = "ot";
57
77
  TradeLinkParam$1["Amount"] = "a";
58
78
  TradeLinkParam$1["Unit"] = "u";
59
79
  TradeLinkParam$1["LimitPrice"] = "lp";
60
80
  TradeLinkParam$1["StopPrice"] = "sp";
61
81
  TradeLinkParam$1["TimeInForce"] = "tif";
82
+ TradeLinkParam$1["TakeProfit"] = "tp";
83
+ TradeLinkParam$1["StopLoss"] = "sl";
62
84
  TradeLinkParam$1["UtmReferred"] = "ref";
63
85
  TradeLinkParam$1["Legs"] = "legs";
64
86
  return TradeLinkParam$1;
65
87
  }({});
66
88
 
67
- //#endregion
68
- //#region src/events.ts
69
- function isTradeItEvent(data) {
70
- if (!data || typeof data !== "object") return false;
71
- const message = data;
72
- return message.source === "tradeit" && message.version === 1 && typeof message.type === "string" && message.type.startsWith("tradeit.");
73
- }
74
- function getTradeItErrorMessage(event) {
75
- const message = event.payload?.message;
76
- return typeof message === "string" ? message : "Trade It embedded experience returned an error event.";
77
- }
78
-
79
89
  //#endregion
80
90
  //#region src/url.ts
81
91
  function appendQueryParam(params, key, value) {
82
- if (value == null) return;
92
+ if (value == null || value === "") return;
83
93
  params.set(key, String(value));
84
94
  }
85
95
  function isMultiLegTradeConfig(config) {
86
96
  return config.tradeType === TradeType.MultiLeg || Array.isArray(config.legs);
87
97
  }
98
+ function toTradeLinkLegConfig(leg) {
99
+ return {
100
+ type: leg.type ?? LegType.Equity,
101
+ action: leg.action ?? TradeAction.Buy,
102
+ position_effect: leg.positionEffect ?? null,
103
+ occ: leg.occ ?? null,
104
+ quantity: leg.quantity ?? 1
105
+ };
106
+ }
107
+ function encodeExitTrigger(trigger) {
108
+ if (!trigger) return void 0;
109
+ return trigger.unit === "%" ? `${trigger.value}%` : String(trigger.value);
110
+ }
88
111
  function toShortLegConfig(leg) {
89
112
  return {
90
113
  type: leg.type,
91
114
  t: leg.action,
92
- pe: leg.positionEffect,
115
+ pe: leg.position_effect,
93
116
  occ: leg.occ,
94
117
  q: leg.quantity
95
118
  };
96
119
  }
97
- function normalizeTradeConfig(config) {
120
+ function tradeConfigToSearchParams(config) {
98
121
  const params = new URLSearchParams();
99
- if (!config) return params;
100
122
  const multiLeg = isMultiLegTradeConfig(config);
101
123
  appendQueryParam(params, TradeLinkParam.TradeType, multiLeg ? TradeType.MultiLeg : TradeType.Simple);
102
- appendQueryParam(params, TradeLinkParam.OrderType, config.orderType);
124
+ appendQueryParam(params, TradeLinkParam.OrderType, config.orderType ?? OrderType.Market);
103
125
  appendQueryParam(params, TradeLinkParam.LimitPrice, config.limitPrice);
104
126
  appendQueryParam(params, TradeLinkParam.StopPrice, config.stopPrice);
105
127
  appendQueryParam(params, TradeLinkParam.TimeInForce, config.timeInForce);
106
128
  appendQueryParam(params, TradeLinkParam.UtmReferred, config.referral);
107
- if (multiLeg) params.set(TradeLinkParam.Legs, JSON.stringify(config.legs.map(toShortLegConfig)));
108
- else {
109
- appendQueryParam(params, TradeLinkParam.Action, config.action);
110
- appendQueryParam(params, TradeLinkParam.Amount, config.amount);
111
- appendQueryParam(params, TradeLinkParam.Unit, config.unit);
129
+ if (multiLeg) {
130
+ appendQueryParam(params, TradeLinkParam.Direction, config.direction);
131
+ if (config.legs.length > 0) params.set(TradeLinkParam.Legs, JSON.stringify(config.legs.map(toTradeLinkLegConfig).map(toShortLegConfig)));
132
+ return params;
112
133
  }
134
+ appendQueryParam(params, TradeLinkParam.Action, config.action);
135
+ appendQueryParam(params, TradeLinkParam.Amount, config.amount);
136
+ appendQueryParam(params, TradeLinkParam.Unit, config.unit ?? TradeUnit.Dollars);
137
+ appendQueryParam(params, TradeLinkParam.TakeProfit, encodeExitTrigger(config.takeProfit));
138
+ appendQueryParam(params, TradeLinkParam.StopLoss, encodeExitTrigger(config.stopLoss));
113
139
  return params;
114
140
  }
115
141
  function mergeSearchParams(url, extra) {
@@ -124,7 +150,7 @@ function resolveLaunchUrl(launch) {
124
150
  const encodedTicker = encodeURIComponent(launch.config.ticker);
125
151
  const rawUrl = launch.url.replace("[ticker]", encodedTicker);
126
152
  const url = new URL(rawUrl, typeof window !== "undefined" ? window.location.href : "https://tradeit.app");
127
- const configParams = normalizeTradeConfig(launch.config);
153
+ const configParams = tradeConfigToSearchParams(launch.config);
128
154
  mergeSearchParams(url, configParams);
129
155
  if (!url.searchParams.has("embedded")) url.searchParams.set("embedded", "1");
130
156
  return url.toString();
@@ -147,6 +173,7 @@ exports.TradeAction = TradeAction;
147
173
  exports.TradeLinkParam = TradeLinkParam;
148
174
  exports.TradeType = TradeType;
149
175
  exports.TradeUnit = TradeUnit;
176
+ exports.TriggerUnit = TriggerUnit;
150
177
  exports.getTradeItErrorMessage = getTradeItErrorMessage;
151
178
  exports.getUrlOrigin = getUrlOrigin;
152
179
  exports.isTradeItEvent = isTradeItEvent;
package/dist/index.d.cts CHANGED
@@ -1,5 +1,3 @@
1
- import { z } from "zod";
2
-
3
1
  //#region ../../lib/types/src/enums/trading.enums.d.ts
4
2
  declare enum TradeAction {
5
3
  Buy = "buy",
@@ -18,6 +16,7 @@ declare enum OrderType {
18
16
  declare enum OrderDirection {
19
17
  Debit = "debit",
20
18
  Credit = "credit",
19
+ Even = "even",
21
20
  }
22
21
  declare enum TimeInForce {
23
22
  Day = "day",
@@ -29,6 +28,15 @@ declare enum TradeUnit {
29
28
  Dollars = "dollars",
30
29
  Shares = "shares",
31
30
  }
31
+ /** How an exit trigger (take-profit / stop-loss) value is expressed */
32
+ declare enum TriggerUnit {
33
+ /** An absolute price level */
34
+ Price = "$",
35
+ /** A percentage offset from the entry price */
36
+ Percent = "%",
37
+ }
38
+ /** Which exit an attached trigger represents */
39
+
32
40
  declare enum LegType {
33
41
  Option = "option",
34
42
  Equity = "equity",
@@ -37,95 +45,62 @@ declare enum TradeType {
37
45
  Simple = 0,
38
46
  MultiLeg = 1,
39
47
  }
40
- //#endregion
41
- //#region ../../lib/types/src/schemas/trade-link.schemas.d.ts
42
- /**
43
- * Trade Link Leg Config - derived from TradeLeg without execution_price
44
- */
45
- declare const zTradeLinkLegConfig: z.ZodObject<Omit<{
46
- type: z.ZodNativeEnum<typeof LegType>;
47
- action: z.ZodNativeEnum<typeof TradeAction>;
48
- position_effect: z.ZodNullable<z.ZodNativeEnum<typeof PositionEffect>>;
49
- occ: z.ZodNullable<z.ZodString>;
50
- quantity: z.ZodNumber;
51
- execution_price: z.ZodNullable<z.ZodObject<{
52
- amount: z.ZodNumber;
53
- currency: z.ZodString;
54
- }, "strip", z.ZodTypeAny, {
55
- amount: number;
56
- currency: string;
57
- }, {
58
- amount: number;
59
- currency: string;
60
- }>>;
61
- }, "execution_price">, "strip", z.ZodTypeAny, {
62
- type: LegType;
63
- action: TradeAction;
64
- position_effect: PositionEffect | null;
65
- occ: string | null;
66
- quantity: number;
67
- }, {
68
- type: LegType;
69
- action: TradeAction;
70
- position_effect: PositionEffect | null;
71
- occ: string | null;
72
- quantity: number;
73
- }>;
74
48
  /**
75
- * Full Trade Link Config - stored in database as JSONB
76
- * Supports both simple trades and multi-leg options trades
49
+ * A brokerage-reported option lifecycle event. Rows carrying an event describe
50
+ * what happened to a contract, so `action` is not meaningful on them.
77
51
  */
78
52
  //#endregion
79
- //#region ../../lib/types/src/types/misc.types.d.ts
80
- type SnakeToCamelCase<S extends string> = S extends `${infer P1}_${infer P2}` ? `${P1}${Capitalize<SnakeToCamelCase<P2>>}` : S;
81
- /** Converts a type's snake_case keys to camelCase keys */
82
- type Camelize<T> = T extends object ? { [K in keyof T as K extends string ? SnakeToCamelCase<K> : K]: Camelize<T[K]> } : T;
83
- //#endregion
84
- //#region ../../lib/types/src/types/trade-link.types.d.ts
85
- /** Configuration for a leg in a multi-leg trade link */
86
- type TradeLinkLegConfig = z.infer<typeof zTradeLinkLegConfig>;
87
- /** Full configuration stored in the trade link */
88
-
89
- /**
90
- * Short query parameter keys for trade link URLs
91
- */
53
+ //#region src/types.d.ts
54
+ type TradeItEventType = 'tradeit.ready' | 'tradeit.close_requested' | 'tradeit.brokerage_connected' | 'tradeit.trade_submitted' | 'tradeit.trade_executed' | 'tradeit.error' | 'tradeit.login_required';
55
+ interface TradeItEvent<TPayload = Record<string, unknown>> {
56
+ source: 'tradeit';
57
+ version: 1;
58
+ type: TradeItEventType;
59
+ payload?: TPayload;
60
+ }
61
+ type TradeItQueryPrimitive = string | number | boolean | null | undefined;
62
+ type TradeItMode = 'connect' | 'trade';
63
+ /** Compact query keys accepted by Trade It trade URLs. */
92
64
  declare enum TradeLinkParam {
93
65
  TradeType = "tt",
94
- // s (simple), m (multi_leg)
95
66
  Action = "t",
96
- // b, s, bto, btc, sto, stc
67
+ Direction = "dir",
97
68
  OrderType = "ot",
98
- // m, l, st, sl
99
69
  Amount = "a",
100
- // number
101
70
  Unit = "u",
102
- // d, s
103
71
  LimitPrice = "lp",
104
- // number
105
72
  StopPrice = "sp",
106
- // number
107
73
  TimeInForce = "tif",
108
- // d, gtc, ioc, fok
74
+ TakeProfit = "tp",
75
+ StopLoss = "sl",
109
76
  UtmReferred = "ref",
110
- // string
111
77
  Legs = "legs",
112
78
  }
113
- //#endregion
114
- //#region src/types.d.ts
115
- type TradeItEventType = 'tradeit.ready' | 'tradeit.close_requested' | 'tradeit.brokerage_connected' | 'tradeit.trade_submitted' | 'tradeit.trade_executed' | 'tradeit.error' | 'tradeit.login_required';
116
- interface TradeItEvent<TPayload = Record<string, unknown>> {
117
- source: 'tradeit';
118
- version: 1;
119
- type: TradeItEventType;
120
- payload?: TPayload;
79
+ /** Canonical snake_case leg shape used by Trade Link APIs. */
80
+ interface TradeLinkLegConfig {
81
+ type: LegType;
82
+ action: TradeAction;
83
+ position_effect: PositionEffect | null;
84
+ occ: string | null;
85
+ quantity: number;
86
+ }
87
+ /** User-facing camelCase leg shape accepted by the embedded SDK. */
88
+ interface TradeItMultiLegTradeLegConfig {
89
+ type?: LegType;
90
+ action?: TradeAction;
91
+ positionEffect?: PositionEffect | null;
92
+ occ?: string | null;
93
+ quantity?: number;
94
+ }
95
+ /** Attached take-profit or stop-loss trigger. */
96
+ interface TradeItExitTrigger {
97
+ value: number;
98
+ unit: '$' | '%';
121
99
  }
122
- type TradeItQueryPrimitive = string | number | boolean | null | undefined;
123
- type TradeItMode = 'connect' | 'trade';
124
- type TradeItMultiLegTradeLegConfig = Partial<Camelize<TradeLinkLegConfig>>;
125
100
  /**
126
101
  * Query params accepted by Trade It's /t/[ticker] page.
127
102
  * User-facing config uses descriptive keys; core converts them to the short-key
128
- * query format used by the page parser (tt, t, ot, a, u, lp, sp, tif, ref, legs).
103
+ * query format used by the page parser (tt, t, dir, ot, a, u, lp, sp, tif, tp, sl, ref, legs).
129
104
  */
130
105
  interface TradeItTradeConfigBase {
131
106
  ticker: string;
@@ -140,6 +115,8 @@ interface TradeItSimpleTradeConfig extends TradeItTradeConfigBase {
140
115
  action?: TradeAction;
141
116
  amount?: number;
142
117
  unit?: TradeUnit;
118
+ takeProfit?: TradeItExitTrigger;
119
+ stopLoss?: TradeItExitTrigger;
143
120
  }
144
121
  interface TradeItMultiLegTradeConfig extends TradeItTradeConfigBase {
145
122
  tradeType: TradeType.MultiLeg;
@@ -171,4 +148,4 @@ declare function getTradeItErrorMessage(event: TradeItEvent): string;
171
148
  declare function resolveLaunchUrl(launch: TradeItLaunch): string;
172
149
  declare function getUrlOrigin(url: string): string | null;
173
150
  //#endregion
174
- export { LegType, OrderDirection, OrderType, PositionEffect, TimeInForce, TradeAction, type TradeItConnectLaunch, type TradeItEvent, type TradeItEventType, type TradeItLaunch, type TradeItLaunchConfigMap, type TradeItMode, type TradeItMultiLegTradeConfig, type TradeItMultiLegTradeLegConfig, type TradeItQueryPrimitive, type TradeItSimpleTradeConfig, type TradeItTradeConfig, type TradeItTradeLaunch, type TradeLinkLegConfig, TradeLinkParam, TradeType, TradeUnit, getTradeItErrorMessage, getUrlOrigin, isTradeItEvent, resolveLaunchUrl };
151
+ export { LegType, OrderDirection, OrderType, PositionEffect, TimeInForce, TradeAction, type TradeItConnectLaunch, type TradeItEvent, type TradeItEventType, type TradeItExitTrigger, type TradeItLaunch, type TradeItLaunchConfigMap, type TradeItMode, type TradeItMultiLegTradeConfig, type TradeItMultiLegTradeLegConfig, type TradeItQueryPrimitive, type TradeItSimpleTradeConfig, type TradeItTradeConfig, type TradeItTradeLaunch, type TradeLinkLegConfig, TradeLinkParam, TradeType, TradeUnit, TriggerUnit, getTradeItErrorMessage, getUrlOrigin, isTradeItEvent, resolveLaunchUrl };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,3 @@
1
- import { z } from "zod";
2
-
3
1
  //#region ../../lib/types/src/enums/trading.enums.d.ts
4
2
  declare enum TradeAction {
5
3
  Buy = "buy",
@@ -18,6 +16,7 @@ declare enum OrderType {
18
16
  declare enum OrderDirection {
19
17
  Debit = "debit",
20
18
  Credit = "credit",
19
+ Even = "even",
21
20
  }
22
21
  declare enum TimeInForce {
23
22
  Day = "day",
@@ -29,6 +28,15 @@ declare enum TradeUnit {
29
28
  Dollars = "dollars",
30
29
  Shares = "shares",
31
30
  }
31
+ /** How an exit trigger (take-profit / stop-loss) value is expressed */
32
+ declare enum TriggerUnit {
33
+ /** An absolute price level */
34
+ Price = "$",
35
+ /** A percentage offset from the entry price */
36
+ Percent = "%",
37
+ }
38
+ /** Which exit an attached trigger represents */
39
+
32
40
  declare enum LegType {
33
41
  Option = "option",
34
42
  Equity = "equity",
@@ -37,95 +45,62 @@ declare enum TradeType {
37
45
  Simple = 0,
38
46
  MultiLeg = 1,
39
47
  }
40
- //#endregion
41
- //#region ../../lib/types/src/schemas/trade-link.schemas.d.ts
42
- /**
43
- * Trade Link Leg Config - derived from TradeLeg without execution_price
44
- */
45
- declare const zTradeLinkLegConfig: z.ZodObject<Omit<{
46
- type: z.ZodNativeEnum<typeof LegType>;
47
- action: z.ZodNativeEnum<typeof TradeAction>;
48
- position_effect: z.ZodNullable<z.ZodNativeEnum<typeof PositionEffect>>;
49
- occ: z.ZodNullable<z.ZodString>;
50
- quantity: z.ZodNumber;
51
- execution_price: z.ZodNullable<z.ZodObject<{
52
- amount: z.ZodNumber;
53
- currency: z.ZodString;
54
- }, "strip", z.ZodTypeAny, {
55
- amount: number;
56
- currency: string;
57
- }, {
58
- amount: number;
59
- currency: string;
60
- }>>;
61
- }, "execution_price">, "strip", z.ZodTypeAny, {
62
- type: LegType;
63
- action: TradeAction;
64
- position_effect: PositionEffect | null;
65
- occ: string | null;
66
- quantity: number;
67
- }, {
68
- type: LegType;
69
- action: TradeAction;
70
- position_effect: PositionEffect | null;
71
- occ: string | null;
72
- quantity: number;
73
- }>;
74
48
  /**
75
- * Full Trade Link Config - stored in database as JSONB
76
- * Supports both simple trades and multi-leg options trades
49
+ * A brokerage-reported option lifecycle event. Rows carrying an event describe
50
+ * what happened to a contract, so `action` is not meaningful on them.
77
51
  */
78
52
  //#endregion
79
- //#region ../../lib/types/src/types/misc.types.d.ts
80
- type SnakeToCamelCase<S extends string> = S extends `${infer P1}_${infer P2}` ? `${P1}${Capitalize<SnakeToCamelCase<P2>>}` : S;
81
- /** Converts a type's snake_case keys to camelCase keys */
82
- type Camelize<T> = T extends object ? { [K in keyof T as K extends string ? SnakeToCamelCase<K> : K]: Camelize<T[K]> } : T;
83
- //#endregion
84
- //#region ../../lib/types/src/types/trade-link.types.d.ts
85
- /** Configuration for a leg in a multi-leg trade link */
86
- type TradeLinkLegConfig = z.infer<typeof zTradeLinkLegConfig>;
87
- /** Full configuration stored in the trade link */
88
-
89
- /**
90
- * Short query parameter keys for trade link URLs
91
- */
53
+ //#region src/types.d.ts
54
+ type TradeItEventType = 'tradeit.ready' | 'tradeit.close_requested' | 'tradeit.brokerage_connected' | 'tradeit.trade_submitted' | 'tradeit.trade_executed' | 'tradeit.error' | 'tradeit.login_required';
55
+ interface TradeItEvent<TPayload = Record<string, unknown>> {
56
+ source: 'tradeit';
57
+ version: 1;
58
+ type: TradeItEventType;
59
+ payload?: TPayload;
60
+ }
61
+ type TradeItQueryPrimitive = string | number | boolean | null | undefined;
62
+ type TradeItMode = 'connect' | 'trade';
63
+ /** Compact query keys accepted by Trade It trade URLs. */
92
64
  declare enum TradeLinkParam {
93
65
  TradeType = "tt",
94
- // s (simple), m (multi_leg)
95
66
  Action = "t",
96
- // b, s, bto, btc, sto, stc
67
+ Direction = "dir",
97
68
  OrderType = "ot",
98
- // m, l, st, sl
99
69
  Amount = "a",
100
- // number
101
70
  Unit = "u",
102
- // d, s
103
71
  LimitPrice = "lp",
104
- // number
105
72
  StopPrice = "sp",
106
- // number
107
73
  TimeInForce = "tif",
108
- // d, gtc, ioc, fok
74
+ TakeProfit = "tp",
75
+ StopLoss = "sl",
109
76
  UtmReferred = "ref",
110
- // string
111
77
  Legs = "legs",
112
78
  }
113
- //#endregion
114
- //#region src/types.d.ts
115
- type TradeItEventType = 'tradeit.ready' | 'tradeit.close_requested' | 'tradeit.brokerage_connected' | 'tradeit.trade_submitted' | 'tradeit.trade_executed' | 'tradeit.error' | 'tradeit.login_required';
116
- interface TradeItEvent<TPayload = Record<string, unknown>> {
117
- source: 'tradeit';
118
- version: 1;
119
- type: TradeItEventType;
120
- payload?: TPayload;
79
+ /** Canonical snake_case leg shape used by Trade Link APIs. */
80
+ interface TradeLinkLegConfig {
81
+ type: LegType;
82
+ action: TradeAction;
83
+ position_effect: PositionEffect | null;
84
+ occ: string | null;
85
+ quantity: number;
86
+ }
87
+ /** User-facing camelCase leg shape accepted by the embedded SDK. */
88
+ interface TradeItMultiLegTradeLegConfig {
89
+ type?: LegType;
90
+ action?: TradeAction;
91
+ positionEffect?: PositionEffect | null;
92
+ occ?: string | null;
93
+ quantity?: number;
94
+ }
95
+ /** Attached take-profit or stop-loss trigger. */
96
+ interface TradeItExitTrigger {
97
+ value: number;
98
+ unit: '$' | '%';
121
99
  }
122
- type TradeItQueryPrimitive = string | number | boolean | null | undefined;
123
- type TradeItMode = 'connect' | 'trade';
124
- type TradeItMultiLegTradeLegConfig = Partial<Camelize<TradeLinkLegConfig>>;
125
100
  /**
126
101
  * Query params accepted by Trade It's /t/[ticker] page.
127
102
  * User-facing config uses descriptive keys; core converts them to the short-key
128
- * query format used by the page parser (tt, t, ot, a, u, lp, sp, tif, ref, legs).
103
+ * query format used by the page parser (tt, t, dir, ot, a, u, lp, sp, tif, tp, sl, ref, legs).
129
104
  */
130
105
  interface TradeItTradeConfigBase {
131
106
  ticker: string;
@@ -140,6 +115,8 @@ interface TradeItSimpleTradeConfig extends TradeItTradeConfigBase {
140
115
  action?: TradeAction;
141
116
  amount?: number;
142
117
  unit?: TradeUnit;
118
+ takeProfit?: TradeItExitTrigger;
119
+ stopLoss?: TradeItExitTrigger;
143
120
  }
144
121
  interface TradeItMultiLegTradeConfig extends TradeItTradeConfigBase {
145
122
  tradeType: TradeType.MultiLeg;
@@ -171,4 +148,4 @@ declare function getTradeItErrorMessage(event: TradeItEvent): string;
171
148
  declare function resolveLaunchUrl(launch: TradeItLaunch): string;
172
149
  declare function getUrlOrigin(url: string): string | null;
173
150
  //#endregion
174
- export { LegType, OrderDirection, OrderType, PositionEffect, TimeInForce, TradeAction, type TradeItConnectLaunch, type TradeItEvent, type TradeItEventType, type TradeItLaunch, type TradeItLaunchConfigMap, type TradeItMode, type TradeItMultiLegTradeConfig, type TradeItMultiLegTradeLegConfig, type TradeItQueryPrimitive, type TradeItSimpleTradeConfig, type TradeItTradeConfig, type TradeItTradeLaunch, type TradeLinkLegConfig, TradeLinkParam, TradeType, TradeUnit, getTradeItErrorMessage, getUrlOrigin, isTradeItEvent, resolveLaunchUrl };
151
+ export { LegType, OrderDirection, OrderType, PositionEffect, TimeInForce, TradeAction, type TradeItConnectLaunch, type TradeItEvent, type TradeItEventType, type TradeItExitTrigger, type TradeItLaunch, type TradeItLaunchConfigMap, type TradeItMode, type TradeItMultiLegTradeConfig, type TradeItMultiLegTradeLegConfig, type TradeItQueryPrimitive, type TradeItSimpleTradeConfig, type TradeItTradeConfig, type TradeItTradeLaunch, type TradeLinkLegConfig, TradeLinkParam, TradeType, TradeUnit, TriggerUnit, getTradeItErrorMessage, getUrlOrigin, isTradeItEvent, resolveLaunchUrl };
package/dist/index.js CHANGED
@@ -19,6 +19,7 @@ let OrderType = /* @__PURE__ */ function(OrderType$1) {
19
19
  let OrderDirection = /* @__PURE__ */ function(OrderDirection$1) {
20
20
  OrderDirection$1["Debit"] = "debit";
21
21
  OrderDirection$1["Credit"] = "credit";
22
+ OrderDirection$1["Even"] = "even";
22
23
  return OrderDirection$1;
23
24
  }({});
24
25
  let TimeInForce = /* @__PURE__ */ function(TimeInForce$1) {
@@ -33,6 +34,14 @@ let TradeUnit = /* @__PURE__ */ function(TradeUnit$1) {
33
34
  TradeUnit$1["Shares"] = "shares";
34
35
  return TradeUnit$1;
35
36
  }({});
37
+ /** How an exit trigger (take-profit / stop-loss) value is expressed */
38
+ let TriggerUnit = /* @__PURE__ */ function(TriggerUnit$1) {
39
+ /** An absolute price level */
40
+ TriggerUnit$1["Price"] = "$";
41
+ /** A percentage offset from the entry price */
42
+ TriggerUnit$1["Percent"] = "%";
43
+ return TriggerUnit$1;
44
+ }({});
36
45
  let LegType = /* @__PURE__ */ function(LegType$1) {
37
46
  LegType$1["Option"] = "option";
38
47
  LegType$1["Equity"] = "equity";
@@ -45,70 +54,87 @@ let TradeType = /* @__PURE__ */ function(TradeType$1) {
45
54
  }({});
46
55
 
47
56
  //#endregion
48
- //#region ../../lib/types/src/types/trade-link.types.ts
49
- /**
50
- * Short query parameter keys for trade link URLs
51
- */
57
+ //#region src/events.ts
58
+ function isTradeItEvent(data) {
59
+ if (!data || typeof data !== "object") return false;
60
+ const message = data;
61
+ return message.source === "tradeit" && message.version === 1 && typeof message.type === "string" && message.type.startsWith("tradeit.");
62
+ }
63
+ function getTradeItErrorMessage(event) {
64
+ const message = event.payload?.message;
65
+ return typeof message === "string" ? message : "Trade It embedded experience returned an error event.";
66
+ }
67
+
68
+ //#endregion
69
+ //#region src/types.ts
70
+ /** Compact query keys accepted by Trade It trade URLs. */
52
71
  let TradeLinkParam = /* @__PURE__ */ function(TradeLinkParam$1) {
53
72
  TradeLinkParam$1["TradeType"] = "tt";
54
73
  TradeLinkParam$1["Action"] = "t";
74
+ TradeLinkParam$1["Direction"] = "dir";
55
75
  TradeLinkParam$1["OrderType"] = "ot";
56
76
  TradeLinkParam$1["Amount"] = "a";
57
77
  TradeLinkParam$1["Unit"] = "u";
58
78
  TradeLinkParam$1["LimitPrice"] = "lp";
59
79
  TradeLinkParam$1["StopPrice"] = "sp";
60
80
  TradeLinkParam$1["TimeInForce"] = "tif";
81
+ TradeLinkParam$1["TakeProfit"] = "tp";
82
+ TradeLinkParam$1["StopLoss"] = "sl";
61
83
  TradeLinkParam$1["UtmReferred"] = "ref";
62
84
  TradeLinkParam$1["Legs"] = "legs";
63
85
  return TradeLinkParam$1;
64
86
  }({});
65
87
 
66
- //#endregion
67
- //#region src/events.ts
68
- function isTradeItEvent(data) {
69
- if (!data || typeof data !== "object") return false;
70
- const message = data;
71
- return message.source === "tradeit" && message.version === 1 && typeof message.type === "string" && message.type.startsWith("tradeit.");
72
- }
73
- function getTradeItErrorMessage(event) {
74
- const message = event.payload?.message;
75
- return typeof message === "string" ? message : "Trade It embedded experience returned an error event.";
76
- }
77
-
78
88
  //#endregion
79
89
  //#region src/url.ts
80
90
  function appendQueryParam(params, key, value) {
81
- if (value == null) return;
91
+ if (value == null || value === "") return;
82
92
  params.set(key, String(value));
83
93
  }
84
94
  function isMultiLegTradeConfig(config) {
85
95
  return config.tradeType === TradeType.MultiLeg || Array.isArray(config.legs);
86
96
  }
97
+ function toTradeLinkLegConfig(leg) {
98
+ return {
99
+ type: leg.type ?? LegType.Equity,
100
+ action: leg.action ?? TradeAction.Buy,
101
+ position_effect: leg.positionEffect ?? null,
102
+ occ: leg.occ ?? null,
103
+ quantity: leg.quantity ?? 1
104
+ };
105
+ }
106
+ function encodeExitTrigger(trigger) {
107
+ if (!trigger) return void 0;
108
+ return trigger.unit === "%" ? `${trigger.value}%` : String(trigger.value);
109
+ }
87
110
  function toShortLegConfig(leg) {
88
111
  return {
89
112
  type: leg.type,
90
113
  t: leg.action,
91
- pe: leg.positionEffect,
114
+ pe: leg.position_effect,
92
115
  occ: leg.occ,
93
116
  q: leg.quantity
94
117
  };
95
118
  }
96
- function normalizeTradeConfig(config) {
119
+ function tradeConfigToSearchParams(config) {
97
120
  const params = new URLSearchParams();
98
- if (!config) return params;
99
121
  const multiLeg = isMultiLegTradeConfig(config);
100
122
  appendQueryParam(params, TradeLinkParam.TradeType, multiLeg ? TradeType.MultiLeg : TradeType.Simple);
101
- appendQueryParam(params, TradeLinkParam.OrderType, config.orderType);
123
+ appendQueryParam(params, TradeLinkParam.OrderType, config.orderType ?? OrderType.Market);
102
124
  appendQueryParam(params, TradeLinkParam.LimitPrice, config.limitPrice);
103
125
  appendQueryParam(params, TradeLinkParam.StopPrice, config.stopPrice);
104
126
  appendQueryParam(params, TradeLinkParam.TimeInForce, config.timeInForce);
105
127
  appendQueryParam(params, TradeLinkParam.UtmReferred, config.referral);
106
- if (multiLeg) params.set(TradeLinkParam.Legs, JSON.stringify(config.legs.map(toShortLegConfig)));
107
- else {
108
- appendQueryParam(params, TradeLinkParam.Action, config.action);
109
- appendQueryParam(params, TradeLinkParam.Amount, config.amount);
110
- appendQueryParam(params, TradeLinkParam.Unit, config.unit);
128
+ if (multiLeg) {
129
+ appendQueryParam(params, TradeLinkParam.Direction, config.direction);
130
+ if (config.legs.length > 0) params.set(TradeLinkParam.Legs, JSON.stringify(config.legs.map(toTradeLinkLegConfig).map(toShortLegConfig)));
131
+ return params;
111
132
  }
133
+ appendQueryParam(params, TradeLinkParam.Action, config.action);
134
+ appendQueryParam(params, TradeLinkParam.Amount, config.amount);
135
+ appendQueryParam(params, TradeLinkParam.Unit, config.unit ?? TradeUnit.Dollars);
136
+ appendQueryParam(params, TradeLinkParam.TakeProfit, encodeExitTrigger(config.takeProfit));
137
+ appendQueryParam(params, TradeLinkParam.StopLoss, encodeExitTrigger(config.stopLoss));
112
138
  return params;
113
139
  }
114
140
  function mergeSearchParams(url, extra) {
@@ -123,7 +149,7 @@ function resolveLaunchUrl(launch) {
123
149
  const encodedTicker = encodeURIComponent(launch.config.ticker);
124
150
  const rawUrl = launch.url.replace("[ticker]", encodedTicker);
125
151
  const url = new URL(rawUrl, typeof window !== "undefined" ? window.location.href : "https://tradeit.app");
126
- const configParams = normalizeTradeConfig(launch.config);
152
+ const configParams = tradeConfigToSearchParams(launch.config);
127
153
  mergeSearchParams(url, configParams);
128
154
  if (!url.searchParams.has("embedded")) url.searchParams.set("embedded", "1");
129
155
  return url.toString();
@@ -137,4 +163,4 @@ function getUrlOrigin(url) {
137
163
  }
138
164
 
139
165
  //#endregion
140
- export { LegType, OrderDirection, OrderType, PositionEffect, TimeInForce, TradeAction, TradeLinkParam, TradeType, TradeUnit, getTradeItErrorMessage, getUrlOrigin, isTradeItEvent, resolveLaunchUrl };
166
+ export { LegType, OrderDirection, OrderType, PositionEffect, TimeInForce, TradeAction, TradeLinkParam, TradeType, TradeUnit, TriggerUnit, getTradeItErrorMessage, getUrlOrigin, isTradeItEvent, resolveLaunchUrl };
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "@trade-it/core",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "description": "Framework-agnostic core for Trade It embedded SDKs.",
5
+ "author": "Trade It, Inc.",
5
6
  "license": "MIT",
6
7
  "type": "module",
7
8
  "main": "./dist/index.cjs",
@@ -15,15 +16,29 @@
15
16
  }
16
17
  },
17
18
  "files": [
18
- "dist"
19
+ "dist",
20
+ "README.md",
21
+ "CHANGELOG.md",
22
+ "LICENSE"
19
23
  ],
20
24
  "sideEffects": false,
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "git+https://github.com/trade-it-inc/trade-it.git",
31
+ "directory": "packages/trade-it-core"
32
+ },
33
+ "homepage": "https://docs.tradeit.app/sdk",
34
+ "bugs": {
35
+ "url": "https://github.com/trade-it-inc/trade-it/issues"
36
+ },
21
37
  "scripts": {
22
38
  "build": "tsdown src/index.ts --format esm,cjs --dts --out-dir dist && node ../../scripts/fix-trade-it-sdk-dts.mjs dist/index.d.ts dist/index.d.cts",
23
- "dev": "tsdown src/index.ts --format esm,cjs --dts --out-dir dist --watch"
24
- },
25
- "dependencies": {
26
- "zod": "^3.25.67"
39
+ "dev": "tsdown src/index.ts --format esm,cjs --dts --out-dir dist --watch",
40
+ "test": "npm run build && tsc -p tsconfig.test.json --noEmit && node --test test/*.test.mjs",
41
+ "prepublishOnly": "npm test"
27
42
  },
28
43
  "devDependencies": {
29
44
  "tsdown": "^0.11.0",