From 0bf01447f673aea4b173cea8724397a93b4a6b37 Mon Sep 17 00:00:00 2001 From: Chloe Date: Tue, 25 Jun 2024 10:58:06 +0700 Subject: [PATCH] set up admin api Signed-off-by: Chloe --- components/form/file-input.tsx | 15 ++++-- components/orders/mobile-order-actions.tsx | 3 +- lib/constants.ts | 1 + lib/shopify/index.ts | 53 ++++++++++++++++++++++ lib/utils.ts | 3 +- 5 files changed, 70 insertions(+), 5 deletions(-) diff --git a/components/form/file-input.tsx b/components/form/file-input.tsx index 7c1ab6aa5..f55be2281 100644 --- a/components/form/file-input.tsx +++ b/components/form/file-input.tsx @@ -1,5 +1,5 @@ import { PhotoIcon } from '@heroicons/react/24/outline'; -import { useId } from 'react'; +import { ChangeEvent, useId, useState } from 'react'; type FileInputProps = { name: string; @@ -8,6 +8,14 @@ type FileInputProps = { const FileInput = ({ name, label }: FileInputProps) => { const id = useId(); + const [file, setFile] = useState(); + + const onFileChange = (e: ChangeEvent) => { + if (e.target.files && e.target.files.length > 0) { + setFile(e.target.files[0]); + } + }; + return (
@@ -17,15 +25,16 @@ const FileInput = ({ name, label }: FileInputProps) => {

or drag and drop

+ {file &&

{file.name}

} ); }; diff --git a/components/orders/mobile-order-actions.tsx b/components/orders/mobile-order-actions.tsx index 5b8ced240..4cfc02f1c 100644 --- a/components/orders/mobile-order-actions.tsx +++ b/components/orders/mobile-order-actions.tsx @@ -44,8 +44,9 @@ const MobileOrderActions = ({ order }: { order: Order }) => { diff --git a/lib/constants.ts b/lib/constants.ts index 2d246a218..21b728f12 100644 --- a/lib/constants.ts +++ b/lib/constants.ts @@ -36,6 +36,7 @@ export const HIDDEN_PRODUCT_TAG = 'nextjs-frontend-hidden'; export const DEFAULT_OPTION = 'Default Title'; export const SHOPIFY_GRAPHQL_API_ENDPOINT = '/api/2024-04/graphql.json'; export const SHOPIFY_GRAPHQL_CUSTOMER_API_ENDPOINT = '/account/customer/api/2024-07/graphql'; +export const SHOPIFY_GRAPHQL_ADMIN_ADMIN_API_ENDPOINT = '/admin/api/2024-04/graphql.json'; export const CORE_WAIVER = 'core-waiver'; export const CORE_VARIANT_ID_KEY = 'coreVariantId'; diff --git a/lib/shopify/index.ts b/lib/shopify/index.ts index e4b3c864e..932cc056f 100644 --- a/lib/shopify/index.ts +++ b/lib/shopify/index.ts @@ -6,6 +6,7 @@ import { MODEL_FILTER_ID, PRICE_FILTER_ID, PRODUCT_METAFIELD_PREFIX, + SHOPIFY_GRAPHQL_ADMIN_ADMIN_API_ENDPOINT, SHOPIFY_GRAPHQL_API_ENDPOINT, SHOPIFY_GRAPHQL_CUSTOMER_API_ENDPOINT, TAGS, @@ -104,12 +105,14 @@ const customerApiUrl = process.env.SHOPIFY_CUSTOMER_ACCOUNT_API_URL; const storefrontEndpoint = `${domain}${SHOPIFY_GRAPHQL_API_ENDPOINT}`; const customerEndpoint = `${customerApiUrl}/${SHOPIFY_GRAPHQL_CUSTOMER_API_ENDPOINT}`; +const adminEndpoint = `${domain}${SHOPIFY_GRAPHQL_ADMIN_ADMIN_API_ENDPOINT}`; const userAgent = '*'; const placeholderProductImage = 'https://cdn.shopify.com/shopifycloud/customer-account-web/production/assets/8bc6556601c510713d76.svg'; const key = process.env.SHOPIFY_STOREFRONT_ACCESS_TOKEN!; +const adminAccessToken = process.env.SHOPIFY_ADMIN_API_ACCESS_TOKEN!; type ExtractVariables = T extends { variables: object } ? T['variables'] : never; @@ -169,6 +172,56 @@ export async function shopifyFetch({ } } +async function adminFetch({ + headers, + query, + variables +}: { + headers?: HeadersInit; + query: string; + variables?: ExtractVariables; +}): Promise<{ status: number; body: T } | never> { + try { + const result = await fetch(adminEndpoint, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-Shopify-Access-Token': adminAccessToken, + ...headers + }, + body: JSON.stringify({ + ...(query && { query }), + ...(variables && { variables }) + }) + }); + + const body = await result.json(); + + if (body.errors) { + throw body.errors[0]; + } + + return { + status: result.status, + body + }; + } catch (e) { + if (isShopifyError(e)) { + throw { + cause: e.cause?.toString() || 'unknown', + status: e.status || 500, + message: e.message, + query + }; + } + + throw { + error: e, + query + }; + } +} + export async function shopifyCustomerFetch({ query, variables diff --git a/lib/utils.ts b/lib/utils.ts index 1233b5c99..766ec9773 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -20,7 +20,8 @@ export const validateEnvironmentVariables = () => { 'SHOPIFY_CUSTOMER_ACCOUNT_API_CLIENT_ID', 'SHOPIFY_CUSTOMER_ACCOUNT_API_URL', 'SHOPIFY_CUSTOMER_API_VERSION', - 'SHOPIFY_ORIGIN_URL' + 'SHOPIFY_ORIGIN_URL', + 'SHOPIFY_ADMIN_API_ACCESS_TOKEN' ]; const missingEnvironmentVariables = [] as string[];