summaryrefslogtreecommitdiff
path: root/src/frontend/pages/api
diff options
context:
space:
mode:
authorSaumit <justsaumit@protonmail.com>2025-09-27 02:14:26 +0530
committerSaumit <justsaumit@protonmail.com>2025-09-27 02:14:26 +0530
commit82e03978b89938219958032efb1448cc76baa181 (patch)
tree626f3e54d52ecd49be0ed3bee30abacc0453d081 /src/frontend/pages/api
Initial snapshot - OpenTelemetry demo 2.1.3 -f
Diffstat (limited to 'src/frontend/pages/api')
-rwxr-xr-xsrc/frontend/pages/api/cart.ts56
-rw-r--r--src/frontend/pages/api/checkout.ts44
-rw-r--r--src/frontend/pages/api/currency.ts25
-rw-r--r--src/frontend/pages/api/data.ts26
-rw-r--r--src/frontend/pages/api/products/[productId]/index.ts26
-rw-r--r--src/frontend/pages/api/products/index.ts26
-rw-r--r--src/frontend/pages/api/recommendations.ts33
-rw-r--r--src/frontend/pages/api/shipping.ts29
8 files changed, 265 insertions, 0 deletions
diff --git a/src/frontend/pages/api/cart.ts b/src/frontend/pages/api/cart.ts
new file mode 100755
index 0000000..3f5b1b7
--- /dev/null
+++ b/src/frontend/pages/api/cart.ts
@@ -0,0 +1,56 @@
+// Copyright The OpenTelemetry Authors
+// SPDX-License-Identifier: Apache-2.0
+
+import type { NextApiHandler } from 'next';
+import CartGateway from '../../gateways/rpc/Cart.gateway';
+import { AddItemRequest, Empty } from '../../protos/demo';
+import ProductCatalogService from '../../services/ProductCatalog.service';
+import { IProductCart, IProductCartItem } from '../../types/Cart';
+import InstrumentationMiddleware from '../../utils/telemetry/InstrumentationMiddleware';
+
+type TResponse = IProductCart | Empty;
+
+const handler: NextApiHandler<TResponse> = async ({ method, body, query }, res) => {
+ switch (method) {
+ case 'GET': {
+ const { sessionId = '', currencyCode = '' } = query;
+ const { userId, items } = await CartGateway.getCart(sessionId as string);
+
+ const productList: IProductCartItem[] = await Promise.all(
+ items.map(async ({ productId, quantity }) => {
+ const product = await ProductCatalogService.getProduct(productId, currencyCode as string);
+
+ return {
+ productId,
+ quantity,
+ product,
+ };
+ })
+ );
+
+ return res.status(200).json({ userId, items: productList });
+ }
+
+ case 'POST': {
+ const { userId, item } = body as AddItemRequest;
+
+ await CartGateway.addItem(userId, item!);
+ const cart = await CartGateway.getCart(userId);
+
+ return res.status(200).json(cart);
+ }
+
+ case 'DELETE': {
+ const { userId } = body as AddItemRequest;
+ await CartGateway.emptyCart(userId);
+
+ return res.status(204).send('');
+ }
+
+ default: {
+ return res.status(405);
+ }
+ }
+};
+
+export default InstrumentationMiddleware(handler);
diff --git a/src/frontend/pages/api/checkout.ts b/src/frontend/pages/api/checkout.ts
new file mode 100644
index 0000000..6007ba2
--- /dev/null
+++ b/src/frontend/pages/api/checkout.ts
@@ -0,0 +1,44 @@
+// Copyright The OpenTelemetry Authors
+// SPDX-License-Identifier: Apache-2.0
+
+import type { NextApiRequest, NextApiResponse } from 'next';
+import InstrumentationMiddleware from '../../utils/telemetry/InstrumentationMiddleware';
+import CheckoutGateway from '../../gateways/rpc/Checkout.gateway';
+import { Empty, PlaceOrderRequest } from '../../protos/demo';
+import { IProductCheckoutItem, IProductCheckout } from '../../types/Cart';
+import ProductCatalogService from '../../services/ProductCatalog.service';
+
+type TResponse = IProductCheckout | Empty;
+
+const handler = async ({ method, body, query }: NextApiRequest, res: NextApiResponse<TResponse>) => {
+ switch (method) {
+ case 'POST': {
+ const { currencyCode = '' } = query;
+ const orderData = body as PlaceOrderRequest;
+ const { order: { items = [], ...order } = {} } = await CheckoutGateway.placeOrder(orderData);
+
+ const productList: IProductCheckoutItem[] = await Promise.all(
+ items.map(async ({ item: { productId = '', quantity = 0 } = {}, cost }) => {
+ const product = await ProductCatalogService.getProduct(productId, currencyCode as string);
+
+ return {
+ cost,
+ item: {
+ productId,
+ quantity,
+ product,
+ },
+ };
+ })
+ );
+
+ return res.status(200).json({ ...order, items: productList });
+ }
+
+ default: {
+ return res.status(405).send('');
+ }
+ }
+};
+
+export default InstrumentationMiddleware(handler);
diff --git a/src/frontend/pages/api/currency.ts b/src/frontend/pages/api/currency.ts
new file mode 100644
index 0000000..fd69909
--- /dev/null
+++ b/src/frontend/pages/api/currency.ts
@@ -0,0 +1,25 @@
+// Copyright The OpenTelemetry Authors
+// SPDX-License-Identifier: Apache-2.0
+
+import type { NextApiRequest, NextApiResponse } from 'next';
+import InstrumentationMiddleware from '../../utils/telemetry/InstrumentationMiddleware';
+import CurrencyGateway from '../../gateways/rpc/Currency.gateway';
+import { Empty } from '../../protos/demo';
+
+type TResponse = string[] | Empty;
+
+const handler = async ({ method }: NextApiRequest, res: NextApiResponse<TResponse>) => {
+ switch (method) {
+ case 'GET': {
+ const { currencyCodes = [] } = await CurrencyGateway.getSupportedCurrencies();
+
+ return res.status(200).json(currencyCodes);
+ }
+
+ default: {
+ return res.status(405);
+ }
+ }
+};
+
+export default InstrumentationMiddleware(handler);
diff --git a/src/frontend/pages/api/data.ts b/src/frontend/pages/api/data.ts
new file mode 100644
index 0000000..7e6ac8f
--- /dev/null
+++ b/src/frontend/pages/api/data.ts
@@ -0,0 +1,26 @@
+// Copyright The OpenTelemetry Authors
+// SPDX-License-Identifier: Apache-2.0
+
+import type { NextApiRequest, NextApiResponse } from 'next';
+import InstrumentationMiddleware from '../../utils/telemetry/InstrumentationMiddleware';
+import AdGateway from '../../gateways/rpc/Ad.gateway';
+import { Ad, Empty } from '../../protos/demo';
+
+type TResponse = Ad[] | Empty;
+
+const handler = async ({ method, query }: NextApiRequest, res: NextApiResponse<TResponse>) => {
+ switch (method) {
+ case 'GET': {
+ const { contextKeys = [] } = query;
+ const { ads: adList } = await AdGateway.listAds(Array.isArray(contextKeys) ? contextKeys : contextKeys.split(','));
+
+ return res.status(200).json(adList);
+ }
+
+ default: {
+ return res.status(405).send('');
+ }
+ }
+};
+
+export default InstrumentationMiddleware(handler);
diff --git a/src/frontend/pages/api/products/[productId]/index.ts b/src/frontend/pages/api/products/[productId]/index.ts
new file mode 100644
index 0000000..eb62465
--- /dev/null
+++ b/src/frontend/pages/api/products/[productId]/index.ts
@@ -0,0 +1,26 @@
+// Copyright The OpenTelemetry Authors
+// SPDX-License-Identifier: Apache-2.0
+
+import type { NextApiRequest, NextApiResponse } from 'next';
+import InstrumentationMiddleware from '../../../../utils/telemetry/InstrumentationMiddleware';
+import { Empty, Product } from '../../../../protos/demo';
+import ProductCatalogService from '../../../../services/ProductCatalog.service';
+
+type TResponse = Product | Empty;
+
+const handler = async ({ method, query }: NextApiRequest, res: NextApiResponse<TResponse>) => {
+ switch (method) {
+ case 'GET': {
+ const { productId = '', currencyCode = '' } = query;
+ const product = await ProductCatalogService.getProduct(productId as string, currencyCode as string);
+
+ return res.status(200).json(product);
+ }
+
+ default: {
+ return res.status(405).send('');
+ }
+ }
+};
+
+export default InstrumentationMiddleware(handler);
diff --git a/src/frontend/pages/api/products/index.ts b/src/frontend/pages/api/products/index.ts
new file mode 100644
index 0000000..74b8937
--- /dev/null
+++ b/src/frontend/pages/api/products/index.ts
@@ -0,0 +1,26 @@
+// Copyright The OpenTelemetry Authors
+// SPDX-License-Identifier: Apache-2.0
+
+import type { NextApiRequest, NextApiResponse } from 'next';
+import InstrumentationMiddleware from '../../../utils/telemetry/InstrumentationMiddleware';
+import { Empty, Product } from '../../../protos/demo';
+import ProductCatalogService from '../../../services/ProductCatalog.service';
+
+type TResponse = Product[] | Empty;
+
+const handler = async ({ method, query }: NextApiRequest, res: NextApiResponse<TResponse>) => {
+ switch (method) {
+ case 'GET': {
+ const { currencyCode = '' } = query;
+ const productList = await ProductCatalogService.listProducts(currencyCode as string);
+
+ return res.status(200).json(productList);
+ }
+
+ default: {
+ return res.status(405).send('');
+ }
+ }
+};
+
+export default InstrumentationMiddleware(handler);
diff --git a/src/frontend/pages/api/recommendations.ts b/src/frontend/pages/api/recommendations.ts
new file mode 100644
index 0000000..dd975a9
--- /dev/null
+++ b/src/frontend/pages/api/recommendations.ts
@@ -0,0 +1,33 @@
+// Copyright The OpenTelemetry Authors
+// SPDX-License-Identifier: Apache-2.0
+
+import type { NextApiRequest, NextApiResponse } from 'next';
+import InstrumentationMiddleware from '../../utils/telemetry/InstrumentationMiddleware';
+import RecommendationsGateway from '../../gateways/rpc/Recommendations.gateway';
+import { Empty, Product } from '../../protos/demo';
+import ProductCatalogService from '../../services/ProductCatalog.service';
+
+type TResponse = Product[] | Empty;
+
+const handler = async ({ method, query }: NextApiRequest, res: NextApiResponse<TResponse>) => {
+ switch (method) {
+ case 'GET': {
+ const { productIds = [], sessionId = '', currencyCode = '' } = query;
+ const { productIds: productList } = await RecommendationsGateway.listRecommendations(
+ sessionId as string,
+ productIds as string[]
+ );
+ const recommendedProductList = await Promise.all(
+ productList.slice(0, 4).map(id => ProductCatalogService.getProduct(id, currencyCode as string))
+ );
+
+ return res.status(200).json(recommendedProductList);
+ }
+
+ default: {
+ return res.status(405).send('');
+ }
+ }
+};
+
+export default InstrumentationMiddleware(handler);
diff --git a/src/frontend/pages/api/shipping.ts b/src/frontend/pages/api/shipping.ts
new file mode 100644
index 0000000..3a29001
--- /dev/null
+++ b/src/frontend/pages/api/shipping.ts
@@ -0,0 +1,29 @@
+// Copyright The OpenTelemetry Authors
+// SPDX-License-Identifier: Apache-2.0
+
+import type { NextApiRequest, NextApiResponse } from 'next';
+import InstrumentationMiddleware from '../../utils/telemetry/InstrumentationMiddleware';
+import ShippingGateway from '../../gateways/http/Shipping.gateway';
+import { Address, CartItem, Empty, Money } from '../../protos/demo';
+import CurrencyGateway from '../../gateways/rpc/Currency.gateway';
+
+type TResponse = Money | Empty;
+
+const handler = async ({ method, query }: NextApiRequest, res: NextApiResponse<TResponse>) => {
+ switch (method) {
+ case 'GET': {
+ const { itemList = '', currencyCode = 'USD', address = '' } = query;
+ const { costUsd } = await ShippingGateway.getShippingCost(JSON.parse(itemList as string) as CartItem[],
+ JSON.parse(address as string) as Address);
+ const cost = await CurrencyGateway.convert(costUsd!, currencyCode as string);
+
+ return res.status(200).json(cost!);
+ }
+
+ default: {
+ return res.status(405);
+ }
+ }
+};
+
+export default InstrumentationMiddleware(handler);