-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_client.ts
59 lines (50 loc) · 1.35 KB
/
test_client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import * as oauth from "oauth4webapi";
import { SPOTIFY_AUTH_URL } from "./auth.ts";
import { z } from "zod";
import { load } from "std/dotenv/mod.ts";
import { SpotifyClient } from "./client.ts";
await load({ export: true });
const env = z
.object({
SPOTIFY_CLIENT_ID: z.string(),
SPOTIFY_CLIENT_SECRET: z.string(),
SPOTIFY_REFRESH_TOKEN: z.string(),
})
.parse(Deno.env.toObject());
const issuer = new URL(SPOTIFY_AUTH_URL);
const authServer = await oauth.processDiscoveryResponse(
issuer,
await oauth.discoveryRequest(issuer),
);
const authClient: oauth.Client = {
client_id: env.SPOTIFY_CLIENT_ID,
client_secret: env.SPOTIFY_CLIENT_SECRET,
token_endpoint_auth_method: "client_secret_basic",
};
const refresher = async () => {
const res = await oauth.refreshTokenGrantRequest(
authServer,
authClient,
env.SPOTIFY_REFRESH_TOKEN,
);
const data = await oauth.processRefreshTokenResponse(
authServer,
authClient,
res,
);
if (oauth.isOAuth2Error(data)) {
throw new Error(data.error + data.error_description);
}
await Deno.writeTextFile("/tmp/soundify_test_cache.txt", data.access_token);
return data.access_token;
};
export const client = new SpotifyClient(null, {
waitForRateLimit: true,
refresher: async () => {
try {
return await Deno.readTextFile("/tmp/soundify_test_cache.txt");
} catch (_) {
return refresher();
}
},
});