Skip to content

Instantly share code, notes, and snippets.

@Yumshot
Created April 29, 2022 02:36
Show Gist options
  • Save Yumshot/33e0d155ff9dc84bd838e7ec8836778f to your computer and use it in GitHub Desktop.
Save Yumshot/33e0d155ff9dc84bd838e7ec8836778f to your computer and use it in GitHub Desktop.
???
import { AxiosInstance } from 'axios';
import { SubmitTokenResponse } from './types';
declare class TDAmeritrade {
CONSUMER_KEY: string;
client: AxiosInstance;
refreshToken: string | undefined;
constructor(consumerKey: string);
submitAuthToken(token: string, offline: boolean, redirectUri: string): Promise<SubmitTokenResponse>;
getNewAccessToken(): Promise<SubmitTokenResponse>;
getOauthLink(redirectUri: string): string;
}
export = TDAmeritrade;
@Yumshot
Copy link
Author

Yumshot commented Apr 29, 2022

Hello, Shock here

This should fix it:

declare module "package-name" {
  declare class TDAmeritrade {
      CONSUMER_KEY: string;
      client: AxiosInstance;
      refreshToken: string | undefined;
      constructor(consumerKey: string);
      submitAuthToken(token: string, offline: boolean, redirectUri: string): Promise<SubmitTokenResponse>;
      getNewAccessToken(): Promise<SubmitTokenResponse>;
      getOauthLink(redirectUri: string): string;
  }

  export = TDAmeritrade;
}

after looking through the package , ive realized i can just yoink it all out myself and just recreate the classes. No dependencies or secrets
Thank you for your help tho.

interface TDAProps {}

interface TDAState {}
import { Logger } from '@nestjs/common';
import axios from 'axios';
class TDA {
  CONSUMER_KEY: string;
  client: any;
  refreshToken: any;
  constructor(consumerKey: string) {
    this.CONSUMER_KEY = consumerKey;
    this.client = axios.create({ baseURL: 'https://api.tdameritrade.com/v1' });
  }
  async submitAuthToken(token: string, offline: boolean, redirectURI: string) {
    try {
      const response = (
        await this.client.post(
          '/oauth2/token',
          JSON.stringify({
            grant_type: 'authorization_code',
            redirect_uri: redirectURI,
            code: decodeURIComponent(token),
            offline: offline ? 'offline' : '',
            client_id: `${this.CONSUMER_KEY}@AMER.OAUTHAP`,
          }),
        )
      ).data;
      this.client.defaults.headers.common[
        'Authorization'
      ] = `Bearer ${response.access_token}`;
      this.refreshToken = response.refresh_token;
      Logger.log('Token: ', response);
      return response;
    } catch (error) {
      return Logger.error(error);
    }
  }
}

export default TDA;

@shockch4rge
Copy link

Cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment