4
0
forked from crowetic/commerce
commerce/packages/spree/src/fetcher.ts
Luis Alvarez D 0afe686fe9
Monorepo with Turborepo (#651)
* Moved everything

* Figuring out how to make imports work

* Updated exports

* Added missing exports

* Added @vercel/commerce-local to `site`

* Updated commerce config

* Updated exports and commerce config

* Updated commerce hoc

* Fixed exports in local

* Added publish config

* Updated imports in site

* It's actually working

* Don't use debugger in dev for better speeds

* Improved DX when editing packages

* Set up eslint with husky

* Updated prettier config

* Added prettier setup to every package

* Moved bigcommerce

* Moved Bigcommerce to src and package updates

* Updated setup of bigcommerce

* Moved definitions script

* Moved commercejs

* Move to src

* Fixed types in commercejs

* Moved kibocommerce

* Moved kibocommerce to src

* Added package/tsconfig to kibocommerce

* Fixed imports and other things

* Moved ordercloud

* Moved ordercloud to src

* Fixed imports

* Added missing prettier files

* Moved Saleor

* Moved Saleor to src

* Fixed imports

* Replaced all imports to @commerce

* Added prettierignore/rc to all providers

* Moved shopify to src

* Build shopify in packages

* Moved Spree

* Moved spree to src

* Updated spree

* Moved swell

* Moved swell to src

* Fixed type imports in swell

* Moved Vendure to packages

* Moved vendure to src

* Fixed imports in vendure

* Added codegen to saleor

* Updated codegen setup for shopify

* Added codegen to vendure

* Added codegen to kibocommerce

* Added all packages to site's deps

* Updated codegen setup in bigcommerce

* Minor fixes

* Updated providers' names in site

* Updated packages based on Bel's changes

* Updated turbo to latest

* Fixed ts complains

* Set npm engine in root

* New lockfile install

* remove engines

* Regen lockfile

* Switched from npm to yarn

* Updated typesVersions in all packages

* Moved dep

* Updated SWR to the just released 1.2.0

* Removed "isolatedModules" from packages

* Updated list of providers and default

* Updated swell declaration

* Removed next import from kibocommerce

* Added COMMERCE_PROVIDER log

* Added another log

* Updated turbo config

* Updated docs

* Removed test logs

Co-authored-by: Jared Palmer <jared@jaredpalmer.com>
2022-02-01 14:14:05 -05:00

124 lines
3.6 KiB
TypeScript

import type { Fetcher } from '@vercel/commerce/utils/types'
import convertSpreeErrorToGraphQlError from './utils/convert-spree-error-to-graph-ql-error'
import { makeClient, errors } from '@spree/storefront-api-v2-sdk'
import type { ResultResponse } from '@spree/storefront-api-v2-sdk/types/interfaces/ResultResponse'
import type { GraphQLFetcherResult } from '@vercel/commerce/api'
import { requireConfigValue } from './isomorphic-config'
import getSpreeSdkMethodFromEndpointPath from './utils/get-spree-sdk-method-from-endpoint-path'
import SpreeSdkMethodFromEndpointPathError from './errors/SpreeSdkMethodFromEndpointPathError'
import type {
FetcherVariables,
SpreeSdkResponse,
SpreeSdkResponseWithRawResponse,
} from './types'
import createCustomizedFetchFetcher, {
fetchResponseKey,
} from './utils/create-customized-fetch-fetcher'
import ensureFreshUserAccessToken from './utils/tokens/ensure-fresh-user-access-token'
import RefreshTokenError from './errors/RefreshTokenError'
import prettyPrintSpreeSdkErrors from './utils/pretty-print-spree-sdk-errors'
const client = makeClient({
host: requireConfigValue('apiHost') as string,
createFetcher: (fetcherOptions) => {
return createCustomizedFetchFetcher({
fetch: globalThis.fetch,
requestConstructor: globalThis.Request,
...fetcherOptions,
})
},
})
const normalizeSpreeSuccessResponse = (
storeResponse: ResultResponse<SpreeSdkResponseWithRawResponse>
): GraphQLFetcherResult<SpreeSdkResponse> => {
const data = storeResponse.success()
const rawFetchResponse = data[fetchResponseKey]
return {
data,
res: rawFetchResponse,
}
}
const fetcher: Fetcher<GraphQLFetcherResult<SpreeSdkResponse>> = async (
requestOptions
) => {
const { url, method, variables, query } = requestOptions
console.log(
'Fetcher called. Configuration: ',
'url = ',
url,
'requestOptions = ',
requestOptions
)
if (!variables) {
throw new SpreeSdkMethodFromEndpointPathError(
`Required FetcherVariables not provided.`
)
}
const {
methodPath,
arguments: args,
refreshExpiredAccessToken = true,
replayUnauthorizedRequest = true,
} = variables as FetcherVariables
if (refreshExpiredAccessToken) {
await ensureFreshUserAccessToken(client)
}
const spreeSdkMethod = getSpreeSdkMethodFromEndpointPath(client, methodPath)
const storeResponse: ResultResponse<SpreeSdkResponseWithRawResponse> =
await spreeSdkMethod(...args)
if (storeResponse.isSuccess()) {
return normalizeSpreeSuccessResponse(storeResponse)
}
const storeResponseError = storeResponse.fail()
if (
storeResponseError instanceof errors.SpreeError &&
storeResponseError.serverResponse.status === 401 &&
replayUnauthorizedRequest
) {
console.info(
'Request ended with 401. Replaying request after refreshing the user token.'
)
await ensureFreshUserAccessToken(client)
const replayedStoreResponse: ResultResponse<SpreeSdkResponseWithRawResponse> =
await spreeSdkMethod(...args)
if (replayedStoreResponse.isSuccess()) {
return normalizeSpreeSuccessResponse(replayedStoreResponse)
}
console.warn('Replaying the request failed', replayedStoreResponse.fail())
throw new RefreshTokenError(
'Could not authorize request with current access token.'
)
}
if (storeResponseError instanceof errors.SpreeError) {
console.error(
`Request to spree resulted in an error:\n\n${prettyPrintSpreeSdkErrors(
storeResponse.fail()
)}`
)
throw convertSpreeErrorToGraphQlError(storeResponseError)
}
throw storeResponseError
}
export default fetcher