From 55a395facdaf4c80c0ddfa7c9b50409f514c436f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Okan=20I=C5=9F=C4=B1ldar?= Date: Tue, 12 Dec 2023 00:04:51 +0300 Subject: [PATCH 1/3] feat: implement get user shows service --- app/(auth)/auth/sign-in/route.ts | 3 ++- lib/services/spotify/get-user-shows.ts | 36 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 lib/services/spotify/get-user-shows.ts diff --git a/app/(auth)/auth/sign-in/route.ts b/app/(auth)/auth/sign-in/route.ts index 5b1b834..3db379d 100644 --- a/app/(auth)/auth/sign-in/route.ts +++ b/app/(auth)/auth/sign-in/route.ts @@ -8,7 +8,8 @@ export async function POST(request: NextRequest) { const supabase = createSupabaseServerClient(cookies()); const redirectURL = new URL('/auth/callback', request.url); - const scopes = 'user-read-email user-read-playback-position'; + const scopes = + 'user-read-email user-read-playback-position user-library-read'; const result = await supabase.auth.signInWithOAuth({ options: { redirectTo: redirectURL.toString(), diff --git a/lib/services/spotify/get-user-shows.ts b/lib/services/spotify/get-user-shows.ts new file mode 100644 index 0000000..ffe2a67 --- /dev/null +++ b/lib/services/spotify/get-user-shows.ts @@ -0,0 +1,36 @@ +type Show = { + description: string; + id: string; + images: string; + language: string; + name: string; + publisher: string; + total_episodes: number; +}; + +type Items = { + added_at: string; + show: Show; +}; + +type SpotifyItems = { + items: Items; + limit: number; + offset: number; +}; + +export const getUserShows = async (spotifyToken: string): Promise => { + try { + const response = await fetch('https://api.spotify.com/v1/me/shows', { + headers: { + Authorization: `Bearer ${spotifyToken}`, + }, + method: 'GET', + }); + + const { items } = (await response.json()) as SpotifyItems; + return items; + } catch (e) { + throw Error('Could not get shows from spotify'); + } +}; From 469869b57c7cbec0dfbcbfd132c7d71e74c9b7f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Okan=20I=C5=9F=C4=B1ldar?= Date: Tue, 12 Dec 2023 11:09:40 +0300 Subject: [PATCH 2/3] chore: change types of search user shows function return --- lib/services/spotify/get-user-shows.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/services/spotify/get-user-shows.ts b/lib/services/spotify/get-user-shows.ts index ffe2a67..452db85 100644 --- a/lib/services/spotify/get-user-shows.ts +++ b/lib/services/spotify/get-user-shows.ts @@ -14,12 +14,12 @@ type Items = { }; type SpotifyItems = { - items: Items; + items: Items[]; limit: number; offset: number; }; -export const getUserShows = async (spotifyToken: string): Promise => { +export const getUserShows = async (spotifyToken: string): Promise => { try { const response = await fetch('https://api.spotify.com/v1/me/shows', { headers: { From 083ce81f05cbb0368af1973218ba26db18bd68f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Okan=20I=C5=9F=C4=B1ldar?= Date: Tue, 12 Dec 2023 15:05:47 +0300 Subject: [PATCH 3/3] chore: add ofetch package, create spotify fetch client --- lib/services/spotify/client.ts | 8 ++++++++ lib/services/spotify/get-user-shows.ts | 20 ++++++++------------ package.json | 1 + pnpm-lock.yaml | 20 +++++++++++++++++++- 4 files changed, 36 insertions(+), 13 deletions(-) create mode 100644 lib/services/spotify/client.ts diff --git a/lib/services/spotify/client.ts b/lib/services/spotify/client.ts new file mode 100644 index 0000000..fae2e14 --- /dev/null +++ b/lib/services/spotify/client.ts @@ -0,0 +1,8 @@ +import { ofetch } from 'ofetch'; + +const SPOTIFY_BASE_URL = 'https://api.spotify.com/v1'; + +export const spotifyFetchClient = ofetch.create({ + baseURL: SPOTIFY_BASE_URL, + params: { limit: 50, offset: 0 }, +}); diff --git a/lib/services/spotify/get-user-shows.ts b/lib/services/spotify/get-user-shows.ts index 452db85..86e49fb 100644 --- a/lib/services/spotify/get-user-shows.ts +++ b/lib/services/spotify/get-user-shows.ts @@ -1,3 +1,5 @@ +import { spotifyFetchClient } from './client'; + type Show = { description: string; id: string; @@ -20,17 +22,11 @@ type SpotifyItems = { }; export const getUserShows = async (spotifyToken: string): Promise => { - try { - const response = await fetch('https://api.spotify.com/v1/me/shows', { - headers: { - Authorization: `Bearer ${spotifyToken}`, - }, - method: 'GET', - }); + const response = await spotifyFetchClient('/me/shows', { + headers: { + Authorization: `Bearer ${spotifyToken}`, + }, + }); - const { items } = (await response.json()) as SpotifyItems; - return items; - } catch (e) { - throw Error('Could not get shows from spotify'); - } + return response.items; }; diff --git a/package.json b/package.json index 1f9e6ce..49829b2 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "@supabase/supabase-js": "^2.39.0", "next": "14.0.4", "next-themes": "1.0.0-beta.0", + "ofetch": "^1.3.3", "react": "^18", "react-dom": "^18", "zod": "^3.22.4" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index bfd0439..2913a9a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -23,6 +23,9 @@ dependencies: next-themes: specifier: 1.0.0-beta.0 version: 1.0.0-beta.0(next@14.0.4)(react-dom@18.2.0)(react@18.2.0) + ofetch: + specifier: ^1.3.3 + version: 1.3.3 react: specifier: ^18 version: 18.2.0 @@ -6869,6 +6872,10 @@ packages: minimalistic-assert: 1.0.1 dev: true + /destr@2.0.2: + resolution: {integrity: sha512-65AlobnZMiCET00KaFFjUefxDX0khFA/E4myqZ7a6Sq1yZtR8+FVIvilVX66vF2uobSumxooYZChiRPCKNqhmg==} + dev: false + /destroy@1.2.0: resolution: {integrity: sha512-2sJGJTaXIIaR1w4iJSNoN0hnMY7Gpc/n8D4qSCJw8QqFWXf7cuAgnEHxBpweaVcPevC2l3KpjYCx3NypQQgaJg==} engines: {node: '>= 0.8', npm: 1.2.8000 || >= 1.4.16} @@ -9952,7 +9959,6 @@ packages: /node-fetch-native@1.4.1: resolution: {integrity: sha512-NsXBU0UgBxo2rQLOeWNZqS3fvflWePMECr8CoSWoSTqCqGbVVsvl9vZu1HfQicYN0g5piV9Gh8RTEvo/uP752w==} - dev: true /node-fetch@2.7.0: resolution: {integrity: sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==} @@ -10144,6 +10150,14 @@ packages: resolution: {integrity: sha512-eJJDYkhJFFbBBAxeh8xW+weHlkI28n2ZdQV/J/DNfWfSKlGEf2xcfAbZTv3riEXHAhL9SVOTs2pRmXiSTf78xg==} dev: true + /ofetch@1.3.3: + resolution: {integrity: sha512-s1ZCMmQWXy4b5K/TW9i/DtiN8Ku+xCiHcjQ6/J/nDdssirrQNOoB165Zu8EqLMA2lln1JUth9a0aW9Ap2ctrUg==} + dependencies: + destr: 2.0.2 + node-fetch-native: 1.4.1 + ufo: 1.3.2 + dev: false + /on-finished@2.4.1: resolution: {integrity: sha512-oVlzkg3ENAhCk2zdv7IJwd/QUD4z2RxRwpkcGY8psCVcCYZNq4wYnVWALHM+brtuJjePWiYF/ClmuDr8Ch5+kg==} engines: {node: '>= 0.8'} @@ -12332,6 +12346,10 @@ packages: hasBin: true dev: true + /ufo@1.3.2: + resolution: {integrity: sha512-o+ORpgGwaYQXgqGDwd+hkS4PuZ3QnmqMMxRuajK/a38L6fTpcE5GPIfrf+L/KemFzfUpeUQc1rRS1iDBozvnFA==} + dev: false + /uglify-js@3.17.4: resolution: {integrity: sha512-T9q82TJI9e/C1TAxYvfb16xO120tMVFZrGA3f9/P4424DNu6ypK103y0GPFVa17yotwSyZW5iYXgjYHkGrJW/g==} engines: {node: '>=0.8.0'}