Skip to content

Commit 9c7f6de

Browse files
committed
refactor: update WebOps API URL handling to use dynamic functions
1 parent 5e7f321 commit 9c7f6de

3 files changed

Lines changed: 32 additions & 24 deletions

File tree

packages/core/src/pages/api/fs/auth/login.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const handler: NextApiHandler = async (
3737
return
3838
}
3939

40-
const webopsResponse = await fetch(sessionUrl, {
40+
const webopsResponse = await fetch(sessionUrl(), {
4141
method: 'POST',
4242
headers: { 'Content-Type': 'application/json' },
4343
body: JSON.stringify({ storeId, password }),

packages/core/src/server/authentication-service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ let publicKeyCache: {
3737
} | null = null
3838

3939
async function fetchPublicKeyPemFromWebOps(): Promise<string> {
40-
const res = await fetch(publicKeyUrl, {
40+
const res = await fetch(publicKeyUrl(), {
4141
signal: AbortSignal.timeout(passwordProtectionTimeouts.publicKeyMs),
4242
})
4343

@@ -188,7 +188,7 @@ export class AuthenticationService {
188188
payload: TokenPayload
189189
): Promise<AuthResult> {
190190
try {
191-
const res = await fetch(renewUrl, {
191+
const res = await fetch(renewUrl(), {
192192
method: 'POST',
193193
headers: { 'Content-Type': 'application/json' },
194194
body: JSON.stringify({
@@ -221,7 +221,7 @@ export class AuthenticationService {
221221

222222
private async handleNoAuth(request: NextRequest): Promise<AuthResult> {
223223
try {
224-
const res = await fetch(protectionStatusUrl, {
224+
const res = await fetch(protectionStatusUrl(), {
225225
signal: AbortSignal.timeout(passwordProtectionTimeouts.defaultMs),
226226
})
227227

packages/core/src/server/password-protection/webops-api.ts

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,34 @@ import discoveryConfig from '../../../discovery.config'
22

33
const DEFAULT_WEBOPS_ORIGIN = 'https://faststore.vtex.com'
44

5-
let origin =
6-
process.env.WEBOPS_API_URL?.trim().replaceAll(/\/+$/g, '') ??
7-
DEFAULT_WEBOPS_ORIGIN
8-
9-
origin = origin.startsWith('http') ? origin : `https://${origin}`
10-
11-
export const publicKeyUrl = new URL(
12-
'/api/v1/password-protection/public-key',
13-
origin
14-
)
15-
16-
export const protectionStatusUrl = new URL(
17-
'/api/v1/password-protection/status',
18-
origin
19-
)
20-
protectionStatusUrl.searchParams.set('storeId', discoveryConfig.api.storeId)
21-
22-
export const sessionUrl = new URL('/api/v1/password-protection/session', origin)
23-
24-
export const renewUrl = new URL('/api/v1/password-protection/renew', origin)
5+
function getWebopsOrigin(): string {
6+
const hostFromEnv = process.env.WEBOPS_API_URL?.trim() ?? ''
7+
const hostWithoutTrailingSlash = hostFromEnv.replace(/\/+$/, '')
8+
const origin =
9+
hostWithoutTrailingSlash.length > 0
10+
? hostWithoutTrailingSlash
11+
: DEFAULT_WEBOPS_ORIGIN
12+
13+
return /^https?:\/\//i.test(origin) ? origin : `https://${origin}`
14+
}
15+
16+
export function publicKeyUrl(): URL {
17+
return new URL('/api/v1/password-protection/public-key', getWebopsOrigin())
18+
}
19+
20+
export function protectionStatusUrl(): URL {
21+
const url = new URL('/api/v1/password-protection/status', getWebopsOrigin())
22+
url.searchParams.set('storeId', discoveryConfig.api.storeId)
23+
return url
24+
}
25+
26+
export function sessionUrl(): URL {
27+
return new URL('/api/v1/password-protection/session', getWebopsOrigin())
28+
}
29+
30+
export function renewUrl(): URL {
31+
return new URL('/api/v1/password-protection/renew', getWebopsOrigin())
32+
}
2533

2634
/** Timeouts for WebOps password-protection calls (middleware / API routes). */
2735
export const passwordProtectionTimeouts = {

0 commit comments

Comments
 (0)