client side api calling
This commit is contained in:
parent
0c54633207
commit
1a1cdf613e
@ -1,44 +0,0 @@
|
|||||||
const usRegion = ['cle1','iad1','pdx1','sfo1'];
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export const load = async ({ params, locals }) => {
|
|
||||||
|
|
||||||
const userRegion = locals.region?.split("::")[0];
|
|
||||||
|
|
||||||
let apiURL;
|
|
||||||
|
|
||||||
if (usRegion?.includes(userRegion)) {
|
|
||||||
apiURL = import.meta.env.VITE_USEAST_API_URL;
|
|
||||||
} else {
|
|
||||||
apiURL = import.meta.env.VITE_EU_API_URL;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
const getAnalystTickerHistory = async () => {
|
|
||||||
|
|
||||||
const postData = {
|
|
||||||
ticker: params.tickerID
|
|
||||||
};
|
|
||||||
|
|
||||||
// make the POST request to the endpoint
|
|
||||||
const response = await fetch(apiURL + '/analyst-ticker-history', {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
body: JSON.stringify(postData)
|
|
||||||
});
|
|
||||||
|
|
||||||
const output = await response.json();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return output;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Make sure to return a promise
|
|
||||||
return {
|
|
||||||
getAnalystTickerHistory: await getAnalystTickerHistory()
|
|
||||||
};
|
|
||||||
};
|
|
||||||
56
src/routes/stocks/[tickerID]/analyst/+page.ts
Normal file
56
src/routes/stocks/[tickerID]/analyst/+page.ts
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
import { userRegion, getCache, setCache } from '$lib/store';
|
||||||
|
|
||||||
|
const usRegion = ['cle1','iad1','pdx1','sfo1'];
|
||||||
|
|
||||||
|
let apiURL = import.meta.env.VITE_EU_API_URL; // Set a default API URL
|
||||||
|
|
||||||
|
userRegion.subscribe(value => {
|
||||||
|
|
||||||
|
if (usRegion.includes(value)) {
|
||||||
|
apiURL = import.meta.env.VITE_USEAST_API_URL;
|
||||||
|
} else {
|
||||||
|
apiURL = import.meta.env.VITE_EU_API_URL;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
export const load = async ({ params }) => {
|
||||||
|
|
||||||
|
const getAnalystTickerHistory = async () => {
|
||||||
|
|
||||||
|
let output;
|
||||||
|
|
||||||
|
const cachedData = getCache(params.tickerID, 'getAnalystTickerHistory');
|
||||||
|
if (cachedData) {
|
||||||
|
output = cachedData;
|
||||||
|
} else {
|
||||||
|
const postData = {
|
||||||
|
ticker: params.tickerID
|
||||||
|
};
|
||||||
|
|
||||||
|
// make the POST request to the endpoint
|
||||||
|
const response = await fetch(apiURL + '/analyst-ticker-history', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify(postData)
|
||||||
|
});
|
||||||
|
|
||||||
|
output = await response.json();
|
||||||
|
|
||||||
|
setCache(params.tickerID, output, 'getAnalystTickerHistory');
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return output;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Make sure to return a promise
|
||||||
|
return {
|
||||||
|
getAnalystTickerHistory: await getAnalystTickerHistory()
|
||||||
|
};
|
||||||
|
};
|
||||||
@ -1,5 +1,18 @@
|
|||||||
|
import { userRegion, getCache, setCache } from '$lib/store';
|
||||||
|
|
||||||
const usRegion = ['cle1','iad1','pdx1','sfo1'];
|
const usRegion = ['cle1','iad1','pdx1','sfo1'];
|
||||||
|
|
||||||
|
let apiURL = import.meta.env.VITE_EU_API_URL; // Set a default API URL
|
||||||
|
|
||||||
|
userRegion.subscribe(value => {
|
||||||
|
|
||||||
|
if (usRegion.includes(value)) {
|
||||||
|
apiURL = import.meta.env.VITE_USEAST_API_URL;
|
||||||
|
} else {
|
||||||
|
apiURL = import.meta.env.VITE_EU_API_URL;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -25,26 +38,19 @@ const transactionTypeMap = {
|
|||||||
'': 'n/a'
|
'': 'n/a'
|
||||||
};
|
};
|
||||||
|
|
||||||
export const load = async ({ params, locals }) => {
|
export const load = async ({ params }) => {
|
||||||
|
|
||||||
const userRegion = locals.region?.split("::")[0];
|
|
||||||
|
|
||||||
let apiURL;
|
|
||||||
|
|
||||||
if (usRegion?.includes(userRegion)) {
|
|
||||||
apiURL = import.meta.env.VITE_USEAST_API_URL;
|
|
||||||
} else {
|
|
||||||
apiURL = import.meta.env.VITE_EU_API_URL;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
const getInsiderTrading = async () => {
|
const getInsiderTrading = async () => {
|
||||||
let output;
|
let output;
|
||||||
|
|
||||||
// Get cached data for the specific tickerID
|
|
||||||
const postData = {
|
const cachedData = getCache(params.tickerID, 'getInsiderTrading');
|
||||||
ticker: params.tickerID
|
if (cachedData) {
|
||||||
};
|
output = cachedData;
|
||||||
|
} else {
|
||||||
|
const postData = {
|
||||||
|
ticker: params.tickerID
|
||||||
|
};
|
||||||
|
|
||||||
// make the POST request to the endpoint
|
// make the POST request to the endpoint
|
||||||
const response = await fetch(apiURL + '/insider-trading', {
|
const response = await fetch(apiURL + '/insider-trading', {
|
||||||
@ -63,7 +69,10 @@ export const load = async ({ params, locals }) => {
|
|||||||
? transactionTypeMap[item?.transactionType](item)
|
? transactionTypeMap[item?.transactionType](item)
|
||||||
: transactionTypeMap[item?.transactionType] || 'n/a'
|
: transactionTypeMap[item?.transactionType] || 'n/a'
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
setCache(params.tickerID, output, 'getInsiderTrading');
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
};
|
};
|
||||||
@ -71,10 +80,20 @@ export const load = async ({ params, locals }) => {
|
|||||||
|
|
||||||
const getInsiderTradingStatistics = async () => {
|
const getInsiderTradingStatistics = async () => {
|
||||||
|
|
||||||
// Get cached data for the specific tickerID
|
let sums = {
|
||||||
const postData = {
|
totalBought: 0,
|
||||||
ticker: params.tickerID
|
totalSold: 0,
|
||||||
};
|
purchases: 0,
|
||||||
|
sales: 0
|
||||||
|
};
|
||||||
|
|
||||||
|
const cachedData = getCache(params.tickerID, 'getInsiderTradingStatistics');
|
||||||
|
if (cachedData) {
|
||||||
|
sums = cachedData;
|
||||||
|
} else {
|
||||||
|
const postData = {
|
||||||
|
ticker: params.tickerID
|
||||||
|
};
|
||||||
|
|
||||||
// make the POST request to the endpoint
|
// make the POST request to the endpoint
|
||||||
const response = await fetch(apiURL + '/insider-trading-statistics', {
|
const response = await fetch(apiURL + '/insider-trading-statistics', {
|
||||||
@ -85,15 +104,7 @@ export const load = async ({ params, locals }) => {
|
|||||||
body: JSON.stringify(postData)
|
body: JSON.stringify(postData)
|
||||||
});
|
});
|
||||||
|
|
||||||
const output = await response.json();
|
const output = await response?.json();
|
||||||
|
|
||||||
// Initialize a new object to store the sums
|
|
||||||
let sums = {
|
|
||||||
totalBought: 0,
|
|
||||||
totalSold: 0,
|
|
||||||
purchases: 0,
|
|
||||||
sales: 0
|
|
||||||
};
|
|
||||||
|
|
||||||
// Iterate through the list and accumulate the values
|
// Iterate through the list and accumulate the values
|
||||||
output?.forEach(item => {
|
output?.forEach(item => {
|
||||||
@ -102,9 +113,10 @@ export const load = async ({ params, locals }) => {
|
|||||||
sums.purchases += item.purchases;
|
sums.purchases += item.purchases;
|
||||||
sums.sales += item.sales;
|
sums.sales += item.sales;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
setCache(params.tickerID, sums, 'getInsiderTradingStatistics');
|
||||||
|
|
||||||
console.log(sums)
|
}
|
||||||
|
|
||||||
|
|
||||||
return sums;
|
return sums;
|
||||||
};
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user