Here's an example of how you might implement these endpoints in an Express.js application:
javascript
const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const app = express();
app.use(bodyParser.json());
app.use(cookieParser());
app.use(session({
secret: 'your_secret_key',
resave: false,
saveUninitialized: true,
cookie: { secure: true } // Use secure cookies in production
}));
// Dummy order data for demonstration purposes
const books = {
"TESTJ" : [
{"side":"S","ioi":true,"act":"U","src":"CROX","price":"25","qty":"10","id":247697979505377280,"time":"1722226022072","priority":1721658058018,"mpid":"FINT","key":"6XO00FX8P694C5"},
{"side":"S","ioi":true,"act":"U","src":"CROX","price":"22","qty":"15","id":247697979505377280,"time":"1722226022072","priority":1721658037827,"mpid":"FINT","key":"6XO00FX8P693C5"},
{"side":"B","ioi":true,"act":"U","src":"CROX","price":"15","qty":"10","id":247697979505377280,"time":"1722226022070","priority":1721657761494,"mpid":"FINT","key":"6XO00FX8P690C5"},
{"side":"B","ioi":true,"act":"U","src":"CROX","price":"15","qty":"10","id":247697979505377280,"time":"1722226022071","priority":1721657774888,"mpid":"FINT","key":"6XO00FX8P691C5"},
{"side":"B","act":"U","src":"CROX","price":"15","qty":"10000","id":247697979505377280,"time":"1722226022073","priority":1721877877656,"mpid":"FINT","key":"6XQ00ISGI4LYC5"}
],
// Others
}
app.get('/firestapi/public/book', (req, res) => {
if (!req.query.symbol)
return res
.status(400)
.send({ message: "error", reason: "symbol not included" });
res.send({ data: book[req.query.symbol] || [] });
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
This example sets up the specified routes using Express.js and handles retrieving user orders, retrieving an order by reference number, and adding new orders. It includes necessary validations and error handling for the endpoints.
javascript
How to get book data from the websocket
After you have logged into the websocket
// Dummy order data for demonstration purposes
const books = {}
ws.send(
JSON.stringify({
type: "subscribe",
request: [
{
security: "*",
msg: "ibook",
},
],
ignoretoken: true,
})
);
const addBook = (currentBookEntry, security) => {
// act === "U"
let books;
const doesBookEntryExist = book[security].some(
(bookItem) => currentBookEntry.key === bookItem.key
);
if (doesBookEntryExist) {
books = book[security].map((bookItem) =>
bookItem.key === currentBookEntry.key ? currentBookEntry : bookItem
);
} else {
books = book[security].concat(currentBookEntry);
}
book[security] = sortBook(books);
};
const removeBook = (bookEntry, security) => {
// act === "R"
book[security] = book[security].filter(
(bookItem) => bookItem.key !== bookEntry.key
);
};
ws.on("message", function requestHandler(data) {
data = JSON.parse(data);
if (data.type == "book") {
if (!book[data.security]) book[data.security] = [];
for (let index = 0; index < data.books.length; index++) {
const bookEntry = data.books[index];
const { act } = bookEntry;
if (act === "U") {
addBook(bookEntry, data.security);
} else if (act === "R") {
removeBook(bookEntry, data.security);
}
}
}
});
This example shows how we can get updates from the websocket data feed.