Skip to content

Commit

Permalink
refactor: double check types in composables
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszherba committed Jun 1, 2022
1 parent 7d2b733 commit a71d833
Show file tree
Hide file tree
Showing 34 changed files with 155 additions and 310 deletions.
200 changes: 3 additions & 197 deletions packages/theme/composables/types.ts
Original file line number Diff line number Diff line change
@@ -1,222 +1,28 @@
import type { DeepReadonly, Ref, useContext } from '@nuxtjs/composition-api';
import type {
AvailableStoresQuery,
CountriesListQuery,
ProductReviewRatingsMetadataQuery,
} from '~/modules/GraphQL/types';

export interface Context<CLIENT = any, CONFIG = any, API = any> {
[x: string]: IntegrationContext<CLIENT, CONFIG, API> | any;
}

export interface IntegrationContext<CLIENT = any, CONFIG = any, API = any> {
client: CLIENT;
config: CONFIG;
api: API;
[x: string]: any;
}

export type ContextedPlatformApi<T extends PlatformApi> = {
[P in keyof T]: T[P] extends (context: Context, ...arg: infer X) => Promise<any>
? (...arg: X) => Promise<any>
: never
};

export type PlatformApi = {
[functionName: string]: (context: Context, ...args: any[]) => Promise<any>
};

export interface Composable<API extends PlatformApi> {
api?: ContextedPlatformApi<API>
}

export type ComputedProperty<T> = DeepReadonly<Ref<T>>;

export interface ProductsSearchParams {
perPage?: number;
page?: number;
sort?: any;
term?: any;
filters?: any;
[x: string]: any;
}

export declare type CustomerProductReviewParams = {
pageSize: number;
currentPage: number;
};
import type { CustomQuery } from '~/types/core';

export declare type AvailableStores = AvailableStoresQuery['availableStores'];
export declare type CustomQuery = Record<string, string>;
export declare type Filter = Record<string, any>;
export declare type Countries = CountriesListQuery['countries'][0];
export declare type ReviewMetadata = ProductReviewRatingsMetadataQuery['productReviewRatingsMetadata']['items'][0];

export declare type ComposableFunctionArgs<T> = T & {
customQuery?: CustomQuery;
};

export interface AgnosticPrice {
regular: number | null;
special?: number | null;
maximum?: number | null;
final?: number | null;
}

export interface AgnosticMediaGalleryItem {
small: string;
normal: string;
big: string;
}

export interface AgnosticAttribute {
name?: string;
value: string | Record<string, any>;
label: string;
}

export interface AgnosticBreadcrumb {
text: string;
link: string;
}

export interface AgnosticTotals {
export interface Totals {
total: number;
subtotal: number;
special?: number;
[x: string]: unknown;
}

export interface AgnosticCoupon {
id: string;
name: string;
code: string;
value: number;
}

export interface AgnosticDiscount {
id: string;
name: string;
description: string;
value: number;
code?: string;
}

export interface AgnosticCategoryTree {
label: string;
slug?: string;
items: AgnosticCategoryTree[];
isCurrent: boolean;
count?: number;
[x: string]: unknown;
}

export interface AgnosticFilter {
id: string;
label: string;
values: {
id: string;
isSlected?: boolean;
count?: number;
label: string;
value: string;
}[]
}

export interface AgnosticProductReview {
id: string;
author: string;
date: Date;
message: string | null;
rating: number | null;
}

export interface AgnosticLocale {
code: string;
label: string;
[x: string]: unknown;
}

export interface AgnosticCountry {
code: string;
label: string;
[x: string]: unknown;
}

export interface AgnosticCurrency {
code: string;
label: string;
prefixSign: boolean;
sign: string;
[x: string]: unknown;
}

export interface AgnosticSortByOption {
label: string;
value: string;
[x: string]: unknown;
}

export interface AgnosticRateCount {
rate: number;
count: number;
}

export interface FacetInterface {
type: string;
id: string;
value: any;
attrName?: string;
count?: number;
selected?: boolean;
metadata?: any;
}

export interface GroupedFacetInterface {
id: string;
label: string;
count?: number;
options: FacetInterface[];
}

export interface AgnosticPagination {
export interface Pagination {
currentPage?: number;
totalPages?: number;
totalItems?: number;
itemsPerPage?: number;
pageOptions?: number[];
}

export interface AgnosticAddress {
addressLine1: string;
addressLine2: string;
[x: string]: unknown;
}

export interface AgnosticGeoLocation {
type: string;
coordinates?: unknown;
[x: string]: unknown;
}

export interface AgnosticStore {
name: string;
id: string;
description?: string;
locales?: AgnosticLocale[];
currencies?: AgnosticCurrency[]
address?: AgnosticAddress;
geoLocation?: AgnosticGeoLocation;
[x: string]: unknown;
}

export interface AgnosticReviewMetadata {
id: string;
name: string;
values: {
label: string | number;
id: string;
}[];
}

export type UseContextReturn = ReturnType<typeof useContext>;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UseContextReturn } from '~/composables/types';
import { UseContextReturn } from '~/types/core';
import type { SubscriptionStatusesEnum } from '~/modules/GraphQL/types';
import type { UseNewsletterUpdateSubscriptionParams } from '../useNewsletter';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { ComposableFunctionArgs, CustomerProductReviewParams } from '~/composables/types';
import { ComposableFunctionArgs } from '~/composables/types';
import { VsfContext } from '~/composables/context';
import { Logger } from '~/helpers/logger';

export declare type CustomerProductReviewParams = {
pageSize: number;
currentPage: number;
};

export const loadCustomerReviewsCommand = {
execute: async (context: VsfContext, params?: ComposableFunctionArgs<CustomerProductReviewParams>) => {
Logger.debug('[Magento] load customer review based on:', { params });
Expand Down
2 changes: 1 addition & 1 deletion packages/theme/composables/useUiHelpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useRoute, useRouter } from '@nuxtjs/composition-api';
import type { FacetInterface } from '~/composables/types';
import type { CategoryTree } from '~/modules/GraphQL/types';
import type { UseUiHelpersInterface } from '~/composables';
import type { Params, QueryParams, FilterParams } from './Params';
import type { FacetInterface } from '~/modules/catalog/category/types';

const nonFilters = new Set(['page', 'sort', 'term', 'itemsPerPage']);

Expand Down
2 changes: 1 addition & 1 deletion packages/theme/composables/useUiHelpers/useUiHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { FacetInterface } from '~/composables/types';
import type { CategoryTree } from '~/modules/GraphQL/types';
import type { FilterParams, Params } from './Params';
import type { FacetInterface } from '~/modules/catalog/category/types';

/**
* Data and methods returned from the {@link useUiHelpers} composable.
Expand Down
2 changes: 1 addition & 1 deletion packages/theme/composables/useUrlResolver/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from '@nuxtjs/composition-api';
import { Logger } from '~/helpers/logger';
import type { EntityUrl } from '~/modules/GraphQL/types';
import type { UseUrlResolverErrors, UseUrlResolverInterface } from './UseUrlResolver';
import type { UseUrlResolverErrors, UseUrlResolverInterface } from '~/composables';

/**
* Allows searching the resolver for current
Expand Down
5 changes: 2 additions & 3 deletions packages/theme/getters/_utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { Product } from '~/modules/catalog/product/types';
import { AgnosticAttribute } from '~/composables/types';
import type { Product, ProductAttribute } from '~/modules/catalog/product/types';

export const getAttributeValue = (attribute) => attribute.values;

export const formatAttributeList = (attributes): AgnosticAttribute[] => attributes.map((attr) => {
export const formatAttributeList = (attributes): ProductAttribute[] => attributes.map((attr) => {
const attrValue = getAttributeValue(attr);
return {
name: attr.attribute_code,
Expand Down
20 changes: 16 additions & 4 deletions packages/theme/getters/reviewGetters.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
/* istanbul ignore file */
import { AgnosticReviewMetadata, AgnosticRateCount } from '~/composables/types';
import {
ProductInterface,
ProductReview,
ProductReviewRatingMetadata,
ProductReviews,
} from '~/modules/GraphQL/types';

export interface RateCount {
rate: number;
count: number;
}

export interface ReviewMetadata {
id: string;
name: string;
values: {
label: string | number;
id: string;
}[];
}

export const getItems = (review): ProductReview[] => review?.reviews?.items || [];

export const getReviewId = (item: ProductReview): string => `${item.nickname}_${item.created_at}_${item.average_rating}`;
Expand All @@ -26,11 +38,11 @@ export const getTotalReviews = (review: ProductInterface): number => review?.rev

export const getAverageRating = (review: ProductInterface): number => ((review?.reviews?.items?.reduce((acc, curr) => Number.parseInt(`${acc}`, 10) + getReviewRating(curr), 0)) ?? 0) / (review?.review_count || 1) || 0;

export const getRatesCount = (_review: ProductReviews): AgnosticRateCount[] => [];
export const getRatesCount = (_review: ProductReviews): RateCount[] => [];

export const getReviewsPage = (review: ProductInterface): number => review?.reviews.page_info?.page_size || 0;

export const getReviewMetadata = (reviewData: ProductReviewRatingMetadata[]): AgnosticReviewMetadata[] => reviewData?.map((m) => ({
export const getReviewMetadata = (reviewData: ProductReviewRatingMetadata[]): ReviewMetadata[] => reviewData?.map((m) => ({
...m,
values: m.values.map((v) => ({
label: (Number.parseInt(v.value, 10) || v.value),
Expand Down
39 changes: 28 additions & 11 deletions packages/theme/getters/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
import {
AgnosticAttribute, AgnosticPrice, AgnosticTotals,
AgnosticCoupon, AgnosticDiscount,
import type {
Totals,
} from '~/composables/types';

import type { ProductAttribute } from '~/modules/catalog/product/types';
import type { Price } from '~/modules/catalog/types';

export interface Coupon {
id: string;
name: string;
code: string;
value: number;
}

export interface CartDiscount {
id: string;
name: string;
description: string;
value: number;
code?: string;
}

export interface UserBillingGetters<USER_BILLING, USER_BILLING_ITEM> {
getAddresses: (billing: USER_BILLING, criteria?: Record<string, any>) => USER_BILLING_ITEM[];
getDefault: (billing: USER_BILLING) => USER_BILLING_ITEM;
Expand Down Expand Up @@ -62,10 +79,10 @@ export interface WishlistGetters<WISHLIST, WISHLIST_ITEM> {
getItems: (wishlist: WISHLIST) => WISHLIST_ITEM[];
getItemName: (wishlistItem: WISHLIST_ITEM) => string;
getItemImage: (wishlistItem: WISHLIST_ITEM) => string;
getItemPrice: (wishlistItem: WISHLIST_ITEM) => AgnosticPrice;
getItemAttributes: (wishlistItem: WISHLIST_ITEM, filters?: Array<string>) => Record<string, AgnosticAttribute | string>;
getItemPrice: (wishlistItem: WISHLIST_ITEM) => Price;
getItemAttributes: (wishlistItem: WISHLIST_ITEM, filters?: Array<string>) => Record<string, ProductAttribute | string>;
getItemSku: (wishlistItem: WISHLIST_ITEM) => string;
getTotals: (wishlist: WISHLIST) => AgnosticTotals;
getTotals: (wishlist: WISHLIST) => Totals;
getTotalItems: (wishlist: WISHLIST) => number;
[getterName: string]: (element: any, options?: any) => unknown;
}
Expand All @@ -74,15 +91,15 @@ export interface CartGetters<CART, CART_ITEM> {
getItems: (cart: CART) => CART_ITEM[];
getItemName: (cartItem: CART_ITEM) => string;
getItemImage: (cartItem: CART_ITEM) => string;
getItemPrice: (cartItem: CART_ITEM) => AgnosticPrice;
getItemPrice: (cartItem: CART_ITEM) => Price;
getItemQty: (cartItem: CART_ITEM) => number;
getItemAttributes: (cartItem: CART_ITEM, filters?: Array<string>) => Record<string, AgnosticAttribute | string>;
getItemAttributes: (cartItem: CART_ITEM, filters?: Array<string>) => Record<string, ProductAttribute | string>;
getItemSku: (cartItem: CART_ITEM) => string;
getTotals: (cart: CART) => AgnosticTotals;
getTotals: (cart: CART) => Totals;
getShippingPrice: (cart: CART) => number;
getTotalItems: (cart: CART) => number;
// @deprecated - use getDiscounts instead
getCoupons: (cart: CART) => AgnosticCoupon[];
getDiscounts: (cart: CART) => AgnosticDiscount[];
getCoupons: (cart: CART) => Coupon[];
getDiscounts: (cart: CART) => CartDiscount[];
[getterName: string]: (element: any, options?: any) => unknown;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import {
import { SfBreadcrumbs } from '@storefront-ui/vue';
import { useUiHelpers } from '~/composables';
import { useTraverseCategory } from '~/modules/catalog/category/helpers/useTraverseCategory';
type Breadcrumb = { text: string, link: string };
import { Breadcrumb } from '~/modules/catalog/types';
export default defineComponent({
components: { SfBreadcrumbs },
Expand Down
Loading

0 comments on commit a71d833

Please sign in to comment.