alpacahq / alpaca-ts

A TypeScript Node.js library for the https://alpaca.markets REST API and WebSocket streams.
ISC License
156 stars 42 forks source link

Support for overriding baseUrls #105

Closed wizard23 closed 2 years ago

wizard23 commented 2 years ago

Is your feature request related to a problem? Please describe. Yes. I use the official Alpaca JS API in the browser but I use a local CORS proxy to access it because otherwise I get a CORS error when opening Orders.

I would love to have types and number/string conversion though so I need to be able to override the the baseUrls (baseUrl, dataBaseUrl, and dataStreamUrl in the official API's Alpaca constructor parameters). This is how I use the official API atm:

const alpaca = new Alpaca({
    keyId: "...",
    secretKey: "...",
    paper: true,
    baseUrl: "http://localhost:8031/proxy",
    dataBaseUrl: "...";
    dataStreamUrl: "...";
})

As far as I can see it is not possible to change the baseUrls in the TS API at the moment.

Describe the solution you'd like I would like to be able to override the baseUrls (named baseUrl, dataBaseUrl, and dataStreamUrl) just like in the official API.

Describe alternatives you've considered Maybe I'm doing it all wrong and there is a way to do it already but I looked at the source code and the URLs seem to be coming from the ./urls.js and I see no code to override them.

Alternatively I'm using it incorrectly all along and one should not use a local CORS proxy at all. But I can place orders successfully via my CORS proxy + official API in the browser with my current setup.

I've managed to create a workaround by modifying the @master-chief\alpaca\dist\alpaca.js in the node_modules folder and setting urls.rest.account = 'http://localhost:8031/proxy/v2' but this is an ugly hack.

Additional context I really appreciate your work. Having the typing and the number parsing is GREAT. If you ever come to Vienna/Austria/Europe please contact me because I would like to invite you for a drink.

117 commented 2 years ago

Taking a look at a potential, simple solution. 😉

117 commented 2 years ago

Okay, with the latest version you will be able to pass custom endpopints param into both the AlpacaStream and AlpacaClient constructors. You don't have to pass all urls, just the ones you want to override, will be overridden.

Ill close this issue when ive published it.

117 commented 2 years ago

Oops closed early lmao. Anyways here is an example:

const client = new AlpacaClient({
  credentials: {
    key: 'mykey',
    secret: 'mysecret',
    paper: true,
  },
  endpoints: {
    rest: {
      account: 'https://my-custom-url-here/v2',
    },
  },
});
117 commented 2 years ago

Okay! This has been added to v6.3.20, hope this assists your use case.

wizard23 commented 2 years ago

That was fast! I tested it and using custom endpoints works. Great! Interesting: The Alpaca server now accepts orders without CORS errors. Did this change just now? Both solutions work now. Double kthnx! Offer for free drinks was serious. If you ever come to Vienna drop me an email :)

117 commented 2 years ago

will do my man :)