0

我正在尝试Alpha Vantage在我的 Deno 应用程序中使用 NPM 包。我尝试使用它的SkyPack版本。但它给了我以下错误:

Duplicate identifier 'alpha'.deno-ts(2300)
Unexpected keyword or identifier.

这是我正在使用的代码:

import alphavantageTs from 'https://cdn.skypack.dev/alphavantage-ts';

export class StockTimeSeries{
        alpha = new alphavantageTs ("ASLDVIWXGEWFWNZG");

        alpha.stocks.intraday("msft").then((data: any) => {
            console.log(data);
            });
        
        alpha.stocks.batch(["msft", "aapl"]).then((data: any) => {
        console.log(data);
        });
    
        alpha.forex.rate("btc", "usd").then((data: any) => {
        console.log(data);
        });
    
        alpha.crypto.intraday("btc", "usd").then((data: any) => {
        console.log(data);
        });
    
        alpha.technicals.sma("msft", "daily", 60, "close").then((data: any) => {
        console.log(data);
        });
    
        alpha.sectors.performance().then((data: any) => {
        console.log(data);
        });
   

}
4

1 回答 1

1

看起来 SkyPack 正在响应401该模块的子依赖项之一。我什至不确定它是否与 Deno 兼容。

也就是说,这里是模块的 repo 源,这里是 API 的文档。看起来它只是一个简单的 REST API,它通过查询参数来区分请求,因此您可以使用该模块作为模板来制作自己的 Deno 客户端,而无需花费太多精力。我会给你一些入门代码:

TS游乐场

export type Params = NonNullable<ConstructorParameters<typeof URLSearchParams>[0]>;

class AlphaVantageNS { constructor (protected readonly api: AlaphaVantage) {} }

class Forex extends AlphaVantageNS {
  rate (from_currency: string, to_currency: string) {
    return this.api.query({
      function: 'CURRENCY_EXCHANGE_RATE',
      from_currency,
      to_currency,
    });
  }
}

export class AlaphaVantage {
  #token: string;

  constructor (token: string) {
    this.#token = token;
  }

  async query <Result = any>(params: Params): Promise<Result> {
    const url = new URL('https://www.alphavantage.co/query');

    const usp = new URLSearchParams(params);
    usp.set('apikey', this.#token);
    url.search = usp.toString();

    const request = new Request(url.href);
    const response = await fetch(request);

    if (!response.ok) throw new Error('Response not OK');

    return response.json();
  }

  forex = new Forex(this);
}


// Use:

const YOUR_API_KEY = 'demo';
const alpha = new AlaphaVantage(YOUR_API_KEY);
alpha.forex.rate('BTC', 'USD').then(data => console.log(data));

于 2022-02-24T07:45:41.177 回答