JMPerez / spotify-web-api-js

A client-side JS wrapper for the Spotify Web API
https://jmperezperez.com/spotify-web-api-js/
MIT License
1.87k stars 260 forks source link

Latest type definition breaks typescript compilation #121

Closed pduchesne closed 4 years ago

pduchesne commented 5 years ago

My typescript code uses the api as follows :

import * as SpotifyWebApi from 'spotify-web-api-js';

let spotifyApi: SpotifyWebApi.SpotifyWebApiJs;

This works fine with 1.1.0 . Since 1.1.1 , this fails with Namespace '".../spotify-web-api"' has no exported member 'SpotifyWebApiJs' .

I could not find a way to work with the new type file.

JMPerez commented 5 years ago

That's interesting. The latest change related to that was https://github.com/JMPerez/spotify-web-api-js/pull/44, which shipped in 1.1.0.

I'm myself not that familiar with Typescript so I would appreciate if someone can jump in and help debugging this issue. Maybe @amelialaundy can help out?

amelialaundy commented 5 years ago

Hey @pduchesne I have the following:

import SpotifyWebApi from 'spotify-web-api-js'
export class Spotify {
    private client: SpotifyWebApi.SpotifyWebApiJs;
    constructor(token: string) {
        this.client = new SpotifyWebApi();
        this.client.setAccessToken(token);
    }

That should work for you.

I think I made those type changes in 1.1.0, and sounds like what you are saying is they are breaking changes so I'll take a look.

pduchesne commented 5 years ago

Thank you both for your response. @amelialaundy I tried your suggestion but to no avail. Actually I have identified the problematic PR as being #112 , included in 1.1.1 .

I have set up a very simple project to highlight the problem : https://github.com/pduchesne/test-spotify-api-js

just check it out and run yarn && yarn test . The test using 1.1.1 will not even compile.

EDIT I updated the failing test to use your suggestion. It then compiles, but fails at runtime.

csantiago132 commented 5 years ago

@JMPerez @amelialaundy @pduchesne in my app, im using it like this (this is in a saga btw but should not matter)

import SpotifyWebApi from 'spotify-web-api-js';

const spotifyApi = new SpotifyWebApi();

whole file looks like this:

import { call, put, takeLatest, fork } from 'redux-saga/effects';
import SpotifyWebApi from 'spotify-web-api-js';
import ActionTypes from './constants';
import * as Actions from './actions';

const spotifyApi = new SpotifyWebApi();

/**
 * @generator
 * @remarks working saga in charge of getting the albums data from Spotify
 *
 */
export function* getAlbumCatalogSaga() {
  const token = sessionStorage.getItem('react-spotify-access-token');
  spotifyApi.setAccessToken(`${token}`);

  /**
   * @function
   * @remarks calls the spotify endpoint that has the user's albums
   *
   * @returns Promise
   */
  const getSpotifyPlaylists = () =>
    spotifyApi
      .getMySavedAlbums()
      .then((response) => response)
      .catch((error) => new Error(`${error}`));

  try {
    const getData = yield call(getSpotifyPlaylists);
    yield put(Actions.fetchSpotifyAlbumSuccess(getData));
  } catch (error) {
    yield put(Actions.fetchSpotifyAlbumError(error.message));
  }
}

/**
 * @generator
 * @remarks root saga manages watcher lifecycle of getAlbumCatalogSaga()
 */
export function* albumCatalogSaga() {
  /**
   * Calls for authorization from Spotify every time it is called,
   * by using `takeLatest` only the result of the latest API call is applied.
   * It will be cancelled automatically on component unmount
   */
  yield takeLatest(ActionTypes.SPOTIFY_ALBUM_IS_LOADING, getAlbumCatalogSaga);
}

/**
 * export for saga registry
 */
export const saga = [fork(albumCatalogSaga)];
bmitchinson commented 5 years ago

Having a similar issue, maybe I'm missing something obvious?

import SpotifyWebApi from 'spotify-web-api-js';
const spotifyApi = new SpotifyWebApi();

spotifyApi.setAccessToken('xxxxx');

results in

TypeError: spotify_web_api_js_1.default is not a constructor
    at Object.<anonymous> (/Users/bmitchinson/Playlists/platformScripts/spotify/spotifyToFirestore.ts:5:20)
    at Module._compile (module.js:652:30)
    at Module.m._compile (/Users/bmitchinson/.nvm/versions/node/v8.11.1/lib/node_modules/ts-node/src/index.ts:473:23)
bmitchinson commented 5 years ago

I'm a loser, reminder that this library is for web, not for node.

Was able to get around that error above by messing around with the transpilled js, and removing default from the constructor call. Obviously that lead to the realization that I should have just read the readme 🥇

JMPerez commented 4 years ago

Closed with commit https://github.com/JMPerez/spotify-web-api-js/commit/1487329717fc12665bd9b983b71040609bdb4069