Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented: code to define success response schema(#85zrhhcgr) #47

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
18 changes: 9 additions & 9 deletions src/modules/product/index.ts
Original file line number Diff line number Diff line change
@@ -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<any | Response> {
let response = {} as Product[] | Response
async function fetchProducts(params: any): Promise<SuccessResponse<Product> | Response> {
let response: SuccessResponse<Product> | Response

const payload = {
"json": {
Expand Down Expand Up @@ -52,15 +52,15 @@ async function fetchProducts(params: any): Promise<any | Response> {

if (resp.status == 200 && !hasError(resp) && resp.data?.response?.numFound > 0) {

const product: Array<Product> = transform(resp.data.response.docs, productTransformRule)
const products: Array<Product> = transform(resp.data.response.docs, productTransformRule)

return {
products: product,
response = {
list: products,
total: resp.data?.response?.numFound
}
} else {
return {
products: {},
response = {
list: [],
total: 0
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ interface Response {
serverResponse?: any;
}

interface SuccessResponse<Type> {
adityasharma7 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should define practices for list and object responses, and name accordingly

list: Type[];
total: number;
groups?: number;
}

export {
ContactMech,
Enumeration,
Expand All @@ -53,5 +59,6 @@ export {
Uom,
User,
Response,
SuccessResponse,
events
}