-
Notifications
You must be signed in to change notification settings - Fork 10
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
Define success response type #38
Comments
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. |
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 |
Concluded that we will use the pattern of interface SuccessResponse<Type> {
list: Type[];
total: number;
groups?: number
} |
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. |
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
} |
Explored around the property/variable naming convention used to hold the In case of solr, In case of open search, https://opensearch.org/docs/latest/opensearch/bucket-agg/ is similar to grouped logic, and in // 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
}
]
}
}
} |
What is the motivation for adding/enhancing this feature?
Currently, for product specific methods the return type is
any | Response
but usingany
is not a recommended way so we need to define a type for ProductResponseWhat are the acceptance criteria?
Can you complete this feature request by yourself?
Additional information
Approach:
Define a separate type in Product type named
ProductResponse
that contain two properties.The text was updated successfully, but these errors were encountered: