Update config and create rest api fetcher

This commit is contained in:
DuvCharles 2022-11-10 11:16:22 +01:00
parent 2e30281e69
commit f5282b332f
2 changed files with 29 additions and 7 deletions

View File

@ -1,6 +1,5 @@
import type { CommerceAPI, CommerceAPIConfig } from '@vercel/commerce/api' import type { CommerceAPI, CommerceAPIConfig } from '@vercel/commerce/api'
import { getCommerceApi as commerceApi } 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 getAllPages from './operations/get-all-pages'
import getPage from './operations/get-page' 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 getAllProductPaths from './operations/get-all-product-paths'
import getAllProducts from './operations/get-all-products' import getAllProducts from './operations/get-all-products'
import getProduct from './operations/get-product' import getProduct from './operations/get-product'
import fetchLocal from './utils/fetch-local'
export interface LocalConfig extends CommerceAPIConfig {} export interface SyliusConfig extends CommerceAPIConfig {
const config: LocalConfig = { fetch: any
}
const config: SyliusConfig = {
commerceUrl: '', commerceUrl: '',
apiToken: '', apiToken: '',
cartCookie: '', cartCookie: '',
customerCookie: '', customerCookie: '',
cartCookieMaxAge: 2592000, cartCookieMaxAge: 2592000,
fetch: createFetcher(() => getCommerceApi().getConfig()), fetch: fetchLocal.fetchRestApi,
} }
const operations = { const operations = {

View File

@ -1,9 +1,9 @@
import { FetcherError } from '@vercel/commerce/utils/errors' import { FetcherError } from '@vercel/commerce/utils/errors'
import type { GraphQLFetcher } from '@vercel/commerce/api' import type { GraphQLFetcher } from '@vercel/commerce/api'
import type { LocalConfig } from '../index' import type { SyliusConfig } from '../index'
import fetch from './fetch' import fetch from './fetch'
const fetchGraphqlApi: (getConfig: () => LocalConfig) => GraphQLFetcher = const fetchGraphqlApi: (getConfig: () => SyliusConfig) => GraphQLFetcher =
(getConfig) => (getConfig) =>
async (query: string, { variables, preview } = {}, fetchOptions) => { async (query: string, { variables, preview } = {}, fetchOptions) => {
const config = getConfig() const config = getConfig()
@ -31,4 +31,24 @@ const fetchGraphqlApi: (getConfig: () => LocalConfig) => GraphQLFetcher =
return { data: json.data, res } 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 }