Coder Social home page Coder Social logo

Define success response type about oms-api HOT 6 CLOSED

hotwax avatar hotwax commented on September 16, 2024
Define success response type

from oms-api.

Comments (6)

ymaheshwari1 avatar ymaheshwari1 commented on September 16, 2024

Another pattern to define product response is:

interface ProductResponse {
 [string]?: number
} & ({
  product: Product
} | {
  products: Product[]
})

Also, we can even think of defining the interface without using union patten and simply define it as:

interface ProductResponse {
  product: Product | Product[],
 [string]?: number
}

But in this way the property name will no more self-explanatory, also it might be difficult to handle the response on application side.

from oms-api.

adityasharma7 avatar adityasharma7 commented on September 16, 2024

We could define a base response interface and extend it to specific responses like Product, Shipment etc

https://www.typescriptlang.org/docs/handbook/2/objects.html#extending-types

Create a basic POCs for these cases then we will conclude accordingly

from oms-api.

ymaheshwari1 avatar ymaheshwari1 commented on September 16, 2024

Concluded that we will use the pattern of generics.

interface SuccessResponse<Type> {
  list: Type[];
  total: number;
  groups?: number
}

from oms-api.

ymaheshwari1 avatar ymaheshwari1 commented on September 16, 2024

Explored around how success response are returned using rest api from ecoms

In initial exploration found that only the original object/array is returned without any other extra property like total, size, count etc

  // For getting a list of products/orders
  {
    products: [{}, {}]
  }
  
  // For getting a single order/product
  {
    product: {}
  }

In another exploration found that there is no specific key used while returning the response

  // Can be accessible using response.data
  // Getting a list of products/orders
  [{}, {}]
  
  // Get a single product/order
  {}

In one more exploration found that, in case of retriving a single product just return a single object and for retriving a list, multiple other fields with items array is returned

  // Get a single product/order
  {}
  
  // Retriving a list of products/orders
  {
    startIndex: 0,
    pageCount: 0,
    totalCount: 0,
    items: [{}, {}]
  }

One more pattern is identified in which for every api endpoint a separate response type is defined which extends another base response type.
For example when accessing api to get a single product, the response is ProductResponse extends BaseResponse, when accessing api to get a list of products, the response type is ProductListResponse extends OtherBaseResponse`.

from oms-api.

ymaheshwari1 avatar ymaheshwari1 commented on September 16, 2024

Discussed on the above points, and concluded that in case of fetching a specific record like a product/order/shipment using it's id, we will define the return type same as the request resource(product/ shipment / order), and in case when requesting a list of records for a resource we will return an array, total (optional), and groups(optional).

For the case of requesting multiple records:

interface ListResponse<T> {
  list: Array<T>;
  total?: number; // number of records on the server for the specific resource
  groups?: number; // number of groups found when fetching the resource by grouping on a field
}

from oms-api.

ymaheshwari1 avatar ymaheshwari1 commented on September 16, 2024

Explored around the property/variable naming convention used to hold the total and groups value.

In case of solr, ngroups contain the count of the total groups, and matches contain the count of records.

In case of open search, https://opensearch.org/docs/latest/opensearch/bucket-agg/ is similar to grouped logic, and in open search case we get doc_count specifying the number of document in each bucket.

// Response in open search in case of buckets
"aggregations" : {
  "response_codes" : {
    "doc_count_error_upper_bound" : 0,
    "sum_other_doc_count" : 0,
    "buckets" : [
      {
        "key" : "200",
        "doc_count" : 12832
      },
      {
        "key" : "404",
        "doc_count" : 801
      },
      {
        "key" : "503",
        "doc_count" : 441
      }
    ]
  }
 }
}

from oms-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.