Skip to content

Commit

Permalink
try using cloudflare kv
Browse files Browse the repository at this point in the history
  • Loading branch information
cubedhuang committed Sep 11, 2023
1 parent 1cd9346 commit d78af37
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 15 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
"spotify-web-api-node": "^5.0.2"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20230904.0",
"@sveltejs/adapter-auto": "^2.1.0",
"@sveltejs/adapter-cloudflare": "^2.3.3",
"@sveltejs/kit": "^1.24.1",
"@types/spotify-web-api-node": "^5.0.7",
"@typescript-eslint/eslint-plugin": "^6.6.0",
Expand Down
34 changes: 34 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion src/app.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import type { KVNamespace } from '@cloudflare/workers-types';

// See https://kit.svelte.dev/docs/types#app
// for information about these interfaces
declare global {
namespace App {
// interface Error {}
// interface Locals {}
// interface PageData {}
// interface Platform {}

interface Platform {
env?: {
SPOTIFY_KV: KVNamespace;
};
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/app.postcss
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@
}

nav {
animation-delay: 1500ms;
animation-delay: 800ms;
}

@each $i in 1, 2, 3, 4, 5, 6, 7, 8, 9 {
main > *:nth-child($(i)) {
animation-delay: calc($(i) * 100ms);
animation-delay: calc($(i) * 50ms);
}
}

Expand Down
28 changes: 18 additions & 10 deletions src/routes/api/now-playing/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,28 @@ import { env } from '$env/dynamic/private';
import type { NowPlayingResponse } from '$lib/types';
import Spotify from 'spotify-web-api-node';

const api = new Spotify({
clientId: env.SPOTIFY_CLIENT_ID,
clientSecret: env.SPOTIFY_CLIENT_SECRET,
refreshToken: env.SPOTIFY_REFRESH_TOKEN
});
let expirationTime = 0;

export async function GET() {
export async function GET({ platform }) {
const api = new Spotify({
clientId: env.SPOTIFY_CLIENT_ID,
clientSecret: env.SPOTIFY_CLIENT_SECRET,
refreshToken: env.SPOTIFY_REFRESH_TOKEN
});

try {
if (Date.now() > expirationTime) {
const accessToken = await platform?.env?.SPOTIFY_KV.get('accessToken');
const expirationTime =
Number(await platform?.env?.SPOTIFY_KV.get('expirationTime')) || 0;

if (!accessToken || Date.now() > expirationTime) {
const response = await api.refreshAccessToken();
api.setAccessToken(response.body.access_token);

expirationTime = Date.now() + response.body.expires_in * 1000;
await platform?.env?.SPOTIFY_KV.put(
'expirationTime',
`${Date.now() + response.body.expires_in * 1000}`
);
} else {
api.setAccessToken(accessToken);
}

const response: NowPlayingResponse = {
Expand Down
5 changes: 5 additions & 0 deletions src/routes/projects/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<main class="wrapper">
<h1 class="text-6xl font-black">Projects</h1>

<p class="mt-4 text-gray-400">Help</p>
</main>
4 changes: 2 additions & 2 deletions svelte.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import preprocess from 'svelte-preprocess';
import adapter from '@sveltejs/adapter-auto';
import adapter from '@sveltejs/adapter-cloudflare';
import { vitePreprocess } from '@sveltejs/kit/vite';
import preprocess from 'svelte-preprocess';

/** @type {import('@sveltejs/kit').Config} */
const config = {
Expand Down

0 comments on commit d78af37

Please sign in to comment.