Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of typings into sdk for Typescript #99

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "3.3.2",
"description": "",
"main": "sdk.js",
"types": "index.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/soundcloud/soundcloud-javascript.git"
Expand Down
2 changes: 2 additions & 0 deletions src/dialog/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ module.exports = {
const options = {};
let stringOptions;

if (!window) return;

options.location = 1;
options.width = width;
options.height = height;
Expand Down
4 changes: 3 additions & 1 deletion src/player-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ module.exports = function(scaudioPlayer) {
let timerId = 0;
let previousPosition = null;
scaudioPlayer.onChange.subscribe(({ playing, seeking, dead }) => {
if (!window) return;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (dead) {
window.clearTimeout(timerId);
} else if (playing !== undefined || seeking !== undefined) {
doEmit();
}
});
function doEmit() {
if (!window) return;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

window.clearTimeout(timerId);
if (scaudioPlayer.isPlaying() && !scaudioPlayer.isEnded()) {
timerId = window.setTimeout(doEmit, TIMEUPDATE_INTERVAL);
Expand Down Expand Up @@ -86,4 +88,4 @@ module.exports = function(scaudioPlayer) {
BackboneEvents.mixin(playerApi);
handleEmittingTimeEvents();
return playerApi;
}
}
4 changes: 2 additions & 2 deletions src/recorder/getusermedia.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const getUserMedia = global.navigator.getUserMedia ||
const getUserMedia = global.navigator ? (global.navigator.getUserMedia ||
global.navigator.webkitGetUserMedia ||
global.navigator.mozGetUserMedia;
global.navigator.mozGetUserMedia) : false;

module.exports = (options, success, error) => {
getUserMedia.call(global.navigator, options, success, error);
Expand Down
94 changes: 94 additions & 0 deletions types/soundcloud/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Type definitions for soundcloud 3.3.2
// Project: https://github.com/soundcloud/soundcloud-javascript
// Definitions by: alrickemilien <https://github.com/soundcloud>

/// <reference lib="dom"/>

export default SC;

declare namespace SC {
interface RecorderOptions {
source: string;
context?: AudioContext;
}

export class Recorder {
constructor(options: RecorderOptions);
delete(): void;
getBuffer(): Promise<AudioBuffer>;
getWAV(): Promise<Blob>;
play(): Promise<AudioBufferSourceNode>;
saveAs(filename: string): Promise<void>;
start(): Promise<AudioNode>;
stop(): void;
}

/**
* SCAudio.Player functions
*/
export interface SCPlayer {
play: (...args: any[]) => any;
pause: (...args: any[]) => any;
seek: (...args: any[]) => any;
getVolume: (...args: any[]) => any;
setVolume: (...args: any[]) => any;
currentTime: (...args: any[]) => any;
getDuration: (...args: any[]) => any;
isBuffering: () => boolean;
isPlaying: () => boolean;
isActuallyPlaying: () => boolean;
isEnded: () => boolean;
isDead: () => boolean;
kill: (...args: any[]) => any;
hasErrored: () => boolean;
getState: () => "playing" | "ended" | "paused" | "dead" | "loading";
}

interface DialogOptions {
client_id: string,
redirect_uri: string,
response_type: string,
scope: string,
display: string,
}

interface SCOptions {
oauth_token?: string;
client_id?: string;
redirect_uri?: string;
baseURL?: string;
connectURL?: string;
}

export function connect(options?: {
client_id?: string,
redirect_uri?: string
}): Promise<DialogOptions>;
export function connectCallback(): void;
export function get(path: string, params: FormData | any): Promise<any>;
export function initialize(options: SCOptions): void;
export function isConnected(): boolean;

export function oEmbed(url: string, options?: {
element?: string,
scope?: string,
url?: string
}): Promise<DialogOptions>;

export function post(path: string, params: FormData | any): Promise<any>;
export function put(path: string, params: FormData | any): Promise<any>;

// delete is a keyword in typescript leading to this trick
function _delete(path: string, params: FormData | any): Promise<any>;
export { _delete as delete };

export function resolve(url: string): Promise<any>;
export function stream(trackPath: string, secretToken: string): Promise<SCPlayer>;
export function upload(options: {
title: string,
asset_data?: string,
file?: string,
progress?: (...args: any[]) => any,
}): Promise<any>;
}

116 changes: 116 additions & 0 deletions types/soundcloud/soundcloud-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import SC from "soundcloud";

/**
* SC.Recorder
*/

const rc: SC.Recorder = new SC.Recorder({
source: 'yayayaya',
context: new AudioContext(),
});

/**
* API
*/

const scOpt = {
oauth_token: 'yayayaya',
client_id: 'yayayaya',
redirect_uri: 'yayayaya',
baseURL: 'yayayaya',
connectURL: 'yayayaya',
};

SC.initialize(scOpt);

console.log('SC.isConnected() => ', SC.isConnected())

SC.connect({
client_id: 'dudul',
redirect_uri: 'http://google.com',
})
.then(() => {
try {
return SC.get('http://souncloud.com', scOpt);
} catch (error) {
console.error(error);
return Promise.resolve();
}
})
.then(() => {
try {
return SC.post('http://souncloud.com', scOpt);
} catch (error) {
console.error(error);
return Promise.resolve();
}
})
.then(() => {
try {
return SC.put('http://souncloud.com', scOpt);
} catch (error) {
console.error(error);
return Promise.resolve();
}
})
.then(() => {
try {
return SC.delete('http://souncloud.com', scOpt);
} catch (error) {
console.error(error);
return Promise.resolve();
}
})
.then(() => {
try {
return SC.resolve('http://souncloud.com');
} catch (error) {
console.error(error);
return Promise.resolve();
}
}).then(() => {
try {
return SC.oEmbed('http://souncloud.com', {
element: 'element',
scope: 'read',
url: 'http://souncloud.com'
});
} catch (error) {
console.error(error);
return Promise.resolve({});
}
}).then(() => {
try {
return SC.oEmbed('http://souncloud.com', {
element: 'element',
scope: 'read',
url: 'http://souncloud.com'
});
} catch (error) {
console.error(error);
return Promise.resolve({});
}
}).then(() => {
try {
return SC.stream('abcdefg', 'token');
} catch (error) {
console.error(error);
return Promise.resolve({});
}
}).then(() => {
try {
return SC.upload({
title: 'Upload title',
asset_data: 'data',
file: 'SomeFile.mp3',
progress: (p) => console.log('Progress: ' + p),
});
} catch (error) {
console.error(error);
return Promise.resolve();
}
});




23 changes: 23 additions & 0 deletions types/soundcloud/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"soundcloud-tests.ts"
]
}
1 change: 1 addition & 0 deletions types/soundcloud/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }