diff --git a/src/shared/API/cart/CartApi.ts b/src/shared/API/cart/CartApi.ts index 047b8671..2716c694 100644 --- a/src/shared/API/cart/CartApi.ts +++ b/src/shared/API/cart/CartApi.ts @@ -51,65 +51,6 @@ export default class CartApi { return data; } - // public async deleteProduct(cart: Cart, product: CartProduct): Promise { - // 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 { - // 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 { - // 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> { const data = await this.client.apiRoot().me().activeCart().get().execute(); return data; @@ -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; -// } diff --git a/src/shared/API/customer/CustomerApi.ts b/src/shared/API/customer/CustomerApi.ts index 2c7e9959..5ac9ff19 100644 --- a/src/shared/API/customer/CustomerApi.ts +++ b/src/shared/API/customer/CustomerApi.ts @@ -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() { @@ -118,12 +118,6 @@ export class CustomerApi { return data; } - public async deleteCustomer(ID: string, version: number): Promise> { - 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> { const data = await this.client.apiRoot().me().post({ body: { actions, version } }).execute(); return data; @@ -189,11 +183,3 @@ export class CustomerApi { return data; } } - -const createCustomerApi = (): CustomerApi => new CustomerApi(); - -const customerApi = createCustomerApi(); - -export default function getCustomerApi(): CustomerApi { - return customerApi; -} diff --git a/src/shared/API/customer/model/CustomerModel.ts b/src/shared/API/customer/model/CustomerModel.ts index bea7fe43..77ebc13f 100644 --- a/src/shared/API/customer/model/CustomerModel.ts +++ b/src/shared/API/customer/model/CustomerModel.ts @@ -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'; @@ -43,7 +43,7 @@ export class CustomerModel { private root: CustomerApi; constructor() { - this.root = getCustomerApi(); + this.root = new CustomerApi(); } public static actionAddAddress(address: Address): MyCustomerUpdateAction { @@ -233,11 +233,6 @@ export class CustomerModel { return this.getCustomerFromData(data); } - public async deleteCustomer(customer: User): Promise { - const data = await this.root.deleteCustomer(customer.id, customer.version); - return this.getCustomerFromData(data) !== null; - } - public async editCustomer(actions: MyCustomerUpdateAction[], customer: User): Promise { const data = await this.root.editCustomer(actions, customer.version); return this.getCustomerFromData(data); diff --git a/src/shared/API/customer/tests/CustomerApi.spec.ts b/src/shared/API/customer/tests/CustomerApi.spec.ts index 2917ee7d..94d05b17 100644 --- a/src/shared/API/customer/tests/CustomerApi.spec.ts +++ b/src/shared/API/customer/tests/CustomerApi.spec.ts @@ -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', () => { diff --git a/src/shared/API/discount/DiscountApi.ts b/src/shared/API/discount/DiscountApi.ts index 04d9075c..6a310b1c 100644 --- a/src/shared/API/discount/DiscountApi.ts +++ b/src/shared/API/discount/DiscountApi.ts @@ -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() { @@ -23,11 +23,3 @@ export class DiscountApi { return data; } } - -const createDiscountApi = (): DiscountApi => new DiscountApi(); - -const discountApi = createDiscountApi(); - -export default function getDiscountApi(): DiscountApi { - return discountApi; -} diff --git a/src/shared/API/discount/model/DiscountModel.ts b/src/shared/API/discount/model/DiscountModel.ts index 4034eb76..98daf32d 100644 --- a/src/shared/API/discount/model/DiscountModel.ts +++ b/src/shared/API/discount/model/DiscountModel.ts @@ -15,7 +15,7 @@ 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[] = []; @@ -23,7 +23,7 @@ export class DiscountModel { private root: DiscountApi; constructor() { - this.root = getDiscountApi(); + this.root = new DiscountApi(); this.init().catch(showErrorMessage); } diff --git a/src/shared/API/product/ProductApi.ts b/src/shared/API/product/ProductApi.ts index c147a546..e1ff50c2 100644 --- a/src/shared/API/product/ProductApi.ts +++ b/src/shared/API/product/ProductApi.ts @@ -22,7 +22,7 @@ enum Facets { enum QueryParams { range = 'range', } -export class ProductApi { +export default class ProductApi { private client: ApiClient; constructor() { @@ -95,11 +95,3 @@ export class ProductApi { return data; } } - -const createProductApi = (): ProductApi => new ProductApi(); - -const productApi = createProductApi(); - -export default function getProductApi(): ProductApi { - return productApi; -} diff --git a/src/shared/API/product/model/ProductModel.ts b/src/shared/API/product/model/ProductModel.ts index 1fa89e28..3c61de4b 100644 --- a/src/shared/API/product/model/ProductModel.ts +++ b/src/shared/API/product/model/ProductModel.ts @@ -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, @@ -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', @@ -60,7 +58,7 @@ export class ProductModel { private root: ProductApi; constructor() { - this.root = getProductApi(); + this.root = new ProductApi(); } private adaptCategoryPagedQueryToClient(data: CategoryPagedQueryResponse): Category[] { diff --git a/src/shared/API/product/tests/ProductApi.spec.ts b/src/shared/API/product/tests/ProductApi.spec.ts index 9485fee2..fbc31dd7 100644 --- a/src/shared/API/product/tests/ProductApi.spec.ts +++ b/src/shared/API/product/tests/ProductApi.spec.ts @@ -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', () => { diff --git a/src/shared/API/product/tests/filter.spec.ts b/src/shared/API/product/tests/filter.spec.ts index edb288ed..a474bad6 100644 --- a/src/shared/API/product/tests/filter.spec.ts +++ b/src/shared/API/product/tests/filter.spec.ts @@ -1,4 +1,3 @@ -// import getProductApi, { ProductApi } from '../ProductApi.ts'; import type { PriceRange } from '../../types/type.ts'; import { FilterFields } from '../../types/type.ts'; diff --git a/src/shared/API/sdk/client.ts b/src/shared/API/sdk/client.ts index a21fcb45..bc4c7f0a 100644 --- a/src/shared/API/sdk/client.ts +++ b/src/shared/API/sdk/client.ts @@ -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; @@ -61,7 +58,6 @@ export class ApiClient { } else { this.anonymConnection = this.createAnonymConnection(); } - this.adminConnection = this.createAdminConnection(); } private addAuthMiddleware( @@ -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(); @@ -157,7 +143,6 @@ export class ApiClient { clientId: this.clientID, clientSecret: this.clientSecret, }, - // fetch, host: URL_AUTH, projectKey: this.projectKey, scopes: this.scopes, @@ -165,10 +150,6 @@ export class ApiClient { }; } - public adminRoot(): ByProjectKeyRequestBuilder { - return this.adminConnection; - } - public apiRoot(): ByProjectKeyRequestBuilder { let client = (this.authConnection && this.isAuth) || (this.authConnection && !this.anonymConnection) diff --git a/src/shared/API/shopping-list/ShoppingListApi.ts b/src/shared/API/shopping-list/ShoppingListApi.ts index ab110cdc..55fe17e4 100644 --- a/src/shared/API/shopping-list/ShoppingListApi.ts +++ b/src/shared/API/shopping-list/ShoppingListApi.ts @@ -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() { @@ -151,11 +151,3 @@ export class ShoppingListApi { return data; } } - -const createShoppingListApi = (): ShoppingListApi => new ShoppingListApi(); - -const shoppingListApi = createShoppingListApi(); - -export default function getShoppingListApi(): ShoppingListApi { - return shoppingListApi; -} diff --git a/src/shared/API/shopping-list/model/ShoppingListModel.ts b/src/shared/API/shopping-list/model/ShoppingListModel.ts index 831f0ea5..39f35c40 100644 --- a/src/shared/API/shopping-list/model/ShoppingListModel.ts +++ b/src/shared/API/shopping-list/model/ShoppingListModel.ts @@ -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', @@ -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(); diff --git a/src/shared/API/shopping-list/test/shoppingList.spec.ts b/src/shared/API/shopping-list/test/shoppingList.spec.ts index b9445753..e6d7abf4 100644 --- a/src/shared/API/shopping-list/test/shoppingList.spec.ts +++ b/src/shared/API/shopping-list/test/shoppingList.spec.ts @@ -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'; /** diff --git a/src/shared/API/types/validation.ts b/src/shared/API/types/validation.ts index a334eb98..2be23a7b 100644 --- a/src/shared/API/types/validation.ts +++ b/src/shared/API/types/validation.ts @@ -12,7 +12,6 @@ import type { FacetRange, FacetTerm, LocalizedString, - Product, ProductDiscountPagedQueryResponse, ProductPagedQueryResponse, ProductProjection, @@ -22,7 +21,6 @@ import type { ShoppingListPagedQueryResponse, TermFacetResult, } from '@commercetools/platform-sdk'; -import type { TokenStore } from '@commercetools/sdk-client-v2'; export function isClientResponse(data: unknown): data is ClientResponse { return Boolean( @@ -80,21 +78,6 @@ export function isCategoryPagedQueryResponse(data: unknown): data is CategoryPag ); } -export function isProductPagedQueryResponse(data: unknown): data is ProductPagedQueryResponse { - return Boolean( - typeof data === 'object' && - data && - 'count' in data && - typeof data.count === 'number' && - 'limit' in data && - typeof data.limit === 'number' && - 'total' in data && - typeof data.total === 'number' && - 'results' in data && - typeof Array.isArray(data.results), - ); -} - export function isLocalizationObj(data: unknown): data is LocalizedString { return Boolean(typeof data === 'object' && data && Object.keys(data).every((key) => typeof key === 'string')); } @@ -110,27 +93,6 @@ export function isAttributePlainEnumValue(data: unknown): data is AttributePlain ); } -export function isProductResponse(data: unknown): data is Product { - return Boolean( - typeof data === 'object' && - data && - 'id' in data && - typeof data.id === 'string' && - 'key' in data && - typeof data.id === 'string' && - 'masterData' in data && - typeof data.masterData === 'object' && - data.masterData !== null && - 'staged' in data.masterData && - typeof data.masterData.staged === 'object' && - data.masterData.staged !== null && - 'categories' in data.masterData.staged && - 'description' in data.masterData.staged && - 'name' in data.masterData.staged && - 'variants' in data.masterData.staged, - ); -} - export function isProductProjectionPagedSearchResponse(data: unknown): data is ProductPagedQueryResponse { return Boolean(typeof data === 'object' && data && 'facets' in data && typeof data.facets === 'object'); } @@ -161,25 +123,6 @@ export function isProductProjection(data: unknown): data is ProductProjection { ); } -export function isProductProjectionPagedQueryResponseWithFacet( - data: unknown, -): data is ProductProjectionPagedQueryResponse { - return Boolean( - typeof data === 'object' && - data && - 'count' in data && - typeof data.count === 'number' && - 'limit' in data && - typeof data.limit === 'number' && - 'total' in data && - typeof data.total === 'number' && - 'facets' in data && - typeof data.facets === 'object' && - 'results' in data && - Array.isArray(data.results), - ); -} - export function isRangeFacetResult(data: unknown): data is RangeFacetResult { return Boolean( typeof data === 'object' && data && 'ranges' in data && Array.isArray(data.ranges) && data.ranges.length, @@ -232,19 +175,6 @@ export function isErrorResponse(data: unknown): data is ErrorResponse { ); } -export function isTokenType(data: unknown): data is TokenStore { - return Boolean( - typeof data === 'object' && - data && - 'expirationTime' in data && - typeof data.expirationTime === 'string' && - 'refreshToken' in data && - typeof data.refreshToken === 'string' && - 'token' in data && - typeof data.token === 'string', - ); -} - export function isCart(data: unknown): data is Cart { return Boolean( typeof data === 'object' &&