0

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

}
Hasani
  • 3,543
  • 14
  • 65
  • 125
  • What's the question? – jsejcksn Feb 24 '22 at 01:02
  • @jsejcksn: I don't know why do I get the errors and how can I get ride of them? – Hasani Feb 24 '22 at 03:20
  • The syntax in your example is not valid TS or JS. Did you mean for all of that code in the class to be in its constructor? – jsejcksn Feb 24 '22 at 03:45
  • @jsejcksn: I found that code from here: https://www.skypack.dev/view/alphavantage-ts I thought it's a working example. – Hasani Feb 24 '22 at 05:16
  • @jsejcksn: Do you know how can I modify and make it work? I am new in TS and Deno. – Hasani Feb 24 '22 at 05:17
  • That module was not written to be used with Deno. It was written for Node. Not all Node modules can work with Deno (_most_ can't). – jsejcksn Feb 24 '22 at 06:18
  • @jsejcksn: So you say at the moment I must forget using this library? – Hasani Feb 24 '22 at 06:21
  • 1
    Unless you can find another source than SkyPack to make it work, then yes. (The API is just a simple REST endpoint), and it doesn't look like that module even provides any endpoint types (and it was last updated in 2018). – jsejcksn Feb 24 '22 at 06:39

1 Answers1

1

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

jsejcksn
  • 27,667
  • 4
  • 38
  • 62
  • Thank you very much. You did a GRET job for me! – Hasani Feb 24 '22 at 18:43
  • Can I ask what does this line of code do? `export type Params = NonNullable[0]>;` – Hasani Feb 25 '22 at 17:07
  • 1
    It uses some [type utilities](https://www.typescriptlang.org/docs/handbook/utility-types.html) to create a type which represents the types of values that can be provided as the first argument to the [`URLSearchParams()`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/URLSearchParams) constructor (excluding `null` and `undefined`). – jsejcksn Feb 25 '22 at 21:19
  • Did you write all of this code yourself or you could find it/modify it from somewhere else? Because I like to know where can I find more information and learn more about it. – Hasani Feb 26 '22 at 20:34
  • 1
    I wrote it in response to your question. – jsejcksn Feb 26 '22 at 22:40