Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal v1 #2545

Open
wants to merge 16 commits into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"dataloader": "^2.1.0",
"fast-deep-equal": "^3.1.3",
"isomorphic-unfetch": "^3.1.0",
"p-limit": "^3.1.0"
"p-limit": "^3.1.0",
"node-fetch": "^2.7.0"
},
"devDependencies": {
"@envelop/core": "^2.6.0",
Expand Down
44 changes: 33 additions & 11 deletions packages/api/src/platforms/vtex/clients/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,44 @@
import fetch from 'isomorphic-unfetch'
import fetch, { HeadersInit, RequestInit } from 'node-fetch'
import packageJson from '../../../../package.json'

const USER_AGENT = `${packageJson.name}@${packageJson.version}`
const IS_PRODUCTION = process.env.NODE_ENV === 'production'

export const fetchAPI = async (info: RequestInfo, init?: RequestInit) => {
const response = await fetch(info, {
...init,
headers: {
...init?.headers,
'User-Agent': USER_AGENT,
},
})
const getProductionRequestInfo = (info: string) => {
const url = new URL(info)
url.protocol = 'http'
const account = url.hostname.split('.')[0]
url.searchParams.append('an', account)
url.hostname = `vtexioapi.vtexinternal.com`
return {
url: url.toString(),
host: `secure.vivara.com.br`,
}
}

export const fetchAPI = async <T>(
info: RequestInfo,
init?: RequestInit
): Promise<T> => {
let requestInfo = info.toString()
let headers: HeadersInit = {
...(init?.headers ?? {}),
'User-Agent': USER_AGENT,
}

if (IS_PRODUCTION && requestInfo.includes('vtexcommercestable')) {
const { url, host } = getProductionRequestInfo(requestInfo)
headers = { ...headers, Host: host }
requestInfo = url
}

const response = await fetch(requestInfo, { ...init, headers })

if (response.ok) {
return response.status !== 204 ? response.json() : undefined
return (response.status !== 204 ? response.json() : undefined) as Promise<T>
}

console.error(info, init, response)
console.error(requestInfo, { ...init, headers }, response)
const text = await response.text()

throw new Error(text)
Expand Down
9 changes: 8 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18216,7 +18216,7 @@ isomorphic-fetch@^3.0.0:

isomorphic-unfetch@^3.1.0:
version "3.1.0"
resolved "https://registry.npmjs.org/isomorphic-unfetch/-/isomorphic-unfetch-3.1.0.tgz"
resolved "https://registry.yarnpkg.com/isomorphic-unfetch/-/isomorphic-unfetch-3.1.0.tgz#87341d5f4f7b63843d468438128cb087b7c3e98f"
integrity sha512-geDJjpoZ8N0kWexiwkX8F9NkTsXhetLPVbZFQ+JTW239QNOwvB0gniuR1Wc6f0AMTn7/mFGyXvHTifrCp/GH8Q==
dependencies:
node-fetch "^2.6.1"
Expand Down Expand Up @@ -20649,6 +20649,13 @@ node-fetch@^2.6.7:
dependencies:
whatwg-url "^5.0.0"

node-fetch@^2.7.0:
version "2.7.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.7.0.tgz#d0f0fa6e3e2dc1d27efcd8ad99d550bda94d187d"
integrity sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==
dependencies:
whatwg-url "^5.0.0"

node-forge@^1:
version "1.3.1"
resolved "https://registry.npmjs.org/node-forge/-/node-forge-1.3.1.tgz"
Expand Down
Loading