๐Ÿžํ”„๋กœ๊ทธ๋ž˜๋ฐ/Next.js

[Next.js] CNN ๊ณตํฌ ํƒ์š• ์ง€์ˆ˜ ๊ณต์‹ api (feat. TypeScript)

TwoIceFish 2024. 8. 3. 18:31

CNN์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ณต์‹ ๋ฐ์ดํ„ฐ ํ˜ธ์ถœ ๋กœ์ง์ž…๋‹ˆ๋‹ค. ๋ธŒ๋ผ์šฐ์ €๋กœ ์ ‘์†์‹œ ๋ฐ์ดํ„ฐ ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ํฌ๋กค๋ง์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ 418 I'm a teapot์„ ๋„์šฐ๊ธฐ ๋•Œ๋ฌธ์— User-Agent๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ํ˜ธ์ถœํ–ˆ์Šต๋‹ˆ๋‹ค.

 

const data = await fetch("https://production.dataviz.cnn.io/index/fearandgreed/graphdata",
    {
        headers: {
            "Content-Type": "application/json",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
        }
    }
)

 

interface FearAndGreedData {
    score: number;
    rating: string;
    timestamp: string;
    previous_close: number;
    previous_1_week: number;
    previous_1_month: number;
    previous_1_year: number;
}

interface HistoricalData {
    timestamp: number;
    score: number;
    rating: string;
    data: {
        x: number;
        y: number;
        rating: string;
    }[];
}

interface MarketMomentumData {
    timestamp: number;
    score: number;
    rating: string;
    data: Array<Record<string, any>>;
}

interface StockPriceData {
    timestamp: number;
    score: number;
    rating: string;
    data: Array<Record<string, any>>;
}

interface PutCallOptionsData {
    timestamp: number;
    score: number;
    rating: string;
    data: Array<Record<string, any>>;
}

interface MarketVolatilityData {
    timestamp: number;
    score: number;
    rating: string;
    data: Array<Record<string, any>>;
}

interface JunkBondDemandData {
    timestamp: number;
    score: number;
    rating: string;
    data: Array<Record<string, any>>;
}

interface SafeHavenDemandData {
    timestamp: number;
    score: number;
    rating: string;
    data: Array<Record<string, any>>;
}

interface FearAndGreedIndex {
    fear_and_greed: FearAndGreedData;
    fear_and_greed_historical: HistoricalData;
    market_momentum_sp500: MarketMomentumData;
    market_momentum_sp125: MarketMomentumData;
    stock_price_strength: StockPriceData;
    stock_price_breadth: StockPriceData;
    put_call_options: PutCallOptionsData;
    market_volatility_vix: MarketVolatilityData;
    market_volatility_vix_50: MarketVolatilityData;
    junk_bond_demand: JunkBondDemandData;
    safe_haven_demand: SafeHavenDemandData;
}