Skip to content

Commit 574e0bf

Browse files
KarinKarin
Karin
authored and
Karin
committed
fix: ensure sdk api is always called with the right host
1 parent 1b9f2f4 commit 574e0bf

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

packages/config/src/api/site_info.ts

+36-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import type { RequestInit } from 'node-fetch'
44

55
import { getEnvelope } from '../env/envelope.js'
66
import { throwUserError } from '../error.js'
7+
import {
8+
EXTENSION_API_BASE_URL,
9+
EXTENSION_API_STAGING_BASE_URL,
10+
NETLIFY_API_STAGING_BASE_URL,
11+
} from '../integrations.js'
712
import { ERROR_CALL_TO_ACTION } from '../log/messages.js'
813
import { IntegrationResponse } from '../types/api.js'
914
import { ModeOption, TestOptions } from '../types/options.js'
@@ -53,7 +58,16 @@ export const getSiteInfo = async function ({
5358

5459
const integrations =
5560
mode === 'buildbot' && !offline
56-
? await getIntegrations({ siteId, testOpts, offline, accountId, token, featureFlags, extensionApiBaseUrl })
61+
? await getIntegrations({
62+
siteId,
63+
testOpts,
64+
offline,
65+
accountId,
66+
token,
67+
featureFlags,
68+
extensionApiBaseUrl,
69+
mode,
70+
})
5771
: []
5872

5973
return { siteInfo, accounts: [], addons: [], integrations }
@@ -63,7 +77,7 @@ export const getSiteInfo = async function ({
6377
getSite(api, siteId, siteFeatureFlagPrefix),
6478
getAccounts(api),
6579
getAddons(api, siteId),
66-
getIntegrations({ siteId, testOpts, offline, accountId, token, featureFlags, extensionApiBaseUrl }),
80+
getIntegrations({ siteId, testOpts, offline, accountId, token, featureFlags, extensionApiBaseUrl, mode }),
6781
]
6882

6983
const [siteInfo, accounts, addons, integrations] = await Promise.all(promises)
@@ -120,6 +134,7 @@ type GetIntegrationsOpts = {
120134
token?: string
121135
featureFlags?: Record<string, boolean>
122136
extensionApiBaseUrl: string
137+
mode: ModeOption
123138
}
124139

125140
const getIntegrations = async function ({
@@ -130,19 +145,29 @@ const getIntegrations = async function ({
130145
token,
131146
featureFlags,
132147
extensionApiBaseUrl,
148+
mode,
133149
}: GetIntegrationsOpts): Promise<IntegrationResponse[]> {
134150
if (!siteId || offline) {
135151
return []
136152
}
137153
const sendBuildBotTokenToJigsaw = featureFlags?.send_build_bot_token_to_jigsaw
138-
const { host, setBaseUrl } = testOpts
154+
const { host: originalHost, setBaseUrl } = testOpts
155+
156+
// TODO(kh): I am adding this purely for local staging development.
157+
// We should remove this once we have fixed https://github.com/netlify/cli/blob/b5a5c7525edd28925c5c2e3e5f0f00c4261eaba5/src/lib/build.ts#L125
158+
const host =
159+
originalHost === 'localhost'
160+
? `http://${originalHost}`
161+
: originalHost?.includes(NETLIFY_API_STAGING_BASE_URL)
162+
? EXTENSION_API_STAGING_BASE_URL
163+
: EXTENSION_API_BASE_URL
164+
165+
const baseUrl = new URL(host ?? extensionApiBaseUrl)
139166

140167
// We only use this for testing
141168
if (host && setBaseUrl) {
142169
setBaseUrl(extensionApiBaseUrl)
143170
}
144-
145-
const baseUrl = new URL(host ? `http://${host}` : extensionApiBaseUrl)
146171
// if accountId isn't present, use safe v1 endpoint
147172
const url = accountId
148173
? `${baseUrl}team/${accountId}/integrations/installations/meta/${siteId}`
@@ -151,8 +176,14 @@ const getIntegrations = async function ({
151176
try {
152177
const requestOptions = {} as RequestInit
153178

179+
// This is used to identify where the request is coming from
180+
requestOptions.headers = {
181+
'netlify-config-mode': mode,
182+
}
183+
154184
if (sendBuildBotTokenToJigsaw && token) {
155185
requestOptions.headers = {
186+
...requestOptions.headers,
156187
'netlify-sdk-build-bot-token': token,
157188
}
158189
}

packages/config/src/main.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ export const resolveConfig = async function (opts) {
5555
}
5656

5757
// TODO(kh): remove this mapping and get the extensionApiHost from the opts
58-
const extensionApiBaseUrl =
59-
host === NETLIFY_API_STAGING_BASE_URL ? EXTENSION_API_STAGING_BASE_URL : EXTENSION_API_BASE_URL
58+
const extensionApiBaseUrl = host?.includes(NETLIFY_API_STAGING_BASE_URL)
59+
? EXTENSION_API_STAGING_BASE_URL
60+
: EXTENSION_API_BASE_URL
6061

6162
const {
6263
config: configOpt,

0 commit comments

Comments
 (0)