soundcloud-wrapper 0.9.1 → 0.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +118 -0
- package/index.d.ts +81 -0
- package/package.json +5 -4
package/README.md
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
# Soundcloud Wrapper ☁️
|
2
|
+
|
3
|
+
Soundcloud Wrapper is a fully open source, lightweight, strongly-typed Node.js wrapper for the Soundcloud Wrapper. It simplifies interaction with SoundCloud's services by providing an intuitive interface for authentication, track management, playlist operations, and user interactions. Built with TypeScript, it offers full type safety and seamless integration for Node.js applications.
|
4
|
+
|
5
|
+
# Docs
|
6
|
+
|
7
|
+
🔗 https://soundcloud-wrapper-docs.vercel.app/docs
|
8
|
+
|
9
|
+
# Usage
|
10
|
+
|
11
|
+
Please visit the docs for full guide on usage. The code samples below are to be seen as guidelines rather than straight copy and paste working solutions. Amend apporopriately to fit your tech stack/use case.
|
12
|
+
|
13
|
+
## Get And Store Token
|
14
|
+
|
15
|
+
```javascript
|
16
|
+
import { Request, Response } from "express"
|
17
|
+
import Token from "../models/token"
|
18
|
+
import axios from "axios"
|
19
|
+
import qs from "qs"
|
20
|
+
|
21
|
+
export const getAccessToken = async (req: Request, res: Response) => {
|
22
|
+
// code in ?code= query from your redirectUri/frontend
|
23
|
+
const codeFromFrontend = req.query.code
|
24
|
+
// handle this however you need to, but ensure the logged in users userId is passed so it can be linked to the token later
|
25
|
+
const userId = req.userId
|
26
|
+
|
27
|
+
// build query to be passed to request
|
28
|
+
let data = qs.stringify({
|
29
|
+
grant_type: "authorization_code",
|
30
|
+
client_id: process.env.CLIENT_ID,
|
31
|
+
client_secret: process.env.CLIENT_SECRET,
|
32
|
+
redirect_uri: process.env.REDIRECT_URI,
|
33
|
+
code_verifier: process.env.PKCE_CODE_VERIFIER,
|
34
|
+
code: codeFromFrontend,
|
35
|
+
})
|
36
|
+
|
37
|
+
// define config for request
|
38
|
+
let config = {
|
39
|
+
method: "POST",
|
40
|
+
maxBodyLength: Infinity,
|
41
|
+
url: "https://secure.soundcloud.com/oauth/token",
|
42
|
+
headers: {
|
43
|
+
accept: "application/json; charset=utf-8",
|
44
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
45
|
+
},
|
46
|
+
// pass query built above
|
47
|
+
data: data,
|
48
|
+
}
|
49
|
+
|
50
|
+
// send request to get token
|
51
|
+
const tokenRequest = await axios
|
52
|
+
.request(config)
|
53
|
+
.then((response) => {
|
54
|
+
// upon success return token
|
55
|
+
return response.data
|
56
|
+
})
|
57
|
+
.catch((error) => {
|
58
|
+
// if request fails log error
|
59
|
+
console.log(error)
|
60
|
+
throw new Error("Failed to get access token")
|
61
|
+
})
|
62
|
+
|
63
|
+
// link logged in userId to token
|
64
|
+
const tokenWithUserId = { ...tokenRequest, id: userId }
|
65
|
+
|
66
|
+
// save token with userId to DB
|
67
|
+
const token = new Token(tokenRequest)
|
68
|
+
await token.save()
|
69
|
+
|
70
|
+
return res.status(201).json({ message: "Successfully created token" })
|
71
|
+
}
|
72
|
+
```
|
73
|
+
|
74
|
+
## Use Token
|
75
|
+
|
76
|
+
```javascript
|
77
|
+
import { Request, Response } from "express"
|
78
|
+
import Token from "../models/token"
|
79
|
+
import axios from "axios"
|
80
|
+
|
81
|
+
export const getMe = async (req: Request, res: Response) => {
|
82
|
+
try {
|
83
|
+
// get token from DB
|
84
|
+
const token = await Token.findById(req.userId)
|
85
|
+
|
86
|
+
// define config for request
|
87
|
+
let config = {
|
88
|
+
method: "get",
|
89
|
+
maxBodyLength: Infinity,
|
90
|
+
url: `https://api.soundcloud.com/me`,
|
91
|
+
headers: {
|
92
|
+
accept: "application/json; charset=utf-8",
|
93
|
+
"Content-Type": "application/json; charset=utf-8",
|
94
|
+
// pass token to request
|
95
|
+
Authorization: `Bearer ${token}`,
|
96
|
+
},
|
97
|
+
}
|
98
|
+
|
99
|
+
// send request to get details of authenticated user
|
100
|
+
const me = await axios
|
101
|
+
.request(config)
|
102
|
+
.then((response: any) => {
|
103
|
+
// upon success return user data
|
104
|
+
return response.data
|
105
|
+
})
|
106
|
+
.catch((error: any) => {
|
107
|
+
// if request fails log error
|
108
|
+
console.log(error)
|
109
|
+
throw new Error("Failed to get me")
|
110
|
+
})
|
111
|
+
// return data of authenticated user
|
112
|
+
return res.status(200).json(me)
|
113
|
+
} catch (e) {
|
114
|
+
console.error(e)
|
115
|
+
throw new Error("Failed to get me")
|
116
|
+
}
|
117
|
+
}
|
118
|
+
```
|
package/index.d.ts
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
declare module "soundcloud-wrapper" {
|
2
|
+
export default class SoundCloudClient {
|
3
|
+
constructor(clientId: string, clientSecret: string, redirectUri: string, PKCECodeVerifier: string)
|
4
|
+
|
5
|
+
tracks: {
|
6
|
+
getTrack(authToken: string, trackId: number): Promise<any>
|
7
|
+
getTrackStreams(authToken: string, trackId: number): Promise<any>
|
8
|
+
getTrackComments(authToken: string, trackId: number): Promise<any>
|
9
|
+
getTrackLikers(authToken: string, trackId: number): Promise<any>
|
10
|
+
getTrackReposters(authToken: string, trackId: number): Promise<any>
|
11
|
+
getRelatedTracks(authToken: string, trackId: number): Promise<any>
|
12
|
+
addComment(authToken: string, trackId: number, comment: { comment: { body: string; timestamp: string } }): Promise<any>
|
13
|
+
updateTrack(authToken: string, trackId: number, data: any): Promise<any>
|
14
|
+
deleteTrack(authToken: string, trackId: number): Promise<any>
|
15
|
+
}
|
16
|
+
|
17
|
+
playlists: {
|
18
|
+
getPlaylist(authToken: string, playlistId: number): Promise<any>
|
19
|
+
getPlaylistTracks(authToken: string, playlistId: number): Promise<any>
|
20
|
+
getPlaylistReposters(authToken: string, playlistId: number): Promise<any>
|
21
|
+
createPlaylist(authToken: string, playlistData: PlaylistData): Promise<any>
|
22
|
+
updatePlaylist(authToken: string, playlistId: number, playlistData: PlaylistData): Promise<any>
|
23
|
+
deletePlaylist(authToken: string, playlistId: number): Promise<any>
|
24
|
+
}
|
25
|
+
|
26
|
+
likes: {
|
27
|
+
likeTrack(authToken: string, trackId: number): Promise<any>
|
28
|
+
likePlaylist(authToken: string, playlistId: number): Promise<any>
|
29
|
+
unlikeTrack(authToken: string, trackId: number): Promise<any>
|
30
|
+
unlikePlaylist(authToken: string, playlistId: number): Promise<any>
|
31
|
+
}
|
32
|
+
|
33
|
+
me: {
|
34
|
+
me(authToken: string): Promise<any>
|
35
|
+
getActivity(authToken: string): Promise<any>
|
36
|
+
getTrackActivity(authToken: string): Promise<any>
|
37
|
+
getTrackLikes(authToken: string): Promise<any>
|
38
|
+
getPlaylistLikes(authToken: string): Promise<any>
|
39
|
+
getFollowings(authToken: string): Promise<any>
|
40
|
+
getFollowingsTracks(authToken: string): Promise<any>
|
41
|
+
followUser(authToken: string, userId: number): Promise<any>
|
42
|
+
unfollowUser(authToken: string, userId: number): Promise<any>
|
43
|
+
}
|
44
|
+
|
45
|
+
users: {
|
46
|
+
getUser(authToken: string, userId: number): Promise<any>
|
47
|
+
getUserFollowers(authToken: string, userId: number): Promise<any>
|
48
|
+
getUserFollowings(authToken: string, userId: number): Promise<any>
|
49
|
+
getUserPlaylists(authToken: string, userId: number): Promise<any>
|
50
|
+
getUserTracks(authToken: string, userId: number): Promise<any>
|
51
|
+
getUserWebProfiles(authToken: string, userId: number): Promise<any>
|
52
|
+
getUserLikedTracks(authToken: string, userId: number): Promise<any>
|
53
|
+
getUserLikedPlaylists(authToken: string, userId: number): Promise<any>
|
54
|
+
}
|
55
|
+
|
56
|
+
search: {
|
57
|
+
tracks(authToken: string, query: string): Promise<any>
|
58
|
+
playlists(authToken: string, query: string): Promise<any>
|
59
|
+
users(authToken: string, query: string): Promise<any>
|
60
|
+
}
|
61
|
+
|
62
|
+
token: {
|
63
|
+
getToken(PKCECodeChallenge: string): Promise<any>
|
64
|
+
}
|
65
|
+
|
66
|
+
misc: {
|
67
|
+
resolveUrl(authToken: string, url: string): Promise<any>
|
68
|
+
}
|
69
|
+
}
|
70
|
+
|
71
|
+
interface PlaylistData {
|
72
|
+
playlist: {
|
73
|
+
title: string
|
74
|
+
description: string
|
75
|
+
sharing: string
|
76
|
+
tracks: {
|
77
|
+
id: number
|
78
|
+
}[]
|
79
|
+
}
|
80
|
+
}
|
81
|
+
}
|
package/package.json
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
{
|
2
2
|
"name": "soundcloud-wrapper",
|
3
|
-
"version": "0.9.
|
3
|
+
"version": "0.9.3",
|
4
4
|
"description": "Node wrapper for the SoundCloud API",
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"files": [
|
7
|
-
"dist"
|
7
|
+
"dist",
|
8
|
+
"index.d.ts"
|
8
9
|
],
|
9
|
-
"repository": "https://github.com/18-28/soundcloud-
|
10
|
-
"homepage": "https://
|
10
|
+
"repository": "https://github.com/18-28/soundcloud-wrapper",
|
11
|
+
"homepage": "https://soundcloud-wrapper-docs.vercel.app/docs",
|
11
12
|
"scripts": {
|
12
13
|
"build": "tsc",
|
13
14
|
"prepare": "npm run build"
|