soundcloudnodejs.js
function addTrackToNewPlaylist(options) {
    return new Promise(function (resolve, reject) {
        var form = new FormData();
        form.append('format', 'json');

        if (!options.tracks) {
            return reject('Error while addTrackToNewPlaylist options.tracks is null');
        } else {
            _.each(options.tracks, function (track) {
                form.append('playlist[tracks][][id]', track.id);
            });
        }
        if (!options.title) {
            return reject('Error while addTrackToNewPlaylist options.title is null');
        } else {
            form.append('playlist[title]', options.title);
        }
        if (!options.sharing) {
            return reject('Error while addTrackToNewPlaylist options.sharing is null');
        } else {
            form.append('playlist[sharing]', options.sharing);
        }
        if (!options.oauth_token) {
            return reject('Error while addTrackToNewPlaylist options.oauth_token is null');
        } else {
            form.append('oauth_token', options.oauth_token);
        }

        form.getLength(function (err, length) {

            fetch(`https://api.soundcloud.com/playlists?oauth_token=${options.oauth_token}`, {
                method: 'POST',
                body: form,
                headers: {'content-length': length}
            }).then(function (res) {

                if (res.status === 201) {
                    if (DEBUG_MODE_ON) {
                        console.log(`INFO: addTrackToNewPlaylist, OK -> ${res.statusCode}.`);
                    }
                    return res.json();
                } else {
                    return null;
                }
            }).then(function (json) {
                if (!json) {
                    reject();
                } else {
                    if (DEBUG_MODE_ON) {
                        console.log('INFO: addTrack successful');
                    }
                    resolve(json);
                }
            }).catch(function (e) {
                reject(e);
            });

        });
    });
}
function addTrackToNewPlaylist(options) {
    return new Promise(function (resolve, reject) {
        var form = new FormData();
        form.append('format', 'json');

        if (!options.tracks) {
            return reject('Error while addTrackToNewPlaylist options.tracks is null');
        } else {
            _.each(options.tracks, function (track) {
                form.append('playlist[tracks][][id]', track.id);
            });
        }
        if (!options.title) {
            return reject('Error while addTrackToNewPlaylist options.title is null');
        } else {
            form.append('playlist[title]', options.title);
        }
        if (!options.sharing) {
            return reject('Error while addTrackToNewPlaylist options.sharing is null');
        } else {
            form.append('playlist[sharing]', options.sharing);
        }
        if (!options.oauth_token) {
            return reject('Error while addTrackToNewPlaylist options.oauth_token is null');
        } else {
            form.append('oauth_token', options.oauth_token);
        }

        form.getLength(function (err, length) {

            fetch(`https://api.soundcloud.com/playlists?oauth_token=${options.oauth_token}`, {
                method: 'POST',
                body: form,
                headers: {'content-length': length}
            }).then(function (res) {

                if (res.status === 201) {
                    if (DEBUG_MODE_ON) {
                        console.log(`INFO: addTrackToNewPlaylist, OK -> ${res.statusCode}.`);
                    }
                    return res.json();
                } else {
                    return null;
                }
            }).then(function (json) {
                if (!json) {
                    reject();
                } else {
                    if (DEBUG_MODE_ON) {
                        console.log('INFO: addTrack successful');
                    }
                    resolve(json);
                }
            }).catch(function (e) {
                reject(e);
            });

        });
    });
}
function addTrackToNewPlaylist ( options ) { return new Promise ( function ( resolve , reject ) { var form = new FormData ( ) ; form . append ( 'format' , 'json' ) ; if ( ! options . tracks ) { return reject ( 'Error while addTrackToNewPlaylist options.tracks is null' ) ; } else { _ . each ( options . tracks , function ( track ) { form . append ( 'playlist[tracks][][id]' , track . id ) ; } ) ; } if ( ! options . title ) { return reject ( 'Error while addTrackToNewPlaylist options.title is null' ) ; } else { form . append ( 'playlist[title]' , options . title ) ; } if ( ! options . sharing ) { return reject ( 'Error while addTrackToNewPlaylist options.sharing is null' ) ; } else { form . append ( 'playlist[sharing]' , options . sharing ) ; } if ( ! options . oauth_token ) { return reject ( 'Error while addTrackToNewPlaylist options.oauth_token is null' ) ; } else { form . append ( 'oauth_token' , options . oauth_token ) ; } form . getLength ( function ( err , length ) { fetch ( ` ${ options . oauth_token } ` , { method : 'POST' , body : form , headers : { 'content-length' : length } } ) . then ( function ( res ) { if ( res . status === 201 ) { if ( DEBUG_MODE_ON ) { console . log ( ` ${ res . statusCode } ` ) ; } return res . json ( ) ; } else { return null ; } } ) . then ( function ( json ) { if ( ! json ) { reject ( ) ; } else { if ( DEBUG_MODE_ON ) { console . log ( 'INFO: addTrack successful' ) ; } resolve ( json ) ; } } ) . catch ( function ( e ) { reject ( e ) ; } ) ; } ) ; } ) ; }
Add track to new playlist
@param options
@param cb
@returns {*}
Add track to new playlist