From c7e493ce3d2855e61787d86714625bc7fc51f9bd Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Wed, 26 Apr 2023 21:37:38 -0600 Subject: AƱade vista de producto individual MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/clients/api_client.ts | 19 +++++++++++++++++-- src/clients/loader.ts | 9 --------- src/clients/loaders.ts | 16 ++++++++++++++++ 3 files changed, 33 insertions(+), 11 deletions(-) delete mode 100644 src/clients/loader.ts create mode 100644 src/clients/loaders.ts (limited to 'src/clients') 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 -- cgit v1.2.3