mirror of
https://github.com/vercel/commerce.git
synced 2025-07-24 18:51:23 +00:00
Use fetch and Request from node-fetch in Spree SDK
This commit is contained in:
@@ -12,7 +12,7 @@ import { SpreeSdkVariables } from 'framework/spree/types'
|
|||||||
import SpreeSdkMethodFromEndpointPathError from 'framework/spree/errors/SpreeSdkMethodFromEndpointPathError'
|
import SpreeSdkMethodFromEndpointPathError from 'framework/spree/errors/SpreeSdkMethodFromEndpointPathError'
|
||||||
import { GraphQLFetcher, GraphQLFetcherResult } from '@commerce/api'
|
import { GraphQLFetcher, GraphQLFetcherResult } from '@commerce/api'
|
||||||
import createCreateFetchFetcher from '../../utils/createCreateFetchFetcher'
|
import createCreateFetchFetcher from '../../utils/createCreateFetchFetcher'
|
||||||
import createVercelFetch from '@vercel/fetch'
|
import fetch, { Request } from 'node-fetch'
|
||||||
|
|
||||||
const createApiFetch: (
|
const createApiFetch: (
|
||||||
getConfig: () => SpreeApiConfig
|
getConfig: () => SpreeApiConfig
|
||||||
@@ -22,7 +22,10 @@ const createApiFetch: (
|
|||||||
const client = makeClient({
|
const client = makeClient({
|
||||||
host: requireConfigValue('spreeApiHost') as string,
|
host: requireConfigValue('spreeApiHost') as string,
|
||||||
fetcherType: 'custom',
|
fetcherType: 'custom',
|
||||||
createFetcher: createCreateFetchFetcher({ fetch: createVercelFetch() }),
|
createFetcher: createCreateFetchFetcher({
|
||||||
|
fetch,
|
||||||
|
requestClass: Request,
|
||||||
|
}),
|
||||||
})
|
})
|
||||||
|
|
||||||
return async (url, queryData = {}, fetchOptions = {}) => {
|
return async (url, queryData = {}, fetchOptions = {}) => {
|
||||||
|
@@ -17,18 +17,16 @@ import createCreateFetchFetcher from './utils/createCreateFetchFetcher'
|
|||||||
const client = makeClient({
|
const client = makeClient({
|
||||||
host: requireConfigValue('spreeApiHost') as string,
|
host: requireConfigValue('spreeApiHost') as string,
|
||||||
fetcherType: 'custom',
|
fetcherType: 'custom',
|
||||||
createFetcher: createCreateFetchFetcher({ fetch: globalThis.fetch }),
|
createFetcher: createCreateFetchFetcher({
|
||||||
|
fetch: globalThis.fetch,
|
||||||
|
requestClass: globalThis.Request,
|
||||||
|
}),
|
||||||
})
|
})
|
||||||
|
|
||||||
const fetcher: Fetcher<
|
const fetcher: Fetcher<
|
||||||
GraphQLFetcherResult<JsonApiSingleResponse | JsonApiListResponse>,
|
GraphQLFetcherResult<JsonApiSingleResponse | JsonApiListResponse>,
|
||||||
SpreeSdkVariables
|
SpreeSdkVariables
|
||||||
> = async (requestOptions) => {
|
> = async (requestOptions) => {
|
||||||
// url?: string
|
|
||||||
// query?: string
|
|
||||||
// method?: string
|
|
||||||
// variables?: any
|
|
||||||
// body?: Body
|
|
||||||
const { url, method, variables, query } = requestOptions
|
const { url, method, variables, query } = requestOptions
|
||||||
const { locale, ...vars } = variables ?? {}
|
const { locale, ...vars } = variables ?? {}
|
||||||
|
|
||||||
|
@@ -3,7 +3,7 @@ import { errors } from '@spree/storefront-api-v2-sdk'
|
|||||||
import type { CreateFetcher } from '@spree/storefront-api-v2-sdk/types/interfaces/ClientConfig'
|
import type { CreateFetcher } from '@spree/storefront-api-v2-sdk/types/interfaces/ClientConfig'
|
||||||
|
|
||||||
const createCreateFetchFetcher =
|
const createCreateFetchFetcher =
|
||||||
({ fetch: rawFetch }): CreateFetcher =>
|
({ fetch: rawFetch, requestClass }): CreateFetcher =>
|
||||||
// TODO: Fix rawFetch any type.
|
// TODO: Fix rawFetch any type.
|
||||||
(fetcherOptions) => {
|
(fetcherOptions) => {
|
||||||
const { FetchError } = errors
|
const { FetchError } = errors
|
||||||
@@ -35,18 +35,20 @@ const createCreateFetchFetcher =
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const request: Request = new requestClass(absoluteUrl.toString(), {
|
||||||
|
method: method.toUpperCase(),
|
||||||
|
headers: { ...sharedHeaders, ...headers },
|
||||||
|
...payload,
|
||||||
|
})
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await rawFetch(absoluteUrl.toString(), {
|
const response: Response = await rawFetch(request)
|
||||||
method: method.toUpperCase(),
|
|
||||||
headers: { ...sharedHeaders, ...headers },
|
|
||||||
...payload,
|
|
||||||
})
|
|
||||||
|
|
||||||
const data = await response.json()
|
const data = await response.json()
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
// Use the "traditional" approach and reject non 2xx responses.
|
// Use the "traditional" approach and reject non 2xx responses.
|
||||||
throw new FetchError(response, null, data)
|
throw new FetchError(response, request, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -56,12 +58,16 @@ const createCreateFetchFetcher =
|
|||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof TypeError) {
|
if (error instanceof TypeError) {
|
||||||
throw new FetchError(null, null, null)
|
throw new FetchError(null, request, null)
|
||||||
}
|
}
|
||||||
|
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
if (error instanceof FetchError) {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
|
||||||
throw new FetchError(null, null, null, error.message)
|
throw new FetchError(null, null, null, error.message)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user