From 14fcbaa5b36b6a522c53cf92ed3eecc8b5d8662c Mon Sep 17 00:00:00 2001 From: Daniel Gent Date: Wed, 1 Sep 2021 14:03:16 +0200 Subject: [PATCH] Run prettier autofix on all files. --- .../customer/get-logged-in-customer.ts | 47 ++++---- framework/bigcommerce/api/index.ts | 6 +- framework/bigcommerce/auth/use-login.tsx | 27 ++--- framework/bigcommerce/auth/use-logout.tsx | 24 ++-- framework/bigcommerce/auth/use-signup.tsx | 24 ++-- framework/bigcommerce/cart/use-add-item.tsx | 24 ++-- framework/bigcommerce/cart/use-cart.tsx | 34 +++--- .../bigcommerce/cart/use-remove-item.tsx | 38 +++---- .../bigcommerce/cart/use-update-item.tsx | 64 +++++------ .../bigcommerce/customer/use-customer.tsx | 18 +-- framework/bigcommerce/lib/normalize.ts | 6 +- framework/bigcommerce/product/use-search.tsx | 30 ++--- framework/bigcommerce/types/wishlist.ts | 3 +- .../bigcommerce/wishlist/use-add-item.tsx | 40 +++---- .../bigcommerce/wishlist/use-remove-item.tsx | 38 ++++--- .../bigcommerce/wishlist/use-wishlist.tsx | 50 +++++---- framework/commerce/api/endpoints/cart.ts | 104 +++++++++--------- framework/commerce/api/endpoints/logout.ts | 54 +++++---- framework/commerce/api/endpoints/signup.ts | 56 +++++----- framework/commerce/types/product.ts | 11 +- framework/commerce/utils/define-property.ts | 22 ++-- framework/saleor/README.md | 2 +- .../saleor/api/endpoints/checkout/index.ts | 6 +- .../saleor/api/operations/get-all-pages.ts | 15 +-- .../saleor/api/operations/get-all-products.ts | 15 +-- framework/saleor/api/operations/get-page.ts | 13 +-- .../saleor/api/operations/get-product.ts | 12 +- .../saleor/api/operations/get-site-info.ts | 2 +- framework/saleor/api/operations/login.ts | 14 +-- framework/saleor/auth/use-signup.tsx | 2 +- framework/saleor/cart/use-remove-item.tsx | 8 +- framework/saleor/cart/use-update-item.tsx | 9 +- framework/saleor/utils/checkout-to-cart.ts | 17 ++- .../shopify/api/operations/get-all-pages.ts | 8 +- framework/shopify/auth/use-login.tsx | 27 ++--- framework/shopify/auth/use-logout.tsx | 24 ++-- framework/shopify/auth/use-signup.tsx | 24 ++-- framework/shopify/cart/use-add-item.tsx | 24 ++-- framework/shopify/cart/use-cart.tsx | 34 +++--- framework/shopify/cart/use-remove-item.tsx | 38 +++---- framework/shopify/cart/use-update-item.tsx | 70 ++++++------ framework/shopify/customer/use-customer.tsx | 18 +-- framework/shopify/product/use-search.tsx | 32 +++--- .../swell/api/operations/get-all-pages.ts | 5 +- framework/swell/auth/use-login.tsx | 24 ++-- framework/swell/auth/use-logout.tsx | 24 ++-- framework/swell/auth/use-signup.tsx | 24 ++-- framework/swell/cart/use-add-item.tsx | 24 ++-- framework/swell/cart/use-cart.tsx | 34 +++--- framework/swell/cart/use-remove-item.tsx | 24 ++-- framework/swell/cart/use-update-item.tsx | 72 ++++++------ framework/swell/customer/use-customer.tsx | 18 +-- framework/swell/product/use-search.tsx | 30 ++--- .../vendure/api/operations/get-all-pages.ts | 5 +- framework/vendure/auth/use-login.tsx | 24 ++-- framework/vendure/auth/use-signup.tsx | 24 ++-- framework/vendure/cart/use-add-item.tsx | 24 ++-- framework/vendure/cart/use-cart.tsx | 34 +++--- framework/vendure/cart/use-remove-item.tsx | 24 ++-- framework/vendure/cart/use-update-item.tsx | 68 ++++++------ framework/vendure/customer/use-customer.tsx | 18 +-- framework/vendure/product/use-search.tsx | 30 ++--- 62 files changed, 855 insertions(+), 809 deletions(-) diff --git a/framework/bigcommerce/api/endpoints/customer/get-logged-in-customer.ts b/framework/bigcommerce/api/endpoints/customer/get-logged-in-customer.ts index cfcce9532..50bd5cf3f 100644 --- a/framework/bigcommerce/api/endpoints/customer/get-logged-in-customer.ts +++ b/framework/bigcommerce/api/endpoints/customer/get-logged-in-customer.ts @@ -24,36 +24,33 @@ export const getLoggedInCustomerQuery = /* GraphQL */ ` export type Customer = NonNullable -const getLoggedInCustomer: CustomerEndpoint['handlers']['getLoggedInCustomer'] = async ({ - req, - res, - config, -}) => { - const token = req.cookies[config.customerCookie] +const getLoggedInCustomer: CustomerEndpoint['handlers']['getLoggedInCustomer'] = + async ({ req, res, config }) => { + const token = req.cookies[config.customerCookie] - if (token) { - const { data } = await config.fetch( - getLoggedInCustomerQuery, - undefined, - { - headers: { - cookie: `${config.customerCookie}=${token}`, - }, + if (token) { + const { data } = await config.fetch( + getLoggedInCustomerQuery, + undefined, + { + headers: { + cookie: `${config.customerCookie}=${token}`, + }, + } + ) + const { customer } = data + + if (!customer) { + return res.status(400).json({ + data: null, + errors: [{ message: 'Customer not found', code: 'not_found' }], + }) } - ) - const { customer } = data - if (!customer) { - return res.status(400).json({ - data: null, - errors: [{ message: 'Customer not found', code: 'not_found' }], - }) + return res.status(200).json({ data: { customer } }) } - return res.status(200).json({ data: { customer } }) + res.status(200).json({ data: null }) } - res.status(200).json({ data: null }) -} - export default getLoggedInCustomer diff --git a/framework/bigcommerce/api/index.ts b/framework/bigcommerce/api/index.ts index 9dbe400f9..ecc0d0e0e 100644 --- a/framework/bigcommerce/api/index.ts +++ b/framework/bigcommerce/api/index.ts @@ -34,7 +34,7 @@ export interface BigcommerceConfig extends CommerceAPIConfig { storeChannelId?: string storeUrl?: string storeApiClientSecret?: string - storeHash?:string + storeHash?: string storeApiFetch(endpoint: string, options?: RequestInit): Promise } @@ -81,8 +81,8 @@ const config: BigcommerceConfig = { storeApiToken: STORE_API_TOKEN, storeApiClientId: STORE_API_CLIENT_ID, storeChannelId: STORE_CHANNEL_ID, - storeUrl:STORE_URL, - storeApiClientSecret:CLIENT_SECRET, + storeUrl: STORE_URL, + storeApiClientSecret: CLIENT_SECRET, storeHash: STOREFRONT_HASH, storeApiFetch: createFetchStoreApi(() => getCommerceApi().getConfig()), } diff --git a/framework/bigcommerce/auth/use-login.tsx b/framework/bigcommerce/auth/use-login.tsx index d366b5260..a8c049603 100644 --- a/framework/bigcommerce/auth/use-login.tsx +++ b/framework/bigcommerce/auth/use-login.tsx @@ -15,8 +15,7 @@ export const handler: MutationHook = { async fetcher({ input: { email, password }, options, fetch }) { if (!(email && password)) { throw new CommerceError({ - message: - 'An email and password are required to login', + message: 'An email and password are required to login', }) } @@ -25,16 +24,18 @@ export const handler: MutationHook = { body: { email, password }, }) }, - useHook: ({ fetch }) => () => { - const { revalidate } = useCustomer() + useHook: + ({ fetch }) => + () => { + const { revalidate } = useCustomer() - return useCallback( - async function login(input) { - const data = await fetch({ input }) - await revalidate() - return data - }, - [fetch, revalidate] - ) - }, + return useCallback( + async function login(input) { + const data = await fetch({ input }) + await revalidate() + return data + }, + [fetch, revalidate] + ) + }, } diff --git a/framework/bigcommerce/auth/use-logout.tsx b/framework/bigcommerce/auth/use-logout.tsx index e75563e04..1e8e9c666 100644 --- a/framework/bigcommerce/auth/use-logout.tsx +++ b/framework/bigcommerce/auth/use-logout.tsx @@ -11,16 +11,18 @@ export const handler: MutationHook = { url: '/api/logout', method: 'GET', }, - useHook: ({ fetch }) => () => { - const { mutate } = useCustomer() + useHook: + ({ fetch }) => + () => { + const { mutate } = useCustomer() - return useCallback( - async function logout() { - const data = await fetch() - await mutate(null, false) - return data - }, - [fetch, mutate] - ) - }, + return useCallback( + async function logout() { + const data = await fetch() + await mutate(null, false) + return data + }, + [fetch, mutate] + ) + }, } diff --git a/framework/bigcommerce/auth/use-signup.tsx b/framework/bigcommerce/auth/use-signup.tsx index da06fd3eb..8f183685f 100644 --- a/framework/bigcommerce/auth/use-signup.tsx +++ b/framework/bigcommerce/auth/use-signup.tsx @@ -29,16 +29,18 @@ export const handler: MutationHook = { body: { firstName, lastName, email, password }, }) }, - useHook: ({ fetch }) => () => { - const { revalidate } = useCustomer() + useHook: + ({ fetch }) => + () => { + const { revalidate } = useCustomer() - return useCallback( - async function signup(input) { - const data = await fetch({ input }) - await revalidate() - return data - }, - [fetch, revalidate] - ) - }, + return useCallback( + async function signup(input) { + const data = await fetch({ input }) + await revalidate() + return data + }, + [fetch, revalidate] + ) + }, } diff --git a/framework/bigcommerce/cart/use-add-item.tsx b/framework/bigcommerce/cart/use-add-item.tsx index 1ac6ac6f8..bdad1862c 100644 --- a/framework/bigcommerce/cart/use-add-item.tsx +++ b/framework/bigcommerce/cart/use-add-item.tsx @@ -29,16 +29,18 @@ export const handler: MutationHook = { return data }, - useHook: ({ fetch }) => () => { - const { mutate } = useCart() + useHook: + ({ fetch }) => + () => { + const { mutate } = useCart() - return useCallback( - async function addItem(input) { - const data = await fetch({ input }) - await mutate(data, false) - return data - }, - [fetch, mutate] - ) - }, + return useCallback( + async function addItem(input) { + const data = await fetch({ input }) + await mutate(data, false) + return data + }, + [fetch, mutate] + ) + }, } diff --git a/framework/bigcommerce/cart/use-cart.tsx b/framework/bigcommerce/cart/use-cart.tsx index 4ba1724d9..53fd3471c 100644 --- a/framework/bigcommerce/cart/use-cart.tsx +++ b/framework/bigcommerce/cart/use-cart.tsx @@ -10,22 +10,24 @@ export const handler: SWRHook = { url: '/api/cart', method: 'GET', }, - useHook: ({ useData }) => (input) => { - const response = useData({ - swrOptions: { revalidateOnFocus: false, ...input?.swrOptions }, - }) + useHook: + ({ useData }) => + (input) => { + const response = useData({ + swrOptions: { revalidateOnFocus: false, ...input?.swrOptions }, + }) - return useMemo( - () => - Object.create(response, { - isEmpty: { - get() { - return (response.data?.lineItems.length ?? 0) <= 0 + return useMemo( + () => + Object.create(response, { + isEmpty: { + get() { + return (response.data?.lineItems.length ?? 0) <= 0 + }, + enumerable: true, }, - enumerable: true, - }, - }), - [response] - ) - }, + }), + [response] + ) + }, } diff --git a/framework/bigcommerce/cart/use-remove-item.tsx b/framework/bigcommerce/cart/use-remove-item.tsx index 1376f29ce..daf155c26 100644 --- a/framework/bigcommerce/cart/use-remove-item.tsx +++ b/framework/bigcommerce/cart/use-remove-item.tsx @@ -30,27 +30,25 @@ export const handler = { }: HookFetcherContext) { return await fetch({ ...options, body: { itemId } }) }, - useHook: ({ fetch }: MutationHookContext) => < - T extends LineItem | undefined = undefined - >( - ctx: { item?: T } = {} - ) => { - const { item } = ctx - const { mutate } = useCart() - const removeItem: RemoveItemFn = async (input) => { - const itemId = input?.id ?? item?.id + useHook: + ({ fetch }: MutationHookContext) => + (ctx: { item?: T } = {}) => { + const { item } = ctx + const { mutate } = useCart() + const removeItem: RemoveItemFn = async (input) => { + const itemId = input?.id ?? item?.id - if (!itemId) { - throw new ValidationError({ - message: 'Invalid input used for this operation', - }) + if (!itemId) { + throw new ValidationError({ + message: 'Invalid input used for this operation', + }) + } + + const data = await fetch({ input: { itemId } }) + await mutate(data, false) + return data } - const data = await fetch({ input: { itemId } }) - await mutate(data, false) - return data - } - - return useCallback(removeItem as RemoveItemFn, [fetch, mutate]) - }, + return useCallback(removeItem as RemoveItemFn, [fetch, mutate]) + }, } diff --git a/framework/bigcommerce/cart/use-update-item.tsx b/framework/bigcommerce/cart/use-update-item.tsx index 0f9f5754d..a4c7d178e 100644 --- a/framework/bigcommerce/cart/use-update-item.tsx +++ b/framework/bigcommerce/cart/use-update-item.tsx @@ -46,39 +46,39 @@ export const handler = { body: { itemId, item }, }) }, - useHook: ({ fetch }: MutationHookContext) => < - T extends LineItem | undefined = undefined - >( - ctx: { - item?: T - wait?: number - } = {} - ) => { - const { item } = ctx - const { mutate } = useCart() as any + useHook: + ({ fetch }: MutationHookContext) => + ( + ctx: { + item?: T + wait?: number + } = {} + ) => { + const { item } = ctx + const { mutate } = useCart() as any - return useCallback( - debounce(async (input: UpdateItemActionInput) => { - const itemId = input.id ?? item?.id - const productId = input.productId ?? item?.productId - const variantId = input.productId ?? item?.variantId + return useCallback( + debounce(async (input: UpdateItemActionInput) => { + const itemId = input.id ?? item?.id + const productId = input.productId ?? item?.productId + const variantId = input.productId ?? item?.variantId - if (!itemId || !productId || !variantId) { - throw new ValidationError({ - message: 'Invalid input used for this operation', + if (!itemId || !productId || !variantId) { + throw new ValidationError({ + message: 'Invalid input used for this operation', + }) + } + + const data = await fetch({ + input: { + itemId, + item: { productId, variantId, quantity: input.quantity }, + }, }) - } - - const data = await fetch({ - input: { - itemId, - item: { productId, variantId, quantity: input.quantity }, - }, - }) - await mutate(data, false) - return data - }, ctx.wait ?? 500), - [fetch, mutate] - ) - }, + await mutate(data, false) + return data + }, ctx.wait ?? 500), + [fetch, mutate] + ) + }, } diff --git a/framework/bigcommerce/customer/use-customer.tsx b/framework/bigcommerce/customer/use-customer.tsx index 238b1229b..ddecc2fcf 100644 --- a/framework/bigcommerce/customer/use-customer.tsx +++ b/framework/bigcommerce/customer/use-customer.tsx @@ -13,12 +13,14 @@ export const handler: SWRHook = { const data = await fetch(options) return data?.customer ?? null }, - useHook: ({ useData }) => (input) => { - return useData({ - swrOptions: { - revalidateOnFocus: false, - ...input?.swrOptions, - }, - }) - }, + useHook: + ({ useData }) => + (input) => { + return useData({ + swrOptions: { + revalidateOnFocus: false, + ...input?.swrOptions, + }, + }) + }, } diff --git a/framework/bigcommerce/lib/normalize.ts b/framework/bigcommerce/lib/normalize.ts index 82a22de00..0f311c615 100644 --- a/framework/bigcommerce/lib/normalize.ts +++ b/framework/bigcommerce/lib/normalize.ts @@ -8,11 +8,7 @@ import getSlug from './get-slug' function normalizeProductOption(productOption: any) { const { - node: { - entityId, - values: { edges = [] } = {}, - ...rest - }, + node: { entityId, values: { edges = [] } = {}, ...rest }, } = productOption return { diff --git a/framework/bigcommerce/product/use-search.tsx b/framework/bigcommerce/product/use-search.tsx index cd6c34612..76fab00a4 100644 --- a/framework/bigcommerce/product/use-search.tsx +++ b/framework/bigcommerce/product/use-search.tsx @@ -33,18 +33,20 @@ export const handler: SWRHook = { method: options.method, }) }, - useHook: ({ useData }) => (input = {}) => { - return useData({ - input: [ - ['search', input.search], - ['categoryId', input.categoryId], - ['brandId', input.brandId], - ['sort', input.sort], - ], - swrOptions: { - revalidateOnFocus: false, - ...input.swrOptions, - }, - }) - }, + useHook: + ({ useData }) => + (input = {}) => { + return useData({ + input: [ + ['search', input.search], + ['categoryId', input.categoryId], + ['brandId', input.brandId], + ['sort', input.sort], + ], + swrOptions: { + revalidateOnFocus: false, + ...input.swrOptions, + }, + }) + }, } diff --git a/framework/bigcommerce/types/wishlist.ts b/framework/bigcommerce/types/wishlist.ts index 1e148b88c..9e9c52353 100644 --- a/framework/bigcommerce/types/wishlist.ts +++ b/framework/bigcommerce/types/wishlist.ts @@ -20,4 +20,5 @@ export type WishlistTypes = { } export type WishlistSchema = Core.WishlistSchema -export type GetCustomerWishlistOperation = Core.GetCustomerWishlistOperation +export type GetCustomerWishlistOperation = + Core.GetCustomerWishlistOperation diff --git a/framework/bigcommerce/wishlist/use-add-item.tsx b/framework/bigcommerce/wishlist/use-add-item.tsx index 1bf086731..6b3f7778d 100644 --- a/framework/bigcommerce/wishlist/use-add-item.tsx +++ b/framework/bigcommerce/wishlist/use-add-item.tsx @@ -13,25 +13,27 @@ export const handler: MutationHook = { url: '/api/wishlist', method: 'POST', }, - useHook: ({ fetch }) => () => { - const { data: customer } = useCustomer() - const { revalidate } = useWishlist() + useHook: + ({ fetch }) => + () => { + const { data: customer } = useCustomer() + const { revalidate } = useWishlist() - return useCallback( - async function addItem(item) { - if (!customer) { - // A signed customer is required in order to have a wishlist - throw new CommerceError({ - message: 'Signed customer not found', - }) - } + return useCallback( + async function addItem(item) { + if (!customer) { + // A signed customer is required in order to have a wishlist + throw new CommerceError({ + message: 'Signed customer not found', + }) + } - // TODO: add validations before doing the fetch - const data = await fetch({ input: { item } }) - await revalidate() - return data - }, - [fetch, revalidate, customer] - ) - }, + // TODO: add validations before doing the fetch + const data = await fetch({ input: { item } }) + await revalidate() + return data + }, + [fetch, revalidate, customer] + ) + }, } diff --git a/framework/bigcommerce/wishlist/use-remove-item.tsx b/framework/bigcommerce/wishlist/use-remove-item.tsx index 9d25c1439..bb8e6415e 100644 --- a/framework/bigcommerce/wishlist/use-remove-item.tsx +++ b/framework/bigcommerce/wishlist/use-remove-item.tsx @@ -15,24 +15,26 @@ export const handler: MutationHook = { url: '/api/wishlist', method: 'DELETE', }, - useHook: ({ fetch }) => ({ wishlist } = {}) => { - const { data: customer } = useCustomer() - const { revalidate } = useWishlist(wishlist) + useHook: + ({ fetch }) => + ({ wishlist } = {}) => { + const { data: customer } = useCustomer() + const { revalidate } = useWishlist(wishlist) - return useCallback( - async function removeItem(input) { - if (!customer) { - // A signed customer is required in order to have a wishlist - throw new CommerceError({ - message: 'Signed customer not found', - }) - } + return useCallback( + async function removeItem(input) { + if (!customer) { + // A signed customer is required in order to have a wishlist + throw new CommerceError({ + message: 'Signed customer not found', + }) + } - const data = await fetch({ input: { itemId: String(input.id) } }) - await revalidate() - return data - }, - [fetch, revalidate, customer] - ) - }, + const data = await fetch({ input: { itemId: String(input.id) } }) + await revalidate() + return data + }, + [fetch, revalidate, customer] + ) + }, } diff --git a/framework/bigcommerce/wishlist/use-wishlist.tsx b/framework/bigcommerce/wishlist/use-wishlist.tsx index b8fc946e3..7c09f835a 100644 --- a/framework/bigcommerce/wishlist/use-wishlist.tsx +++ b/framework/bigcommerce/wishlist/use-wishlist.tsx @@ -24,30 +24,32 @@ export const handler: SWRHook = { method: options.method, }) }, - useHook: ({ useData }) => (input) => { - const { data: customer } = useCustomer() - const response = useData({ - input: [ - ['customerId', customer?.entityId], - ['includeProducts', input?.includeProducts], - ], - swrOptions: { - revalidateOnFocus: false, - ...input?.swrOptions, - }, - }) + useHook: + ({ useData }) => + (input) => { + const { data: customer } = useCustomer() + const response = useData({ + input: [ + ['customerId', customer?.entityId], + ['includeProducts', input?.includeProducts], + ], + swrOptions: { + revalidateOnFocus: false, + ...input?.swrOptions, + }, + }) - return useMemo( - () => - Object.create(response, { - isEmpty: { - get() { - return (response.data?.items?.length || 0) <= 0 + return useMemo( + () => + Object.create(response, { + isEmpty: { + get() { + return (response.data?.items?.length || 0) <= 0 + }, + enumerable: true, }, - enumerable: true, - }, - }), - [response] - ) - }, + }), + [response] + ) + }, } diff --git a/framework/commerce/api/endpoints/cart.ts b/framework/commerce/api/endpoints/cart.ts index ca39e7da3..abd5df492 100644 --- a/framework/commerce/api/endpoints/cart.ts +++ b/framework/commerce/api/endpoints/cart.ts @@ -3,60 +3,58 @@ import { CommerceAPIError } from '../utils/errors' import isAllowedOperation from '../utils/is-allowed-operation' import type { GetAPISchema } from '..' -const cartEndpoint: GetAPISchema< - any, - CartSchema ->['endpoint']['handler'] = async (ctx) => { - const { req, res, handlers, config } = ctx +const cartEndpoint: GetAPISchema>['endpoint']['handler'] = + async (ctx) => { + const { req, res, handlers, config } = ctx - if ( - !isAllowedOperation(req, res, { - GET: handlers['getCart'], - POST: handlers['addItem'], - PUT: handlers['updateItem'], - DELETE: handlers['removeItem'], - }) - ) { - return + if ( + !isAllowedOperation(req, res, { + GET: handlers['getCart'], + POST: handlers['addItem'], + PUT: handlers['updateItem'], + DELETE: handlers['removeItem'], + }) + ) { + return + } + + const { cookies } = req + const cartId = cookies[config.cartCookie] + + try { + // Return current cart info + if (req.method === 'GET') { + const body = { cartId } + return await handlers['getCart']({ ...ctx, body }) + } + + // Create or add an item to the cart + if (req.method === 'POST') { + const body = { ...req.body, cartId } + return await handlers['addItem']({ ...ctx, body }) + } + + // Update item in cart + if (req.method === 'PUT') { + const body = { ...req.body, cartId } + return await handlers['updateItem']({ ...ctx, body }) + } + + // Remove an item from the cart + if (req.method === 'DELETE') { + const body = { ...req.body, cartId } + return await handlers['removeItem']({ ...ctx, body }) + } + } catch (error) { + console.error(error) + + const message = + error instanceof CommerceAPIError + ? 'An unexpected error ocurred with the Commerce API' + : 'An unexpected error ocurred' + + res.status(500).json({ data: null, errors: [{ message }] }) + } } - const { cookies } = req - const cartId = cookies[config.cartCookie] - - try { - // Return current cart info - if (req.method === 'GET') { - const body = { cartId } - return await handlers['getCart']({ ...ctx, body }) - } - - // Create or add an item to the cart - if (req.method === 'POST') { - const body = { ...req.body, cartId } - return await handlers['addItem']({ ...ctx, body }) - } - - // Update item in cart - if (req.method === 'PUT') { - const body = { ...req.body, cartId } - return await handlers['updateItem']({ ...ctx, body }) - } - - // Remove an item from the cart - if (req.method === 'DELETE') { - const body = { ...req.body, cartId } - return await handlers['removeItem']({ ...ctx, body }) - } - } catch (error) { - console.error(error) - - const message = - error instanceof CommerceAPIError - ? 'An unexpected error ocurred with the Commerce API' - : 'An unexpected error ocurred' - - res.status(500).json({ data: null, errors: [{ message }] }) - } -} - export default cartEndpoint diff --git a/framework/commerce/api/endpoints/logout.ts b/framework/commerce/api/endpoints/logout.ts index 8da11acb0..c2640eac3 100644 --- a/framework/commerce/api/endpoints/logout.ts +++ b/framework/commerce/api/endpoints/logout.ts @@ -3,35 +3,33 @@ import { CommerceAPIError } from '../utils/errors' import isAllowedOperation from '../utils/is-allowed-operation' import type { GetAPISchema } from '..' -const logoutEndpoint: GetAPISchema< - any, - LogoutSchema ->['endpoint']['handler'] = async (ctx) => { - const { req, res, handlers } = ctx +const logoutEndpoint: GetAPISchema['endpoint']['handler'] = + async (ctx) => { + const { req, res, handlers } = ctx - if ( - !isAllowedOperation(req, res, { - GET: handlers['logout'], - }) - ) { - return + if ( + !isAllowedOperation(req, res, { + GET: handlers['logout'], + }) + ) { + return + } + + try { + const redirectTo = req.query.redirect_to + const body = typeof redirectTo === 'string' ? { redirectTo } : {} + + return await handlers['logout']({ ...ctx, body }) + } catch (error) { + console.error(error) + + const message = + error instanceof CommerceAPIError + ? 'An unexpected error ocurred with the Commerce API' + : 'An unexpected error ocurred' + + res.status(500).json({ data: null, errors: [{ message }] }) + } } - try { - const redirectTo = req.query.redirect_to - const body = typeof redirectTo === 'string' ? { redirectTo } : {} - - return await handlers['logout']({ ...ctx, body }) - } catch (error) { - console.error(error) - - const message = - error instanceof CommerceAPIError - ? 'An unexpected error ocurred with the Commerce API' - : 'An unexpected error ocurred' - - res.status(500).json({ data: null, errors: [{ message }] }) - } -} - export default logoutEndpoint diff --git a/framework/commerce/api/endpoints/signup.ts b/framework/commerce/api/endpoints/signup.ts index aa73ae739..78c4cf588 100644 --- a/framework/commerce/api/endpoints/signup.ts +++ b/framework/commerce/api/endpoints/signup.ts @@ -3,36 +3,34 @@ import { CommerceAPIError } from '../utils/errors' import isAllowedOperation from '../utils/is-allowed-operation' import type { GetAPISchema } from '..' -const signupEndpoint: GetAPISchema< - any, - SignupSchema ->['endpoint']['handler'] = async (ctx) => { - const { req, res, handlers, config } = ctx +const signupEndpoint: GetAPISchema['endpoint']['handler'] = + async (ctx) => { + const { req, res, handlers, config } = ctx - if ( - !isAllowedOperation(req, res, { - POST: handlers['signup'], - }) - ) { - return + if ( + !isAllowedOperation(req, res, { + POST: handlers['signup'], + }) + ) { + return + } + + const { cookies } = req + const cartId = cookies[config.cartCookie] + + try { + const body = { ...req.body, cartId } + return await handlers['signup']({ ...ctx, body }) + } catch (error) { + console.error(error) + + const message = + error instanceof CommerceAPIError + ? 'An unexpected error ocurred with the Commerce API' + : 'An unexpected error ocurred' + + res.status(500).json({ data: null, errors: [{ message }] }) + } } - const { cookies } = req - const cartId = cookies[config.cartCookie] - - try { - const body = { ...req.body, cartId } - return await handlers['signup']({ ...ctx, body }) - } catch (error) { - console.error(error) - - const message = - error instanceof CommerceAPIError - ? 'An unexpected error ocurred with the Commerce API' - : 'An unexpected error ocurred' - - res.status(500).json({ data: null, errors: [{ message }] }) - } -} - export default signupEndpoint diff --git a/framework/commerce/types/product.ts b/framework/commerce/types/product.ts index 6a68d8ad1..400ac4160 100644 --- a/framework/commerce/types/product.ts +++ b/framework/commerce/types/product.ts @@ -77,12 +77,11 @@ export type ProductsSchema = { } } -export type GetAllProductPathsOperation< - T extends ProductTypes = ProductTypes -> = { - data: { products: Pick[] } - variables: { first?: number } -} +export type GetAllProductPathsOperation = + { + data: { products: Pick[] } + variables: { first?: number } + } export type GetAllProductsOperation = { data: { products: T['product'][] } diff --git a/framework/commerce/utils/define-property.ts b/framework/commerce/utils/define-property.ts index 875aaaa82..e89735226 100644 --- a/framework/commerce/utils/define-property.ts +++ b/framework/commerce/utils/define-property.ts @@ -11,16 +11,18 @@ type InferValue = Desc extends { ? Record : never -type DefineProperty = - Desc extends { writable: any; set(val: any): any } - ? never - : Desc extends { writable: any; get(): any } - ? never - : Desc extends { writable: false } - ? Readonly> - : Desc extends { writable: true } - ? InferValue - : Readonly> +type DefineProperty< + Prop extends PropertyKey, + Desc extends PropertyDescriptor +> = Desc extends { writable: any; set(val: any): any } + ? never + : Desc extends { writable: any; get(): any } + ? never + : Desc extends { writable: false } + ? Readonly> + : Desc extends { writable: true } + ? InferValue + : Readonly> export default function defineProperty< Obj extends object, diff --git a/framework/saleor/README.md b/framework/saleor/README.md index 00af272b0..dba25a243 100644 --- a/framework/saleor/README.md +++ b/framework/saleor/README.md @@ -12,7 +12,7 @@ Copy the `.env.template` file in this directory to `.env.local` in the main dire cp framework/saleor/.env.template .env.local ``` -Then, set the environment following variables in your `.env.local`. Both, `NEXT_PUBLIC_SALEOR_API_URL` and `COMMERCE_IMAGE_HOST` must point to your own Saleor instance. +Then, set the environment following variables in your `.env.local`. Both, `NEXT_PUBLIC_SALEOR_API_URL` and `COMMERCE_IMAGE_HOST` must point to your own Saleor instance. ``` COMMERCE_PROVIDER=saleor diff --git a/framework/saleor/api/endpoints/checkout/index.ts b/framework/saleor/api/endpoints/checkout/index.ts index f15672435..f3d68abf0 100644 --- a/framework/saleor/api/endpoints/checkout/index.ts +++ b/framework/saleor/api/endpoints/checkout/index.ts @@ -6,11 +6,7 @@ export type CheckoutAPI = GetAPISchema export type CheckoutEndpoint = CheckoutAPI['endpoint'] -const checkout: CheckoutEndpoint['handlers']['checkout'] = async ({ - req, - res, - config, -}) => { +const checkout: CheckoutEndpoint['handlers']['checkout'] = async ({ req, res, config }) => { try { const html = ` diff --git a/framework/saleor/api/operations/get-all-pages.ts b/framework/saleor/api/operations/get-all-pages.ts index f3ed54e27..7ba07450d 100644 --- a/framework/saleor/api/operations/get-all-pages.ts +++ b/framework/saleor/api/operations/get-all-pages.ts @@ -6,14 +6,9 @@ import * as Query from '../../utils/queries' export type Page = any - export type GetAllPagesResult< - T extends { pages: any[] } = { pages: Page[] } - > = T - -export default function getAllPagesOperation({ - commerce, -}: OperationContext) { +export type GetAllPagesResult = T +export default function getAllPagesOperation({ commerce }: OperationContext) { async function getAllPages({ query = Query.PageMany, config, @@ -27,7 +22,9 @@ export default function getAllPagesOperation({ } = {}): Promise { const { fetch, locale, locales = ['en-US'] } = commerce.getConfig(config) - const { data } = await fetch(query, { variables }, + const { data } = await fetch( + query, + { variables }, { ...(locale && { headers: { @@ -42,7 +39,7 @@ export default function getAllPagesOperation({ url: `/${locale}/${slug}`, name, })) - + return { pages } } diff --git a/framework/saleor/api/operations/get-all-products.ts b/framework/saleor/api/operations/get-all-products.ts index a1a7ce0c9..4cb7555d6 100644 --- a/framework/saleor/api/operations/get-all-products.ts +++ b/framework/saleor/api/operations/get-all-products.ts @@ -12,9 +12,7 @@ type ReturnType = { products: Product[] } -export default function getAllProductsOperation({ - commerce, -}: OperationContext) { +export default function getAllProductsOperation({ commerce }: OperationContext) { async function getAllProducts({ query = Query.ProductMany, variables, @@ -22,7 +20,7 @@ export default function getAllProductsOperation({ featured, }: { query?: string - variables?: any + variables?: any config?: Partial preview?: boolean featured?: boolean @@ -30,10 +28,9 @@ export default function getAllProductsOperation({ const { fetch, locale } = commerce.getConfig(config) if (featured) { - variables = { ...variables, categoryId: 'Q29sbGVjdGlvbjo0' }; + variables = { ...variables, categoryId: 'Q29sbGVjdGlvbjo0' } query = Query.CollectionOne - } - + } const { data }: GraphQLFetcherResult = await fetch( query, @@ -48,7 +45,8 @@ export default function getAllProductsOperation({ ) if (featured) { - const products = data.collection.products?.edges?.map(({ node: p }: ProductCountableEdge) => normalizeProduct(p)) ?? [] + const products = + data.collection.products?.edges?.map(({ node: p }: ProductCountableEdge) => normalizeProduct(p)) ?? [] return { products, @@ -60,7 +58,6 @@ export default function getAllProductsOperation({ products, } } - } return getAllProducts diff --git a/framework/saleor/api/operations/get-page.ts b/framework/saleor/api/operations/get-page.ts index af2d5b8e6..1e6c8cf2b 100644 --- a/framework/saleor/api/operations/get-page.ts +++ b/framework/saleor/api/operations/get-page.ts @@ -6,19 +6,16 @@ import * as Query from '../../utils/queries' export type Page = any - export type GetPageResult = T - -export default function getPageOperation({ - commerce, -}: OperationContext) { +export type GetPageResult = T +export default function getPageOperation({ commerce }: OperationContext) { async function getPage({ query = Query.PageOne, variables, config, }: { query?: string - variables: QueryPageArgs, + variables: QueryPageArgs config?: Partial preview?: boolean }): Promise { @@ -26,7 +23,9 @@ export default function getPageOperation({ const { data: { page }, - } = await fetch(query, { variables }, + } = await fetch( + query, + { variables }, { ...(locale && { headers: { diff --git a/framework/saleor/api/operations/get-product.ts b/framework/saleor/api/operations/get-product.ts index 85fca934a..e5e8e596f 100644 --- a/framework/saleor/api/operations/get-product.ts +++ b/framework/saleor/api/operations/get-product.ts @@ -1,5 +1,5 @@ import type { OperationContext } from '@commerce/api/operations' -import { normalizeProduct, } from '../../utils' +import { normalizeProduct } from '../../utils' import type { Provider, SaleorConfig } from '..' import * as Query from '../../utils/queries' @@ -12,22 +12,22 @@ type ReturnType = { product: any } -export default function getProductOperation({ - commerce, -}: OperationContext) { +export default function getProductOperation({ commerce }: OperationContext) { async function getProduct({ query = Query.ProductOneBySlug, variables, config: cfg, }: { query?: string - variables: Variables + variables: Variables config?: Partial preview?: boolean }): Promise { const { fetch, locale } = commerce.getConfig(cfg) - const { data } = await fetch(query, { variables }, + const { data } = await fetch( + query, + { variables }, { ...(locale && { headers: { diff --git a/framework/saleor/api/operations/get-site-info.ts b/framework/saleor/api/operations/get-site-info.ts index eca0f2246..917b96ce4 100644 --- a/framework/saleor/api/operations/get-site-info.ts +++ b/framework/saleor/api/operations/get-site-info.ts @@ -18,7 +18,7 @@ export default function getSiteInfoOperation({ commerce }: OperationContext preview?: boolean - variables?: any + variables?: any } = {}): Promise { const cfg = commerce.getConfig(config) diff --git a/framework/saleor/api/operations/login.ts b/framework/saleor/api/operations/login.ts index ca680b82c..9d3a7fe9f 100644 --- a/framework/saleor/api/operations/login.ts +++ b/framework/saleor/api/operations/login.ts @@ -1,28 +1,26 @@ import type { ServerResponse } from 'http' import type { OperationContext } from '@commerce/api/operations' import type { Provider, SaleorConfig } from '..' -import { - throwUserErrors, -} from '../../utils' +import { throwUserErrors } from '../../utils' import * as Mutation from '../../utils/mutations' -export default function loginOperation({ - commerce, -}: OperationContext) { +export default function loginOperation({ commerce }: OperationContext) { async function login({ query = Mutation.SessionCreate, variables, config, }: { query?: string - variables: any + variables: any res: ServerResponse config?: SaleorConfig }): Promise { config = commerce.getConfig(config) - const { data: { customerAccessTokenCreate } } = await config.fetch(query, { variables }) + const { + data: { customerAccessTokenCreate }, + } = await config.fetch(query, { variables }) throwUserErrors(customerAccessTokenCreate?.customerUserErrors) diff --git a/framework/saleor/auth/use-signup.tsx b/framework/saleor/auth/use-signup.tsx index d9e91b468..06df516a5 100644 --- a/framework/saleor/auth/use-signup.tsx +++ b/framework/saleor/auth/use-signup.tsx @@ -29,7 +29,7 @@ export const handler: MutationHook = { email, password, redirectUrl: 'https://localhost.com', - channel: 'default-channel' + channel: 'default-channel', }, }, }) diff --git a/framework/saleor/cart/use-remove-item.tsx b/framework/saleor/cart/use-remove-item.tsx index 81f9c122f..17be31e0c 100644 --- a/framework/saleor/cart/use-remove-item.tsx +++ b/framework/saleor/cart/use-remove-item.tsx @@ -21,9 +21,9 @@ export const handler = { }) return checkoutToCart(data.checkoutLineDelete) }, - useHook: ({ fetch }: MutationHookContext) => < - T extends LineItem | undefined = undefined - > () => { + useHook: + ({ fetch }: MutationHookContext) => + () => { const { mutate } = useCart() return useCallback( @@ -34,6 +34,6 @@ export const handler = { return data }, [fetch, mutate] - ); + ) }, } diff --git a/framework/saleor/cart/use-update-item.tsx b/framework/saleor/cart/use-update-item.tsx index 361ae5cdf..bd596132d 100644 --- a/framework/saleor/cart/use-update-item.tsx +++ b/framework/saleor/cart/use-update-item.tsx @@ -23,11 +23,7 @@ export default useUpdateItem as UseUpdateItem export const handler = { fetchOptions: { query: mutation.CheckoutLineUpdate }, - async fetcher({ - input: { itemId, item }, - options, - fetch - }: HookFetcherContext) { + async fetcher({ input: { itemId, item }, options, fetch }: HookFetcherContext) { if (Number.isInteger(item.quantity)) { // Also allow the update hook to remove an item if the quantity is lower than 1 if (item.quantity! < 1) { @@ -59,7 +55,8 @@ export const handler = { return checkoutToCart(checkoutLinesUpdate) }, - useHook: ({ fetch }: MutationHookContext) => + useHook: + ({ fetch }: MutationHookContext) => ( ctx: { item?: T diff --git a/framework/saleor/utils/checkout-to-cart.ts b/framework/saleor/utils/checkout-to-cart.ts index 638ca815e..0bfcf6ed9 100644 --- a/framework/saleor/utils/checkout-to-cart.ts +++ b/framework/saleor/utils/checkout-to-cart.ts @@ -1,7 +1,15 @@ import { Cart } from '../types' import { CommerceError } from '@commerce/utils/errors' -import { CheckoutLinesAdd, CheckoutLinesUpdate, CheckoutCreate, CheckoutError, Checkout, Maybe, CheckoutLineDelete } from '../schema' +import { + CheckoutLinesAdd, + CheckoutLinesUpdate, + CheckoutCreate, + CheckoutError, + Checkout, + Maybe, + CheckoutLineDelete, +} from '../schema' import { normalizeCart } from './normalize' import throwUserErrors from './throw-user-errors' @@ -11,7 +19,12 @@ export type CheckoutQuery = { errors?: Array } -export type CheckoutPayload = CheckoutLinesAdd | CheckoutLinesUpdate | CheckoutCreate | CheckoutQuery | CheckoutLineDelete +export type CheckoutPayload = + | CheckoutLinesAdd + | CheckoutLinesUpdate + | CheckoutCreate + | CheckoutQuery + | CheckoutLineDelete const checkoutToCart = (checkoutPayload?: Maybe): Cart => { if (!checkoutPayload) { diff --git a/framework/shopify/api/operations/get-all-pages.ts b/framework/shopify/api/operations/get-all-pages.ts index 58bc6a94b..38285ccca 100644 --- a/framework/shopify/api/operations/get-all-pages.ts +++ b/framework/shopify/api/operations/get-all-pages.ts @@ -38,9 +38,11 @@ export default function getAllPagesOperation({ preview?: boolean query?: string } = {}): Promise { - const { fetch, locale, locales = ['en-US', 'es'] } = commerce.getConfig( - config - ) + const { + fetch, + locale, + locales = ['en-US', 'es'], + } = commerce.getConfig(config) const { data } = await fetch( query, diff --git a/framework/shopify/auth/use-login.tsx b/framework/shopify/auth/use-login.tsx index e1de89c99..98b8cdd56 100644 --- a/framework/shopify/auth/use-login.tsx +++ b/framework/shopify/auth/use-login.tsx @@ -21,8 +21,7 @@ export const handler: MutationHook = { async fetcher({ input: { email, password }, options, fetch }) { if (!(email && password)) { throw new CommerceError({ - message: - 'An email and password are required to login', + message: 'An email and password are required to login', }) } @@ -47,16 +46,18 @@ export const handler: MutationHook = { return null }, - useHook: ({ fetch }) => () => { - const { revalidate } = useCustomer() + useHook: + ({ fetch }) => + () => { + const { revalidate } = useCustomer() - return useCallback( - async function login(input) { - const data = await fetch({ input }) - await revalidate() - return data - }, - [fetch, revalidate] - ) - }, + return useCallback( + async function login(input) { + const data = await fetch({ input }) + await revalidate() + return data + }, + [fetch, revalidate] + ) + }, } diff --git a/framework/shopify/auth/use-logout.tsx b/framework/shopify/auth/use-logout.tsx index 30074b8d5..fdba8df84 100644 --- a/framework/shopify/auth/use-logout.tsx +++ b/framework/shopify/auth/use-logout.tsx @@ -22,16 +22,18 @@ export const handler: MutationHook = { setCustomerToken(null) return null }, - useHook: ({ fetch }) => () => { - const { mutate } = useCustomer() + useHook: + ({ fetch }) => + () => { + const { mutate } = useCustomer() - return useCallback( - async function logout() { - const data = await fetch() - await mutate(null, false) - return data - }, - [fetch, mutate] - ) - }, + return useCallback( + async function logout() { + const data = await fetch() + await mutate(null, false) + return data + }, + [fetch, mutate] + ) + }, } diff --git a/framework/shopify/auth/use-signup.tsx b/framework/shopify/auth/use-signup.tsx index 29557e960..0a55c17d4 100644 --- a/framework/shopify/auth/use-signup.tsx +++ b/framework/shopify/auth/use-signup.tsx @@ -50,16 +50,18 @@ export const handler: MutationHook = { return null }, - useHook: ({ fetch }) => () => { - const { revalidate } = useCustomer() + useHook: + ({ fetch }) => + () => { + const { revalidate } = useCustomer() - return useCallback( - async function signup(input) { - const data = await fetch({ input }) - await revalidate() - return data - }, - [fetch, revalidate] - ) - }, + return useCallback( + async function signup(input) { + const data = await fetch({ input }) + await revalidate() + return data + }, + [fetch, revalidate] + ) + }, } diff --git a/framework/shopify/cart/use-add-item.tsx b/framework/shopify/cart/use-add-item.tsx index 5f0809d01..099b52c77 100644 --- a/framework/shopify/cart/use-add-item.tsx +++ b/framework/shopify/cart/use-add-item.tsx @@ -46,16 +46,18 @@ export const handler: MutationHook = { return checkoutToCart(checkoutLineItemsAdd) }, - useHook: ({ fetch }) => () => { - const { mutate } = useCart() + useHook: + ({ fetch }) => + () => { + const { mutate } = useCart() - return useCallback( - async function addItem(input) { - const data = await fetch({ input }) - await mutate(data, false) - return data - }, - [fetch, mutate] - ) - }, + return useCallback( + async function addItem(input) { + const data = await fetch({ input }) + await mutate(data, false) + return data + }, + [fetch, mutate] + ) + }, } diff --git a/framework/shopify/cart/use-cart.tsx b/framework/shopify/cart/use-cart.tsx index d920d058a..bea2a682e 100644 --- a/framework/shopify/cart/use-cart.tsx +++ b/framework/shopify/cart/use-cart.tsx @@ -37,21 +37,23 @@ export const handler: SWRHook = { return checkoutToCart({ checkout }) }, - useHook: ({ useData }) => (input) => { - const response = useData({ - swrOptions: { revalidateOnFocus: false, ...input?.swrOptions }, - }) - return useMemo( - () => - Object.create(response, { - isEmpty: { - get() { - return (response.data?.lineItems.length ?? 0) <= 0 + useHook: + ({ useData }) => + (input) => { + const response = useData({ + swrOptions: { revalidateOnFocus: false, ...input?.swrOptions }, + }) + return useMemo( + () => + Object.create(response, { + isEmpty: { + get() { + return (response.data?.lineItems.length ?? 0) <= 0 + }, + enumerable: true, }, - enumerable: true, - }, - }), - [response] - ) - }, + }), + [response] + ) + }, } diff --git a/framework/shopify/cart/use-remove-item.tsx b/framework/shopify/cart/use-remove-item.tsx index bf9fb2d95..5e193358b 100644 --- a/framework/shopify/cart/use-remove-item.tsx +++ b/framework/shopify/cart/use-remove-item.tsx @@ -41,27 +41,25 @@ export const handler = { }) return checkoutToCart(data.checkoutLineItemsRemove) }, - useHook: ({ fetch }: MutationHookContext) => < - T extends LineItem | undefined = undefined - >( - ctx: { item?: T } = {} - ) => { - const { item } = ctx - const { mutate } = useCart() - const removeItem: RemoveItemFn = async (input) => { - const itemId = input?.id ?? item?.id + useHook: + ({ fetch }: MutationHookContext) => + (ctx: { item?: T } = {}) => { + const { item } = ctx + const { mutate } = useCart() + const removeItem: RemoveItemFn = async (input) => { + const itemId = input?.id ?? item?.id - if (!itemId) { - throw new ValidationError({ - message: 'Invalid input used for this operation', - }) + if (!itemId) { + throw new ValidationError({ + message: 'Invalid input used for this operation', + }) + } + + const data = await fetch({ input: { itemId } }) + await mutate(data, false) + return data } - const data = await fetch({ input: { itemId } }) - await mutate(data, false) - return data - } - - return useCallback(removeItem as RemoveItemFn, [fetch, mutate]) - }, + return useCallback(removeItem as RemoveItemFn, [fetch, mutate]) + }, } diff --git a/framework/shopify/cart/use-update-item.tsx b/framework/shopify/cart/use-update-item.tsx index 3f1cf4315..83d0a220a 100644 --- a/framework/shopify/cart/use-update-item.tsx +++ b/framework/shopify/cart/use-update-item.tsx @@ -64,42 +64,42 @@ export const handler = { return checkoutToCart(checkoutLineItemsUpdate) }, - useHook: ({ fetch }: MutationHookContext) => < - T extends LineItem | undefined = undefined - >( - ctx: { - item?: T - wait?: number - } = {} - ) => { - const { item } = ctx - const { mutate } = useCart() as any + useHook: + ({ fetch }: MutationHookContext) => + ( + ctx: { + item?: T + wait?: number + } = {} + ) => { + const { item } = ctx + const { mutate } = useCart() as any - return useCallback( - debounce(async (input: UpdateItemActionInput) => { - const itemId = input.id ?? item?.id - const productId = input.productId ?? item?.productId - const variantId = input.productId ?? item?.variantId - if (!itemId || !productId || !variantId) { - throw new ValidationError({ - message: 'Invalid input used for this operation', - }) - } + return useCallback( + debounce(async (input: UpdateItemActionInput) => { + const itemId = input.id ?? item?.id + const productId = input.productId ?? item?.productId + const variantId = input.productId ?? item?.variantId + if (!itemId || !productId || !variantId) { + throw new ValidationError({ + message: 'Invalid input used for this operation', + }) + } - const data = await fetch({ - input: { - item: { - productId, - variantId, - quantity: input.quantity, + const data = await fetch({ + input: { + item: { + productId, + variantId, + quantity: input.quantity, + }, + itemId, }, - itemId, - }, - }) - await mutate(data, false) - return data - }, ctx.wait ?? 500), - [fetch, mutate] - ) - }, + }) + await mutate(data, false) + return data + }, ctx.wait ?? 500), + [fetch, mutate] + ) + }, } diff --git a/framework/shopify/customer/use-customer.tsx b/framework/shopify/customer/use-customer.tsx index be097fe80..5c470e7de 100644 --- a/framework/shopify/customer/use-customer.tsx +++ b/framework/shopify/customer/use-customer.tsx @@ -21,12 +21,14 @@ export const handler: SWRHook = { } return null }, - useHook: ({ useData }) => (input) => { - return useData({ - swrOptions: { - revalidateOnFocus: false, - ...input?.swrOptions, - }, - }) - }, + useHook: + ({ useData }) => + (input) => { + return useData({ + swrOptions: { + revalidateOnFocus: false, + ...input?.swrOptions, + }, + }) + }, } diff --git a/framework/shopify/product/use-search.tsx b/framework/shopify/product/use-search.tsx index 72dc8bb65..b975c03fe 100644 --- a/framework/shopify/product/use-search.tsx +++ b/framework/shopify/product/use-search.tsx @@ -71,19 +71,21 @@ export const handler: SWRHook = { found: !!products?.length, } }, - useHook: ({ useData }) => (input = {}) => { - return useData({ - input: [ - ['search', input.search], - ['categoryId', input.categoryId], - ['brandId', input.brandId], - ['sort', input.sort], - ['locale', input.locale], - ], - swrOptions: { - revalidateOnFocus: false, - ...input.swrOptions, - }, - }) - }, + useHook: + ({ useData }) => + (input = {}) => { + return useData({ + input: [ + ['search', input.search], + ['categoryId', input.categoryId], + ['brandId', input.brandId], + ['sort', input.sort], + ['locale', input.locale], + ], + swrOptions: { + revalidateOnFocus: false, + ...input.swrOptions, + }, + }) + }, } diff --git a/framework/swell/api/operations/get-all-pages.ts b/framework/swell/api/operations/get-all-pages.ts index 6abaa155c..e157c0138 100644 --- a/framework/swell/api/operations/get-all-pages.ts +++ b/framework/swell/api/operations/get-all-pages.ts @@ -2,9 +2,8 @@ import { Provider, SwellConfig } from '..' import type { OperationContext } from '@commerce/api/operations' import type { Page } from '../../types/page' -export type GetAllPagesResult< - T extends { pages: any[] } = { pages: Page[] } -> = T +export type GetAllPagesResult = + T export default function getAllPagesOperation({ commerce, diff --git a/framework/swell/auth/use-login.tsx b/framework/swell/auth/use-login.tsx index 587163d97..5a34975fa 100644 --- a/framework/swell/auth/use-login.tsx +++ b/framework/swell/auth/use-login.tsx @@ -59,16 +59,18 @@ export const handler: MutationHook = { return null }, - useHook: ({ fetch }) => () => { - const { revalidate } = useCustomer() + useHook: + ({ fetch }) => + () => { + const { revalidate } = useCustomer() - return useCallback( - async function login(input) { - const data = await fetch({ input }) - await revalidate() - return data - }, - [fetch, revalidate] - ) - }, + return useCallback( + async function login(input) { + const data = await fetch({ input }) + await revalidate() + return data + }, + [fetch, revalidate] + ) + }, } diff --git a/framework/swell/auth/use-logout.tsx b/framework/swell/auth/use-logout.tsx index 7dc7411ec..0e08ce9c6 100644 --- a/framework/swell/auth/use-logout.tsx +++ b/framework/swell/auth/use-logout.tsx @@ -22,16 +22,18 @@ export const handler: MutationHook = { setCustomerToken(null) return null }, - useHook: ({ fetch }) => () => { - const { mutate } = useCustomer() + useHook: + ({ fetch }) => + () => { + const { mutate } = useCustomer() - return useCallback( - async function logout() { - const data = await fetch() - await mutate(null, false) - return data - }, - [fetch, mutate] - ) - }, + return useCallback( + async function logout() { + const data = await fetch() + await mutate(null, false) + return data + }, + [fetch, mutate] + ) + }, } diff --git a/framework/swell/auth/use-signup.tsx b/framework/swell/auth/use-signup.tsx index 674b9861f..246285ece 100644 --- a/framework/swell/auth/use-signup.tsx +++ b/framework/swell/auth/use-signup.tsx @@ -44,16 +44,18 @@ export const handler: MutationHook = { } catch (error) {} return data }, - useHook: ({ fetch }) => () => { - const { revalidate } = useCustomer() + useHook: + ({ fetch }) => + () => { + const { revalidate } = useCustomer() - return useCallback( - async function signup(input) { - const data = await fetch({ input }) - await revalidate() - return data - }, - [fetch, revalidate] - ) - }, + return useCallback( + async function signup(input) { + const data = await fetch({ input }) + await revalidate() + return data + }, + [fetch, revalidate] + ) + }, } diff --git a/framework/swell/cart/use-add-item.tsx b/framework/swell/cart/use-add-item.tsx index e324a44d9..dac4fb9bb 100644 --- a/framework/swell/cart/use-add-item.tsx +++ b/framework/swell/cart/use-add-item.tsx @@ -44,16 +44,18 @@ export const handler: MutationHook = { return checkoutToCart(response) as any }, - useHook: ({ fetch }) => () => { - const { mutate } = useCart() + useHook: + ({ fetch }) => + () => { + const { mutate } = useCart() - return useCallback( - async function addItem(input) { - const data = await fetch({ input }) - await mutate(data, false) - return data - }, - [fetch, mutate] - ) - }, + return useCallback( + async function addItem(input) { + const data = await fetch({ input }) + await mutate(data, false) + return data + }, + [fetch, mutate] + ) + }, } diff --git a/framework/swell/cart/use-cart.tsx b/framework/swell/cart/use-cart.tsx index 885f889f5..7dc48b052 100644 --- a/framework/swell/cart/use-cart.tsx +++ b/framework/swell/cart/use-cart.tsx @@ -17,21 +17,23 @@ export const handler: SWRHook = { return cart ? normalizeCart(cart) : null }, - useHook: ({ useData }) => (input) => { - const response = useData({ - swrOptions: { revalidateOnFocus: false, ...input?.swrOptions }, - }) - return useMemo( - () => - Object.create(response, { - isEmpty: { - get() { - return (response.data?.lineItems.length ?? 0) <= 0 + useHook: + ({ useData }) => + (input) => { + const response = useData({ + swrOptions: { revalidateOnFocus: false, ...input?.swrOptions }, + }) + return useMemo( + () => + Object.create(response, { + isEmpty: { + get() { + return (response.data?.lineItems.length ?? 0) <= 0 + }, + enumerable: true, }, - enumerable: true, - }, - }), - [response] - ) - }, + }), + [response] + ) + }, } diff --git a/framework/swell/cart/use-remove-item.tsx b/framework/swell/cart/use-remove-item.tsx index 7ef3af7cd..fe1ee24ca 100644 --- a/framework/swell/cart/use-remove-item.tsx +++ b/framework/swell/cart/use-remove-item.tsx @@ -33,17 +33,19 @@ export const handler = { return checkoutToCart(response) }, - useHook: ({ fetch }: MutationHookContext) => () => { - const { mutate } = useCart() + useHook: + ({ fetch }: MutationHookContext) => + () => { + const { mutate } = useCart() - return useCallback( - async function removeItem(input) { - const data = await fetch({ input: { itemId: input.id } }) - await mutate(data, false) + return useCallback( + async function removeItem(input) { + const data = await fetch({ input: { itemId: input.id } }) + await mutate(data, false) - return data - }, - [fetch, mutate] - ) - }, + return data + }, + [fetch, mutate] + ) + }, } diff --git a/framework/swell/cart/use-update-item.tsx b/framework/swell/cart/use-update-item.tsx index 631a394f6..5d705c5e5 100644 --- a/framework/swell/cart/use-update-item.tsx +++ b/framework/swell/cart/use-update-item.tsx @@ -57,43 +57,43 @@ export const handler = { return checkoutToCart(response) }, - useHook: ({ fetch }: MutationHookContext) => < - T extends LineItem | undefined = undefined - >( - ctx: { - item?: T - wait?: number - } = {} - ) => { - const { item } = ctx - const { mutate, data: cartData } = useCart() as any + useHook: + ({ fetch }: MutationHookContext) => + ( + ctx: { + item?: T + wait?: number + } = {} + ) => { + const { item } = ctx + const { mutate, data: cartData } = useCart() as any - return useCallback( - debounce(async (input: UpdateItemActionInput) => { - const firstLineItem = cartData.lineItems[0] - const itemId = item?.id || firstLineItem.id - const productId = item?.productId || firstLineItem.productId - const variantId = item?.variant.id || firstLineItem.variant.id - if (!itemId || !productId) { - throw new ValidationError({ - message: 'Invalid input used for this operation', - }) - } + return useCallback( + debounce(async (input: UpdateItemActionInput) => { + const firstLineItem = cartData.lineItems[0] + const itemId = item?.id || firstLineItem.id + const productId = item?.productId || firstLineItem.productId + const variantId = item?.variant.id || firstLineItem.variant.id + if (!itemId || !productId) { + throw new ValidationError({ + message: 'Invalid input used for this operation', + }) + } - const data = await fetch({ - input: { - item: { - productId, - variantId, - quantity: input.quantity, + const data = await fetch({ + input: { + item: { + productId, + variantId, + quantity: input.quantity, + }, + itemId, }, - itemId, - }, - }) - await mutate(data, false) - return data - }, ctx.wait ?? 500), - [fetch, mutate] - ) - }, + }) + await mutate(data, false) + return data + }, ctx.wait ?? 500), + [fetch, mutate] + ) + }, } diff --git a/framework/swell/customer/use-customer.tsx b/framework/swell/customer/use-customer.tsx index 50e85d3f6..3371db89c 100644 --- a/framework/swell/customer/use-customer.tsx +++ b/framework/swell/customer/use-customer.tsx @@ -16,12 +16,14 @@ export const handler: SWRHook = { }) return data ? normalizeCustomer(data) : null }, - useHook: ({ useData }) => (input) => { - return useData({ - swrOptions: { - revalidateOnFocus: false, - ...input?.swrOptions, - }, - }) - }, + useHook: + ({ useData }) => + (input) => { + return useData({ + swrOptions: { + revalidateOnFocus: false, + ...input?.swrOptions, + }, + }) + }, } diff --git a/framework/swell/product/use-search.tsx b/framework/swell/product/use-search.tsx index bdcca3ff2..62f813368 100644 --- a/framework/swell/product/use-search.tsx +++ b/framework/swell/product/use-search.tsx @@ -42,18 +42,20 @@ export const handler: SWRHook = { found, } }, - useHook: ({ useData }) => (input = {}) => { - return useData({ - input: [ - ['search', input.search], - ['categoryId', input.categoryId], - ['brandId', input.brandId], - ['sort', input.sort], - ], - swrOptions: { - revalidateOnFocus: false, - ...input.swrOptions, - }, - }) - }, + useHook: + ({ useData }) => + (input = {}) => { + return useData({ + input: [ + ['search', input.search], + ['categoryId', input.categoryId], + ['brandId', input.brandId], + ['sort', input.sort], + ], + swrOptions: { + revalidateOnFocus: false, + ...input.swrOptions, + }, + }) + }, } diff --git a/framework/vendure/api/operations/get-all-pages.ts b/framework/vendure/api/operations/get-all-pages.ts index 43170cda8..f76b7390f 100644 --- a/framework/vendure/api/operations/get-all-pages.ts +++ b/framework/vendure/api/operations/get-all-pages.ts @@ -4,9 +4,8 @@ import { Provider } from '../../../bigcommerce/api' export type Page = any -export type GetAllPagesResult< - T extends { pages: any[] } = { pages: Page[] } -> = T +export type GetAllPagesResult = + T export default function getAllPagesOperation({ commerce, diff --git a/framework/vendure/auth/use-login.tsx b/framework/vendure/auth/use-login.tsx index f10e38d0f..a9b63e895 100644 --- a/framework/vendure/auth/use-login.tsx +++ b/framework/vendure/auth/use-login.tsx @@ -36,16 +36,18 @@ export const handler: MutationHook = { return null }, - useHook: ({ fetch }) => () => { - const { revalidate } = useCustomer() + useHook: + ({ fetch }) => + () => { + const { revalidate } = useCustomer() - return useCallback( - async function login(input) { - const data = await fetch({ input }) - await revalidate() - return data - }, - [fetch, revalidate] - ) - }, + return useCallback( + async function login(input) { + const data = await fetch({ input }) + await revalidate() + return data + }, + [fetch, revalidate] + ) + }, } diff --git a/framework/vendure/auth/use-signup.tsx b/framework/vendure/auth/use-signup.tsx index 864b1a18a..4018baed6 100644 --- a/framework/vendure/auth/use-signup.tsx +++ b/framework/vendure/auth/use-signup.tsx @@ -54,16 +54,18 @@ export const handler: MutationHook = { return null }, - useHook: ({ fetch }) => () => { - const { revalidate } = useCustomer() + useHook: + ({ fetch }) => + () => { + const { revalidate } = useCustomer() - return useCallback( - async function signup(input) { - const data = await fetch({ input }) - await revalidate() - return data - }, - [fetch, revalidate] - ) - }, + return useCallback( + async function signup(input) { + const data = await fetch({ input }) + await revalidate() + return data + }, + [fetch, revalidate] + ) + }, } diff --git a/framework/vendure/cart/use-add-item.tsx b/framework/vendure/cart/use-add-item.tsx index 88bff40e4..7c131eaea 100644 --- a/framework/vendure/cart/use-add-item.tsx +++ b/framework/vendure/cart/use-add-item.tsx @@ -37,16 +37,18 @@ export const handler: MutationHook = { } throw new CommerceError(addItemToOrder) }, - useHook: ({ fetch }) => () => { - const { mutate } = useCart() + useHook: + ({ fetch }) => + () => { + const { mutate } = useCart() - return useCallback( - async function addItem(input) { - const data = await fetch({ input }) - await mutate(data, false) - return data - }, - [fetch, mutate] - ) - }, + return useCallback( + async function addItem(input) { + const data = await fetch({ input }) + await mutate(data, false) + return data + }, + [fetch, mutate] + ) + }, } diff --git a/framework/vendure/cart/use-cart.tsx b/framework/vendure/cart/use-cart.tsx index 400df0c8c..de42036b8 100644 --- a/framework/vendure/cart/use-cart.tsx +++ b/framework/vendure/cart/use-cart.tsx @@ -23,22 +23,24 @@ export const handler: SWRHook = { const { activeOrder } = await fetch(options) return activeOrder ? normalizeCart(activeOrder) : null }, - useHook: ({ useData }) => (input) => { - const response = useData({ - swrOptions: { revalidateOnFocus: false, ...input?.swrOptions }, - }) + useHook: + ({ useData }) => + (input) => { + const response = useData({ + swrOptions: { revalidateOnFocus: false, ...input?.swrOptions }, + }) - return useMemo( - () => - Object.create(response, { - isEmpty: { - get() { - return (response.data?.lineItems.length ?? 0) <= 0 + return useMemo( + () => + Object.create(response, { + isEmpty: { + get() { + return (response.data?.lineItems.length ?? 0) <= 0 + }, + enumerable: true, }, - enumerable: true, - }, - }), - [response] - ) - }, + }), + [response] + ) + }, } diff --git a/framework/vendure/cart/use-remove-item.tsx b/framework/vendure/cart/use-remove-item.tsx index 39855e1ac..dde8e18b3 100644 --- a/framework/vendure/cart/use-remove-item.tsx +++ b/framework/vendure/cart/use-remove-item.tsx @@ -37,16 +37,18 @@ export const handler: MutationHook = { } throw new CommerceError(removeOrderLine) }, - useHook: ({ fetch }) => () => { - const { mutate } = useCart() + useHook: + ({ fetch }) => + () => { + const { mutate } = useCart() - return useCallback( - async function removeItem(input) { - const data = await fetch({ input: { itemId: input.id } }) - await mutate(data, false) - return data - }, - [fetch, mutate] - ) - }, + return useCallback( + async function removeItem(input) { + const data = await fetch({ input: { itemId: input.id } }) + await mutate(data, false) + return data + }, + [fetch, mutate] + ) + }, } diff --git a/framework/vendure/cart/use-update-item.tsx b/framework/vendure/cart/use-update-item.tsx index d04de944a..029966c5a 100644 --- a/framework/vendure/cart/use-update-item.tsx +++ b/framework/vendure/cart/use-update-item.tsx @@ -42,39 +42,41 @@ export const handler = { } throw new CommerceError(adjustOrderLine) }, - useHook: ({ fetch }: MutationHookContext) => ( - ctx: { - item?: LineItem - wait?: number - } = {} - ) => { - const { item } = ctx - const { mutate } = useCart() + useHook: + ({ fetch }: MutationHookContext) => + ( + ctx: { + item?: LineItem + wait?: number + } = {} + ) => { + const { item } = ctx + const { mutate } = useCart() - return useCallback( - async function addItem(input: UpdateItemActionInput) { - const itemId = item?.id - const productId = input.productId ?? item?.productId - const variantId = input.productId ?? item?.variantId - if (!itemId || !productId || !variantId) { - throw new ValidationError({ - message: 'Invalid input used for this operation', - }) - } - const data = await fetch({ - input: { - item: { - productId, - variantId, - quantity: input.quantity, + return useCallback( + async function addItem(input: UpdateItemActionInput) { + const itemId = item?.id + const productId = input.productId ?? item?.productId + const variantId = input.productId ?? item?.variantId + if (!itemId || !productId || !variantId) { + throw new ValidationError({ + message: 'Invalid input used for this operation', + }) + } + const data = await fetch({ + input: { + item: { + productId, + variantId, + quantity: input.quantity, + }, + itemId, }, - itemId, - }, - }) - await mutate(data, false) - return data - }, - [fetch, mutate] - ) - }, + }) + await mutate(data, false) + return data + }, + [fetch, mutate] + ) + }, } diff --git a/framework/vendure/customer/use-customer.tsx b/framework/vendure/customer/use-customer.tsx index 1d5a53d48..e9047485b 100644 --- a/framework/vendure/customer/use-customer.tsx +++ b/framework/vendure/customer/use-customer.tsx @@ -22,12 +22,14 @@ export const handler: SWRHook = { } as any) : null }, - useHook: ({ useData }) => (input) => { - return useData({ - swrOptions: { - revalidateOnFocus: false, - ...input?.swrOptions, - }, - }) - }, + useHook: + ({ useData }) => + (input) => { + return useData({ + swrOptions: { + revalidateOnFocus: false, + ...input?.swrOptions, + }, + }) + }, } diff --git a/framework/vendure/product/use-search.tsx b/framework/vendure/product/use-search.tsx index e34db2ac2..827b23623 100644 --- a/framework/vendure/product/use-search.tsx +++ b/framework/vendure/product/use-search.tsx @@ -45,18 +45,20 @@ export const handler: SWRHook = { products: search.items.map((item) => normalizeSearchResult(item)) ?? [], } }, - useHook: ({ useData }) => (input = {}) => { - return useData({ - input: [ - ['search', input.search], - ['categoryId', input.categoryId], - ['brandId', input.brandId], - ['sort', input.sort], - ], - swrOptions: { - revalidateOnFocus: false, - ...input.swrOptions, - }, - }) - }, + useHook: + ({ useData }) => + (input = {}) => { + return useData({ + input: [ + ['search', input.search], + ['categoryId', input.categoryId], + ['brandId', input.brandId], + ['sort', input.sort], + ], + swrOptions: { + revalidateOnFocus: false, + ...input.swrOptions, + }, + }) + }, }