summaryrefslogtreecommitdiff
path: root/src/clients/loaders.ts
blob: 2af77707d3450d7e9d3fc2340080b715802c11c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { redirect } from "react-router-dom";
import Token from "../lib/token";
import { ApiClient } from "./api_client";

export async function loader({ request }) {
  const client = new ApiClient();
  const url = new URL(request.url)
  const response = await client.get(`${url.pathname}${url.search}`);
  
  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);
  
  return [response[0], response[1]];
}