Skip to content

Commit

Permalink
services
Browse files Browse the repository at this point in the history
  • Loading branch information
Ifechukwudaniel committed Dec 12, 2023
1 parent 9e39c96 commit 5d2e7bc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
32 changes: 20 additions & 12 deletions packages/nextjs/services/http/HttpBase.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { HttpError } from "./HttpError";
import axios, { AxiosRequestConfig } from "axios";
import axios, { AxiosInstance, AxiosRequestConfig } from "axios";
import { merge } from "lodash";
import { Result, err, ok } from "neverthrow";

export class HttpBase {
protected urlBase = "/api";
protected axiosInstance: AxiosInstance;

/**
* Creates a new service instance.
* @param path A base path for all requests this service will make. Defaults to `/api`.
*/
public constructor(path?: string) {
this.urlBase = path ?? "/api";
this.axiosInstance = axios.create();
}

/**
Expand All @@ -24,18 +26,24 @@ export class HttpBase {
};
}

/**
* Set the default headers for all requests this service makes.
* @param headers A config object to merge onto the base config.
* @protected
*/
protected setHeaders(headers: Record<string, string>) {
this.axiosInstance.defaults.headers = merge(this.axiosInstance.defaults.headers, headers);
}

/**
* Make a GET request.
* @param path A path to append to the base url.
* @param configOverrides A config object to merge onto the base config.
* @protected
*/
protected async get<T>(
path = "",
configOverrides: AxiosRequestConfig | undefined = undefined,
): Promise<Result<T, HttpError>> {
async get<T>(path = "", configOverrides: AxiosRequestConfig | undefined = undefined): Promise<Result<T, HttpError>> {
return await this.requestResultWrapper<T>(path, configOverrides, (fullPath, config) => {
return axios.get(fullPath, config);
return this.axiosInstance.get(fullPath, config);
});
}

Expand All @@ -46,13 +54,13 @@ export class HttpBase {
* @param configOverrides A config object to merge onto the base config.
* @protected
*/
protected async post<T>(
async post<T>(
path = "",
data: unknown = undefined,
configOverrides: AxiosRequestConfig | undefined = undefined,
): Promise<Result<T, HttpError>> {
return await this.requestResultWrapper<T>(path, configOverrides, (fullPath, config) => {
return axios.post(fullPath, data, config);
return this.axiosInstance.post(fullPath, data, config);
});
}

Expand All @@ -63,13 +71,13 @@ export class HttpBase {
* @param configOverrides A config object to merge onto the base config.
* @protected
*/
protected async put<T>(
async put<T>(
path = "",
data: unknown = undefined,
configOverrides: AxiosRequestConfig | undefined = undefined,
): Promise<Result<T, HttpError>> {
return await this.requestResultWrapper<T>(path, configOverrides, (fullPath, config) => {
return axios.put(fullPath, data, config);
return this.axiosInstance.put(fullPath, data, config);
});
}

Expand All @@ -86,7 +94,7 @@ export class HttpBase {
configOverrides: AxiosRequestConfig | undefined = undefined,
): Promise<Result<T, HttpError>> {
return await this.requestResultWrapper<T>(path, configOverrides, (fullPath, config) => {
return axios.patch(fullPath, data, config);
return this.axiosInstance.patch(fullPath, data, config);
});
}

Expand All @@ -101,7 +109,7 @@ export class HttpBase {
configOverrides: AxiosRequestConfig | undefined = undefined,
): Promise<Result<T, HttpError>> {
return await this.requestResultWrapper<T>(path, configOverrides, (fullPath, config) => {
return axios.delete(fullPath, config);
return this.axiosInstance.delete(fullPath, config);
});
}

Expand Down
28 changes: 26 additions & 2 deletions packages/nextjs/services/spotify/SpotifyBase.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
import { HttpBase } from "../http";
import { MySession } from "~~/types/session";

class SpotifyBase extends HttpBase {
constructor() {
constructor(bearerToken = "") {
super("https://api.spotify.com/v1");
this.setHeaders({
Authorization: `Bearer ${bearerToken}`,
});
}

setBearerToken(session: MySession | null) {
if (session == null) {
this.setHeaders({
Authorization: "",
});
return;
}
this.setHeaders({
Authorization: `Bearer ${session?.user.accessToken}`,
});
}

public getAllPlaylists(limit = 50, offset = 0) {
return this.get(`/me/playlists?limit=${limit}&offset=${offset}`);
}

public getPlaylist(playlistId: string) {
return this.get(`/playlists/${playlistId}`);
}
}

export default new SpotifyBase();
export default SpotifyBase;
1 change: 1 addition & 0 deletions packages/nextjs/services/spotify/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./SpotifyBase";

0 comments on commit 5d2e7bc

Please sign in to comment.