重复的标识符 'alpha'.deno-ts(2300) 意外的关键字或标识符

发布于 2025-01-09 15:22:32 字数 1170 浏览 0 评论 0原文

我正在尝试在我的 Deno 应用程序中使用 Alpha Vantage 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);
        });
   

}

I am trying to use Alpha Vantage NPM package inside my Deno application. I tried to use SkyPack version of it. But it gives me the following error:

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

This is the code I am using:

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);
        });
   

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴我心暖 2025-01-16 15:22:32

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

也就是说,这是该模块的存储库源,以及这里是 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));

It looks like SkyPack is responding with a 401 for one of the sub-dependencies for that module. I'm also not even sure it's compatible with Deno.

That said, here's the repo source for the module, and here's the documentation for the API. It looks like it's just a simple REST API which discriminates requests by query parameters, so you can make your own Deno client without too much effort using that module as a template. I'll give you some starter code:

TS Playground

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));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文