Skip to content

Commit

Permalink
feat: query options on base request
Browse files Browse the repository at this point in the history
  • Loading branch information
N3aar committed Jun 23, 2024
1 parent 8173bd1 commit f9a6c05
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
11 changes: 9 additions & 2 deletions src/shared/base/BaseRequest.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
export default class BaseRequest {
private url: string;
private options: RequestInit;
private queryOptions: URLSearchParams;

constructor(url: string, options: RequestInit) {
constructor(url: string, options: RequestInit, queryOptions = {}) {
this.url = url;
this.options = options;
this.queryOptions = new URLSearchParams(queryOptions);
}

private async request<T>(path: string, options: RequestInit): Promise<T> {
const endpoint = `${this.url}${path.startsWith("/") ? "" : "/"}${path}`;
const dash = path.startsWith("/") ? "" : "/";
const prefix = path.includes("?") ? "&" : "?";

const query = `${prefix}${this.queryOptions.toString()}`;
const endpoint = `${this.url}${dash}${path}${query}`;

const response: Response = await fetch(endpoint, {
...this.options,
...options,
Expand Down
32 changes: 12 additions & 20 deletions src/shared/integrations/LastFmAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ import BaseRequest from "@/shared/base/BaseRequest.js";
import type { TopArtistsData, UserArtistData } from "../types/lastFmTypes.js";

export default class LastFmAPI extends BaseRequest {
apiKey: string;

constructor() {
super("http://ws.audioscrobbler.com/2.0/", {});

this.apiKey = process.env.LASTFM_APIKEY ?? "";
}

private applyApiKey(method: string) {
return `${method}&api_key=${this.apiKey}`;
super(
"http://ws.audioscrobbler.com/2.0",
{},
{
api_key: process.env.LASTFM_APIKEY ?? "",
},
);
}

public async getTopArtists(
Expand All @@ -20,9 +18,7 @@ export default class LastFmAPI extends BaseRequest {
page: number,
): Promise<UserArtistData[] | null> {
const data: TopArtistsData = await this.get(
this.applyApiKey(
`?method=user.getTopArtists&user=${username}&format=json&limit=${limit}&page=${page}`,
),
`?method=user.getTopArtists&user=${username}&format=json&limit=${limit}&page=${page}`,
);

if (!data) return null;
Expand All @@ -32,9 +28,7 @@ export default class LastFmAPI extends BaseRequest {

public async getTotalArtists(username: string): Promise<number | null> {
const data: TopArtistsData = await this.get(
this.applyApiKey(
`?method=user.getTopArtists&user=${username}&format=json&limit=1`,
),
`?method=user.getTopArtists&user=${username}&format=json&limit=1`,
);

if (!data) return null;
Expand All @@ -44,11 +38,9 @@ export default class LastFmAPI extends BaseRequest {

public async getArtistInfo(artistName: string): Promise<number> {
return await this.get(
this.applyApiKey(
`?method=artist.getInfo&artist=${encodeURIComponent(
artistName,
)}&format=json`,
),
`?method=artist.getInfo&artist=${encodeURIComponent(
artistName,
)}&format=json`,
);
}
}

0 comments on commit f9a6c05

Please sign in to comment.