firstock 1.0.6 → 1.0.8

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.
@@ -0,0 +1,4 @@
1
+ import { Firstock } from "./Classes/Firstock";
2
+ import { FirstockWebSocket } from "./websockets/websockets";
3
+ export { Firstock, FirstockWebSocket };
4
+ export default Firstock;
@@ -0,0 +1,26 @@
1
+ interface BasketMarginObject {
2
+ exchange?: string;
3
+ tradingSymbol?: string;
4
+ quantity?: string | number;
5
+ transactionType?: string;
6
+ [key: string]: any;
7
+ }
8
+ interface JsonData {
9
+ [key: string]: {
10
+ jKey: string;
11
+ [key: string]: any;
12
+ };
13
+ }
14
+ declare const saveData: (data: any, file: string, callback: (err: NodeJS.ErrnoException | null) => void) => void;
15
+ declare const readData: (callback: (err: Error | string | null, data: JsonData | null) => void) => void;
16
+ declare const checkifUserLoggedIn: ({ userId, jsonData }: {
17
+ userId: string;
18
+ jsonData: JsonData;
19
+ }, callback: (err: string | null, jKey: string | null) => void) => void;
20
+ declare const errorMessageMapping: (jsonData: {
21
+ message?: string;
22
+ }) => string;
23
+ declare const validateBasketMarginObject: (data: BasketMarginObject) => boolean;
24
+ declare const validateBasketMargin: (data: BasketMarginObject[]) => boolean;
25
+ declare const handleError: (error: any) => any;
26
+ export { saveData, readData, validateBasketMarginObject, validateBasketMargin, handleError, checkifUserLoggedIn, errorMessageMapping, };
@@ -0,0 +1,2 @@
1
+ declare const API_LINK = "https://api.firstock.in/V1";
2
+ export { API_LINK };
package/dist/test.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,70 @@
1
+ import WebSocket from 'ws';
2
+ export declare enum UpdateType {
3
+ ORDER = "order",
4
+ POSITION = "position",
5
+ MARKET_FEED = "market_feed",
6
+ OPTION_GREEKS = "option_greeks"
7
+ }
8
+ export interface Config {
9
+ scheme?: string;
10
+ host?: string;
11
+ path?: string;
12
+ source?: string;
13
+ accept_encoding?: string;
14
+ accept_language?: string;
15
+ origin?: string;
16
+ max_websocket_connection_retries?: number;
17
+ time_interval?: number;
18
+ }
19
+ export interface WebSocketModel {
20
+ tokens?: string[];
21
+ option_greeks_tokens?: string[];
22
+ order_data?: (data: any) => void;
23
+ position_data?: (data: any) => void;
24
+ subscribe_feed_data?: (data: any) => void;
25
+ subscribe_option_greeks_data?: (data: any) => void;
26
+ on_reconnect?: (ws: WebSocket) => void;
27
+ }
28
+ declare class SafeConn {
29
+ ws: WebSocket;
30
+ lock: boolean;
31
+ constructor(ws: WebSocket);
32
+ acquireLock(): Promise<void>;
33
+ releaseLock(): void;
34
+ }
35
+ declare class ConnectionManager {
36
+ private connMap;
37
+ private indexMap;
38
+ private lock;
39
+ private acquireLock;
40
+ private releaseLock;
41
+ countConnections(): Promise<number>;
42
+ addConnection(ws: WebSocket): Promise<SafeConn>;
43
+ checkIfConnectionExists(ws: WebSocket): Promise<boolean>;
44
+ writeMessage(ws: WebSocket, data: string): Promise<string | null>;
45
+ deleteConnection(ws: WebSocket): Promise<void>;
46
+ }
47
+ export declare const connections: ConnectionManager;
48
+ export declare function getUrlAndHeaderData(userId: string, config: Config): [string, any, Error | null];
49
+ export declare function readMessage(userId: string, ws: WebSocket, model: WebSocketModel, config: Config): Promise<void>;
50
+ export declare function subscribe(ws: WebSocket, tokens: string[]): Promise<{
51
+ error: {
52
+ message: string;
53
+ };
54
+ } | null>;
55
+ export declare function unsubscribe(ws: WebSocket, tokens: string[]): Promise<{
56
+ error: {
57
+ message: string;
58
+ };
59
+ } | null>;
60
+ export declare function subscribeOptionGreeks(ws: WebSocket, tokens: string[]): Promise<{
61
+ error: {
62
+ message: string;
63
+ };
64
+ } | null>;
65
+ export declare function unsubscribeOptionGreeks(ws: WebSocket, tokens: string[]): Promise<{
66
+ error: {
67
+ message: string;
68
+ };
69
+ } | null>;
70
+ export {};
@@ -0,0 +1,53 @@
1
+ import WebSocket from 'ws';
2
+ import { Config, WebSocketModel } from './websocket_functions';
3
+ export declare class FirstockWebSocket {
4
+ tokens?: string[];
5
+ option_greeks_tokens?: string[];
6
+ order_data?: (data: any) => void;
7
+ position_data?: (data: any) => void;
8
+ subscribe_feed_data?: (data: any) => void;
9
+ subscribe_option_greeks_data?: (data: any) => void;
10
+ on_reconnect?: (ws: WebSocket) => void;
11
+ constructor(options?: {
12
+ tokens?: string[];
13
+ option_greeks_tokens?: string[];
14
+ order_data?: (data: any) => void;
15
+ position_data?: (data: any) => void;
16
+ subscribe_feed_data?: (data: any) => void;
17
+ subscribe_option_greeks_data?: (data: any) => void;
18
+ on_reconnect?: (ws: WebSocket) => void;
19
+ });
20
+ toDict(): WebSocketModel;
21
+ }
22
+ export declare class Firstock {
23
+ static initializeWebsockets(userId: string, model: FirstockWebSocket, config?: Config): Promise<[WebSocket | null, {
24
+ error: {
25
+ message: string;
26
+ };
27
+ } | null]>;
28
+ static closeWebsocket(ws: WebSocket | null): Promise<{
29
+ error: {
30
+ message: string;
31
+ };
32
+ } | null>;
33
+ static subscribe(ws: WebSocket | null, tokens: string[]): Promise<{
34
+ error: {
35
+ message: string;
36
+ };
37
+ } | null>;
38
+ static unsubscribe(ws: WebSocket | null, tokens: string[]): Promise<{
39
+ error: {
40
+ message: string;
41
+ };
42
+ } | null>;
43
+ static subscribeOptionGreeks(ws: WebSocket | null, tokens: string[]): Promise<{
44
+ error: {
45
+ message: string;
46
+ };
47
+ } | null>;
48
+ static unsubscribeOptionGreeks(ws: WebSocket | null, tokens: string[]): Promise<{
49
+ error: {
50
+ message: string;
51
+ };
52
+ } | null>;
53
+ }
package/package.json CHANGED
@@ -1,11 +1,18 @@
1
1
  {
2
2
  "name": "firstock",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "description": "Node js package for using firstock developer apis",
5
5
  "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
6
7
  "type": "commonjs",
8
+ "files": [
9
+ "dist",
10
+ "README.md",
11
+ "LICENSE"
12
+ ],
7
13
  "scripts": {
8
14
  "build": "tsc",
15
+ "prepublishOnly": "npm run build",
9
16
  "test": "echo \"Error: no test specified\" && exit 1"
10
17
  },
11
18
  "repository": {
@@ -1,187 +0,0 @@
1
- const { Firstock, FirstockWebSocket } = require('./websockets');
2
- const { appendFileSync } = require('fs');
3
-
4
- function subscribeFeedData(data) {
5
- try {
6
- appendFileSync('websocket.log', `${JSON.stringify(data)}\n`);
7
- console.log(data);
8
- } catch (e) {
9
- console.error(`Error writing to log: ${e}`);
10
- }
11
- }
12
-
13
- function subscribeOptionGreeksData(data) {
14
- try {
15
- const timestamp = new Date().toISOString();
16
- appendFileSync('option_greeks.log', `${timestamp} - ${JSON.stringify(data)}\n`);
17
- } catch (e) {
18
- console.error(`Error writing option Greeks: ${e}`);
19
- }
20
- }
21
-
22
- function orderBookData(data) {
23
- try {
24
- const timestamp = new Date().toISOString();
25
- appendFileSync('order_detail.log', `${timestamp} - ${JSON.stringify(data)}\n`);
26
- } catch (e) {
27
- console.error(`Error opening log file: ${e}`);
28
- }
29
- }
30
-
31
- function positionBookData(data) {
32
- try {
33
- const timestamp = new Date().toISOString();
34
- appendFileSync('position_detail.log', `${timestamp} - ${JSON.stringify(data)}\n`);
35
- } catch (e) {
36
- console.error(`Error opening log file: ${e}`);
37
- }
38
- }
39
-
40
- function subscribeFeedData2(data) {
41
- try {
42
- const timestamp = new Date().toISOString();
43
- appendFileSync('websocket2.log', `${timestamp} - ${JSON.stringify(data)}\n`);
44
- } catch (e) {
45
- console.error(`Error opening log file: ${e}`);
46
- }
47
- }
48
-
49
- function subscribeFeedData3(data) {
50
- try {
51
- const timestamp = new Date().toISOString();
52
- appendFileSync('websocket3.log', `${timestamp} - ${JSON.stringify(data)}\n`);
53
- } catch (e) {
54
- console.error(`Error opening log file: ${e}`);
55
- }
56
- }
57
-
58
- function subscribeFeedData4(data) {
59
- try {
60
- const timestamp = new Date().toISOString();
61
- appendFileSync('websocket4.log', `${timestamp} - ${JSON.stringify(data)}\n`);
62
- } catch (e) {
63
- console.error(`Error opening log file: ${e}`);
64
- }
65
- }
66
-
67
- async function main() {
68
- const userId = 'NP2997';
69
-
70
- // Connection reference holder
71
- const connectionRef = { conn: null };
72
-
73
- // Reconnection callback
74
- function onReconnectCallback(newWs) {
75
- console.log("🔄 Connection reference updated");
76
- connectionRef.conn = newWs;
77
- }
78
-
79
- // Create WebSocket model
80
- const model = new FirstockWebSocket({
81
- tokens: [],
82
- option_greeks_tokens: [],
83
- order_data: orderBookData,
84
- position_data: positionBookData,
85
- subscribe_feed_data: subscribeFeedData,
86
- subscribe_option_greeks_data: subscribeOptionGreeksData,
87
- on_reconnect: onReconnectCallback
88
- });
89
-
90
- // Initialize WebSocket
91
- const [conn, err] = await Firstock.initializeWebsockets(userId, model);
92
- connectionRef.conn = conn;
93
-
94
- console.log("Error:", err);
95
-
96
- if (err) {
97
- console.log(`Connection failed: ${JSON.stringify(err)}`);
98
- return;
99
- } else {
100
- console.log("WebSocket connected successfully!");
101
- }
102
-
103
- // Subscribe to tokens
104
- const subscribeErr = await Firstock.subscribe(connectionRef.conn, ["BSE:500470|NSE:26000"]);
105
- console.log("Subscribe Error:", subscribeErr);
106
-
107
- // Subscribe to option Greeks (uncomment to use)
108
- // const optionErr = await Firstock.subscribeOptionGreeks(connectionRef.conn, ["NFO:44297"]);
109
- // console.log("Option Greeks Subscribe Error:", optionErr);
110
-
111
- // // Later, unsubscribe (uncomment to use)
112
- // await new Promise(resolve => setTimeout(resolve, 30000));
113
- // const unsubErr = await Firstock.unsubscribeOptionGreeks(connectionRef.conn, ["NFO:44283"]);
114
- // console.log("Option Greeks Unsubscribe Error:", unsubErr);
115
-
116
- // Multiple connections example (uncomment to use)
117
-
118
- // const model2 = new FirstockWebSocket({
119
- // tokens: [],
120
- // subscribe_feed_data: subscribeFeedData2
121
- // });
122
- // const [conn2, err2] = await Firstock.initializeWebsockets(userId, model2);
123
- // console.log("Error:", err2);
124
-
125
- // const subscribeErr2 = await Firstock.subscribe(conn2, ["BSE:1"]);
126
- // console.log("Error:", subscribeErr2);
127
-
128
- // const model3 = new FirstockWebSocket({
129
- // tokens: [],
130
- // subscribe_feed_data: subscribeFeedData3
131
- // });
132
- // const [conn3, err3] = await Firstock.initializeWebsockets(userId, model3);
133
- // console.log("Error:", err3);
134
-
135
- // const subscribeErr3 = await Firstock.subscribe(conn3, ["NSE:26000|BSE:1"]);
136
- // console.log("Error:", subscribeErr3);
137
-
138
- // const model4 = new FirstockWebSocket({
139
- // tokens: [],
140
- // subscribe_feed_data: subscribeFeedData4
141
- // });
142
- // const [conn4, err4] = await Firstock.initializeWebsockets(userId, model4);
143
- // console.log("Error:", err4);
144
-
145
- // const subscribeErr4 = await Firstock.subscribe(conn4, ["NSE:26000"]);
146
- // console.log("Error:", subscribeErr4);
147
-
148
- // Unsubscribe example
149
- await new Promise(resolve => setTimeout(resolve, 2000));
150
- const unsubErr = await Firstock.unsubscribe(connectionRef.conn, ["BSE:500470|NSE:26000"]);
151
- console.log("Unsubscribe Error:", unsubErr);
152
-
153
- // Wait for 25 seconds
154
- await new Promise(resolve => setTimeout(resolve, 200000));
155
-
156
- // Close WebSocket connection
157
- const closeErr = await Firstock.closeWebsocket(connectionRef.conn);
158
- console.log("Close Error:", closeErr);
159
-
160
- // Close additional connections (uncomment if using multiple connections)
161
- // const closeErr2 = await Firstock.closeWebsocket(conn2);
162
- // console.log("Close Error:", closeErr2);
163
-
164
- // const closeErr3 = await Firstock.closeWebsocket(conn3);
165
- // console.log("Close Error:", closeErr3);
166
-
167
- // Keep program running
168
- console.log("\nWebSocket test running. Press Ctrl+C to exit.");
169
-
170
- // Handle graceful shutdown
171
- process.on('SIGINT', async () => {
172
- console.log("\nExiting...");
173
- if (connectionRef.conn) {
174
- await Firstock.closeWebsocket(connectionRef.conn);
175
- }
176
- process.exit(0);
177
- });
178
-
179
- // Keep alive - never resolves, keeps running until SIGINT
180
- await new Promise(() => {});
181
- }
182
-
183
- // Run main function
184
- main().catch(error => {
185
- console.error("Fatal error:", error);
186
- process.exit(1);
187
- });