-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
328 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 0 additions & 5 deletions
5
src/features/commands/internal/Spotify/internal/funcs/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1 @@ | ||
export { getAuthUrl } from './internal/getAuthUrl/index.js'; | ||
export { | ||
fetchSpotifyToken, | ||
type SpotifyTokenResponse, | ||
GRANT_TYPE, | ||
} from './internal/fetchSpotifyToken/index.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { singleton } from '@/features/others/singleton/index.js'; | ||
|
||
import { Store } from './internal/Store.class.js'; | ||
export type { Store } from './internal/Store.class.js'; | ||
export type { SpotifyToken } from './internal/Store.types.js'; | ||
|
||
const createStoreInstance = () => new Store(); | ||
|
||
export const getStoreInstance = singleton(createStoreInstance); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { Guild } from '@/features/library/index.js'; | ||
import type { SpotifyTokenResponse } from '@/features/others/spotify/index.js'; | ||
|
||
import type { SpotifyToken } from './Store.types.js'; | ||
|
||
export class Store { | ||
#spotifyTokens: { | ||
[key: Guild['id']]: SpotifyToken; | ||
} = {}; | ||
|
||
public putSpotifyTokenByGuildId({ | ||
guildId, | ||
token, | ||
}: { | ||
guildId: Guild['id']; | ||
token: SpotifyTokenResponse; | ||
}) { | ||
this.#spotifyTokens[guildId] = { | ||
...token, | ||
updatedAt: new Date(), | ||
}; | ||
} | ||
|
||
public getSpotifyTokenByGuildId({ | ||
guildId, | ||
}: { | ||
guildId: Guild['id']; | ||
}): SpotifyToken | undefined { | ||
return this.#spotifyTokens[guildId]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type { SpotifyTokenResponse } from '@/features/others/spotify/index.js'; | ||
|
||
export type SpotifyToken = SpotifyTokenResponse & { | ||
updatedAt: Date; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export type { SpotifyTokenResponse } from './internal/spotify.types.js'; | ||
export { | ||
fetchSpotifyToken, | ||
GRANT_TYPE, | ||
} from './internal/fetchSpotifyToken/index.js'; | ||
export { spotifyFetcher } from './internal/spotifyFetcher/index.js'; |
1 change: 0 additions & 1 deletion
1
...funcs/internal/fetchSpotifyToken/index.ts → ...otify/internal/fetchSpotifyToken/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
export { fetchSpotifyToken } from './internal/fetchSpotifyToken.func.js'; | ||
export type { SpotifyTokenResponse } from './internal/fetchSpotifyToken.types.js'; | ||
export { GRANT_TYPE } from './internal/fetchSpotifyToken.constants.js'; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { spotifyFetcher } from './internal/spotifyFetcher.func.js'; |
112 changes: 112 additions & 0 deletions
112
src/features/others/spotify/internal/spotifyFetcher/internal/spotifyFetcher.func.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { getConfig } from '@/features/config/index.js'; | ||
import { | ||
getStoreInstance, | ||
type SpotifyToken, | ||
type Store, | ||
} from '@/features/core/index.js'; | ||
import type { Guild } from '@/features/library/index.js'; | ||
import { LogicException } from '@/features/others/Error/LogicException.js'; | ||
|
||
import { | ||
fetchSpotifyToken, | ||
GRANT_TYPE, | ||
} from '../../fetchSpotifyToken/index.js'; | ||
|
||
type RequestMethod = { | ||
Delete: 'DELETE'; | ||
Get: 'GET'; | ||
Patch: 'PATCH'; | ||
Post: 'POST'; | ||
Put: 'PUT'; | ||
}; | ||
|
||
export type SpotifyFetcher = ( | ||
endpoint: string, | ||
options: { | ||
store?: Store; | ||
method: RequestMethod[keyof RequestMethod]; | ||
guildId: Guild['id']; | ||
body?: URLSearchParams; | ||
apiBaseUrl?: string; | ||
fetchFn?: typeof fetch; | ||
refreshFn?: typeof refresh; | ||
} | ||
) => Promise<Response>; | ||
|
||
/** | ||
* Spotify専用のfetcher | ||
* Tokenがexpiredしている場合は自動でrefreshする | ||
*/ | ||
export const spotifyFetcher: SpotifyFetcher = async ( | ||
endpoint, | ||
{ | ||
store = getStoreInstance(), | ||
apiBaseUrl = 'https://api.spotify.com/v1', | ||
guildId, | ||
method, | ||
body, | ||
fetchFn = fetch, | ||
refreshFn = refresh, | ||
} | ||
) => { | ||
// NOTE: リフレッシュしたら書き換えが走るためlet | ||
let tokens = store.getSpotifyTokenByGuildId({ guildId }); | ||
|
||
if (tokens === undefined) { | ||
throw new Error('Authentication is required.'); | ||
} | ||
|
||
// expires_inは発行からの経過秒数で表現される | ||
// 現在時刻とupdatedAtの差分がexpires_inより大きい場合はrefreshが必要 | ||
const shouldRefresh = | ||
tokens.expires_in * 1000 + tokens.updatedAt.getTime() < Date.now(); | ||
|
||
if (shouldRefresh) { | ||
await refreshFn({ tokens, store, guildId }); | ||
} | ||
|
||
// NOTE: 以降はスコープ内で書き換えることはないため | ||
tokens = Object.freeze(store.getSpotifyTokenByGuildId({ guildId })); | ||
if (tokens === undefined) { | ||
throw new LogicException('tokens is undefined.'); | ||
} | ||
|
||
const requestHeader = { | ||
Authorization: `Bearer ${tokens.access_token}`, | ||
'Content-Type': 'application/json', | ||
} as const; | ||
|
||
const response = await fetchFn(`${apiBaseUrl}/${endpoint}`, { | ||
method, | ||
headers: requestHeader, | ||
body: body ?? null, | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new LogicException( | ||
`Failed to fetch Spotify API: ${response.statusText}` | ||
); | ||
} | ||
|
||
return response; | ||
}; | ||
|
||
const refresh = async ({ | ||
tokens, | ||
store, | ||
guildId, | ||
config = getConfig(), | ||
}: { | ||
tokens: SpotifyToken; | ||
store: Store; | ||
guildId: Guild['id']; | ||
config?: ReturnType<typeof getConfig>; | ||
}): Promise<void> => { | ||
const token = await fetchSpotifyToken({ | ||
type: GRANT_TYPE.RefreshToken, | ||
code: tokens.refresh_token, | ||
spotifyClientId: config.SPOTIFY_CLIENT_ID, | ||
spotifyClientSecret: config.SPOTIFY_CLIENT_SECRET, | ||
}); | ||
store.putSpotifyTokenByGuildId({ guildId, token }); | ||
}; |
Oops, something went wrong.