From 7c7ebcb3f0ac40b3cea85e61adcaffb178e13b4d Mon Sep 17 00:00:00 2001 From: Yash Maheshwari Date: Mon, 9 Jan 2023 11:38:12 +0530 Subject: [PATCH 1/5] Implemented: support to define generic interface for success response(#85zrhhcgr) --- src/modules/product/index.ts | 14 +++++++------- src/types/index.ts | 6 ++++++ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/modules/product/index.ts b/src/modules/product/index.ts index 7d8ff9c..07a1a07 100644 --- a/src/modules/product/index.ts +++ b/src/modules/product/index.ts @@ -1,11 +1,11 @@ import api from "@/api"; -import { OPERATOR, Product, Response } from "@/types"; -import { hasError, isError } from "@/util"; +import { OPERATOR, Product, Response, SuccessResponse } from "@/types"; +import { hasError } from "@/util"; import { transform } from 'node-json-transform' import { productTransformRule } from "@/mappings/product"; -async function fetchProducts(params: any): Promise { - let response = {} as Product[] | Response +async function fetchProducts(params: any): Promise | Response> { + let response: SuccessResponse | Response const payload = { "json": { @@ -52,15 +52,15 @@ async function fetchProducts(params: any): Promise { if (resp.status == 200 && !hasError(resp) && resp.data?.response?.numFound > 0) { - const product: Array = transform(resp.data.response.docs, productTransformRule) + const products: Array = transform(resp.data.response.docs, productTransformRule) return { - products: product, + list: products, total: resp.data?.response?.numFound } } else { return { - products: {}, + list: [], total: 0 } } diff --git a/src/types/index.ts b/src/types/index.ts index 485731b..42726e1 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -39,6 +39,11 @@ interface Response { serverResponse?: any; } +interface SuccessResponse { + list: Type[]; + total: number; +} + export { ContactMech, Enumeration, @@ -53,5 +58,6 @@ export { Uom, User, Response, + SuccessResponse, events } \ No newline at end of file From 4316505f10417d21a79d6d48cfd73bf6c7edca15 Mon Sep 17 00:00:00 2001 From: Yash Maheshwari Date: Mon, 9 Jan 2023 14:37:51 +0530 Subject: [PATCH 2/5] Improved: schema to update the generics structure(#85zrhhcgr) --- src/modules/product/index.ts | 8 ++++++-- src/types/index.ts | 4 +++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/modules/product/index.ts b/src/modules/product/index.ts index 07a1a07..c53731a 100644 --- a/src/modules/product/index.ts +++ b/src/modules/product/index.ts @@ -56,12 +56,16 @@ async function fetchProducts(params: any): Promise | Re return { list: products, - total: resp.data?.response?.numFound + count: { + total: resp.data?.response?.numFound + } } } else { return { list: [], - total: 0 + count: { + total: 0 + } } } } catch (err) { diff --git a/src/types/index.ts b/src/types/index.ts index 42726e1..57635c5 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -41,7 +41,9 @@ interface Response { interface SuccessResponse { list: Type[]; - total: number; + count: { + [x: string]: number; + } } export { From d5069f8738241bfde5e2d26c1ef88126260e6258 Mon Sep 17 00:00:00 2001 From: Yash Maheshwari Date: Mon, 9 Jan 2023 14:39:16 +0530 Subject: [PATCH 3/5] Implemented: the res schema by extending interface(#85zrhhcgr) --- src/types/index.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/types/index.ts b/src/types/index.ts index 57635c5..c6f6cdf 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -46,6 +46,20 @@ interface SuccessResponse { } } +interface ResponseCount { + total?: number; + matches?: number; + ngroups?: number; +} + +interface ProductResponse extends ResponseCount{ + products: Product[] +} + +interface OrderResponse extends ResponseCount{ + orders: Order[] +} + export { ContactMech, Enumeration, From 388c6b31b504b9fbfa1222cc726b2edbd45e3378 Mon Sep 17 00:00:00 2001 From: Yash Maheshwari Date: Mon, 9 Jan 2023 16:18:24 +0530 Subject: [PATCH 4/5] Removed: extending approach schema for success response and removed indexable types from the schema, also added the total, groups property at root(#85zrhhcgr) --- src/modules/product/index.ts | 12 ++++-------- src/types/index.ts | 19 ++----------------- 2 files changed, 6 insertions(+), 25 deletions(-) diff --git a/src/modules/product/index.ts b/src/modules/product/index.ts index c53731a..f88bcc2 100644 --- a/src/modules/product/index.ts +++ b/src/modules/product/index.ts @@ -54,18 +54,14 @@ async function fetchProducts(params: any): Promise | Re const products: Array = transform(resp.data.response.docs, productTransformRule) - return { + response = { list: products, - count: { - total: resp.data?.response?.numFound - } + total: resp.data?.response?.numFound } } else { - return { + response = { list: [], - count: { - total: 0 - } + total: 0 } } } catch (err) { diff --git a/src/types/index.ts b/src/types/index.ts index c6f6cdf..d17fa37 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -41,23 +41,8 @@ interface Response { interface SuccessResponse { list: Type[]; - count: { - [x: string]: number; - } -} - -interface ResponseCount { - total?: number; - matches?: number; - ngroups?: number; -} - -interface ProductResponse extends ResponseCount{ - products: Product[] -} - -interface OrderResponse extends ResponseCount{ - orders: Order[] + total: number; + groups?: number; } export { From b528d5a7a245f2c57ac1a4e8bca67cb604f7707c Mon Sep 17 00:00:00 2001 From: Yash Maheshwari Date: Thu, 12 Jan 2023 18:37:21 +0530 Subject: [PATCH 5/5] Improved: code to define a list response type(#85zrhhcgr) --- src/index.ts | 3 ++- src/modules/product/index.ts | 36 ++++++++++++++++++------------------ src/types/index.ts | 4 ++-- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/index.ts b/src/index.ts index 8e16dbc..88e8006 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import { events, Product, Response, Stock, Order, OrderItem, OrderPart, OPERATOR, User } from '@/types' +import { events, Product, Response, Stock, ListResponse, Order, OrderItem, OrderPart, OPERATOR, User } from '@/types' import { init, resetConfig, updateToken, updateInstanceUrl } from '@/api' import { isError } from '@/util' import { fetchProducts, fetchProductsGroupedBy, fetchProductsGroupedByParent, fetchProductsStockAtFacility, getOrderDetails, getProfile, updateOrderStatus } from '@/modules' @@ -17,6 +17,7 @@ export { fetchProductsStockAtFacility, getProfile, events, + ListResponse, Product, Response, Stock, diff --git a/src/modules/product/index.ts b/src/modules/product/index.ts index a6ad6e8..dbd665d 100644 --- a/src/modules/product/index.ts +++ b/src/modules/product/index.ts @@ -1,10 +1,10 @@ import api from "@/api"; -import { OPERATOR, Product, Response, SuccessResponse } from "@/types"; +import { OPERATOR, Product, Response, ListResponse } from "@/types"; import { hasError } from "@/util"; import { transform } from 'node-json-transform' import { productTransformRule } from "@/mappings/product"; -async function fetchProducts(params: any): Promise { +async function fetchProducts(params: any): Promise | Response> { const payload = { "json": { @@ -53,15 +53,15 @@ async function fetchProducts(params: any): Promise { const products: Array = transform(resp.data.response.docs, productTransformRule) - response = { + return Promise.resolve({ list: products, total: resp.data?.response?.numFound - } + }) } else { - response = { + return Promise.resolve({ list: [], total: 0 - } + }) } } catch (err) { return Promise.reject({ @@ -72,7 +72,7 @@ async function fetchProducts(params: any): Promise { } } -async function fetchProductsGroupedBy(params: any): Promise { +async function fetchProductsGroupedBy(params: any): Promise | Response> { const payload = { "json": { @@ -132,17 +132,17 @@ async function fetchProductsGroupedBy(params: any): Promise { } }) - return { - products, - matches: resp.data?.grouped?.groupId.matches, - ngroups: resp.data?.grouped?.groupId.ngroups - } + return Promise.resolve({ + list: products, + total: resp.data?.grouped?.groupId.matches, + groups: resp.data?.grouped?.groupId.ngroups + }) } else { - return { - products: {}, - matches: 0, - ngroups: 0 - } + return Promise.resolve({ + list: [], + total: 0, + groups: 0 + }) } } catch (err) { return Promise.reject({ @@ -153,7 +153,7 @@ async function fetchProductsGroupedBy(params: any): Promise { } } -async function fetchProductsGroupedByParent(params: any): Promise { +async function fetchProductsGroupedByParent(params: any): Promise | Response> { const payload = { ...params, diff --git a/src/types/index.ts b/src/types/index.ts index 965e8d1..89c4543 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -40,7 +40,7 @@ interface Response { serverResponse?: any; } -interface SuccessResponse { +interface ListResponse { list: Array; total?: number; groups?: number; @@ -51,6 +51,7 @@ export { Enumeration, events, Geo, + ListResponse, Order, OrderItem, OrderPart, @@ -61,6 +62,5 @@ export { Status, Stock, Uom, - SuccessResponse, User } \ No newline at end of file