Skip to content

Commit df62562

Browse files
nadr0jacebrowning
andauthored
[Chore] All api urls are now using the helper function (#7672)
* fix: logging information about the login * chore: improving the withBaseURL workflow * chore: moving VITE_KC_API_BASE_URL to the helper function * fix: env to helper function api base url * chore: fixing another api base url * chore: shortlinks with base api helper function * chore: prompt edit with base helper function * fix: auto fmt * fix: withAPIBaseURL for all urls * fix: AI caught my typo, RIP * fix: expected * fix: renaming this so it is less specific to environment --------- Co-authored-by: Jace Browning <jacebrowning@gmail.com>
1 parent e5d082f commit df62562

File tree

13 files changed

+104
-46
lines changed

13 files changed

+104
-46
lines changed

scripts/known/urls.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
URL STATUS
66
000 https://${BASE_URL}
7+
405 https://api.dev.zoo.dev/oauth2/token/revoke
8+
401 https://api.dev.zoo.dev/users
79
301 https://discord.gg/JQEpHR7Nt2
810
404 https://github.com/KittyCAD/engine/issues/3528
911
404 https://github.com/KittyCAD/modeling-app/commit/${ref}

src/components/LspProvider.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
LanguageServerClient,
88
LspWorkerEventType,
99
} from '@kittycad/codemirror-lsp-client'
10-
import { TEST, VITE_KC_API_BASE_URL } from '@src/env'
10+
import { TEST } from '@src/env'
1111
import React, { createContext, useContext, useMemo, useState } from 'react'
1212
import { useNavigate } from 'react-router-dom'
1313
import type * as LSP from 'vscode-languageserver-protocol'
@@ -28,6 +28,7 @@ import type { FileEntry } from '@src/lib/project'
2828
import { codeManager } from '@src/lib/singletons'
2929
import { err } from '@src/lib/trap'
3030
import { useToken } from '@src/lib/singletons'
31+
import { withAPIBaseURL } from '@src/lib/withBaseURL'
3132

3233
function getWorkspaceFolders(): LSP.WorkspaceFolder[] {
3334
return []
@@ -85,7 +86,7 @@ export const LspProvider = ({ children }: { children: React.ReactNode }) => {
8586
const initEvent: KclWorkerOptions = {
8687
wasmUrl: wasmUrl(),
8788
token: token,
88-
apiBaseUrl: VITE_KC_API_BASE_URL,
89+
apiBaseUrl: withAPIBaseURL(''),
8990
}
9091
lspWorker.postMessage({
9192
worker: LspWorker.Kcl,
@@ -178,7 +179,7 @@ export const LspProvider = ({ children }: { children: React.ReactNode }) => {
178179
const initEvent: CopilotWorkerOptions = {
179180
wasmUrl: wasmUrl(),
180181
token: token,
181-
apiBaseUrl: VITE_KC_API_BASE_URL,
182+
apiBaseUrl: withAPIBaseURL(''),
182183
}
183184
lspWorker.postMessage({
184185
worker: LspWorker.Copilot,

src/lib/coredump.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { VITE_KC_API_BASE_URL } from '@src/env'
21
import { UAParser } from 'ua-parser-js'
32

43
import type { OsInfo } from '@rust/kcl-lib/bindings/OsInfo'
@@ -11,6 +10,7 @@ import { isDesktop } from '@src/lib/isDesktop'
1110
import type RustContext from '@src/lib/rustContext'
1211
import screenshot from '@src/lib/screenshot'
1312
import { APP_VERSION } from '@src/routes/utils'
13+
import { withAPIBaseURL } from '@src/lib/withBaseURL'
1414

1515
/* eslint-disable suggest-no-throw/suggest-no-throw --
1616
* All the throws in CoreDumpManager are intentional and should be caught and handled properly
@@ -35,7 +35,7 @@ export class CoreDumpManager {
3535
codeManager: CodeManager
3636
rustContext: RustContext
3737
token: string | undefined
38-
baseUrl: string = VITE_KC_API_BASE_URL
38+
baseUrl: string = withAPIBaseURL('')
3939

4040
constructor(
4141
engineCommandManager: EngineCommandManager,

src/lib/desktop.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { err } from '@src/lib/trap'
2626
import type { DeepPartial } from '@src/lib/types'
2727
import { getInVariableCase } from '@src/lib/utils'
2828
import { IS_STAGING } from '@src/routes/utils'
29+
import { withAPIBaseURL } from '@src/lib/withBaseURL'
2930

3031
export async function renameProjectDirectory(
3132
projectPath: string,
@@ -697,7 +698,9 @@ export const readTokenFile = async () => {
697698
export const writeTokenFile = async (token: string) => {
698699
const tokenFilePath = await getTokenFilePath()
699700
if (err(token)) return Promise.reject(token)
700-
return window.electron.writeFile(tokenFilePath, token)
701+
const result = window.electron.writeFile(tokenFilePath, token)
702+
console.log('token written to disk')
703+
return result
701704
}
702705

703706
export const writeTelemetryFile = async (content: string) => {
@@ -722,12 +725,9 @@ export const setState = async (state: Project | undefined): Promise<void> => {
722725
appStateStore = state
723726
}
724727

725-
export const getUser = async (
726-
token: string,
727-
hostname: string
728-
): Promise<Models['User_type']> => {
728+
export const getUser = async (token: string): Promise<Models['User_type']> => {
729729
try {
730-
const user = await fetch(`${hostname}/users/me`, {
730+
const user = await fetch(withAPIBaseURL('/users/me'), {
731731
headers: new Headers({
732732
Authorization: `Bearer ${token}`,
733733
}),

src/lib/links.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { VITE_KC_API_BASE_URL, VITE_KC_SITE_APP_URL } from '@src/env'
1+
import { VITE_KC_SITE_APP_URL } from '@src/env'
22
import toast from 'react-hot-toast'
33

44
import { stringToBase64 } from '@src/lib/base64'
@@ -7,6 +7,7 @@ import {
77
CREATE_FILE_URL_PARAM,
88
} from '@src/lib/constants'
99
import { err } from '@src/lib/trap'
10+
import { withAPIBaseURL } from '@src/lib/withBaseURL'
1011

1112
export interface FileLinkParams {
1213
code: string
@@ -96,7 +97,7 @@ export async function createShortlink(
9697
if (password) {
9798
body.password = password
9899
}
99-
const response = await fetch(`${VITE_KC_API_BASE_URL}/user/shortlinks`, {
100+
const response = await fetch(withAPIBaseURL('/user/shortlinks'), {
100101
method: 'POST',
101102
headers: {
102103
'Content-type': 'application/json',

src/lib/promptToEdit.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { SelectionRange } from '@codemirror/state'
22
import { EditorSelection, Transaction } from '@codemirror/state'
33
import type { Models } from '@kittycad/lib'
4-
import { VITE_KC_API_BASE_URL, VITE_KC_SITE_BASE_URL } from '@src/env'
4+
import { VITE_KC_SITE_BASE_URL } from '@src/env'
55
import { diffLines } from 'diff'
66
import toast from 'react-hot-toast'
77
import type { TextToCadMultiFileIteration_type } from '@kittycad/lib/dist/types/src/models'
@@ -28,6 +28,7 @@ import { uuidv4 } from '@src/lib/utils'
2828
import type { File as KittyCadLibFile } from '@kittycad/lib/dist/types/src/models'
2929
import type { FileMeta } from '@src/lib/types'
3030
import type { RequestedKCLFile } from '@src/machines/systemIO/utils'
31+
import { withAPIBaseURL } from '@src/lib/withBaseURL'
3132

3233
type KclFileMetaMap = {
3334
[execStateFileNamesIndex: number]: Extract<FileMeta, { type: 'kcl' }>
@@ -77,7 +78,7 @@ async function submitTextToCadRequest(
7778
})
7879

7980
const response = await fetch(
80-
`${VITE_KC_API_BASE_URL}/ml/text-to-cad/multi-file/iteration`,
81+
withAPIBaseURL('/ml/text-to-cad/multi-file/iteration'),
8182
{
8283
method: 'POST',
8384
headers: {
@@ -304,7 +305,7 @@ export async function getPromptToEditResult(
304305
id: string,
305306
token?: string
306307
): Promise<Models['TextToCadMultiFileIteration_type'] | Error> {
307-
const url = VITE_KC_API_BASE_URL + '/async/operations/' + id
308+
const url = withAPIBaseURL(`/async/operations/${id}`)
308309
const data: Models['TextToCadMultiFileIteration_type'] | Error =
309310
await crossPlatformFetch(
310311
url,
@@ -340,7 +341,7 @@ export async function doPromptEdit({
340341
;(window as any).process = {
341342
env: {
342343
ZOO_API_TOKEN: token,
343-
ZOO_HOST: VITE_KC_API_BASE_URL,
344+
ZOO_HOST: withAPIBaseURL(''),
344345
},
345346
}
346347
try {

src/lib/singletons.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { VITE_KC_API_BASE_URL } from '@src/env'
1+
import { withAPIBaseURL } from '@src/lib/withBaseURL'
22

33
import EditorManager from '@src/editor/manager'
44
import { KclManager } from '@src/lang/KclSingleton'
@@ -171,7 +171,7 @@ const appMachine = setup({
171171
systemId: BILLING,
172172
input: {
173173
...BILLING_CONTEXT_DEFAULTS,
174-
urlUserService: VITE_KC_API_BASE_URL,
174+
urlUserService: withAPIBaseURL(''),
175175
},
176176
}),
177177
],

src/lib/textToCad.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { Models } from '@kittycad/lib'
2-
import { VITE_KC_API_BASE_URL } from '@src/env'
32
import toast from 'react-hot-toast'
43
import type { NavigateFunction } from 'react-router-dom'
54
import {
@@ -19,6 +18,7 @@ import { err, reportRejection } from '@src/lib/trap'
1918
import { toSync } from '@src/lib/utils'
2019
import { getAllSubDirectoriesAtProjectRoot } from '@src/machines/systemIO/snapshotContext'
2120
import { joinOSPaths } from '@src/lib/paths'
21+
import { withAPIBaseURL } from '@src/lib/withBaseURL'
2222

2323
export async function submitTextToCadPrompt(
2424
prompt: string,
@@ -32,7 +32,7 @@ export async function submitTextToCadPrompt(
3232
kcl_version: kclManager.kclVersion,
3333
}
3434
// Glb has a smaller footprint than gltf, should we want to render it.
35-
const url = VITE_KC_API_BASE_URL + '/ai/text-to-cad/glb?kcl=true'
35+
const url = withAPIBaseURL('/ai/text-to-cad/glb?kcl=true')
3636
const data: Models['TextToCad_type'] | Error = await crossPlatformFetch(
3737
url,
3838
{
@@ -58,7 +58,7 @@ export async function getTextToCadResult(
5858
id: string,
5959
token?: string
6060
): Promise<Models['TextToCad_type'] | Error> {
61-
const url = VITE_KC_API_BASE_URL + '/user/text-to-cad/' + id
61+
const url = withAPIBaseURL(`/user/text-to-cad/${id}`)
6262
const data: Models['TextToCad_type'] | Error = await crossPlatformFetch(
6363
url,
6464
{

src/lib/textToCadTelemetry.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import type { Models } from '@kittycad/lib/dist/types/src'
2-
import { VITE_KC_API_BASE_URL } from '@src/env'
32
import crossPlatformFetch from '@src/lib/crossPlatformFetch'
3+
import { withAPIBaseURL } from '@src/lib/withBaseURL'
44

55
export async function sendTelemetry(
66
id: string,
77
feedback: Models['MlFeedback_type'],
88
token?: string
99
): Promise<void> {
10-
const url =
11-
VITE_KC_API_BASE_URL + '/user/text-to-cad/' + id + '?feedback=' + feedback
10+
const url = withAPIBaseURL(`/user/text-to-cad/${id}?feedback=${feedback}`)
1211
await crossPlatformFetch(
1312
url,
1413
{

src/lib/withBaseURL.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { withAPIBaseURL } from '@src/lib/withBaseURL'
2+
3+
describe('withBaseURL', () => {
4+
/**
5+
* running in the development environment
6+
* the .env.development should load
7+
*/
8+
describe('withAPIBaseUrl', () => {
9+
it('should return base url', () => {
10+
const expected = 'https://api.dev.zoo.dev'
11+
const actual = withAPIBaseURL('')
12+
expect(actual).toBe(expected)
13+
})
14+
it('should return base url with /users', () => {
15+
const expected = 'https://api.dev.zoo.dev/users'
16+
const actual = withAPIBaseURL('/users')
17+
expect(actual).toBe(expected)
18+
})
19+
it('should return a longer base url with /oauth2/token/revoke', () => {
20+
const expected = 'https://api.dev.zoo.dev/oauth2/token/revoke'
21+
const actual = withAPIBaseURL('/oauth2/token/revoke')
22+
expect(actual).toBe(expected)
23+
})
24+
it('should ensure base url does not have ending slash', () => {
25+
const expected = 'https://api.dev.zoo.dev'
26+
const actual = withAPIBaseURL('')
27+
expect(actual).toBe(expected)
28+
const expectedEndsWith = expected[expected.length - 1]
29+
const actualEndsWith = actual[actual.length - 1]
30+
expect(actual).toBe(expected)
31+
expect(actualEndsWith).toBe(expectedEndsWith)
32+
})
33+
})
34+
})

0 commit comments

Comments
 (0)