@ekwoka/spotify-api
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -1,2 +0,8 @@ | ||
import { QueryConstructor } from '../../spotifyApi'; | ||
import { QueryConstructor } from '../..'; | ||
/** | ||
* Accesses the Spotify /me endpoint to get information regarding the current | ||
* user. The User data is cached and put in deep freeze to prevent needing | ||
* to refetch the data or having other functions modify the cached data | ||
* @returns User | ||
*/ | ||
export declare const getCurrentUser: QueryConstructor<Promise<User>>; | ||
@@ -3,0 +9,0 @@ declare type User = { |
@@ -1,13 +0,14 @@ | ||
import { SPOTIFY_URL } from '../../../constants'; | ||
import { deepFreeze } from '../../../utils'; | ||
import { deepFreeze, spotifyFetch } from '../../../utils'; | ||
let cachedUser; | ||
export const getCurrentUser = () => async ({ current: token }) => { | ||
/** | ||
* Accesses the Spotify /me endpoint to get information regarding the current | ||
* user. The User data is cached and put in deep freeze to prevent needing | ||
* to refetch the data or having other functions modify the cached data | ||
* @returns User | ||
*/ | ||
export const getCurrentUser = () => async ({ token }) => { | ||
if (cachedUser) | ||
return cachedUser; | ||
const endpoint = `${SPOTIFY_URL}/me`; | ||
const headers = { | ||
Authorization: `Bearer ${token}`, | ||
}; | ||
const response = await fetch(endpoint, { headers }); | ||
const data = await response.json(); | ||
const endpoint = `me`; | ||
const data = await spotifyFetch(endpoint, token); | ||
deepFreeze(data); | ||
@@ -14,0 +15,0 @@ cachedUser = data; |
export { setToken } from './setToken'; | ||
export { spotifyApi } from './spotifyApi'; | ||
export type { Token, QueryFunction, QueryConstructor } from './spotifyApi'; | ||
export { spotifyApiClient } from './spotifyApiClient'; | ||
export type { PersistentApiProperties, SpotifyApiClient, QueryFunction, QueryConstructor, } from './types'; | ||
//# sourceMappingURL=index.d.ts.map |
export { setToken } from './setToken'; | ||
export { spotifyApi } from './spotifyApi'; | ||
export { spotifyApiClient } from './spotifyApiClient'; |
@@ -1,3 +0,3 @@ | ||
import { QueryConstructor } from './spotifyApi'; | ||
export declare const setToken: QueryConstructor<boolean>; | ||
import { QueryConstructor } from './'; | ||
export declare const setToken: QueryConstructor; | ||
//# sourceMappingURL=setToken.d.ts.map |
@@ -1,4 +0,1 @@ | ||
export const setToken = (token) => (tkn) => { | ||
tkn.current = token; | ||
return true; | ||
}; | ||
export const setToken = (newToken) => (Client) => (Client.token = newToken); |
export { SPOTIFY_URL } from './constants'; | ||
export { spotifyApi } from './core/spotifyApi'; | ||
export { getCurrentUser } from './core/endpoints/users/getCurrentUser'; | ||
export { setToken } from './core/setToken'; | ||
export { spotifyApiClient } from './core/spotifyApiClient'; | ||
export { deepFreeze } from './utils/deepFreeze'; | ||
export type { Token, QueryFunction, QueryConstructor } from './core/spotifyApi'; | ||
export { spotifyFetch } from './utils/spotifyFetch'; | ||
export type { PersistentApiProperties, SpotifyApiClient, QueryFunction, QueryConstructor, } from './core/types'; | ||
//# sourceMappingURL=index.d.ts.map |
export { SPOTIFY_URL } from './constants'; | ||
export { spotifyApi } from './core/spotifyApi'; | ||
export { getCurrentUser } from './core/endpoints/users/getCurrentUser'; | ||
export { setToken } from './core/setToken'; | ||
export { spotifyApiClient } from './core/spotifyApiClient'; | ||
export { deepFreeze } from './utils/deepFreeze'; | ||
export { spotifyFetch } from './utils/spotifyFetch'; |
@@ -1,2 +0,2 @@ | ||
export declare const deepFreeze: (obj: any) => any; | ||
export declare const deepFreeze: (obj: unknown) => Readonly<unknown>; | ||
//# sourceMappingURL=deepFreeze.d.ts.map |
@@ -1,2 +0,5 @@ | ||
export { deepFreeze } from "./deepFreeze"; | ||
export { deepFreeze } from './deepFreeze'; | ||
export { isBrowser, isNode } from './isBrowserOrNode'; | ||
export { spotifyFetch } from './spotifyFetch'; | ||
export { toBase64 } from './toBase64'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,1 +0,4 @@ | ||
export { deepFreeze } from "./deepFreeze"; | ||
export { deepFreeze } from './deepFreeze'; | ||
export { isBrowser, isNode } from './isBrowserOrNode'; | ||
export { spotifyFetch } from './spotifyFetch'; | ||
export { toBase64 } from './toBase64'; |
@@ -11,3 +11,3 @@ { | ||
"license": "MIT", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Composable Wrapper for the Spotify Web Api and Spotify Web Playback SDK", | ||
@@ -24,2 +24,5 @@ "keywords": [ | ||
"devDependencies": { | ||
"@types/node": "^18.6.1", | ||
"@typescript-eslint/eslint-plugin": "^5.30.7", | ||
"@typescript-eslint/parser": "^5.30.7", | ||
"c8": "^7.12.0", | ||
@@ -38,6 +41,9 @@ "esbuild": "^0.14.49", | ||
}, | ||
"dependencies": { | ||
"dotenv": "^16.0.1" | ||
}, | ||
"scripts": { | ||
"build": "node scripts/esbuild.js", | ||
"build:tsc": "tsc", | ||
"lint": "eslint --fix .; prettier --write . --loglevel error", | ||
"lint": "eslint --fix ./src; prettier --write ./src --loglevel error", | ||
"patch": "pnpm run test:run && pnpm run build:tsc && pnpm version patch && pnpm publish", | ||
@@ -44,0 +50,0 @@ "minor": "pnpm run test:run && pnpm run build:tsc && pnpm version minor && pnpm publish", |
# ⚡️A tree-shakable, composable, lightweight wrapper for the multiple Spotify APIs🔥 | ||
[<img src="https://badgen.net/npm/v/@ekwoka/spotify-api">](https://www.npmjs.com/package/@ekwoka/spotify-api) | ||
<img src="https://badgen.net/npm/types/@ekwoka/spotify-api?icon=typescript"> | ||
[<img src="https://badgen.net/npm/v/@ekwoka/spotify-api">](https://www.npmjs.com/package/@ekwoka/spotify-api) | ||
<img src="https://badgen.net/npm/types/@ekwoka/spotify-api?icon=typescript"> | ||
<img src="https://badgen.net/npm/dt/@ekwoka/spotify-api" > | ||
@@ -11,3 +11,2 @@ | ||
## Why is this good? | ||
@@ -21,3 +20,2 @@ | ||
## Installation | ||
@@ -39,5 +37,5 @@ | ||
```js | ||
import { SpotifyApi } from '@ekwoka/spotify-api' | ||
import { SpotifyApi } from '@ekwoka/spotify-api'; | ||
export const client = SpotifyApi() // optionally pass in the users access token string | ||
export const client = SpotifyApi('tokenhere'); // client requires an initial token to initialize. Initialize client after recieving token from Spotify. | ||
``` | ||
@@ -47,16 +45,58 @@ | ||
If a token is not set (and is required for a request) the client will throw an error. | ||
To update the token during ongoing usage simply, import and use the `setToken` composable with your client. | ||
To set a token after initialization (or update during ongoing usage) simply import and use the `setToken` composable with your client. | ||
```js | ||
import { SpotifyApi, setToken } from '@ekwoka/spotify-api'; | ||
const client = SpotifyApi('initial_token'); // original token | ||
// after some event, update the token | ||
client(setToken('my_new_token')); // updated token | ||
``` | ||
As you'll notice, this is not a method on the client object like many other libraries. This is a composable function. The goal is for all interactions with the client and APIs to be composable functions. This will enable very aggressive tree-shaking to keep minimal clients from shipping lots of unused code, as well as enable code-splitting for larger applications. This should be reflected in a much more modest bundle size for the majority of use cases. | ||
## Authentication | ||
Includes in this package are some additional helper functions for interacting with Spotify's authentication API. These should only be used on a server, as they require client secrets. | ||
These helpers are: | ||
- `getTokenFromCode`: Accepts a code from the Spotify authentication flow and returns a suite of tokens (access and refresh). | ||
- `refreshToken`: Accepts a refresh token and returns a new access token. | ||
These currently depend on you setting up and exposing certain environment variables for the functions to access: | ||
- `SPOTIFY_CLIENT`: Client id from Spotify Developer Dashboard. | ||
- `SPOTIFY_SECRET`: Client secret. | ||
If these are not defined, the function will throw. | ||
### Examples (Pseudo Express code) | ||
```js | ||
import { SpotifyApi, setToken } from '@ekwoka/spotify-api' | ||
import { getTokenFromCode, refreshToken } from '@ekwoka/spotify-api'; | ||
const client = SpotifyApi() // no token | ||
const codeHandler = async (req, res) => { | ||
try { | ||
const { code } = JSON.parse(req.body); | ||
const { access_token, refresh_token } = await getTokenFromCode(code); | ||
res.cookie('refresh_token', refresh_token); | ||
res.status(200).json({ access_token }); | ||
} catch (err) { | ||
res.status(500).json({ error: err.message }); | ||
} | ||
}; | ||
client(setToken('my_new_token')) // adds new token | ||
const refreshHandler = async (req, res) => { | ||
try { | ||
const { refresh_token } = req.cookies; | ||
const { access_token } = await refreshToken(refresh_token); | ||
res.status(200).json({ access_token }); | ||
} catch (err) { | ||
res.status(500).json({ error: err.message }); | ||
} | ||
}; | ||
``` | ||
As you'll notice, this is not a method on the client object like many other libraries. This is a composable function. The goal is for all interactions with the client and APIs to be composable functions. This will enable very aggressive tree-shaking to keep minimal clients from shipping lots of unused code, as well as enable code-splitting for larger applications. | ||
## Endpoints | ||
@@ -77,5 +117,5 @@ | ||
```js | ||
const user = client(getCurrentUser()) | ||
const user = client(getCurrentUser()); | ||
console.log(user) // should log user | ||
``` | ||
console.log(user); // should log user | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 3 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21638
50
302
117
1
11
6
4
+ Addeddotenv@^16.0.1
+ Addeddotenv@16.4.5(transitive)