save before rebase

This commit is contained in:
Reza Babaei
2021-09-18 00:48:20 +03:00
committed by Reza Babaei
parent d92907e462
commit 73f3b18dcb
6 changed files with 94 additions and 42 deletions

12
.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,12 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach to application",
"skipFiles": ["<node_internals>/**"],
"port": 9229
}
]
}

View File

@@ -41,25 +41,35 @@ export default function getAllProductsOperation({
} = {}): Promise<T['data']> { } = {}): Promise<T['data']> {
const { fetch, locale } = commerce.getConfig(config) const { fetch, locale } = commerce.getConfig(config)
const { data } = await fetch< try {
GetAllProductsQuery, const { data } = await fetch<
GetAllProductsQueryVariables GetAllProductsQuery,
>( GetAllProductsQueryVariables
query, >(
{ variables }, query,
{ { variables },
...(locale && { {
headers: { ...(locale && {
'Accept-Language': locale, headers: {
}, 'Accept-Language': locale,
}), },
}),
}
)
console.log({ data })
return {
products: [],
} }
)
return { // return {
products: data.products.edges.map(({ node }) => // products: data?.products?.edges
normalizeProduct(node as WooCommerceProduct) // ? data.products.edges.map(({ node }) =>
), // normalizeProduct(node as WooCommerceProduct)
// )
// : [],
// }
} catch (e) {
throw e
} }
} }

View File

@@ -10,6 +10,22 @@ const fetchGraphqlApi: GraphQLFetcher = async (
fetchOptions fetchOptions
) => { ) => {
try { try {
console.log({
resss: {
API_URL,
...fetchOptions,
method: 'POST',
headers: {
...fetchOptions?.headers,
'Content-Type': 'application/json',
},
body: JSON.stringify({
query,
variables,
}),
},
})
const res = await fetch(API_URL, { const res = await fetch(API_URL, {
...fetchOptions, ...fetchOptions,
method: 'POST', method: 'POST',
@@ -26,18 +42,22 @@ const fetchGraphqlApi: GraphQLFetcher = async (
const { data, errors, status } = await res.json() const { data, errors, status } = await res.json()
if (errors) { if (errors) {
throw getError(errors, status) console.log({ errors: errors[0].extensions })
console.log(getError(errors, status))
} }
return { data, res } return { data, res }
} catch (err) { } catch (err) {
throw getError( console.log({ err })
[ console.log(
{ getError(
message: `${err} \n Most likely related to an unexpected output. e.g the store might be protected with password or not available.`, [
}, {
], message: `${err} \n Most likely related to an unexpected output. e.g the store might be protected with password or not available.`,
500 },
],
500
)
) )
} }
} }

View File

@@ -1,7 +1,7 @@
import { FetcherError } from '@commerce/utils/errors' import { FetcherError } from '@commerce/utils/errors'
export function getError(errors: any[] | null, status: number) { export function getError(errors: any[] | null, status: number) {
errors = errors ?? [{ message: 'Failed to fetch Shopify API' }] errors = errors ?? [{ message: 'Failed to fetch WooCommerce API' }]
return new FetcherError({ errors, status }) return new FetcherError({ errors, status })
} }

View File

@@ -1,3 +1,4 @@
import { useEffect } from 'react'
import commerce from '@lib/api/commerce' import commerce from '@lib/api/commerce'
import { Layout } from '@components/common' import { Layout } from '@components/common'
import { ProductCard } from '@components/product' import { ProductCard } from '@components/product'
@@ -11,26 +12,35 @@ export async function getStaticProps({
locales, locales,
}: GetStaticPropsContext) { }: GetStaticPropsContext) {
const config = { locale, locales } const config = { locale, locales }
const productsPromise = commerce.getAllProducts({ const productsPromise = await commerce.getAllProducts({
variables: { first: 6 }, variables: { first: 6 },
config, config,
preview, preview,
// Saleor provider only // // Saleor provider only
...({ featured: true } as any), // ...({ featured: true } as any),
})
// // const pagesPromise = commerce.getAllPages({ config, preview })
// // const siteInfoPromise = commerce.getSiteInfo({ config, preview })
// // const { products } = await productsPromise
// // const { pages } = await pagesPromise
// // const { categories, brands } = await siteInfoPromise
console.log({
query: {
variables: { first: 6 },
config,
preview,
// // Saleor provider only
// ...({ featured: true } as any),
},
productsPromise,
}) })
// const pagesPromise = commerce.getAllPages({ config, preview })
// const siteInfoPromise = commerce.getSiteInfo({ config, preview })
// const { products } = await productsPromise
// const { pages } = await pagesPromise
// const { categories, brands } = await siteInfoPromise
return { return {
// props: { props: {
// products, products: [],
// categories, // categories,
// brands, // brands,
// pages, // pages,
// }, },
revalidate: 60, revalidate: 60,
} }
} }

View File

@@ -23,7 +23,7 @@ export async function getStaticProps({
preview, preview,
}) })
const allProductsPromise = commerce.getAllProducts({ const allProductsPromise = await commerce.getAllProducts({
variables: { first: 4 }, variables: { first: 4 },
config, config,
preview, preview,