From 0c6ea16ced95cd4ec25539ecf3116757db9f7ada Mon Sep 17 00:00:00 2001 From: cond0r <1243434+cond0r@users.noreply.github.com> Date: Fri, 22 Jul 2022 14:18:11 +0300 Subject: [PATCH] Update utils structure --- .../src/api/operations/get-site-info.ts | 6 ++- .../src/api/utils/fetch-graphql-api.ts | 2 +- packages/shopify/src/cart/use-add-item.tsx | 18 ++++---- packages/shopify/src/cart/use-cart.tsx | 10 ++-- packages/shopify/src/cart/use-update-item.tsx | 26 ++++++----- packages/shopify/src/fetcher.ts | 2 +- packages/shopify/src/product/use-search.tsx | 6 +-- packages/shopify/src/utils/cart-create.ts | 46 ------------------- packages/shopify/src/utils/get-brands.ts | 46 ------------------- packages/shopify/src/utils/get-categories.ts | 32 ------------- .../src/utils/handle-account-activation.ts | 28 ----------- packages/shopify/src/utils/handle-login.ts | 36 +++++++++++++-- 12 files changed, 69 insertions(+), 189 deletions(-) delete mode 100644 packages/shopify/src/utils/cart-create.ts delete mode 100644 packages/shopify/src/utils/get-brands.ts delete mode 100644 packages/shopify/src/utils/get-categories.ts delete mode 100644 packages/shopify/src/utils/handle-account-activation.ts diff --git a/packages/shopify/src/api/operations/get-site-info.ts b/packages/shopify/src/api/operations/get-site-info.ts index 7c09bc47c..a6fdf12cf 100644 --- a/packages/shopify/src/api/operations/get-site-info.ts +++ b/packages/shopify/src/api/operations/get-site-info.ts @@ -2,9 +2,11 @@ import type { OperationContext, OperationOptions, } from '@vercel/commerce/api/operations' -import { GetSiteInfoQueryVariables } from '../../../schema' + import type { ShopifyConfig, Provider } from '..' -import { GetSiteInfoOperation } from '../../types/site' +import type { GetSiteInfoQueryVariables } from '../../../schema' +import type { GetSiteInfoOperation } from '../../types/site' + import { getBrands, getCategories } from '../../utils' export const getSiteInfoQuery = /* GraphQL */ ` diff --git a/packages/shopify/src/api/utils/fetch-graphql-api.ts b/packages/shopify/src/api/utils/fetch-graphql-api.ts index e8fe8bda3..30bfbccb0 100644 --- a/packages/shopify/src/api/utils/fetch-graphql-api.ts +++ b/packages/shopify/src/api/utils/fetch-graphql-api.ts @@ -41,7 +41,7 @@ const fetchGraphqlApi: GraphQLFetcher = async ( throw getError( [ { - message: `${err} \n Most likely related to an unexpected output. e.g the store might be protected with password or not available.`, + message: `${err} \n Most likely related to an unexpected output. e.g the store might be not available.`, }, ], 500 diff --git a/packages/shopify/src/cart/use-add-item.tsx b/packages/shopify/src/cart/use-add-item.tsx index 53046f724..b075baebd 100644 --- a/packages/shopify/src/cart/use-add-item.tsx +++ b/packages/shopify/src/cart/use-add-item.tsx @@ -1,23 +1,21 @@ import { useCallback } from 'react' -import type { MutationHook } from '@vercel/commerce/utils/types' import { CommerceError } from '@vercel/commerce/utils/errors' import useAddItem, { UseAddItem } from '@vercel/commerce/cart/use-add-item' -import type { AddItemHook } from '../types/cart' + import useCart from './use-cart' -import { - cartLinesAddMutation, - getCartId, - cartCreate, - normalizeCart, - throwUserErrors, -} from '../utils' +import type { AddItemHook } from '../types/cart' +import type { MutationHook } from '@vercel/commerce/utils/types' -import { +import type { CartLinesAddMutation, CartLinesAddMutationVariables, } from '../../schema' +import { getCartId, cartCreate, normalizeCart, throwUserErrors } from '../utils' + +import { cartLinesAddMutation } from '../utils/mutations/cart-mutations' + export default useAddItem as UseAddItem export const handler: MutationHook = { diff --git a/packages/shopify/src/cart/use-cart.tsx b/packages/shopify/src/cart/use-cart.tsx index f78d4ddbd..504932676 100644 --- a/packages/shopify/src/cart/use-cart.tsx +++ b/packages/shopify/src/cart/use-cart.tsx @@ -1,13 +1,13 @@ import { useMemo } from 'react' -import useCommerceCart, { UseCart } from '@vercel/commerce/cart/use-cart' -import { SWRHook } from '@vercel/commerce/utils/types' import Cookies from 'js-cookie' +import useCommerceCart, { UseCart } from '@vercel/commerce/cart/use-cart' -import { getCartQuery, normalizeCart, setCartUrlCookie } from '../utils' -import { GetCartHook } from '../types/cart' +import type { SWRHook } from '@vercel/commerce/utils/types' +import type { GetCartHook } from '../types/cart' +import type { GetCartQueryVariables, QueryRoot } from '../../schema' -import { GetCartQueryVariables, QueryRoot } from '../../schema' import { SHOPIFY_CART_ID_COOKIE } from '../const' +import { getCartQuery, normalizeCart, setCartUrlCookie } from '../utils' export default useCommerceCart as UseCart export const handler: SWRHook = { diff --git a/packages/shopify/src/cart/use-update-item.tsx b/packages/shopify/src/cart/use-update-item.tsx index b0a366350..2d3de6f72 100644 --- a/packages/shopify/src/cart/use-update-item.tsx +++ b/packages/shopify/src/cart/use-update-item.tsx @@ -1,26 +1,28 @@ import { useCallback } from 'react' import debounce from 'lodash.debounce' -import type { - HookFetcherContext, - MutationHookContext, -} from '@vercel/commerce/utils/types' import { ValidationError } from '@vercel/commerce/utils/errors' -import useUpdateItem, { - UseUpdateItem, -} from '@vercel/commerce/cart/use-update-item' import useCart from './use-cart' -import { handler as removeItemHandler } from './use-remove-item' - -import { getCartId, normalizeCart, cartLinesUpdateMutation } from '../utils' - -import type { UpdateItemHook, LineItem } from '../types/cart' import type { CartLinesUpdateMutation, CartLinesUpdateMutationVariables, } from '../../schema' +import type { + HookFetcherContext, + MutationHookContext, +} from '@vercel/commerce/utils/types' + +import type { UpdateItemHook, LineItem } from '../types/cart' + +import useUpdateItem, { + UseUpdateItem, +} from '@vercel/commerce/cart/use-update-item' + +import { handler as removeItemHandler } from './use-remove-item' +import { getCartId, normalizeCart, cartLinesUpdateMutation } from '../utils' + export type UpdateItemActionInput = T extends LineItem ? Partial : UpdateItemHook['actionInput'] diff --git a/packages/shopify/src/fetcher.ts b/packages/shopify/src/fetcher.ts index 64f492620..0b5363cd7 100644 --- a/packages/shopify/src/fetcher.ts +++ b/packages/shopify/src/fetcher.ts @@ -1,6 +1,6 @@ import { Fetcher } from '@vercel/commerce/utils/types' import { API_TOKEN, API_URL } from './const' -import { handleFetchResponse } from './utils' +import { handleFetchResponse } from './utils/handle-fetch-response' const fetcher: Fetcher = async ({ url = API_URL, diff --git a/packages/shopify/src/product/use-search.tsx b/packages/shopify/src/product/use-search.tsx index 582423e9f..ebdaaf249 100644 --- a/packages/shopify/src/product/use-search.tsx +++ b/packages/shopify/src/product/use-search.tsx @@ -1,7 +1,7 @@ import { SWRHook } from '@vercel/commerce/utils/types' import useSearch, { UseSearch } from '@vercel/commerce/product/use-search' -import { +import type { CollectionEdge, GetAllProductsQuery, GetProductsFromCollectionQueryVariables, @@ -9,6 +9,8 @@ import { ProductEdge, } from '../../schema' +import type { SearchProductsHook } from '../types/product' + import { getAllProductsQuery, getCollectionProductsQuery, @@ -16,8 +18,6 @@ import { normalizeProduct, } from '../utils' -import type { SearchProductsHook } from '../types/product' - export type SearchProductsInput = { search?: string categoryId?: number diff --git a/packages/shopify/src/utils/cart-create.ts b/packages/shopify/src/utils/cart-create.ts deleted file mode 100644 index e14d9bb1b..000000000 --- a/packages/shopify/src/utils/cart-create.ts +++ /dev/null @@ -1,46 +0,0 @@ -import Cookies from 'js-cookie' -import { SHOPIFY_CART_ID_COOKIE, SHOPIFY_COOKIE_EXPIRE } from '../const' -import { cartCreateMutation } from './mutations/cart-mutations' - -import { - CartCreateMutation, - CartCreateMutationVariables, - CartDetailsFragment, - CartLineInput, -} from '../../schema' - -import { setCartUrlCookie } from './helpers' -import { throwUserErrors } from './throw-user-errors' -import type { FetcherOptions } from '@vercel/commerce/utils/types' - -export const cartCreate = async ( - fetch: (options: FetcherOptions) => Promise, - lines?: Array | CartLineInput -): Promise => { - const { cartCreate } = await fetch< - CartCreateMutation, - CartCreateMutationVariables - >({ - query: cartCreateMutation, - variables: { - input: { - lines, - }, - }, - }) - - const cart = cartCreate?.cart - - throwUserErrors(cartCreate?.userErrors) - - if (cart?.id) { - const options = { - expires: SHOPIFY_COOKIE_EXPIRE, - } - Cookies.set(SHOPIFY_CART_ID_COOKIE, cart.id, options) - } - - setCartUrlCookie(cart?.checkoutUrl) - - return cart -} diff --git a/packages/shopify/src/utils/get-brands.ts b/packages/shopify/src/utils/get-brands.ts deleted file mode 100644 index 48da73a35..000000000 --- a/packages/shopify/src/utils/get-brands.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { ShopifyConfig } from '../api' - -import { - GetAllProductVendorsQuery, - GetAllProductVendorsQueryVariables, -} from '../../schema' - -import { getAllProductVendors } from './queries' - -export type Brand = { - entityId: string - name: string - path: string -} - -export type BrandEdge = { - node: Brand -} - -export type Brands = BrandEdge[] - -export const getBrands = async ( - config: ShopifyConfig -): Promise => { - const { data } = await config.fetch< - GetAllProductVendorsQuery, - GetAllProductVendorsQueryVariables - >(getAllProductVendors, { - variables: { - first: 250, - }, - }) - - let vendorsStrings = data.products.edges.map(({ node: { vendor } }) => vendor) - - return [...new Set(vendorsStrings)].map((v) => { - const id = v.replace(/\s+/g, '-').toLowerCase() - return { - node: { - entityId: id, - name: v, - path: `brands/${id}`, - }, - } - }) -} diff --git a/packages/shopify/src/utils/get-categories.ts b/packages/shopify/src/utils/get-categories.ts deleted file mode 100644 index 99ea65d46..000000000 --- a/packages/shopify/src/utils/get-categories.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { ShopifyConfig } from '../api' -import type { Category } from '../types/site' -import { CollectionEdge } from '../../schema' -import { normalizeCategory } from '../utils/normalize' -import { getSiteCollectionsQuery } from './queries' - -export const getCategories = async ({ - fetch, - locale, -}: ShopifyConfig): Promise => { - const { data } = await fetch( - getSiteCollectionsQuery, - { - variables: { - first: 250, - }, - }, - { - ...(locale && { - headers: { - 'Accept-Language': locale, - }, - }), - } - ) - - return ( - data.collections?.edges?.map(({ node }: CollectionEdge) => - normalizeCategory(node) - ) ?? [] - ) -} diff --git a/packages/shopify/src/utils/handle-account-activation.ts b/packages/shopify/src/utils/handle-account-activation.ts deleted file mode 100644 index f3ca6ef1b..000000000 --- a/packages/shopify/src/utils/handle-account-activation.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { FetcherOptions } from '@vercel/commerce/utils/types' -import { throwUserErrors } from './throw-user-errors' - -import { - MutationCustomerActivateArgs, - MutationCustomerActivateByUrlArgs, -} from '../../schema' -import { Mutation } from '../../schema' -import { customerActivateByUrlMutation } from './mutations' - -export const handleAccountActivation = async ( - fetch: (options: FetcherOptions) => Promise, - input: MutationCustomerActivateByUrlArgs -) => { - try { - const { customerActivateByUrl } = await fetch< - Mutation, - MutationCustomerActivateArgs - >({ - query: customerActivateByUrlMutation, - variables: { - input, - }, - }) - - throwUserErrors(customerActivateByUrl?.customerUserErrors) - } catch (error) {} -} diff --git a/packages/shopify/src/utils/handle-login.ts b/packages/shopify/src/utils/handle-login.ts index 2eb0c911c..426177a5e 100644 --- a/packages/shopify/src/utils/handle-login.ts +++ b/packages/shopify/src/utils/handle-login.ts @@ -1,9 +1,20 @@ -import { FetcherOptions } from '@vercel/commerce/utils/types' -import { CustomerAccessTokenCreateInput } from '../../schema' +import type { FetcherOptions } from '@vercel/commerce/utils/types' + +import type { + Mutation, + MutationCustomerActivateArgs, + MutationCustomerActivateByUrlArgs, + CustomerAccessTokenCreateInput, +} from '../../schema' + import { setCustomerToken } from './helpers' -import { customerAccessTokenCreateMutation } from './mutations' import { throwUserErrors } from './throw-user-errors' +import { + customerActivateByUrlMutation, + customerAccessTokenCreateMutation, +} from './mutations' + export const handleLogin = (data: any) => { const response = data.customerAccessTokenCreate throwUserErrors(response?.customerUserErrors) @@ -32,3 +43,22 @@ export const handleAutomaticLogin = async ( handleLogin(loginData) } catch (error) {} } + +export const handleAccountActivation = async ( + fetch: (options: FetcherOptions) => Promise, + input: MutationCustomerActivateByUrlArgs +) => { + try { + const { customerActivateByUrl } = await fetch< + Mutation, + MutationCustomerActivateArgs + >({ + query: customerActivateByUrlMutation, + variables: { + input, + }, + }) + + throwUserErrors(customerActivateByUrl?.customerUserErrors) + } catch (error) {} +}