-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: increments commerce client (#2647)
## What's the purpose of this pull request? To add the Reviews & Ratings API integration through commerce client ## How it works? Adds 3 new calls to the client: - client.commerce.rating (retrieves rating information for a specific product) - client.commerce.reviews.list (retrieves all reviews for a specific product) - client.commerce.reviews.create (creates a new review for a specific product) ## How to test it? Creates a `.ts` file on the root folder of the project and adds the following code: ```typescript import { getContextFactory, Options } from "./packages/api/src/platforms/vtex"; const apiOptions = { platform: 'vtex', account: 'storeframework', locale: 'en-US', environment: 'vtexcommercestable', channel: '{"salesChannel":"1"}', showSponsored: false, } as Options const apiCtx = getContextFactory(apiOptions) const commerceApiClient = apiCtx({}).clients.commerce ``` After that you can use the `commerceApiClient` to call the new methods. To run the file locally use the following command: ```bash npx tsx ``` ## References [JIRA Task: SFS-2092](https://vtex-dev.atlassian.net/browse/SFS-2092) [Reviews & Ratings API Doc](https://developers.vtex.com/docs/api-reference/reviews-and-ratings-api#overview) ## Checklist **PR Description** - [ ] Added Rating types - [ ] Added Reviews types - [ ] Incremented ProductSearchReviewResult - [ ] Created adapatObject function on `utils` - [ ] Created camelToSnakeCase function on `utils`
- Loading branch information
1 parent
60fdaec
commit c678ed4
Showing
6 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
packages/api/src/platforms/vtex/clients/commerce/types/ProductRating.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface ProductRating { | ||
average: number | ||
totalCount: number | ||
} |
56 changes: 56 additions & 0 deletions
56
packages/api/src/platforms/vtex/clients/commerce/types/ProductReview.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
export interface ProductReview { | ||
id: string | ||
productId: string | ||
rating: number | ||
title: string | ||
text: string | ||
reviewerName: string | ||
shopperId: string | ||
reviewDateTime: string | ||
searchDate: string | ||
verifiedPurchaser: boolean | ||
sku: string | null | ||
approved: boolean | ||
location: string | null | ||
locale: string | null | ||
pastReviews: string | null | ||
} | ||
|
||
export enum ProductReviewsInputOrderBy { | ||
productId = 'ProductId', | ||
shopperId = 'ShopperId', | ||
approved = 'Approved', | ||
reviewDateTime = 'ReviewDateTime', | ||
searchDate = 'SearchDate', | ||
rating = 'Rating', | ||
locale = 'Locale', | ||
} | ||
|
||
export interface ProductReviewsInput { | ||
searchTerm?: string | ||
from?: number | ||
to?: number | ||
orderBy?: ProductReviewsInputOrderBy | ||
orderWay?: 'asc' | 'desc' | ||
status?: boolean | ||
productId?: string | ||
rating?: number | ||
} | ||
|
||
export interface ProductReviewsResult { | ||
data: ProductReview[] | ||
range: { | ||
from: number | ||
to: number | ||
total: number | ||
} | ||
} | ||
|
||
export interface CreateProductReviewInput { | ||
productId: string | ||
rating: number | ||
title: string | ||
text: string | ||
reviewerName: string | ||
approved: boolean | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* Transforms an object's keys and values based on provided formatters and a predicate filter. | ||
* | ||
* @template T - The type of the transformed values. | ||
* @param obj - The object to transform. | ||
* @param predicate - A predicate function that determines whether a key-value pair should be included in the output. | ||
* @param keyFormatter - A function that formats the object keys. Defaults to returning the key as is. | ||
* @param valueFormatter - A function that formats the object values. Defaults to returning the value as is. | ||
* @returns A new object with transformed keys and values, including only the key-value pairs that satisfy the predicate. | ||
* | ||
* @example <caption>Select all keys that have a defined value and also makes all keys uppercase and all values as numbers</caption> | ||
* ```ts | ||
* const obj = { john: "25", will: "10", bob: undefined }; | ||
* const result = adaptObject<number>( | ||
* obj, | ||
* (key, value) => value !== undefined, | ||
* key => key.toUpperCase(), | ||
* Integer.parseInt | ||
* ); | ||
* console.log(result); // { JOHN: 25, WILL: 10 } | ||
* ``` | ||
*/ | ||
export function adaptObject<T>( | ||
obj: Record<string, unknown>, | ||
predicate: (key: string, value: unknown) => boolean, | ||
keyFormatter: (key: string) => string = (key) => key, | ||
valueFormatter: (value: unknown) => T = (value) => value as T | ||
): Record<string, T> { | ||
return Object.entries(obj).reduce( | ||
(acc, [key, value]) => { | ||
if (predicate(key, value)) { | ||
acc[keyFormatter(key)] = valueFormatter(value) | ||
} | ||
|
||
return acc | ||
}, | ||
{} as Record<string, T> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function camelToSnakeCase(str: string): string { | ||
return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`) | ||
} |