mirror of
https://github.com/vercel/commerce.git
synced 2025-05-16 14:36:59 +00:00
refactor: add hook type and use constant for api endpoint
This commit is contained in:
parent
1248984f29
commit
0c180869b1
@ -1,7 +1,8 @@
|
||||
import { OperationContext } from '@vercel/commerce/api/operations'
|
||||
import type { Product } from '@vercel/commerce/types/product'
|
||||
import { SyliusConfig } from 'api'
|
||||
import { SyliusConfig, Provider } from 'api'
|
||||
import { SyliusProduct } from 'types/products'
|
||||
import { PRODUCTS_ENDPOINT } from '../../utils/constant/api-endpoints'
|
||||
import { normalizeProduct } from '../../utils/normalize/normalize-product'
|
||||
|
||||
export type GetAllProductPathsResult = {
|
||||
@ -10,7 +11,7 @@ export type GetAllProductPathsResult = {
|
||||
|
||||
export default function getAllProductPathsOperation({
|
||||
commerce,
|
||||
}: OperationContext<any>) {
|
||||
}: OperationContext<Provider>) {
|
||||
async function getAllProductPaths({
|
||||
query,
|
||||
variables,
|
||||
@ -22,7 +23,7 @@ export default function getAllProductPathsOperation({
|
||||
preview?: boolean
|
||||
} = {}): Promise<GetAllProductPathsResult> {
|
||||
const config = commerce.getConfig(cfg)
|
||||
const syliusProducts = await config.fetch('GET', '/api/v2/shop/products')
|
||||
const syliusProducts = await config.fetch('GET', PRODUCTS_ENDPOINT)
|
||||
const products = syliusProducts.map((syliusProduct: SyliusProduct) =>
|
||||
normalizeProduct(syliusProduct)
|
||||
)
|
||||
|
@ -3,6 +3,7 @@ import type { OperationContext } from '@vercel/commerce/api/operations'
|
||||
import type { Provider, SyliusConfig } from '../index'
|
||||
import { SyliusProduct } from '../../types/products'
|
||||
import { normalizeProduct } from '../../utils/normalize/normalize-product'
|
||||
import { PRODUCTS_ENDPOINT } from '../../utils/constant/api-endpoints'
|
||||
|
||||
export default function getAllProductsOperation({
|
||||
commerce,
|
||||
@ -23,7 +24,7 @@ export default function getAllProductsOperation({
|
||||
preview?: boolean
|
||||
} = {}): Promise<{ products: Product[] }> {
|
||||
const config = commerce.getConfig(cfg)
|
||||
const syliusProducts = await config.fetch('GET', '/api/v2/shop/products')
|
||||
const syliusProducts = await config.fetch('GET', PRODUCTS_ENDPOINT)
|
||||
const products = syliusProducts.map((syliusProduct: SyliusProduct) =>
|
||||
normalizeProduct(syliusProduct)
|
||||
)
|
||||
|
@ -1,4 +1,4 @@
|
||||
import type { SyliusConfig } from '../index'
|
||||
import type { Provider, SyliusConfig } from '../index'
|
||||
import { Product } from '@vercel/commerce/types/product'
|
||||
import { GetProductOperation } from '@vercel/commerce/types/product'
|
||||
import type { OperationContext } from '@vercel/commerce/api/operations'
|
||||
@ -6,7 +6,7 @@ import { normalizeProduct } from '../../utils/normalize/normalize-product'
|
||||
|
||||
export default function getProductOperation({
|
||||
commerce,
|
||||
}: OperationContext<any>) {
|
||||
}: OperationContext<Provider>) {
|
||||
async function getProduct<T extends GetProductOperation>({
|
||||
query = '',
|
||||
variables,
|
||||
|
@ -1,8 +1,9 @@
|
||||
import { OperationContext } from '@vercel/commerce/api/operations'
|
||||
import { Category } from '@vercel/commerce/types/site'
|
||||
import { SyliusCategorie } from 'types/site'
|
||||
import { TAXONS_ENDPOINT } from '../../utils/constant/api-endpoints'
|
||||
import { normalizeCategorie } from '../../utils/normalize/normalize-site'
|
||||
import { SyliusConfig } from '../index'
|
||||
import { SyliusConfig, Provider } from '../index'
|
||||
|
||||
export type GetSiteInfoResult<
|
||||
T extends { categories: any[]; brands: any[] } = {
|
||||
@ -13,7 +14,7 @@ export type GetSiteInfoResult<
|
||||
|
||||
export default function getSiteInfoOperation({
|
||||
commerce,
|
||||
}: OperationContext<any>) {
|
||||
}: OperationContext<Provider>) {
|
||||
async function getSiteInfo({
|
||||
query,
|
||||
variables,
|
||||
@ -25,7 +26,7 @@ export default function getSiteInfoOperation({
|
||||
preview?: boolean
|
||||
} = {}): Promise<GetSiteInfoResult> {
|
||||
const config = commerce.getConfig(cfg)
|
||||
const syliusCategories = await config.fetch('GET', '/api/v2/shop/taxons')
|
||||
const syliusCategories = await config.fetch('GET', TAXONS_ENDPOINT)
|
||||
const categories = syliusCategories.map(
|
||||
(syliusCategories: SyliusCategorie) =>
|
||||
normalizeCategorie(syliusCategories)
|
||||
|
@ -5,12 +5,13 @@ import useCustomer from '@vercel/commerce/customer/use-customer'
|
||||
import { setCustomerToken } from '../utils/token/customer-token'
|
||||
import { setCustomerRoute } from '../utils/token/customer-route'
|
||||
import { LoginHook } from '@vercel/commerce/types/login'
|
||||
import { LOGIN_ENDPOINT } from '../utils/constant/api-endpoints'
|
||||
|
||||
export default useLogin as UseLogin<typeof handler>
|
||||
|
||||
export const handler: MutationHook<LoginHook> = {
|
||||
fetchOptions: {
|
||||
url: '/api/v2/shop/authentication-token',
|
||||
url: LOGIN_ENDPOINT,
|
||||
method: 'POST',
|
||||
},
|
||||
fetcher: async ({ input, options, fetch }) => {
|
||||
|
@ -3,10 +3,11 @@ import useLogout, { UseLogout } from '@vercel/commerce/auth/use-logout'
|
||||
import { useCustomer } from '../customer'
|
||||
import { useCallback } from 'react'
|
||||
import { setCustomerToken } from '../utils/token/customer-token'
|
||||
import { LogoutHook } from '@vercel/commerce/types/logout'
|
||||
|
||||
export default useLogout as UseLogout<typeof handler>
|
||||
|
||||
export const handler: MutationHook<any> = {
|
||||
export const handler: MutationHook<LogoutHook> = {
|
||||
fetchOptions: {
|
||||
url: '',
|
||||
},
|
||||
@ -17,6 +18,7 @@ export const handler: MutationHook<any> = {
|
||||
async function logout() {
|
||||
setCustomerToken(null)
|
||||
await mutate()
|
||||
return null
|
||||
},
|
||||
[mutate]
|
||||
)
|
||||
|
@ -2,12 +2,13 @@ import { useCallback } from 'react'
|
||||
import { MutationHook } from '@vercel/commerce/utils/types'
|
||||
import useSignup, { UseSignup } from '@vercel/commerce/auth/use-signup'
|
||||
import { SignupHook } from '@vercel/commerce/types/signup'
|
||||
import { CUSTOMERS_ENDPOINT } from '../utils/constant/api-endpoints'
|
||||
|
||||
export default useSignup as UseSignup<typeof handler>
|
||||
|
||||
export const handler: MutationHook<SignupHook> = {
|
||||
fetchOptions: {
|
||||
url: '/api/v2/shop/customers',
|
||||
url: CUSTOMERS_ENDPOINT,
|
||||
method: 'POST',
|
||||
},
|
||||
fetcher: async ({
|
||||
|
@ -1,13 +1,17 @@
|
||||
import useAddItem, { UseAddItem } from '@vercel/commerce/cart/use-add-item'
|
||||
import { AddItemHook } from '@vercel/commerce/types/cart'
|
||||
import { MutationHook } from '@vercel/commerce/utils/types'
|
||||
import { useCallback } from 'react'
|
||||
import { CUSTOMER_ORDERS_ENDPOINT } from '../utils/constant/api-endpoints'
|
||||
import { normalizeCart } from '../utils/normalize/normalize-cart'
|
||||
import { getCartToken, setCartToken } from '../utils/token/cart-token'
|
||||
import useCart from './use-cart'
|
||||
|
||||
export default useAddItem as UseAddItem<typeof handler>
|
||||
export const handler: MutationHook<any> = {
|
||||
|
||||
export const handler: MutationHook<AddItemHook> = {
|
||||
fetchOptions: {
|
||||
url: '/api/v2/shop/orders',
|
||||
url: CUSTOMER_ORDERS_ENDPOINT,
|
||||
method: 'POST',
|
||||
},
|
||||
fetcher: async ({ input: { productId, variantId }, options, fetch }) => {
|
||||
@ -21,7 +25,8 @@ export const handler: MutationHook<any> = {
|
||||
})
|
||||
setCartToken(syliusOrder.tokenValue)
|
||||
}
|
||||
await fetch({
|
||||
|
||||
const syliusCart = await fetch({
|
||||
url: `${options.url}/${getCartToken()}/items`,
|
||||
method: options.method,
|
||||
body: {
|
||||
@ -29,6 +34,8 @@ export const handler: MutationHook<any> = {
|
||||
quantity: 1,
|
||||
},
|
||||
})
|
||||
|
||||
return normalizeCart(syliusCart)
|
||||
},
|
||||
useHook:
|
||||
({ fetch }) =>
|
||||
|
@ -4,12 +4,13 @@ import useCart, { UseCart } from '@vercel/commerce/cart/use-cart'
|
||||
import { getCartToken } from '../utils/token/cart-token'
|
||||
import { normalizeCart } from '../utils/normalize/normalize-cart'
|
||||
import { GetCartHook } from '@vercel/commerce/types/cart'
|
||||
import { CUSTOMER_ORDERS_ENDPOINT } from '../utils/constant/api-endpoints'
|
||||
|
||||
export default useCart as UseCart<typeof handler>
|
||||
|
||||
export const handler: SWRHook<GetCartHook> = {
|
||||
fetchOptions: {
|
||||
url: `/api/v2/shop/orders`,
|
||||
url: CUSTOMER_ORDERS_ENDPOINT,
|
||||
method: 'GET',
|
||||
},
|
||||
fetcher: async ({ options, fetch }) => {
|
||||
|
@ -7,12 +7,13 @@ import useCart from './use-cart'
|
||||
import { RemoveItemHook } from '@vercel/commerce/types/cart'
|
||||
import { useCallback } from 'react'
|
||||
import { normalizeCart } from '../utils/normalize/normalize-cart'
|
||||
import { CUSTOMER_ORDERS_ENDPOINT } from '../utils/constant/api-endpoints'
|
||||
|
||||
export default useRemoveItem as UseRemoveItem<typeof handler>
|
||||
|
||||
export const handler: MutationHook<any> = {
|
||||
export const handler: MutationHook<RemoveItemHook> = {
|
||||
fetchOptions: {
|
||||
url: '/api/v2/shop/orders',
|
||||
url: CUSTOMER_ORDERS_ENDPOINT,
|
||||
method: 'DELETE',
|
||||
},
|
||||
fetcher: async ({ input: { itemId }, options, fetch }) => {
|
||||
|
@ -12,12 +12,13 @@ import useCart from './use-cart'
|
||||
import { LineItem, UpdateItemHook } from '@vercel/commerce/types/cart'
|
||||
import { ValidationError } from '@vercel/commerce/utils/errors'
|
||||
import { normalizeCart } from '../utils/normalize/normalize-cart'
|
||||
import { CUSTOMER_ORDERS_ENDPOINT } from '../utils/constant/api-endpoints'
|
||||
|
||||
export default useUpdateItem as UseUpdateItem<any>
|
||||
|
||||
export const handler: MutationHook<any> = {
|
||||
export const handler: MutationHook<UpdateItemHook> = {
|
||||
fetchOptions: {
|
||||
url: '/api/v2/shop/orders',
|
||||
url: CUSTOMER_ORDERS_ENDPOINT,
|
||||
method: 'PATCH',
|
||||
},
|
||||
fetcher: async ({
|
||||
|
@ -6,21 +6,22 @@ import { getCustomerRoute } from '../utils/token/customer-route'
|
||||
import { normalizeCustomer } from '../utils/normalize/normalize-customer'
|
||||
import { getCustomerToken } from '../utils/token/customer-token'
|
||||
import { CustomerHook } from '@vercel/commerce/types/customer'
|
||||
import { CUSTOMERS_ENDPOINT } from '../utils/constant/api-endpoints'
|
||||
|
||||
export default useCustomer as UseCustomer<typeof handler>
|
||||
|
||||
export const handler: SWRHook<CustomerHook> = {
|
||||
fetchOptions: {
|
||||
url: `/api/v2/shop/customers/`,
|
||||
url: CUSTOMERS_ENDPOINT,
|
||||
method: 'GET',
|
||||
},
|
||||
fetcher: async ({ options, fetch }) => {
|
||||
if (getCustomerToken()) {
|
||||
const syliusCustomer = await fetch({
|
||||
const customerData = await fetch({
|
||||
url: getCustomerRoute() ?? '',
|
||||
method: options.method,
|
||||
})
|
||||
return normalizeCustomer(syliusCustomer)
|
||||
return normalizeCustomer(customerData?.customer)
|
||||
}
|
||||
return null
|
||||
},
|
||||
|
@ -2,9 +2,9 @@ import { SWRHook } from '@vercel/commerce/utils/types'
|
||||
import useSearch, { UseSearch } from '@vercel/commerce/product/use-search'
|
||||
import { normalizeProduct } from '../utils/normalize/normalize-product'
|
||||
import { SyliusProduct } from 'types/products'
|
||||
import { url } from 'inspector'
|
||||
import { API_URL } from './../const'
|
||||
import { Product } from '@vercel/commerce/types/product'
|
||||
import { SearchProductsHook } from '@vercel/commerce/types/product'
|
||||
import { PRODUCTS_ENDPOINT } from '../utils/constant/api-endpoints'
|
||||
export default useSearch as UseSearch<typeof handler>
|
||||
|
||||
export type SearchProductsInput = {
|
||||
@ -15,15 +15,16 @@ export type SearchProductsInput = {
|
||||
locale?: string
|
||||
}
|
||||
|
||||
export const handler: SWRHook<any> = {
|
||||
export const handler: SWRHook<SearchProductsHook> = {
|
||||
fetchOptions: {
|
||||
url: '/api/v2/shop/products',
|
||||
url: PRODUCTS_ENDPOINT,
|
||||
method: 'GET',
|
||||
},
|
||||
fetcher: async ({ input: { search, categoryId, sort }, options, fetch }) => {
|
||||
const url = new URL(options.url!, API_URL)
|
||||
|
||||
if (categoryId) url.searchParams.set('productTaxons.taxon.code', categoryId)
|
||||
if (categoryId)
|
||||
url.searchParams.set('productTaxons.taxon.code', categoryId as string)
|
||||
if (search) url.searchParams.set('translations.name', search)
|
||||
if (sort) {
|
||||
switch (sort) {
|
||||
|
6
packages/sylius/src/utils/constant/api-endpoints.ts
Normal file
6
packages/sylius/src/utils/constant/api-endpoints.ts
Normal file
@ -0,0 +1,6 @@
|
||||
export const PRODUCTS_ENDPOINT = '/api/v2/shop/products'
|
||||
export const PRODUCT_BY_SLUG_ENDPOINT = '/api/v2/shop/products-by-slug'
|
||||
export const TAXONS_ENDPOINT = '/api/v2/shop/taxons'
|
||||
export const LOGIN_ENDPOINT = '/api/v2/shop/authentication-token'
|
||||
export const CUSTOMERS_ENDPOINT = '/api/v2/shop/customers'
|
||||
export const CUSTOMER_ORDERS_ENDPOINT = '/api/v2/shop/orders'
|
@ -1,18 +1,3 @@
|
||||
export { default as handleFetchResponse } from './handle-fetch-response'
|
||||
export * from './normalize/normalize-product'
|
||||
export * from './normalize/normalize-site'
|
||||
/*
|
||||
export { default as getSearchVariables } from './get-search-variables'
|
||||
export { default as getSortVariables } from './get-sort-variables'
|
||||
export { default as getBrands } from './get-brands'
|
||||
export { default as getCategories } from './get-categories'
|
||||
export { default as getCheckoutId } from './get-checkout-id'
|
||||
export { default as checkoutCreate } from './checkout-create'
|
||||
export { default as checkoutToCart } from './checkout-to-cart'
|
||||
export { default as handleLogin, handleAutomaticLogin } from './handle-login'
|
||||
export { default as handleAccountActivation } from './handle-account-activation'
|
||||
export { default as throwUserErrors } from './throw-user-errors'
|
||||
export * from './queries'
|
||||
export * from './mutations'
|
||||
export * from './customer-token'
|
||||
*/
|
||||
|
@ -109,7 +109,9 @@ const normalizeProductOptionValue = (
|
||||
}
|
||||
}
|
||||
|
||||
const normalizeProductImage = (image: SyliusProductImage): ProductImage => ({
|
||||
export const normalizeProductImage = (
|
||||
image: SyliusProductImage
|
||||
): ProductImage => ({
|
||||
url: process.env.NEXT_PUBLIC_SYLIUS_ALLOWED_IMAGE_URL + image.path,
|
||||
})
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user