Skip to content

Commit

Permalink
No more typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
ceckenrode committed Apr 15, 2019
1 parent 7f184a0 commit 0a70bd4
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 1,839 deletions.
1 change: 0 additions & 1 deletion .npmignore
@@ -1,3 +1,2 @@
.DS_Store
node_modules
src
1 change: 0 additions & 1 deletion dist/index.js

This file was deleted.

6 changes: 0 additions & 6 deletions index.js

This file was deleted.

12 changes: 1 addition & 11 deletions package.json
@@ -1,26 +1,16 @@
{
"name": "node-spotify-api",
"version": "1.0.9",
"version": "1.1.0",
"description": "A simple wrapper for the spotify api",
"repository": {
"type": "git",
"url": "https://github.com/ceckenrode/node-spotify-api.git"
},
"main": "index.js",
"scripts": {
"build": "tsc",
"build:prod": "tsc && uglifyjs dist/index.js -o dist/index.js && rm dist/types.js"
},
"author": "Christian Eckenrode <christian@develop.io>",
"license": "ISC",
"dependencies": {
"request": "^2.81.0",
"request-promise": "^4.2.1"
},
"devDependencies": {
"@types/request-promise": "^4.1.36",
"rollup": "^0.47.6",
"typescript": "^2.4.2",
"uglifyjs": "^2.4.11"
}
}
46 changes: 18 additions & 28 deletions src/index.ts → src/index.js
@@ -1,19 +1,9 @@
import * as rp from "request-promise";
import {
credentials,
search,
cb,
token,
header,
searchOpts,
tokenOpts
} from "./types";
const rp = require("request-promise");
const TOKEN_URI = "https://accounts.spotify.com/api/token";
const SEARCH_URI = "https://api.spotify.com/v1/search?type=";

class Spotify {
private token: token;
constructor(private credentials: credentials) {
constructor(credentials) {
if (!credentials || !credentials.id || !credentials.secret) {
throw new Error(
'Could not initialize Spotify client. You must supply an object containing your Spotify client "id" and "secret".'
Expand All @@ -23,9 +13,9 @@ class Spotify {
this.token;
}

public search(search: search, cb?: cb) {
search(search, cb) {
let request;
const opts: searchOpts = {
const opts = {
method: "GET",
uri:
SEARCH_URI +
Expand Down Expand Up @@ -59,21 +49,21 @@ class Spotify {

if (cb) {
request
.then((response: any) => cb(null, response))
.catch((err: Error) => cb(err, null));
.then((response) => cb(null, response))
.catch((err) => cb(err, null));
} else {
return request;
}
}

public request(query: string, cb?: cb) {
request(query, cb) {
if (!query || typeof query !== "string") {
throw new Error(
"You must pass in a Spotify API endpoint to use this method."
);
}
let request;
const opts: searchOpts = { method: "GET", uri: query, json: true };
const opts = { method: "GET", uri: query, json: true };

if (
!this.token ||
Expand All @@ -93,14 +83,14 @@ class Spotify {

if (cb) {
request
.then((response: any) => cb(null, response))
.catch((err: Error) => cb(err, null));
.then((response) => cb(null, response))
.catch((err) => cb(err, null));
} else {
return request;
}
}

private isTokenExpired(): boolean {
isTokenExpired() {
if (this.token) {
if (Date.now() / 1000 >= this.token.expires_at - 300) {
return true;
Expand All @@ -109,15 +99,15 @@ class Spotify {
return false;
}

private setToken(): any {
setToken() {
const opts = {
method: "POST",
uri: TOKEN_URI,
form: { grant_type: "client_credentials" },
headers: this.getCredentialHeader(),
json: true
};
return rp(opts).then((token: token) => {
return rp(opts).then((token) => {
this.token = token;
const currentTime = new Date();
const expireTime = new Date(+currentTime);
Expand All @@ -126,7 +116,7 @@ class Spotify {
});
}

private getTokenHeader(): never | header {
getTokenHeader() {
if (!this.token || !this.token.access_token) {
throw new Error(
"An error has occurred. Make sure you're using a valid client id and secret.'"
Expand All @@ -135,15 +125,15 @@ class Spotify {
return { Authorization: "Bearer " + this.token.access_token };
}

private getCredentialHeader(): header {
getCredentialHeader() {
return {
Authorization:
"Basic " +
new Buffer(
Buffer.from(
this.credentials.id + ":" + this.credentials.secret
).toString("base64")
, "ascii").toString("base64")
};
}
}

export default Spotify;
module.exports = Spotify;
26 changes: 0 additions & 26 deletions src/types.ts

This file was deleted.

51 changes: 0 additions & 51 deletions tsconfig.json

This file was deleted.

0 comments on commit 0a70bd4

Please sign in to comment.