summaryrefslogtreecommitdiff
path: root/src/clients
diff options
context:
space:
mode:
Diffstat (limited to 'src/clients')
-rw-r--r--src/clients/api_client.ts12
-rw-r--r--src/clients/loaders.ts17
2 files changed, 23 insertions, 6 deletions
diff --git a/src/clients/api_client.ts b/src/clients/api_client.ts
index c55a365..7d2cf34 100644
--- a/src/clients/api_client.ts
+++ b/src/clients/api_client.ts
@@ -3,16 +3,16 @@ import axios from "axios";
export class ApiClient {
readonly url = "http://localhost:3000/api";
- async get(path: string, params?: URLSearchParams) {
+ async get(path: string, params?: URLSearchParams, headers?: object) {
const request_url = `${ this.url }${ path }`;
- const response = await this.makeGetRequest(request_url);
+ const response = await this.makeGetRequest(request_url, headers);
return response;
}
- async post(path: string, data: FormData) {
+ async post(path: string, data: FormData, headers?: object) {
const request_url = `${ this.url }${ path }`;
- const response = await axios.post(request_url, data, { headers: { "Content-Type": "multipart/form-data"} });
+ const response = await axios.post(request_url, data, headers);
return response;
}
@@ -27,9 +27,9 @@ export class ApiClient {
return [product_response, product_reviews];
}
- private async makeGetRequest(request_url: string) {
+ private async makeGetRequest(request_url: string, headers?: object) {
try {
- const response = await axios.get(request_url);
+ const response = await axios.get(request_url, headers);
return response
} catch(error) {
diff --git a/src/clients/loaders.ts b/src/clients/loaders.ts
index 334f8aa..2af7770 100644
--- a/src/clients/loaders.ts
+++ b/src/clients/loaders.ts
@@ -1,3 +1,5 @@
+import { redirect } from "react-router-dom";
+import Token from "../lib/token";
import { ApiClient } from "./api_client";
export async function loader({ request }) {
@@ -8,6 +10,21 @@ export async function loader({ request }) {
return response;
}
+export async function accountLoader() {
+ const client = new ApiClient();
+ const token = new Token();
+
+ if(!token.present())
+ return redirect("/products");
+
+ const headers = {
+ "Authentication": `Bearer ${ token.get() }`
+ };
+
+ const response = await client.get("/account", undefined, headers);
+ return response;
+}
+
export async function productLoader({ params }) {
const client = new ApiClient();
const response = await client.getProduct(params.productId);