Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

createServerRpc.ts

30 lines847 Bytes

1import { TSS_SERVER_FUNCTION } from '@tanstack/start-client-core'
2import invariant from 'tiny-invariant'
3​
4let baseUrl: string
5function sanitizeBase(base: string) {
6 return base.replace(/^\/|\/$/g, '')
7}
8​
9export const createServerRpc = (
10 functionId: string,
11 splitImportFn: (...args: any) => any,
12) => {
13 if (!baseUrl) {
14 const sanitizedAppBase = sanitizeBase(process.env.TSS_APP_BASE || '/')
15 const sanitizedServerBase = sanitizeBase(process.env.TSS_SERVER_FN_BASE!)
16 baseUrl = `${sanitizedAppBase ? `/${sanitizedAppBase}` : ''}/${sanitizedServerBase}/`
17 }
18 invariant(
19 splitImportFn,
20 '🚨splitImportFn required for the server functions server runtime, but was not provided.',
21 )
22​
23 const url = baseUrl + functionId
24​
25 return Object.assign(splitImportFn, {
26 url,
27 functionId,
28 [TSS_SERVER_FUNCTION]: true,
29 })