mirror of
https://github.com/vercel/commerce.git
synced 2025-05-17 06:56:59 +00:00
Update config and create rest api fetcher
This commit is contained in:
parent
2e30281e69
commit
f5282b332f
@ -1,6 +1,5 @@
|
||||
import type { CommerceAPI, CommerceAPIConfig } from '@vercel/commerce/api'
|
||||
import { getCommerceApi as commerceApi } from '@vercel/commerce/api'
|
||||
import createFetcher from './utils/fetch-local'
|
||||
|
||||
import getAllPages from './operations/get-all-pages'
|
||||
import getPage from './operations/get-page'
|
||||
@ -9,15 +8,18 @@ import getCustomerWishlist from './operations/get-customer-wishlist'
|
||||
import getAllProductPaths from './operations/get-all-product-paths'
|
||||
import getAllProducts from './operations/get-all-products'
|
||||
import getProduct from './operations/get-product'
|
||||
import fetchLocal from './utils/fetch-local'
|
||||
|
||||
export interface LocalConfig extends CommerceAPIConfig {}
|
||||
const config: LocalConfig = {
|
||||
export interface SyliusConfig extends CommerceAPIConfig {
|
||||
fetch: any
|
||||
}
|
||||
const config: SyliusConfig = {
|
||||
commerceUrl: '',
|
||||
apiToken: '',
|
||||
cartCookie: '',
|
||||
customerCookie: '',
|
||||
cartCookieMaxAge: 2592000,
|
||||
fetch: createFetcher(() => getCommerceApi().getConfig()),
|
||||
fetch: fetchLocal.fetchRestApi,
|
||||
}
|
||||
|
||||
const operations = {
|
||||
|
@ -1,9 +1,9 @@
|
||||
import { FetcherError } from '@vercel/commerce/utils/errors'
|
||||
import type { GraphQLFetcher } from '@vercel/commerce/api'
|
||||
import type { LocalConfig } from '../index'
|
||||
import type { SyliusConfig } from '../index'
|
||||
import fetch from './fetch'
|
||||
|
||||
const fetchGraphqlApi: (getConfig: () => LocalConfig) => GraphQLFetcher =
|
||||
const fetchGraphqlApi: (getConfig: () => SyliusConfig) => GraphQLFetcher =
|
||||
(getConfig) =>
|
||||
async (query: string, { variables, preview } = {}, fetchOptions) => {
|
||||
const config = getConfig()
|
||||
@ -31,4 +31,24 @@ const fetchGraphqlApi: (getConfig: () => LocalConfig) => GraphQLFetcher =
|
||||
return { data: json.data, res }
|
||||
}
|
||||
|
||||
export default fetchGraphqlApi
|
||||
export const fetchRestApi = async <T>(
|
||||
method: string,
|
||||
path: string,
|
||||
body?: Record<string, unknown>,
|
||||
fetchOptions?: Record<string, any>
|
||||
) => {
|
||||
const res = await fetch(process.env.NEXT_PUBLIC_SYLIUS_API_URL + path, {
|
||||
...fetchOptions,
|
||||
method,
|
||||
headers: {
|
||||
...fetchOptions?.headers,
|
||||
'Content-Type': 'application/json',
|
||||
accept: 'application/json, text/plain',
|
||||
},
|
||||
body: body ? JSON.stringify(body) : undefined,
|
||||
})
|
||||
const jsonResponse = await res.json()
|
||||
return jsonResponse as T
|
||||
}
|
||||
|
||||
export default { fetchGraphqlApi, fetchRestApi }
|
||||
|
Loading…
x
Reference in New Issue
Block a user