diff options
Diffstat (limited to 'src/clients')
-rw-r--r-- | src/clients/api_client.ts | 19 | ||||
-rw-r--r-- | src/clients/loader.ts | 9 | ||||
-rw-r--r-- | src/clients/loaders.ts | 16 |
3 files changed, 33 insertions, 11 deletions
diff --git a/src/clients/api_client.ts b/src/clients/api_client.ts index a7b5e0d..0e96c36 100644 --- a/src/clients/api_client.ts +++ b/src/clients/api_client.ts @@ -5,13 +5,28 @@ export class ApiClient { async get(path: string) { 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; + return response } catch(error) { - return error.response; + return error; } } }
\ No newline at end of file diff --git a/src/clients/loader.ts b/src/clients/loader.ts deleted file mode 100644 index c529806..0000000 --- a/src/clients/loader.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { ApiClient } from "./api_client"; - -export default async function productLoader({ request }) { - const client = new ApiClient(); - const url = new URL(request.url) - const response = await client.get(url.pathname); - - return response.data; -}
\ No newline at end of file diff --git a/src/clients/loaders.ts b/src/clients/loaders.ts new file mode 100644 index 0000000..43c1581 --- /dev/null +++ b/src/clients/loaders.ts @@ -0,0 +1,16 @@ +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); + + return response; +} + +export async function productLoader({ params }) { + const client = new ApiClient(); + const response = await client.getProduct(params.productId); + + return [response[0], response[1]]; +}
\ No newline at end of file |