chore: improving the withBaseURL workflow

This commit is contained in:
Kevin
2025-07-02 11:33:23 -05:00
parent e2247669f0
commit 8f2a2391d1
4 changed files with 48 additions and 14 deletions

View File

@ -0,0 +1,36 @@
import {
withAPIBaseURL
} from '@src/lib/withBaseURL'
describe('withBaseURL', () => {
/**
* running in the development environment
* the .env.development should load
*/
describe('withAPIBaseUrl', () => {
it('should return base url', () => {
const expected = 'https://api.dev.zoo.dev'
const actual = withAPIBaseURL('')
expect(actual).toBe(expected)
})
it('should return base url with /users', () => {
const expected = 'https://api.dev.zoo.dev/users'
const actual = withAPIBaseURL('/users')
expect(actual).toBe(expected)
})
it ('should return a longer base url with /oauth2/token/revoke', () => {
const expected = 'https://api.dev.zoo.dev/oauth2/token/revoke'
const actual = withAPIBaseURL('/oauth2/token/revoke')
expect(actual).toBe(expected)
})
it('should ensure base url does not have ending slash', () => {
const expected = 'https://api.dev.zoo.dev'
const actual = withAPIBaseURL('')
expect(actual).toBe(expected)
const expectedEndsWith = expected[expected.length-1]
const actualEndsWith = actual[actual.length-1]
expect(actual).toBe(expected)
expect(actualEndsWith).toBe(expectedEndsWith)
})
})
})

View File

@ -1,5 +1,5 @@
import { VITE_KC_API_BASE_URL } from '@src/env'
export default function withBaseUrl(path: string): string {
export function withAPIBaseURL (path: string) : string {
return VITE_KC_API_BASE_URL + path
}

View File

@ -11,8 +11,7 @@ import {
import { isDesktop } from '@src/lib/isDesktop'
import { markOnce } from '@src/lib/performance'
import {
default as withBaseURL,
default as withBaseUrl,
withAPIBaseURL
} from '@src/lib/withBaseURL'
import { ACTOR_IDS } from '@src/machines/machineConstants'
@ -38,12 +37,13 @@ export const TOKEN_PERSIST_KEY = 'TOKEN_PERSIST_KEY'
const persistedCookie = getCookie(COOKIE_NAME)
const persistedLocalStorage = localStorage?.getItem(TOKEN_PERSIST_KEY) || ''
const persistedDevToken = VITE_KC_DEV_TOKEN
export const persistedToken = persistedDevToken || persistedCookie || persistedLocalStorage
export const persistedToken =
persistedDevToken || persistedCookie || persistedLocalStorage
console.log('Initial persisted token')
console.table([
['cookie', !!persistedCookie],
['local storage', !!persistedLocalStorage],
['dev token', !!persistedDevToken]
['dev token', !!persistedDevToken],
])
export const authMachine = setup({
@ -141,7 +141,7 @@ export const authMachine = setup({
async function getUser(input: { token?: string }) {
const token = await getAndSyncStoredToken(input)
const url = withBaseURL('/user')
const url = withAPIBaseURL('/user')
const headers: { [key: string]: string } = {
'Content-Type': 'application/json',
}
@ -201,9 +201,7 @@ async function getAndSyncStoredToken(input: {
// dev mode
if (VITE_KC_DEV_TOKEN) {
console.log('Token used for authentication')
console.table([
['dev token', !!VITE_KC_DEV_TOKEN],
])
console.table([['dev token', !!VITE_KC_DEV_TOKEN]])
return VITE_KC_DEV_TOKEN
}
@ -217,7 +215,7 @@ async function getAndSyncStoredToken(input: {
['persisted token', !!inputToken],
['cookie', !!cookieToken],
['local storage', !!localStorageToken],
['dev token', !!VITE_KC_DEV_TOKEN]
['dev token', !!VITE_KC_DEV_TOKEN],
])
if (token) {
// has just logged in, update storage
@ -244,7 +242,7 @@ async function logout() {
if (token) {
try {
await fetch(withBaseUrl('/oauth2/token/revoke'), {
await fetch(withAPIBaseURL('/oauth2/token/revoke'), {
method: 'POST',
credentials: 'include',
headers: {
@ -267,7 +265,7 @@ async function logout() {
}
}
return fetch(withBaseUrl('/logout'), {
return fetch(withAPIBaseURL('/logout'), {
method: 'POST',
credentials: 'include',
})