Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ekwoka/spotify-api

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ekwoka/spotify-api - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

dist/auth/index.d.ts

8

dist/core/endpoints/users/getCurrentUser.d.ts

@@ -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 = {

19

dist/core/endpoints/users/getCurrentUser.js

@@ -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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc