forked from crowetic/commerce
This commit is contained in:
parent
c95f292743
commit
b88d3345f1
@ -1,5 +1,6 @@
|
|||||||
import vercelFetch from '@vercel/fetch'
|
import vercelFetch from '@vercel/fetch'
|
||||||
import { FetcherError } from '@commerce/utils/errors'
|
import { FetcherError } from '@commerce/utils/errors'
|
||||||
|
import { CustomNodeJsGlobal } from '../../types/node';
|
||||||
|
|
||||||
import { OrdercloudConfig } from '../index'
|
import { OrdercloudConfig } from '../index'
|
||||||
|
|
||||||
@ -143,17 +144,20 @@ export const createBuyerFetcher: (
|
|||||||
body?: Record<string, unknown>,
|
body?: Record<string, unknown>,
|
||||||
fetchOptions?: Record<string, any>
|
fetchOptions?: Record<string, any>
|
||||||
) => {
|
) => {
|
||||||
|
const customGlobal = global as CustomNodeJsGlobal;
|
||||||
|
|
||||||
// Get provider config
|
// Get provider config
|
||||||
const config = getConfig()
|
const config = getConfig()
|
||||||
|
|
||||||
|
|
||||||
// If a token was passed, set it on global
|
// If a token was passed, set it on global
|
||||||
if (fetchOptions?.token) {
|
if (fetchOptions?.token) {
|
||||||
global.token = fetchOptions.token
|
customGlobal.token = fetchOptions.token
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get a token
|
// Get a token
|
||||||
if (!global.token) {
|
if (!customGlobal.token) {
|
||||||
global.token = await getToken({
|
customGlobal.token = await getToken({
|
||||||
baseUrl: config.commerceUrl,
|
baseUrl: config.commerceUrl,
|
||||||
clientId: process.env.ORDERCLOUD_BUYER_CLIENT_ID as string,
|
clientId: process.env.ORDERCLOUD_BUYER_CLIENT_ID as string,
|
||||||
})
|
})
|
||||||
@ -161,7 +165,7 @@ export const createBuyerFetcher: (
|
|||||||
|
|
||||||
// Return the data and specify the expected type
|
// Return the data and specify the expected type
|
||||||
const data = await fetchData<T>({
|
const data = await fetchData<T>({
|
||||||
token: global.token as string,
|
token: customGlobal.token as string,
|
||||||
fetchOptions,
|
fetchOptions,
|
||||||
config,
|
config,
|
||||||
method,
|
method,
|
||||||
@ -171,6 +175,6 @@ export const createBuyerFetcher: (
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
...data,
|
...data,
|
||||||
meta: { token: global.token as string },
|
meta: { token: customGlobal.token as string },
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
6
framework/ordercloud/types/node.d.ts
vendored
6
framework/ordercloud/types/node.d.ts
vendored
@ -1,5 +1,3 @@
|
|||||||
declare module NodeJS {
|
export interface CustomNodeJsGlobal extends NodeJS.Global {
|
||||||
interface Global {
|
token: string | null | undefined
|
||||||
token: string | null | undefined
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user