mirror of
https://github.com/vercel/commerce.git
synced 2025-05-17 15:06:59 +00:00
Update utils structure
This commit is contained in:
parent
d8f6703b21
commit
0c6ea16ced
@ -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 */ `
|
||||
|
@ -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
|
||||
|
@ -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<typeof handler>
|
||||
|
||||
export const handler: MutationHook<AddItemHook> = {
|
||||
|
@ -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<typeof handler>
|
||||
export const handler: SWRHook<GetCartHook> = {
|
||||
|
@ -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 = any> = T extends LineItem
|
||||
? Partial<UpdateItemHook['actionInput']>
|
||||
: UpdateItemHook['actionInput']
|
||||
|
@ -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,
|
||||
|
@ -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
|
||||
|
@ -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: <T = any, B = Body>(options: FetcherOptions<B>) => Promise<T>,
|
||||
lines?: Array<CartLineInput> | CartLineInput
|
||||
): Promise<CartDetailsFragment | null | undefined> => {
|
||||
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
|
||||
}
|
@ -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<BrandEdge[]> => {
|
||||
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}`,
|
||||
},
|
||||
}
|
||||
})
|
||||
}
|
@ -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<Category[]> => {
|
||||
const { data } = await fetch(
|
||||
getSiteCollectionsQuery,
|
||||
{
|
||||
variables: {
|
||||
first: 250,
|
||||
},
|
||||
},
|
||||
{
|
||||
...(locale && {
|
||||
headers: {
|
||||
'Accept-Language': locale,
|
||||
},
|
||||
}),
|
||||
}
|
||||
)
|
||||
|
||||
return (
|
||||
data.collections?.edges?.map(({ node }: CollectionEdge) =>
|
||||
normalizeCategory(node)
|
||||
) ?? []
|
||||
)
|
||||
}
|
@ -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: <T = any, B = Body>(options: FetcherOptions<B>) => Promise<T>,
|
||||
input: MutationCustomerActivateByUrlArgs
|
||||
) => {
|
||||
try {
|
||||
const { customerActivateByUrl } = await fetch<
|
||||
Mutation,
|
||||
MutationCustomerActivateArgs
|
||||
>({
|
||||
query: customerActivateByUrlMutation,
|
||||
variables: {
|
||||
input,
|
||||
},
|
||||
})
|
||||
|
||||
throwUserErrors(customerActivateByUrl?.customerUserErrors)
|
||||
} catch (error) {}
|
||||
}
|
@ -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: <T = any, B = Body>(options: FetcherOptions<B>) => Promise<T>,
|
||||
input: MutationCustomerActivateByUrlArgs
|
||||
) => {
|
||||
try {
|
||||
const { customerActivateByUrl } = await fetch<
|
||||
Mutation,
|
||||
MutationCustomerActivateArgs
|
||||
>({
|
||||
query: customerActivateByUrlMutation,
|
||||
variables: {
|
||||
input,
|
||||
},
|
||||
})
|
||||
|
||||
throwUserErrors(customerActivateByUrl?.customerUserErrors)
|
||||
} catch (error) {}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user