Skip to content

Commit

Permalink
feat: remove comments remove singleton api obj
Browse files Browse the repository at this point in the history
  • Loading branch information
YulikK committed Jun 17, 2024
1 parent 057d2aa commit c91134a
Show file tree
Hide file tree
Showing 15 changed files with 17 additions and 219 deletions.
67 changes: 0 additions & 67 deletions src/shared/API/cart/CartApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,65 +51,6 @@ export default class CartApi {
return data;
}

// public async deleteProduct(cart: Cart, product: CartProduct): Promise<ClientResponse> {
// const data = await this.client
// .apiRoot()
// .me()
// .carts()
// .withId({ ID: cart.id })
// .post({
// body: {
// actions: [
// {
// action: Actions.removeLineItem,
// lineItemId: product.lineItemId,
// },
// ],
// version: cart.version,
// },
// })
// .execute();
// return data;
// }

// public async editCart(cart: Cart, action: MyCartUpdateAction): Promise<ClientResponse> {
// const data = await this.client
// .apiRoot()
// .me()
// .carts()
// .withId({ ID: cart.id })
// .post({
// body: {
// actions: [action],
// version: cart.version,
// },
// })
// .execute();
// return data;
// }

// public async editProductCount(cart: Cart, editCartItem: EditCartItem): Promise<ClientResponse> {
// const data = await this.client
// .apiRoot()
// .me()
// .carts()
// .withId({ ID: cart.id })
// .post({
// body: {
// actions: [
// {
// action: Actions.changeLineItemQuantity,
// lineItemId: editCartItem.lineId,
// quantity: editCartItem.quantity,
// },
// ],
// version: cart.version,
// },
// })
// .execute();
// return data;
// }

public async getActiveCart(): Promise<ClientResponse<CartResponse>> {
const data = await this.client.apiRoot().me().activeCart().get().execute();
return data;
Expand Down Expand Up @@ -156,11 +97,3 @@ export default class CartApi {
return data;
}
}

// const createCartApi = (): CartApi => new CartApi();

// const cartApi = createCartApi();

// export default function getCartApi(): CartApi {
// return cartApi;
// }
16 changes: 1 addition & 15 deletions src/shared/API/customer/CustomerApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const CART_MERGE_MODE = 'MergeWithExistingCustomerCart';
const CART_TYPE_ID = 'cart';
const EMAIL = 'email';

export class CustomerApi {
export default class CustomerApi {
private client: ApiClient;

constructor() {
Expand Down Expand Up @@ -118,12 +118,6 @@ export class CustomerApi {
return data;
}

public async deleteCustomer(ID: string, version: number): Promise<ClientResponse<Customer>> {
const data = await this.client.adminRoot().customers().withId({ ID }).delete({ queryArgs: { version } }).execute();
await this.logoutUser();
return data;
}

public async editCustomer(actions: MyCustomerUpdateAction[], version: number): Promise<ClientResponse<Customer>> {
const data = await this.client.apiRoot().me().post({ body: { actions, version } }).execute();
return data;
Expand Down Expand Up @@ -189,11 +183,3 @@ export class CustomerApi {
return data;
}
}

const createCustomerApi = (): CustomerApi => new CustomerApi();

const customerApi = createCustomerApi();

export default function getCustomerApi(): CustomerApi {
return customerApi;
}
9 changes: 2 additions & 7 deletions src/shared/API/customer/model/CustomerModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
isCustomerResponse,
isCustomerSignInResultResponse,
} from '../../types/validation.ts';
import getCustomerApi, { type CustomerApi } from '../CustomerApi.ts';
import CustomerApi from '../CustomerApi.ts';

const CUSTOMER_FIELD = 'customer';

Expand All @@ -43,7 +43,7 @@ export class CustomerModel {
private root: CustomerApi;

constructor() {
this.root = getCustomerApi();
this.root = new CustomerApi();
}

public static actionAddAddress(address: Address): MyCustomerUpdateAction {
Expand Down Expand Up @@ -233,11 +233,6 @@ export class CustomerModel {
return this.getCustomerFromData(data);
}

public async deleteCustomer(customer: User): Promise<boolean> {
const data = await this.root.deleteCustomer(customer.id, customer.version);
return this.getCustomerFromData(data) !== null;
}

public async editCustomer(actions: MyCustomerUpdateAction[], customer: User): Promise<User | null> {
const data = await this.root.editCustomer(actions, customer.version);
return this.getCustomerFromData(data);
Expand Down
4 changes: 2 additions & 2 deletions src/shared/API/customer/tests/CustomerApi.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import getCustomerApi, { CustomerApi } from '../CustomerApi.ts';
import CustomerApi from '../CustomerApi.ts';

/**
* @vitest-environment jsdom
*/

const root = getCustomerApi();
const root = new CustomerApi();

describe('Checking CustomerApi', () => {
it('should check if root is defined', () => {
Expand Down
10 changes: 1 addition & 9 deletions src/shared/API/discount/DiscountApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {

import getApiClient, { type ApiClient } from '../sdk/client.ts';

export class DiscountApi {
export default class DiscountApi {
private client: ApiClient;

constructor() {
Expand All @@ -23,11 +23,3 @@ export class DiscountApi {
return data;
}
}

const createDiscountApi = (): DiscountApi => new DiscountApi();

const discountApi = createDiscountApi();

export default function getDiscountApi(): DiscountApi {
return discountApi;
}
4 changes: 2 additions & 2 deletions src/shared/API/discount/model/DiscountModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import {
isDiscountCodePagedQueryResponse,
isProductDiscountPagedQueryResponse,
} from '../../types/validation.ts';
import getDiscountApi, { type DiscountApi } from '../DiscountApi.ts';
import DiscountApi from '../DiscountApi.ts';

export class DiscountModel {
private coupons: Coupon[] = [];

private root: DiscountApi;

constructor() {
this.root = getDiscountApi();
this.root = new DiscountApi();
this.init().catch(showErrorMessage);
}

Expand Down
10 changes: 1 addition & 9 deletions src/shared/API/product/ProductApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ enum Facets {
enum QueryParams {
range = 'range',
}
export class ProductApi {
export default class ProductApi {
private client: ApiClient;

constructor() {
Expand Down Expand Up @@ -95,11 +95,3 @@ export class ProductApi {
return data;
}
}

const createProductApi = (): ProductApi => new ProductApi();

const productApi = createProductApi();

export default function getProductApi(): ProductApi {
return productApi;
}
6 changes: 2 additions & 4 deletions src/shared/API/product/model/ProductModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import type {
import { PRICE_FRACTIONS } from '@/shared/constants/product.ts';
import { getLevel, getSize } from '@/shared/utils/size.ts';

import type { ProductApi } from '../ProductApi.ts';

import {
Attribute,
type CategoriesProductCount,
Expand All @@ -40,7 +38,7 @@ import {
isRangeFacetResult,
isTermFacetResult,
} from '../../types/validation.ts';
import getProductApi from '../ProductApi.ts';
import ProductApi from '../ProductApi.ts';

enum ProductConstant {
categoriesId = 'categories.id',
Expand All @@ -60,7 +58,7 @@ export class ProductModel {
private root: ProductApi;

constructor() {
this.root = getProductApi();
this.root = new ProductApi();
}

private adaptCategoryPagedQueryToClient(data: CategoryPagedQueryResponse): Category[] {
Expand Down
4 changes: 2 additions & 2 deletions src/shared/API/product/tests/ProductApi.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import getProductApi, { ProductApi } from '../ProductApi.ts';
import ProductApi from '../ProductApi.ts';

/**
* @vitest-environment jsdom
*/

const root = getProductApi();
const root = new ProductApi();

describe('Checking ProductApi', () => {
it('should check if root is defined', () => {
Expand Down
1 change: 0 additions & 1 deletion src/shared/API/product/tests/filter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// import getProductApi, { ProductApi } from '../ProductApi.ts';
import type { PriceRange } from '../../types/type.ts';

import { FilterFields } from '../../types/type.ts';
Expand Down
19 changes: 0 additions & 19 deletions src/shared/API/sdk/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,10 @@ const URL_HTTP = 'https://api.europe-west1.gcp.commercetools.com';
const USE_SAVE_TOKEN = true;

const httpMiddlewareOptions: HttpMiddlewareOptions = {
// fetch,
host: URL_HTTP,
};

export class ApiClient {
private adminConnection: ByProjectKeyRequestBuilder;

private anonymConnection: ByProjectKeyRequestBuilder | null = null;

private authConnection: ByProjectKeyRequestBuilder | null = null;
Expand All @@ -61,7 +58,6 @@ export class ApiClient {
} else {
this.anonymConnection = this.createAnonymConnection();
}
this.adminConnection = this.createAdminConnection();
}

private addAuthMiddleware(
Expand Down Expand Up @@ -97,16 +93,6 @@ export class ApiClient {
}
}

private createAdminConnection(): ByProjectKeyRequestBuilder {
const defaultOptions = this.getDefaultOptions();
const client = this.getDefaultClient();

client.withClientCredentialsFlow(defaultOptions);

this.adminConnection = this.getConnection(client.build());
return this.adminConnection;
}

private createAnonymConnection(): ByProjectKeyRequestBuilder {
const defaultOptions = this.getDefaultOptions(TokenType.ANONYM);
const client = this.getDefaultClient();
Expand Down Expand Up @@ -157,18 +143,13 @@ export class ApiClient {
clientId: this.clientID,
clientSecret: this.clientSecret,
},
// fetch,
host: URL_AUTH,
projectKey: this.projectKey,
scopes: this.scopes,
tokenCache: USE_SAVE_TOKEN && tokenType === TokenType.AUTH ? getTokenCache(tokenType) : undefined,
};
}

public adminRoot(): ByProjectKeyRequestBuilder {
return this.adminConnection;
}

public apiRoot(): ByProjectKeyRequestBuilder {
let client =
(this.authConnection && this.isAuth) || (this.authConnection && !this.anonymConnection)
Expand Down
10 changes: 1 addition & 9 deletions src/shared/API/shopping-list/ShoppingListApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type { OptionsRequest } from '../types/type.ts';
import makeSortRequest from '../product/utils/sort.ts';
import getApiClient, { type ApiClient } from '../sdk/client.ts';

export class ShoppingListApi {
export default class ShoppingListApi {
private client: ApiClient;

constructor() {
Expand Down Expand Up @@ -151,11 +151,3 @@ export class ShoppingListApi {
return data;
}
}

const createShoppingListApi = (): ShoppingListApi => new ShoppingListApi();

const shoppingListApi = createShoppingListApi();

export default function getShoppingListApi(): ShoppingListApi {
return shoppingListApi;
}
4 changes: 2 additions & 2 deletions src/shared/API/shopping-list/model/ShoppingListModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
isShoppingList,
isShoppingListPagedQueryResponse,
} from '../../types/validation.ts';
import getShoppingListApi, { type ShoppingListApi } from '../ShoppingListApi.ts';
import ShoppingListApi from '../ShoppingListApi.ts';

enum ACTIONS {
addLineItem = 'addLineItem',
Expand All @@ -37,7 +37,7 @@ export class ShoppingListModel {
private subscribers: ShoppingListChangeHandler[] = [];

constructor() {
this.root = getShoppingListApi();
this.root = new ShoppingListApi();
this.getShoppingList()
.then(() => {
const { anonymousId } = getStore().getState();
Expand Down
2 changes: 1 addition & 1 deletion src/shared/API/shopping-list/test/shoppingList.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ShoppingListProduct } from '@/shared/types/shopping-list.ts';

import { ShoppingListApi } from '../ShoppingListApi.ts';
import ShoppingListApi from '../ShoppingListApi.ts';
import getShoppingListModel, { ShoppingListModel } from '../model/ShoppingListModel.ts';

/**
Expand Down
Loading

0 comments on commit c91134a

Please sign in to comment.