Skip to content
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
7 changes: 6 additions & 1 deletion packages/start-client-core/src/client-rpc/createClientRpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import { serverFnFetcher } from './serverFnFetcher'
import type { ClientFnMeta } from '../constants'

export function createClientRpc(functionId: string) {
const url = process.env.TSS_SERVER_FN_BASE + functionId
const serverFnBase =
(typeof process !== 'undefined' && process.env?.TSS_SERVER_FN_BASE) ||
(typeof import.meta !== 'undefined' &&
(import.meta as any).env?.TSS_SERVER_FN_BASE) ||
'/_serverFn/'
const url = serverFnBase + functionId
const serverFnMeta: ClientFnMeta = { id: functionId }

const clientFn = (...args: Array<any>) => {
Expand Down
44 changes: 44 additions & 0 deletions packages/start-client-core/tests/createClientRpc.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import { createClientRpc } from '../src/client-rpc/createClientRpc'
import { TSS_SERVER_FUNCTION } from '../src/constants'

describe('createClientRpc', () => {
const originalEnv = process.env.TSS_SERVER_FN_BASE

afterEach(() => {
if (originalEnv !== undefined) {
process.env.TSS_SERVER_FN_BASE = originalEnv
} else {
delete process.env.TSS_SERVER_FN_BASE
}
})

it('uses process.env.TSS_SERVER_FN_BASE when defined', () => {
process.env.TSS_SERVER_FN_BASE = '/custom-base/'
const rpc = createClientRpc('testFn')

expect(rpc.url).toBe('/custom-base/testFn')
expect(rpc.serverFnMeta).toEqual({ id: 'testFn' })
expect(rpc[TSS_SERVER_FUNCTION]).toBe(true)
})

it('falls back to default server function base when TSS_SERVER_FN_BASE is undefined', () => {
delete process.env.TSS_SERVER_FN_BASE
const rpc = createClientRpc('testFn')

expect(rpc.url).toBe('/_serverFn/testFn')
})

it('safely falls back without ReferenceError when global process is undefined', () => {
const originalProcess = globalThis.process
try {
// @ts-expect-error simulating browser environment where process is not defined
delete globalThis.process

const rpc = createClientRpc('testFn')
expect(rpc.url).toBe('/_serverFn/testFn')
} finally {
globalThis.process = originalProcess
}
})
})