Skip to content

Commit

Permalink
vercel#629: Type error: Property 'token' does not exist on type Global (
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverheywood451 authored Jan 4, 2022
1 parent c95f292 commit b88d334
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
14 changes: 9 additions & 5 deletions framework/ordercloud/api/utils/fetch-rest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import vercelFetch from '@vercel/fetch'
import { FetcherError } from '@commerce/utils/errors'
import { CustomNodeJsGlobal } from '../../types/node';

import { OrdercloudConfig } from '../index'

Expand Down Expand Up @@ -143,25 +144,28 @@ export const createBuyerFetcher: (
body?: Record<string, unknown>,
fetchOptions?: Record<string, any>
) => {
const customGlobal = global as CustomNodeJsGlobal;

// Get provider config
const config = getConfig()


// If a token was passed, set it on global
if (fetchOptions?.token) {
global.token = fetchOptions.token
customGlobal.token = fetchOptions.token
}

// Get a token
if (!global.token) {
global.token = await getToken({
if (!customGlobal.token) {
customGlobal.token = await getToken({
baseUrl: config.commerceUrl,
clientId: process.env.ORDERCLOUD_BUYER_CLIENT_ID as string,
})
}

// Return the data and specify the expected type
const data = await fetchData<T>({
token: global.token as string,
token: customGlobal.token as string,
fetchOptions,
config,
method,
Expand All @@ -171,6 +175,6 @@ export const createBuyerFetcher: (

return {
...data,
meta: { token: global.token as string },
meta: { token: customGlobal.token as string },
}
}
6 changes: 2 additions & 4 deletions framework/ordercloud/types/node.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
declare module NodeJS {
interface Global {
token: string | null | undefined
}
export interface CustomNodeJsGlobal extends NodeJS.Global {
token: string | null | undefined
}

0 comments on commit b88d334

Please sign in to comment.