Skip to content

Commit

Permalink
refactor: got rid of Composable parts from error types
Browse files Browse the repository at this point in the history
  • Loading branch information
Fifciuu committed Jan 7, 2021
1 parent bcfb459 commit 0805dac
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 47 deletions.
4 changes: 2 additions & 2 deletions packages/core/core/src/factories/useCartFactory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CustomQuery, UseCart, Context, FactoryParams, UseCartComposableErrors } from '../types';
import { CustomQuery, UseCart, Context, FactoryParams, UseCartErrors } from '../types';
import { Ref, computed } from '@vue/composition-api';
import { sharedRef, Logger, generateContext } from '../utils';

Expand Down Expand Up @@ -34,7 +34,7 @@ export const useCartFactory = <CART, CART_ITEM, PRODUCT, COUPON>(
const loading: Ref<boolean> = sharedRef(false, 'useCart-loading');
const cart: Ref<CART> = sharedRef(null, 'useCart-cart');
const context = generateContext(factoryParams);
const error: Ref<UseCartComposableErrors> = sharedRef({}, 'useCart-error');
const error: Ref<UseCartErrors> = sharedRef({}, 'useCart-error');

const setCart = (newCart: CART) => {
cart.value = newCart;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/core/src/factories/useCategoryFactory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CustomQuery, UseCategory, Context, FactoryParams, UseCategoryComposableErrors } from '../types';
import { CustomQuery, UseCategory, Context, FactoryParams, UseCategoryErrors } from '../types';
import { Ref, computed } from '@vue/composition-api';
import { sharedRef, Logger, generateContext } from '../utils';

Expand All @@ -13,7 +13,7 @@ export function useCategoryFactory<CATEGORY, CATEGORY_SEARCH_PARAMS>(
const categories: Ref<CATEGORY[]> = sharedRef([], `useCategory-categories-${id}`);
const loading = sharedRef(false, `useCategory-loading-${id}`);
const context = generateContext(factoryParams);
const error: Ref<UseCategoryComposableErrors> = sharedRef({}, `useCategory-error-${id}`);
const error: Ref<UseCategoryErrors> = sharedRef({}, `useCategory-error-${id}`);

const search = async (searchParams) => {
Logger.debug('useCategory.search', searchParams);
Expand Down
4 changes: 2 additions & 2 deletions packages/core/core/src/factories/useContentFactory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Ref, computed } from '@vue/composition-api';
import { RenderComponent, UseContent, Context, FactoryParams, UseContentComposableErrors } from '../types';
import { RenderComponent, UseContent, Context, FactoryParams, UseContentErrors } from '../types';
import { sharedRef, Logger, generateContext } from '../utils';
import { PropOptions, VNode } from 'vue';

Expand All @@ -13,7 +13,7 @@ export function useContentFactory<CONTENT, CONTENT_SEARCH_PARAMS>(
return function useContent(id: string): UseContent<CONTENT, CONTENT_SEARCH_PARAMS> {
const content: Ref<CONTENT> = sharedRef([], `useContent-content-${id}`);
const loading: Ref<boolean> = sharedRef(false, `useContent-loading-${id}`);
const error: Ref<UseContentComposableErrors> = sharedRef({}, `useContent-error-${id}`);
const error: Ref<UseContentErrors> = sharedRef({}, `useContent-error-${id}`);
const context = generateContext(factoryParams);

const search = async(params: CONTENT_SEARCH_PARAMS): Promise<void> => {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/core/src/factories/useFacetFactory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Ref, computed } from '@vue/composition-api';
import { sharedRef, vsfRef, Logger, generateContext } from '../utils';
import { UseFacet, FacetSearchResult, AgnosticFacetSearchParams, Context, FactoryParams, UseFacetComposableErrors } from '../types';
import { UseFacet, FacetSearchResult, AgnosticFacetSearchParams, Context, FactoryParams, UseFacetErrors } from '../types';

interface UseFacetFactoryParams<SEARCH_DATA> extends FactoryParams {
search: (context: Context, params?: FacetSearchResult<SEARCH_DATA>) => Promise<SEARCH_DATA>;
Expand All @@ -13,7 +13,7 @@ const useFacetFactory = <SEARCH_DATA>(factoryParams: UseFacetFactoryParams<SEARC
const loading: Ref<boolean> = vsfRef(false, `${ssrKey}-loading`);
const result: Ref<FacetSearchResult<SEARCH_DATA>> = vsfRef({ data: null, input: null }, `${ssrKey}-facets`);
const context = generateContext(factoryParams);
const error: Ref<UseFacetComposableErrors> = sharedRef({}, `useFacet-error-${id}`);
const error: Ref<UseFacetErrors> = sharedRef({}, `useFacet-error-${id}`);

const search = async (params?: AgnosticFacetSearchParams) => {
Logger.debug('useFacet.search', params);
Expand Down
4 changes: 2 additions & 2 deletions packages/core/core/src/factories/useProductFactory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CustomQuery, ProductsSearchParams, UseProduct, Context, FactoryParams, UseProductComposableErrors } from '../types';
import { CustomQuery, ProductsSearchParams, UseProduct, Context, FactoryParams, UseProductErrors } from '../types';
import { Ref, computed } from '@vue/composition-api';
import { sharedRef, Logger, generateContext } from '../utils';
export interface UseProductFactoryParams<PRODUCTS, PRODUCT_SEARCH_PARAMS extends ProductsSearchParams> extends FactoryParams {
Expand All @@ -12,7 +12,7 @@ export function useProductFactory<PRODUCTS, PRODUCT_SEARCH_PARAMS>(
const products: Ref<PRODUCTS> = sharedRef([], `useProduct-products-${id}`);
const loading = sharedRef(false, `useProduct-loading-${id}`);
const context = generateContext(factoryParams);
const error: Ref<UseProductComposableErrors> = sharedRef({}, `useProduct-error-${id}`);
const error: Ref<UseProductErrors> = sharedRef({}, `useProduct-error-${id}`);

const search = async (searchParams) => {
Logger.debug('useProduct.search', searchParams);
Expand Down
4 changes: 2 additions & 2 deletions packages/core/core/src/factories/useReviewFactory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Ref, computed } from '@vue/composition-api';
import { CustomQuery, UseReview, Context, FactoryParams, UseReviewComposableErrors } from '../types';
import { CustomQuery, UseReview, Context, FactoryParams, UseReviewErrors } from '../types';
import { sharedRef, Logger, generateContext } from '../utils';

export interface UseReviewFactoryParams<REVIEW, REVIEWS_SEARCH_PARAMS, REVIEW_ADD_PARAMS> extends FactoryParams {
Expand All @@ -13,7 +13,7 @@ export function useReviewFactory<REVIEW, REVIEWS_SEARCH_PARAMS, REVIEW_ADD_PARAM
return function useReview(id: string): UseReview<REVIEW, REVIEWS_SEARCH_PARAMS, REVIEW_ADD_PARAMS> {
const reviews: Ref<REVIEW> = sharedRef([], `useReviews-reviews-${id}`);
const loading: Ref<boolean> = sharedRef(false, `useReviews-loading-${id}`);
const error: Ref<UseReviewComposableErrors> = sharedRef({}, `useProduct-error-${id}`);
const error: Ref<UseReviewErrors> = sharedRef({}, `useProduct-error-${id}`);
const context = generateContext(factoryParams);

const search = async (searchParams): Promise<void> => {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/core/src/factories/useUserBillingFactory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Ref, unref, computed } from '@vue/composition-api';
import { UseUserBilling, Context, FactoryParams, UseUserBillingComposableErrors } from '../types';
import { UseUserBilling, Context, FactoryParams, UseUserBillingErrors } from '../types';
import { sharedRef, Logger, generateContext } from '../utils';

export interface UseUserBillingFactoryParams<USER_BILLING, USER_BILLING_ITEM> extends FactoryParams{
Expand Down Expand Up @@ -42,7 +42,7 @@ export const useUserBillingFactory = <USER_BILLING, USER_BILLING_ITEM>(
const loading: Ref<boolean> = sharedRef(false, 'useUserBilling-loading');
const billing: Ref<USER_BILLING> = sharedRef({}, 'useUserBilling-billing');
const context = generateContext(factoryParams);
const error: Ref<UseUserBillingComposableErrors> = sharedRef({}, 'useUserBilling-error');
const error: Ref<UseUserBillingErrors> = sharedRef({}, 'useUserBilling-error');

const readonlyBilling: Readonly<USER_BILLING> = unref(billing);

Expand Down
4 changes: 2 additions & 2 deletions packages/core/core/src/factories/useUserFactory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Ref, computed } from '@vue/composition-api';
import { UseUser, Context, FactoryParams, UseUserComposableErrors } from '../types';
import { UseUser, Context, FactoryParams, UseUserErrors } from '../types';
import { sharedRef, Logger, mask, generateContext } from '../utils';

export interface UseUserFactoryParams<USER, UPDATE_USER_PARAMS, REGISTER_USER_PARAMS> extends FactoryParams {
Expand All @@ -20,7 +20,7 @@ export const useUserFactory = <USER, UPDATE_USER_PARAMS, REGISTER_USER_PARAMS ex
const loading: Ref<boolean> = sharedRef(false, 'useUser-loading');
const isAuthenticated = computed(() => Boolean(user.value));
const context = generateContext(factoryParams);
const error: Ref<UseUserComposableErrors> = sharedRef({}, 'useUser-error');
const error: Ref<UseUserErrors> = sharedRef({}, 'useUser-error');

const setUser = (newUser: USER) => {
user.value = newUser;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/core/src/factories/useUserOrdersFactory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Ref, computed } from '@vue/composition-api';
import { CustomQuery, UseUserOrders, Context, FactoryParams, UseUserOrdersComposableErrors } from '../types';
import { CustomQuery, UseUserOrders, Context, FactoryParams, UseUserOrdersErrors } from '../types';
import { sharedRef, Logger, generateContext } from '../utils';

export interface UseUserOrdersFactoryParams<ORDERS, ORDER_SEARCH_PARAMS> extends FactoryParams {
Expand All @@ -11,7 +11,7 @@ export function useUserOrdersFactory<ORDERS, ORDER_SEARCH_PARAMS>(factoryParams:
const orders: Ref<ORDERS> = sharedRef([], 'useUserOrders-orders');
const loading: Ref<boolean> = sharedRef(false, 'useUserOrders-loading');
const context = generateContext(factoryParams);
const error: Ref<UseUserOrdersComposableErrors> = sharedRef({}, 'useUserOrders-error');
const error: Ref<UseUserOrdersErrors> = sharedRef({}, 'useUserOrders-error');

const search = async (searchParams): Promise<void> => {
Logger.debug('useUserOrders.search', searchParams);
Expand Down
4 changes: 2 additions & 2 deletions packages/core/core/src/factories/useUserShippingFactory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Ref, unref, computed } from '@vue/composition-api';
import { UseUserShipping, Context, FactoryParams, UseUserShippingComposableErrors } from '../types';
import { UseUserShipping, Context, FactoryParams, UseUserShippingErrors } from '../types';
import { sharedRef, Logger, mask, generateContext } from '../utils';

export interface UseUserShippingFactoryParams<USER_SHIPPING, USER_SHIPPING_ITEM> extends FactoryParams {
Expand Down Expand Up @@ -43,7 +43,7 @@ export const useUserShippingFactory = <USER_SHIPPING, USER_SHIPPING_ITEM>(
const shipping: Ref<USER_SHIPPING> = sharedRef({}, 'useUserShipping-shipping');
const context = generateContext(factoryParams);
const readonlyShipping: Readonly<USER_SHIPPING> = unref(shipping);
const error: Ref<UseUserShippingComposableErrors> = sharedRef({}, 'useUserShipping-error');
const error: Ref<UseUserShippingErrors> = sharedRef({}, 'useUserShipping-error');

const addAddress = async ({ address }) => {
Logger.debug('useUserShipping.addAddress', mask(address));
Expand Down
4 changes: 2 additions & 2 deletions packages/core/core/src/factories/useWishlistFactory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UseWishlist, CustomQuery, Context, FactoryParams, UseWishlistComposableErrors } from '../types';
import { UseWishlist, CustomQuery, Context, FactoryParams, UseWishlistErrors } from '../types';
import { Ref, computed } from '@vue/composition-api';
import { sharedRef, Logger, generateContext } from '../utils';

Expand Down Expand Up @@ -29,7 +29,7 @@ export const useWishlistFactory = <WISHLIST, WISHLIST_ITEM, PRODUCT>(
const loading: Ref<boolean> = sharedRef<boolean>(false, 'useWishlist-loading');
const wishlist: Ref<WISHLIST> = sharedRef(null, 'useWishlist-wishlist');
const context = generateContext(factoryParams);
const error: Ref<UseWishlistComposableErrors> = sharedRef({}, 'useWishlist-error');
const error: Ref<UseWishlistErrors> = sharedRef({}, 'useWishlist-error');

const setWishlist = (newWishlist: WISHLIST) => {
wishlist.value = newWishlist;
Expand Down
44 changes: 22 additions & 22 deletions packages/core/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ export interface ProductsSearchParams {
filters?: any;
[x: string]: any;
}
export interface UseProductComposableErrors {
export interface UseProductErrors {
search?: Error;
}
export interface UseProduct<PRODUCTS, PRODUCT_SEARCH_PARAMS> {
products: ComputedProperty<PRODUCTS>;
loading: ComputedProperty<boolean>;
error: ComputedProperty<UseProductComposableErrors>;
error: ComputedProperty<UseProductErrors>;
search(params: ComposableFunctionArgs<PRODUCT_SEARCH_PARAMS>): Promise<void>;
[x: string]: any;
}
Expand All @@ -43,7 +43,7 @@ export interface UseUserLoginParams {
password: string;
[x: string]: any;
}
export interface UseUserComposableErrors {
export interface UseUserErrors {
updateUser?: Error;
register?: Error;
login?: Error;
Expand All @@ -66,7 +66,7 @@ export interface UseUser
load: () => Promise<void>;
isAuthenticated: Ref<boolean>;
loading: ComputedProperty<boolean>;
error: ComputedProperty<UseUserComposableErrors>;
error: ComputedProperty<UseUserErrors>;
}

export interface UseUserOrdersSearchParams {
Expand All @@ -75,14 +75,14 @@ export interface UseUserOrdersSearchParams {
perPage?: number;
[x: string]: any;
}
export interface UseUserOrdersComposableErrors {
export interface UseUserOrdersErrors {
search?: Error;
}
export interface UseUserOrders<ORDERS, ORDER_SEARCH_PARAMS> {
orders: ComputedProperty<ORDERS>;
search(params: ComposableFunctionArgs<ORDER_SEARCH_PARAMS>): Promise<void>;
loading: ComputedProperty<boolean>;
error: ComputedProperty<UseUserOrdersComposableErrors>;
error: ComputedProperty<UseUserOrdersErrors>;
}

export interface UseUserAddress<ADDRESS> {
Expand All @@ -94,7 +94,7 @@ export interface UseUserAddress<ADDRESS> {
searchAddresses: (params?: { [x: string]: any }) => Promise<void>;
loading: ComputedProperty<boolean>;
}
export interface UseUserShippingComposableErrors {
export interface UseUserShippingErrors {
addAddress?: Error;
deleteAddress?: Error;
updateAddress?: Error;
Expand All @@ -109,7 +109,7 @@ export interface UseUserShipping<USER_SHIPPING, USER_SHIPPING_ITEM> {
load: () => Promise<void>;
setDefaultAddress: (params: { address: USER_SHIPPING_ITEM }) => Promise<void>;
loading: ComputedProperty<boolean>;
error: ComputedProperty<UseUserShippingComposableErrors>;
error: ComputedProperty<UseUserShippingErrors>;
}

export interface UserShippingGetters<USER_SHIPPING, USER_SHIPPING_ITEM> {
Expand All @@ -133,7 +133,7 @@ export interface UserShippingGetters<USER_SHIPPING, USER_SHIPPING_ITEM> {
isDefault: (address: USER_SHIPPING_ITEM) => boolean;
}

export interface UseUserBillingComposableErrors {
export interface UseUserBillingErrors {
addAddress?: Error;
deleteAddress?: Error;
updateAddress?: Error;
Expand All @@ -148,7 +148,7 @@ export interface UseUserBilling<USER_BILLING, USER_BILLING_ITEM> {
load: () => Promise<void>;
setDefaultAddress: (params: { address: USER_BILLING_ITEM }) => Promise<void>;
loading: ComputedProperty<boolean>;
error: ComputedProperty<UseUserBillingComposableErrors>;
error: ComputedProperty<UseUserBillingErrors>;
}

export interface UserBillingGetters<USER_BILLING, USER_BILLING_ITEM> {
Expand All @@ -172,17 +172,17 @@ export interface UserBillingGetters<USER_BILLING, USER_BILLING_ITEM> {
isDefault: (address: USER_BILLING_ITEM) => boolean;
}

export interface UseCategoryComposableErrors {
export interface UseCategoryErrors {
search?: Error;
}
export interface UseCategory<CATEGORY, CATEGORY_SEARCH_PARAMS> {
categories: ComputedProperty<CATEGORY[]>;
search(params: ComposableFunctionArgs<CATEGORY_SEARCH_PARAMS>): Promise<void>;
loading: ComputedProperty<boolean>;
error: ComputedProperty<UseCategoryComposableErrors>;
error: ComputedProperty<UseCategoryErrors>;
}

export interface UseCartComposableErrors {
export interface UseCartErrors {
addItem?: Error;
removeItem?: Error;
updateItemQty?: Error;
Expand All @@ -209,10 +209,10 @@ export interface UseCart
removeCoupon(params: { coupon: COUPON; customQuery?: CustomQuery }): Promise<void>;
load(): Promise<void>;
load(params: { customQuery?: CustomQuery }): Promise<void>;
error: ComputedProperty<UseCartComposableErrors>;
error: ComputedProperty<UseCartErrors>;
loading: ComputedProperty<boolean>;
}
export interface UseWishlistComposableErrors {
export interface UseWishlistErrors {
addItem?: Error;
removeItem?: Error;
load?: Error;
Expand All @@ -233,7 +233,7 @@ export interface UseWishlist
clear(): Promise<void>;
setWishlist: (wishlist: WISHLIST) => void;
isOnWishlist({ product: PRODUCT }): boolean;
error: ComputedProperty<UseWishlistComposableErrors>;
error: ComputedProperty<UseWishlistErrors>;
}

export interface UseCompare<PRODUCT> {
Expand Down Expand Up @@ -265,35 +265,35 @@ export interface UseCheckout
placeOrder: PLACE_ORDER;
loading: ComputedProperty<boolean>;
}
export interface UseReviewComposableErrors {
export interface UseReviewErrors {
search?: Error;
addReview?: Error;
}
export interface UseReview<REVIEW, REVIEWS_SEARCH_PARAMS, REVIEW_ADD_PARAMS> {
search(params: ComposableFunctionArgs<REVIEWS_SEARCH_PARAMS>): Promise<void>;
addReview(params: ComposableFunctionArgs<REVIEW_ADD_PARAMS>): Promise<void>;
error: ComputedProperty<UseReviewComposableErrors>;
error: ComputedProperty<UseReviewErrors>;
reviews: ComputedProperty<REVIEW>;
loading: ComputedProperty<boolean>;
[x: string]: any;
}
export interface UseFacetComposableErrors {
export interface UseFacetErrors {
search?: Error;
}
export interface UseFacet<SEARCH_DATA> {
result: ComputedProperty<FacetSearchResult<SEARCH_DATA>>;
loading: ComputedProperty<boolean>;
search: (params?: AgnosticFacetSearchParams) => Promise<void>;
error: ComputedProperty<UseFacetComposableErrors>;
error: ComputedProperty<UseFacetErrors>;
}
export interface UseContentComposableErrors {
export interface UseContentErrors {
search?: Error;
}
export interface UseContent<CONTENT, CONTENT_SEARCH_PARAMS> {
search: (params: CONTENT_SEARCH_PARAMS) => Promise<void>;
content: ComputedProperty<CONTENT>;
loading: ComputedProperty<boolean>;
error: ComputedProperty<UseContentComposableErrors>;
error: ComputedProperty<UseContentErrors>;
}

export interface RenderComponent {
Expand Down
6 changes: 3 additions & 3 deletions packages/core/docs/general/error-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default {

There is a dedicated interface for each factory, example one for the `useCart`:
```ts
export interface UseCartComposableErrors {
export interface UseCartErrors {
addItem?: Error;
removeItem?: Error;
updateItemQty?: Error;
Expand All @@ -52,9 +52,9 @@ watch(error, (error, prevError) => {
## Where can I find interface of the error property from a certain factory?
When you are writing a code inside a script part of the Vue's component, your IDE should give you hints dedicated for each type of composable. That's why you probably do not need to check these interfaces in the core's code.

However, if somewhy you still want to do that, you could find them inside [`packages/core/core/src/types.ts`](https://github.com/vuestorefront/vue-storefront/blob/next/packages/core/core/src/types.ts). Just search for `UseCartComposableErrors` with your IDE inside.
However, if somewhy you still want to do that, you could find them inside [`packages/core/core/src/types.ts`](https://github.com/vuestorefront/vue-storefront/blob/next/packages/core/core/src/types.ts). Just search for `UseCartErrors` with your IDE inside.

Feel free to replace `UseCart` part with other composable name - `UseFacetComposableErrors`, `UseWishlistComposableErrors`, `UseProductComposableErrors` etc.
Feel free to replace `UseCart` part with other composable name - `UseFacetErrors`, `UseWishlistErrors`, `UseProductErrors` etc.

## Where does error come from?
Inside each factory's async method we are clearing the current error before integration's method call and setting it in catch block.
Expand Down

0 comments on commit 0805dac

Please sign in to comment.