Skip to content

Commit

Permalink
refactor: remove UseContext return type duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
sethidden committed May 26, 2022
1 parent 7b8bedc commit 9b44e15
Show file tree
Hide file tree
Showing 12 changed files with 25 additions and 45 deletions.
4 changes: 3 additions & 1 deletion packages/theme/composables/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DeepReadonly, Ref } from '@nuxtjs/composition-api';
import type { DeepReadonly, Ref, useContext } from '@nuxtjs/composition-api';
import type {
AvailableStoresQuery,
CountriesListQuery,
Expand Down Expand Up @@ -233,3 +233,5 @@ export interface AgnosticReviewMetadata {
id: string;
}[];
}

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

type Context = ReturnType<typeof useContext>;

export const updateSubscriptionCommand = {
execute: async (context: Context, params: UseNewsletterUpdateSubscriptionParams): Promise<SubscriptionStatusesEnum | null> => {
execute: async (context: UseContextReturn, params: UseNewsletterUpdateSubscriptionParams): Promise<SubscriptionStatusesEnum | null> => {
const { data } = await context.app.$vsf.$magento.api.subscribeEmailToNewsletter({
email: params.email,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { useContext } from '@nuxtjs/composition-api';
import { UseContextReturn } from '~/composables/types';
import type { GetProductSearchParams } from '~/modules/catalog/product/types';

type Context = ReturnType<typeof useContext>;

export const getProductDetailsCommand = {
execute: async (context: Context, searchParams: GetProductSearchParams, customQuery = { productDetail: 'productDetail' }) => {
execute: async (context: UseContextReturn, searchParams: GetProductSearchParams, customQuery = { productDetail: 'productDetail' }) => {
const { data } = await context.app.$vsf.$magento.api.productDetail(searchParams, customQuery);

return data?.products ?? null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { useContext } from '@nuxtjs/composition-api';
import { UseContextReturn } from '~/composables/types';
import type { GetProductSearchParams } from '~/modules/catalog/product/types';

type Context = ReturnType<typeof useContext>;

export const getProductListCommand = {
execute: async (context: Context, searchParams: GetProductSearchParams, customQuery = { products: 'products' }) => {
execute: async (context: UseContextReturn, searchParams: GetProductSearchParams, customQuery = { products: 'products' }) => {
const { data } = await context.app.$vsf.$magento.api.products(searchParams, customQuery);

return data?.products ?? null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { useContext } from '@nuxtjs/composition-api';
import { UseContextReturn } from '~/composables/types';
import type { PlaceOrderOutput } from '~/modules/GraphQL/types';

type Context = ReturnType<typeof useContext>;

export const placeOrderCommand = {
execute: async (context: Context, cartId: string): Promise<PlaceOrderOutput | null> => {
execute: async (context: UseContextReturn, cartId: string): Promise<PlaceOrderOutput | null> => {
const { data } = await context.app.$vsf.$magento.api.placeOrder({ cart_id: cartId });

return data?.placeOrder ?? null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { useContext } from '@nuxtjs/composition-api';
import { UseContextReturn } from '~/composables/types';
import type { AvailablePaymentMethod } from '~/modules/GraphQL/types';

type Context = ReturnType<typeof useContext>;

export const getAvailablePaymentMethodsCommand = {
execute: async (context: Context, cartId: string): Promise<AvailablePaymentMethod[]> => {
execute: async (context: UseContextReturn, cartId: string): Promise<AvailablePaymentMethod[]> => {
const { data } = await context.app.$vsf.$magento.api.getAvailablePaymentMethods({ cartId });

return data?.cart?.available_payment_methods ?? [];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import type { useContext } from '@nuxtjs/composition-api';
import { UseContextReturn } from '~/composables/types';
import type { AvailablePaymentMethod } from '~/modules/GraphQL/types';
import type { PaymentMethodParams } from '../usePaymentProvider';

type Context = ReturnType<typeof useContext>;

export const setPaymentMethodOnCartCommand = {
execute: async (context: Context, params: PaymentMethodParams): Promise<AvailablePaymentMethod[]> => {
execute: async (context: UseContextReturn, params: PaymentMethodParams): Promise<AvailablePaymentMethod[]> => {
const { data } = await context.app.$vsf.$magento.api.setPaymentMethodOnCart(params);

return data?.setPaymentMethodOnCart?.cart.available_payment_methods ?? [];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { useContext } from '@nuxtjs/composition-api';
import { UseContextReturn } from '~/composables/types';
import type { SetShippingMethodsOnCartInput, Cart } from '~/modules/GraphQL/types';

type Context = ReturnType<typeof useContext>;

export const setShippingMethodsOnCartCommand = {
execute: async (context: Context, shippingMethodParams: SetShippingMethodsOnCartInput): Promise<Cart | null> => {
execute: async (context: UseContextReturn, shippingMethodParams: SetShippingMethodsOnCartInput): Promise<Cart | null> => {
const { data } = await context.app.$vsf.$magento.api.setShippingMethodsOnCart(shippingMethodParams);

// TODO: Find out why 'Cart' doesn't match the type of the response data.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import type { useContext } from '@nuxtjs/composition-api';
import { UseContextReturn } from '~/composables/types';
import { Logger } from '~/helpers/logger';
import { SetGuestEmailOnCartInput } from '~/modules/GraphQL/types';

type Context = ReturnType<typeof useContext>;

export const attachToCartCommand = {
execute: async (context: Context, params): Promise<void> => {
execute: async (context: UseContextReturn, params): Promise<void> => {
Logger.debug('[Magento]: Attach guest cart to user');

const emailOnCartInput: SetGuestEmailOnCartInput = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { useContext } from '@nuxtjs/composition-api';
import { UseContextReturn } from '~/composables/types';
import { CustomerAddressInput } from '~/modules/GraphQL/types';

type Context = ReturnType<typeof useContext>;

export const createCustomerAddressCommand = {
execute: async (context: Context, params: CustomerAddressInput) => {
execute: async (context: UseContextReturn, params: CustomerAddressInput) => {
const { data } = await context.app.$vsf.$magento.api.createCustomerAddress(params);

return data?.createCustomerAddress ?? {};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { useContext } from '@nuxtjs/composition-api';
import { UseContextReturn } from '~/composables/types';
import { CustomerAddress } from '~/modules/GraphQL/types';

type Context = ReturnType<typeof useContext>;

export const deleteCustomerAddressCommand = {
execute: async (context: Context, address: CustomerAddress) => {
execute: async (context: UseContextReturn, address: CustomerAddress) => {
const { data } = await context.app.$vsf.$magento.api.deleteCustomerAddress(address.id);

return data?.deleteCustomerAddress ?? {};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { useContext } from '@nuxtjs/composition-api';
import { UseContextReturn } from '~/composables/types';
import { CustomerAddressInput } from '~/modules/GraphQL/types';

type Context = ReturnType<typeof useContext>;

export const updateCustomerAddressCommand = {
execute: async (context: Context, params: {
execute: async (context: UseContextReturn, params: {
addressId: number;
input: CustomerAddressInput;
}) => {
Expand Down

0 comments on commit 9b44e15

Please sign in to comment.