trcp: use env variables
This commit is contained in:
parent
a650f88d73
commit
78f181b17e
@ -6,7 +6,8 @@ const envSchema = z.object({
|
||||
.unknown()
|
||||
.optional()
|
||||
.transform((val) => !!val),
|
||||
NEXT_PUBLIC_BASE_PATH: z.string().optional(),
|
||||
PORT: z.string().optional(),
|
||||
NEXT_PUBLIC_BASE_PATH: z.string().optional().default(''),
|
||||
NEXT_PUBLIC_URL_CRM_GRAPHQL_DIRECT: z.string(),
|
||||
NEXT_PUBLIC_URL_GET_USER_DIRECT: z.string(),
|
||||
NEXT_PUBLIC_URL_CORE_FINGAP_DIRECT: z.string(),
|
||||
|
||||
@ -7,6 +7,8 @@ const publicRuntimeConfigSchema = envSchema.pick({
|
||||
});
|
||||
|
||||
const serverRuntimeConfigSchema = envSchema.pick({
|
||||
PORT: true,
|
||||
NEXT_PUBLIC_BASE_PATH: true,
|
||||
NEXT_PUBLIC_URL_CRM_GRAPHQL_DIRECT: true,
|
||||
NEXT_PUBLIC_URL_GET_USER_DIRECT: true,
|
||||
NEXT_PUBLIC_URL_CORE_FINGAP_DIRECT: true,
|
||||
|
||||
@ -11,22 +11,27 @@ function getUrls() {
|
||||
NEXT_PUBLIC_URL_CRM_GRAPHQL_DIRECT,
|
||||
NEXT_PUBLIC_URL_GET_USER_DIRECT,
|
||||
NEXT_PUBLIC_URL_CORE_FINGAP_DIRECT,
|
||||
NEXT_PUBLIC_BASE_PATH,
|
||||
PORT,
|
||||
} = serverRuntimeConfigSchema.parse(serverRuntimeConfig);
|
||||
|
||||
return {
|
||||
PORT,
|
||||
BASE_PATH: NEXT_PUBLIC_BASE_PATH,
|
||||
URL_CRM_GRAPHQL: NEXT_PUBLIC_URL_CRM_GRAPHQL_DIRECT,
|
||||
URL_GET_USER: NEXT_PUBLIC_URL_GET_USER_DIRECT,
|
||||
URL_CORE_FINGAP: NEXT_PUBLIC_URL_CORE_FINGAP_DIRECT,
|
||||
};
|
||||
}
|
||||
|
||||
const { NEXT_PUBLIC_BASE_PATH = '' } = publicRuntimeConfigSchema.parse(publicRuntimeConfig);
|
||||
const { NEXT_PUBLIC_BASE_PATH } = publicRuntimeConfigSchema.parse(publicRuntimeConfig);
|
||||
|
||||
function withBasePath(url: string) {
|
||||
return NEXT_PUBLIC_BASE_PATH + url;
|
||||
}
|
||||
|
||||
return {
|
||||
BASE_PATH: NEXT_PUBLIC_BASE_PATH,
|
||||
URL_CRM_GRAPHQL: withBasePath(urls.URL_CRM_GRAPHQL_PROXY),
|
||||
URL_GET_USER: withBasePath(urls.URL_GET_USER_PROXY),
|
||||
URL_CORE_FINGAP: withBasePath(urls.URL_CORE_FINGAP_PROXY),
|
||||
|
||||
@ -1,22 +1,26 @@
|
||||
import { createTRPCProxyClient, httpBatchLink } from '@trpc/client';
|
||||
import { createTRPCNext } from '@trpc/next';
|
||||
import getUrls from 'config/urls';
|
||||
import SuperJSON from 'superjson';
|
||||
import { isServer } from 'tools/common';
|
||||
import type { AppRouter } from './routers';
|
||||
|
||||
function getBaseUrl() {
|
||||
if (typeof window !== 'undefined') {
|
||||
return process.env.NEXT_PUBLIC_BASE_PATH ?? '';
|
||||
}
|
||||
const { BASE_PATH, PORT } = getUrls();
|
||||
|
||||
return `http://localhost:${process.env.PORT ?? 3000}${process.env.NEXT_PUBLIC_BASE_PATH ?? ''}`;
|
||||
function getBaseUrl() {
|
||||
if (!isServer()) return BASE_PATH;
|
||||
|
||||
return `http://localhost:${PORT ?? 3000}${BASE_PATH}`;
|
||||
}
|
||||
|
||||
const url = `${getBaseUrl()}/api/trpc`;
|
||||
|
||||
export const trpcClient = createTRPCNext<AppRouter>({
|
||||
config() {
|
||||
return {
|
||||
links: [
|
||||
httpBatchLink({
|
||||
url: `${getBaseUrl()}/api/trpc`,
|
||||
url,
|
||||
}),
|
||||
],
|
||||
transformer: SuperJSON,
|
||||
@ -28,7 +32,7 @@ export const trpcClient = createTRPCNext<AppRouter>({
|
||||
export const trpcPureClient = createTRPCProxyClient<AppRouter>({
|
||||
links: [
|
||||
httpBatchLink({
|
||||
url: `${getBaseUrl()}/api/trpc`,
|
||||
url,
|
||||
}),
|
||||
],
|
||||
transformer: SuperJSON,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user