diff --git a/packages/commerce/src/api/endpoints/wishlist.ts b/packages/commerce/src/api/endpoints/wishlist.ts index 233ac5294..0ce9de8ab 100644 --- a/packages/commerce/src/api/endpoints/wishlist.ts +++ b/packages/commerce/src/api/endpoints/wishlist.ts @@ -34,13 +34,13 @@ const wishlistEndpoint: GetAPISchema< // Add an item to the wishlist if (req.method === 'POST') { - const body = { ...req.body, customerToken } + const body = { ...req.body.variables, customerToken } return await handlers['addItem']({ ...ctx, body }) } // Remove an item from the wishlist if (req.method === 'DELETE') { - const body = { ...req.body, customerToken } + const body = { ...req.body.variables, customerToken } return await handlers['removeItem']({ ...ctx, body }) } } catch (error) { diff --git a/packages/commerce/src/utils/types.ts b/packages/commerce/src/utils/types.ts index 317fea165..cb79efa37 100644 --- a/packages/commerce/src/utils/types.ts +++ b/packages/commerce/src/utils/types.ts @@ -27,6 +27,7 @@ export type FetcherOptions
= { method?: string variables?: any body?: Body + useAdminApi?: boolean } export type HookFetcher = ( diff --git a/packages/shopify/src/api/index.ts b/packages/shopify/src/api/index.ts index 7ae6a4206..b277f9b25 100644 --- a/packages/shopify/src/api/index.ts +++ b/packages/shopify/src/api/index.ts @@ -5,7 +5,8 @@ import { } from '@vercel/commerce/api' import { - API_URL, + STOREFRONT_API_URL, + ADMIN_ACCESS_TOKEN, API_TOKEN, SHOPIFY_CUSTOMER_TOKEN_COOKIE, SHOPIFY_CHECKOUT_ID_COOKIE, @@ -15,7 +16,7 @@ import fetchGraphqlApi from './utils/fetch-graphql-api' import * as operations from './operations' -if (!API_URL) { +if (!STOREFRONT_API_URL) { throw new Error( `The environment variable NEXT_PUBLIC_SHOPIFY_STORE_DOMAIN is missing and it's required to access your store` ) @@ -31,7 +32,7 @@ export interface ShopifyConfig extends CommerceAPIConfig {} const ONE_DAY = 60 * 60 * 24 const config: ShopifyConfig = { - commerceUrl: API_URL, + commerceUrl: STOREFRONT_API_URL, apiToken: API_TOKEN, customerCookie: SHOPIFY_CUSTOMER_TOKEN_COOKIE, cartCookie: SHOPIFY_CHECKOUT_ID_COOKIE, diff --git a/packages/shopify/src/api/utils/fetch-graphql-api.ts b/packages/shopify/src/api/utils/fetch-graphql-api.ts index 1970db572..4cd3bb9f8 100644 --- a/packages/shopify/src/api/utils/fetch-graphql-api.ts +++ b/packages/shopify/src/api/utils/fetch-graphql-api.ts @@ -1,36 +1,66 @@ import type { GraphQLFetcher } from '@vercel/commerce/api' import fetch from './fetch' -import { API_URL, API_TOKEN } from '../../const' +import { + STOREFRONT_API_URL, + ADMIN_API_URL, + API_TOKEN, + ADMIN_ACCESS_TOKEN, +} from '../../const' import { getError } from '../../utils/handle-fetch-response' const fetchGraphqlApi: GraphQLFetcher = async ( query: string, { variables } = {}, - fetchOptions + fetchOptions, + useAdminApi = false ) => { try { - const res = await fetch(API_URL, { - ...fetchOptions, - method: 'POST', - headers: { - 'X-Shopify-Storefront-Access-Token': API_TOKEN!, - ...fetchOptions?.headers, - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - query, - variables, - }), - }) + if (useAdminApi) { + console.log('graphQL fetch from admin api') - const { data, errors, status } = await res.json() + const res = await fetch(ADMIN_API_URL, { + ...fetchOptions, + method: 'POST', + headers: { + 'X-Shopify-Access-Token': ADMIN_ACCESS_TOKEN!, + ...fetchOptions?.headers, + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + query, + variables, + }), + }) + const { data, errors, status } = await res.json() + if (errors) { + throw getError(errors, status) + } - if (errors) { - throw getError(errors, status) + return { data, res } + } else { + console.log('graphQL fetch from storefront api') + + const res = await fetch(STOREFRONT_API_URL, { + ...fetchOptions, + method: 'POST', + headers: { + 'X-Shopify-Storefront-Access-Token': API_TOKEN!, + ...fetchOptions?.headers, + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + query, + variables, + }), + }) + const { data, errors, status } = await res.json() + if (errors) { + throw getError(errors, status) + } + + return { data, res } } - - return { data, res } } catch (err) { throw getError( [ diff --git a/packages/shopify/src/const.ts b/packages/shopify/src/const.ts index a8ee70586..fc230a61a 100644 --- a/packages/shopify/src/const.ts +++ b/packages/shopify/src/const.ts @@ -8,6 +8,9 @@ export const STORE_DOMAIN = process.env.NEXT_PUBLIC_SHOPIFY_STORE_DOMAIN export const SHOPIFY_COOKIE_EXPIRE = 30 -export const API_URL = `https://${STORE_DOMAIN}/api/2021-07/graphql.json` +export const STOREFRONT_API_URL = `https://${STORE_DOMAIN}/api/2022-01/graphql.json` +export const ADMIN_API_URL = `https://${STORE_DOMAIN}/admin/api/2022-01/graphql.json` export const API_TOKEN = process.env.NEXT_PUBLIC_SHOPIFY_STOREFRONT_ACCESS_TOKEN +export const ADMIN_ACCESS_TOKEN = + process.env.NEXT_PUBLIC_SHOPIFY_ADMIN_ACCESS_TOKEN diff --git a/packages/shopify/src/fetcher.ts b/packages/shopify/src/fetcher.ts index 64f492620..bc6fa5a6b 100644 --- a/packages/shopify/src/fetcher.ts +++ b/packages/shopify/src/fetcher.ts @@ -1,27 +1,58 @@ import { Fetcher } from '@vercel/commerce/utils/types' -import { API_TOKEN, API_URL } from './const' +import { + API_TOKEN, + STOREFRONT_API_URL, + ADMIN_ACCESS_TOKEN, + ADMIN_API_URL, +} from './const' import { handleFetchResponse } from './utils' const fetcher: Fetcher = async ({ - url = API_URL, + url = STOREFRONT_API_URL, method = 'POST', variables, query, + useAdminApi = false, }) => { const { locale, ...vars } = variables ?? {} - return handleFetchResponse( - await fetch(url, { - method, - body: JSON.stringify({ query, variables: vars }), - headers: { - 'X-Shopify-Storefront-Access-Token': API_TOKEN!, - 'Content-Type': 'application/json', - ...(locale && { - 'Accept-Language': locale, - }), - }, - }) - ) + + if (method === 'POST' || method === 'DELETE') { + if (useAdminApi) { + url = ADMIN_API_URL + console.log('admin api', url, query, method) + + return handleFetchResponse( + await fetch(url, { + method, + body: JSON.stringify({ query, variables: vars }), + headers: { + 'X-Shopify-Access-Token': ADMIN_ACCESS_TOKEN!, + 'Content-Type': 'application/json', + ...(locale && { + 'Accept-Language': locale, + }), + }, + }) + ) + } else { + console.log('storefront api:', url, query, method) + return handleFetchResponse( + await fetch(url, { + method, + body: JSON.stringify({ query, variables: vars }), + headers: { + 'X-Shopify-Storefront-Access-Token': API_TOKEN!, + 'Content-Type': 'application/json', + ...(locale && { + 'Accept-Language': locale, + }), + }, + }) + ) + } + } + + return handleFetchResponse(await fetch(url)) } export default fetcher diff --git a/packages/shopify/src/provider.ts b/packages/shopify/src/provider.ts index 00db5c1d3..289b76f33 100644 --- a/packages/shopify/src/provider.ts +++ b/packages/shopify/src/provider.ts @@ -12,6 +12,10 @@ import { handler as useLogin } from './auth/use-login' import { handler as useLogout } from './auth/use-logout' import { handler as useSignup } from './auth/use-signup' +import { handler as useWishlist } from './wishlist/use-wishlist' +import { handler as useWishlistAddItem } from './wishlist/use-add-item' +import { handler as useWishlistRemoveItem } from './wishlist/use-remove-item' + import fetcher from './fetcher' export const shopifyProvider = { @@ -22,6 +26,11 @@ export const shopifyProvider = { customer: { useCustomer }, products: { useSearch }, auth: { useLogin, useLogout, useSignup }, + wishlist: { + useWishlist, + useAddItem: useWishlistAddItem, + useRemoveItem: useWishlistRemoveItem, + }, } export type ShopifyProvider = typeof shopifyProvider diff --git a/packages/shopify/src/types/wishlist.ts b/packages/shopify/src/types/wishlist.ts index 3778ff7e4..a2e77dc06 100644 --- a/packages/shopify/src/types/wishlist.ts +++ b/packages/shopify/src/types/wishlist.ts @@ -19,12 +19,6 @@ export type WishlistTypes = { customer: Customer } -export type RemoveItemHook