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

View file

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