Skip to content

Commit

Permalink
Renaming + docs + small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fbeaudoincoveo committed Aug 25, 2023
1 parent 175760f commit 9f0f789
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {SortBy} from '../../../../product-listing.index';
import {buildMockProductListingV2APIClient} from '../../../../test/mock-product-listing-v2-api-client';
import {PlatformClient} from '../../../platform-client';
import {ProductListingV2APIClient} from './product-listing-v2-api-client';
import {ProductListingAPIClient} from './product-listing-v2-api-client';
import {Mode} from './product-listing-v2-params';
import {
ProductListingV2SuccessResponse,
Expand All @@ -14,7 +14,7 @@ describe('product listing v2 api client', () => {
const accessToken = 'some-access-token';
const trackingId = 'some-tracking-id';

let client: ProductListingV2APIClient;
let client: ProductListingAPIClient;
let platformCallMock: jest.Mock;

beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,75 +12,69 @@ import {
import {buildAPIResponseFromErrorOrThrow} from '../../../search/search-api-error-response';
import {
buildProductListingV2Request,
ProductListingV2Request,
ProductListingV2SuccessResponse,
ProductListingV2Request as ProductListingRequest,
ProductListingV2SuccessResponse as ProductListingSuccessResponse,
} from './product-listing-v2-request';

export interface AsyncThunkProductListingV2Options<
T extends Partial<ProductListingV2AppState>
> {
state: T;
rejectValue: ProductListingV2APIErrorStatusResponse;
rejectValue: ProductListingAPIErrorStatusResponse;
extra: ProductListingV2ThunkExtraArguments;
}

/**
* Initialization options for the `ProductListingV2APIClient`.
* The initialization options for the product listing API client.
*/
export interface ProductListingV2APIClientOptions {
export interface ProductListingAPIClientOptions {
logger: Logger;
preprocessRequest: PreprocessRequest;
}

/**
* Defines a Product Listing API response. It can represent an error or a successful response.
* A product listing API response.
*/
export type ProductListingAPIResponse<TSuccessContent> =
| ProductListingAPISuccessResponse<TSuccessContent>
| ProductListingAPIErrorResponse;

/**
* Defines a Product Listing API successful response.
* A product listing API successful response.
*/
export interface ProductListingAPISuccessResponse<TContent> {
success: TContent;
}

/**
* Defines the content of a Product Listing API error response.
* The content of a product listing API error response.
*/
export interface ProductListingV2APIErrorStatusResponse {
export interface ProductListingAPIErrorStatusResponse {
statusCode: number;
message: string;
type: string;
ignored?: boolean;
}

/**
* Defines a Product Listing API error response.
* A Product Listing API error response.
*/
export interface ProductListingAPIErrorResponse {
error: ProductListingV2APIErrorStatusResponse;
error: ProductListingAPIErrorStatusResponse;
}

/**
* The client to use to interface with the Product Listing API.
* The client to use to interface with the product listing API.
*/
export class ProductListingV2APIClient implements FacetSearchAPIClient {
export class ProductListingAPIClient implements FacetSearchAPIClient {
constructor(
private options: ProductListingV2APIClientOptions,
private options: ProductListingAPIClientOptions,
private searchAPIClient: SearchAPIClient
) {}

/**
* Retrieves the product listing from the API.
*
* @param req - The request parameters.
* @returns The products for the requested product listing.
*/
async getListing(
req: ProductListingV2Request
): Promise<ProductListingAPIResponse<ProductListingV2SuccessResponse>> {
req: ProductListingRequest
): Promise<ProductListingAPIResponse<ProductListingSuccessResponse>> {
const response = await PlatformClient.call({
...buildProductListingV2Request(req),
...this.options,
Expand All @@ -92,8 +86,8 @@ export class ProductListingV2APIClient implements FacetSearchAPIClient {

const body = await response.json();
return response.ok
? {success: body as ProductListingV2SuccessResponse}
: {error: body as ProductListingV2APIErrorStatusResponse};
? {success: body as ProductListingSuccessResponse}
: {error: body as ProductListingAPIErrorStatusResponse};
}

async facetSearch(req: FacetSearchRequest): Promise<FacetSearchResponse> {
Expand Down
7 changes: 3 additions & 4 deletions packages/headless/src/app/commerce-engine/commerce-engine.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {StateFromReducersMapObject} from '@reduxjs/toolkit';
import {Logger} from 'pino';
import {ProductListingV2APIClient} from '../../api/commerce/product-listings/v2/product-listing-v2-api-client';
import {ProductListingAPIClient} from '../../api/commerce/product-listings/v2/product-listing-v2-api-client';
import {NoopPreprocessRequest} from '../../api/preprocess-request';
import {SearchAPIClient} from '../../api/search/search-api-client';
import {
Expand All @@ -17,7 +17,6 @@ import {
ExternalEngineOptions,
} from '../engine';
import {buildLogger} from '../logger';
import {ProductListingThunkExtraArguments} from '../product-listing-thunk-extra-arguments';
import {ProductListingV2ThunkExtraArguments} from '../product-listing-v2-thunk-extra-arguments';
import {SearchEngineConfiguration} from '../search-engine/search-engine-configuration';
import {buildThunkExtraArguments} from '../thunk-extra-arguments';
Expand All @@ -39,7 +38,7 @@ type CommerceEngineState = StateFromReducersMapObject<CommerceEngineReducers> &
export interface CommerceEngine<State extends object = {}>
extends CoreEngine<
State & CommerceEngineState,
ProductListingThunkExtraArguments
ProductListingV2ThunkExtraArguments
> {}

export interface CommerceEngineOptions
Expand Down Expand Up @@ -120,7 +119,7 @@ function createCommerceAPIClient(
logger: Logger,
searchAPIClient: SearchAPIClient
) {
return new ProductListingV2APIClient(
return new ProductListingAPIClient(
{
logger,
preprocessRequest:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {ProductListingV2APIClient} from '../api/commerce/product-listings/v2/product-listing-v2-api-client';
import {ProductListingAPIClient} from '../api/commerce/product-listings/v2/product-listing-v2-api-client';
import {ClientThunkExtraArguments} from './thunk-extra-arguments';

export interface ProductListingV2ThunkExtraArguments
extends ClientThunkExtraArguments<ProductListingV2APIClient> {}
extends ClientThunkExtraArguments<ProductListingAPIClient> {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ProductListingV2APIErrorStatusResponse} from '../../../api/commerce/product-listings/v2/product-listing-v2-api-client';
import {ProductListingAPIErrorStatusResponse} from '../../../api/commerce/product-listings/v2/product-listing-v2-api-client';
import {Context} from '../../../api/commerce/product-listings/v2/product-listing-v2-params';
import {Mode} from '../../../api/commerce/product-listings/v2/product-listing-v2-params';
import {ProductRecommendation} from '../../../api/search/search/product-recommendation';
Expand All @@ -9,7 +9,7 @@ export interface ProductListingV2State {
locale: string;
mode: Mode;
clientId: string;
error: ProductListingV2APIErrorStatusResponse | null;
error: ProductListingAPIErrorStatusResponse | null;
isLoading: boolean;
responseId: string;
products: ProductRecommendation[];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import pino from 'pino';
import {
ProductListingV2APIClient,
ProductListingV2APIClientOptions,
ProductListingAPIClient,
ProductListingAPIClientOptions,
} from '../api/commerce/product-listings/v2/product-listing-v2-api-client';
import {NoopPreprocessRequest} from '../api/preprocess-request';
import {buildMockSearchAPIClient} from './mock-search-api-client';

export function buildMockProductListingV2APIClient(
options?: Partial<ProductListingV2APIClientOptions>
options?: Partial<ProductListingAPIClientOptions>
) {
return new ProductListingV2APIClient(
return new ProductListingAPIClient(
{
preprocessRequest: NoopPreprocessRequest,
logger: pino({level: 'silent'}),
Expand Down

0 comments on commit 9f0f789

Please sign in to comment.