refactor(api): replace deprecated toPromise calls

Details: https://rxjs.dev/deprecations/to-promise
This commit is contained in:
Julian Tölle 2022-06-27 20:07:38 +02:00
parent b8b6592a18
commit 85c31705ef
2 changed files with 26 additions and 22 deletions

View file

@ -1,5 +1,6 @@
import { HttpService } from "@nestjs/axios";
import { Injectable } from "@nestjs/common";
import { firstValueFrom } from "rxjs";
import { SpotifyConnection } from "../spotify-connection.entity";
import { AlbumObject } from "./entities/album-object";
import { ArtistObject } from "./entities/artist-object";
@ -18,12 +19,15 @@ export class SpotifyApiService {
limit: 50,
};
const history = await this.httpService
.get<PagingObject<PlayHistoryObject>>(`v1/me/player/recently-played`, {
headers: { Authorization: `Bearer ${accessToken}` },
params: parameters,
})
.toPromise();
const history = await firstValueFrom(
this.httpService.get<PagingObject<PlayHistoryObject>>(
`v1/me/player/recently-played`,
{
headers: { Authorization: `Bearer ${accessToken}` },
params: parameters,
}
)
);
return history.data.items;
}
@ -32,31 +36,30 @@ export class SpotifyApiService {
accessToken: string,
spotifyID: string
): Promise<ArtistObject> {
const artist = await this.httpService
.get<ArtistObject>(`v1/artists/${spotifyID}`, {
const artist = await firstValueFrom(
this.httpService.get<ArtistObject>(`v1/artists/${spotifyID}`, {
headers: { Authorization: `Bearer ${accessToken}` },
})
.toPromise();
);
return artist.data;
}
async getAlbum(accessToken: string, spotifyID: string): Promise<AlbumObject> {
const album = await this.httpService
.get<AlbumObject>(`v1/albums/${spotifyID}`, {
const album = await firstValueFrom(
this.httpService.get<AlbumObject>(`v1/albums/${spotifyID}`, {
headers: { Authorization: `Bearer ${accessToken}` },
})
.toPromise();
);
return album.data;
}
async getTrack(accessToken: string, spotifyID: string): Promise<TrackObject> {
const track = await this.httpService
.get<TrackObject>(`v1/tracks/${spotifyID}`, {
const track = await firstValueFrom(
this.httpService.get<TrackObject>(`v1/tracks/${spotifyID}`, {
headers: { Authorization: `Bearer ${accessToken}` },
})
.toPromise();
);
return track.data;
}

View file

@ -1,6 +1,7 @@
import { HttpService } from "@nestjs/axios";
import { Injectable } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { firstValueFrom } from "rxjs";
import { SpotifyConnection } from "../spotify-connection.entity";
@Injectable()
@ -17,8 +18,8 @@ export class SpotifyAuthService {
}
async clientCredentialsGrant(): Promise<string> {
const response = await this.httpService
.post<{ access_token: string }>(
const response = await firstValueFrom(
this.httpService.post<{ access_token: string }>(
`api/token`,
"grant_type=client_credentials",
{
@ -28,14 +29,14 @@ export class SpotifyAuthService {
},
}
)
.toPromise();
);
return response.data.access_token;
}
async refreshAccessToken(connection: SpotifyConnection): Promise<string> {
const response = await this.httpService
.post<any>(
const response = await firstValueFrom(
this.httpService.post<any>(
`api/token`,
`grant_type=refresh_token&refresh_token=${connection.refreshToken}`,
{
@ -45,7 +46,7 @@ export class SpotifyAuthService {
},
}
)
.toPromise();
);
return response.data.access_token;
}