summaryrefslogtreecommitdiff
path: root/src/clients/api_client.ts
blob: 9a0d3f5c3ca78cc88c04bbef52b527ca32326e9e (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
import axios from "axios";

export class ApiClient {
  readonly url = "http://localhost:3000/api";

  async get(path: string, params?: URLSearchParams) {
    const request_url = `${ this.url }${ path }`;
    const response = await this.makeRequest(request_url);

    return response;
  }

  async getProduct(id: string) {
    const request_url = `${ this.url }/products/${ id }`;
    const [product_response, product_reviews] = await Promise.all([
      this.makeRequest(request_url),
      this.makeRequest(`${ request_url }/reviews`)
    ]);

    return [product_response, product_reviews];
  }

  private async makeRequest(request_url: string) {
    try {
      const response = await axios.get(request_url);

      return response
    } catch(error) {
      return error;
    }
  }
}