diff --git a/.env b/.env index c305002..b47d5a6 100644 --- a/.env +++ b/.env @@ -1,2 +1,11 @@ +####### COMMON ####### +USE_DEV_COLORS= +BASE_PATH= + ####### USERS ######## USERS_SUPER=["akalinina","vchikalkin"] + +####### URLS ######## +URL_GET_USER_DIRECT= +URL_CRM_GRAPHQL_DIRECT= +URL_CORE_FINGAP_DIRECT= \ No newline at end of file diff --git a/@packages/tools/common.ts b/@packages/tools/common.ts new file mode 100644 index 0000000..421f808 --- /dev/null +++ b/@packages/tools/common.ts @@ -0,0 +1,4 @@ +/* eslint-disable import/prefer-default-export */ +export function isServer() { + return typeof window === 'undefined'; +} diff --git a/@packages/ui/colors.js b/@packages/ui/colors.js deleted file mode 100644 index 853ac50..0000000 --- a/@packages/ui/colors.js +++ /dev/null @@ -1,11 +0,0 @@ -/* eslint-disable import/prefer-default-export */ -import { createGlobalStyle } from 'styled-components'; - -export const GlobalStyle = createGlobalStyle` -:root { - --color-background: rgb(240, 240, 240); - --color-primary: ${process.env.NEXT_PUBLIC_COLOR_PRIMARY}; - --color-secondary: ${process.env.NEXT_PUBLIC_COLOR_SECONDARY}; - --color-tertiarty: ${process.env.NEXT_PUBLIC_COLOR_TERTIARTY}; - } -`; diff --git a/Dockerfile b/Dockerfile index ebd73d8..18c781e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,23 +16,10 @@ WORKDIR /app COPY --from=deps /app/node_modules ./node_modules COPY . . -ARG NEXT_PUBLIC_BASE_PATH -ARG NEXT_PUBLIC_COLOR_PRIMARY -ARG NEXT_PUBLIC_COLOR_SECONDARY -ARG NEXT_PUBLIC_COLOR_TERTIARTY -ARG NEXT_PUBLIC_FAVICON -ARG NEXT_TELEMETRY_DISABLED -ARG NEXT_PUBLIC_URL_CRM_GRAPHQL_PROXY -ARG NEXT_PUBLIC_URL_CRM_GRAPHQL_DIRECT -ARG NEXT_PUBLIC_URL_GET_USER_PROXY -ARG NEXT_PUBLIC_URL_GET_USER_DIRECT -ARG NEXT_PUBLIC_URL_CORE_FINGAP_PROXY -ARG NEXT_PUBLIC_URL_CORE_FINGAP_DIRECT - # Next.js collects completely anonymous telemetry data about general usage. # Learn more here: https://nextjs.org/telemetry # Uncomment the following line in case you want to disable telemetry during the build. -# ENV NEXT_TELEMETRY_DISABLED 1 +ENV NEXT_TELEMETRY_DISABLED 1 RUN yarn build @@ -45,7 +32,7 @@ WORKDIR /app ENV NODE_ENV production # Uncomment the following line in case you want to disable telemetry during runtime. -# ENV NEXT_TELEMETRY_DISABLED 1 +ENV NEXT_TELEMETRY_DISABLED 1 RUN addgroup --system --gid 1001 nodejs RUN adduser --system --uid 1001 nextjs diff --git a/api/core/query.ts b/api/core/query.ts index 4f42c02..5c39f17 100644 --- a/api/core/query.ts +++ b/api/core/query.ts @@ -1,16 +1,15 @@ /* eslint-disable import/prefer-default-export */ import type { QueryFunctionContext } from '@tanstack/react-query'; import axios from 'axios'; +import getUrls from 'config/urls'; import type { RequestFinGAP, ResponseFinGAP } from './types'; +const { URL_CORE_FINGAP } = getUrls(); + export async function calculateFinGAP(payload: RequestFinGAP, { signal }: QueryFunctionContext) { - const { data } = await axios.post( - process.env.NEXT_PUBLIC_URL_CORE_FINGAP_PROXY!, - payload, - { - signal, - } - ); + const { data } = await axios.post(URL_CORE_FINGAP, payload, { + signal, + }); return data; } diff --git a/api/user/query.ts b/api/user/query.ts index 5605a83..40d89af 100644 --- a/api/user/query.ts +++ b/api/user/query.ts @@ -1,16 +1,14 @@ /* eslint-disable import/prefer-default-export */ import type { AxiosRequestConfig } from 'axios'; import axios from 'axios'; +import getUrls from 'config/urls'; import { love } from './tools'; import type { User } from './types'; -// prettier-ignore -const uri = typeof window === 'undefined' - ? process.env.NEXT_PUBLIC_URL_GET_USER_DIRECT - : process.env.NEXT_PUBLIC_URL_GET_USER_PROXY; +const { URL_GET_USER } = getUrls(); export async function getUser(config: AxiosRequestConfig) { - const user = await axios.get(uri!, config).then((res) => love(res.data)); + const user = await axios.get(URL_GET_USER, config).then((res) => love(res.data)); return user; } diff --git a/apollo.config.js b/apollo.config.js index a8179d9..2b50295 100644 --- a/apollo.config.js +++ b/apollo.config.js @@ -3,7 +3,7 @@ module.exports = { client: { service: { name: 'crmgraphql', - url: process.env.NEXT_PUBLIC_URL_CRM_GRAPHQL_DIRECT, + url: process.env.URL_CRM_GRAPHQL_DIRECT, localSchemaFile: './graphql/crm.schema.graphql', }, excludes: ['graphql/**/*'], diff --git a/apollo/client.js b/apollo/client.js index 1fb0e6d..ef8319b 100644 --- a/apollo/client.js +++ b/apollo/client.js @@ -1,19 +1,18 @@ /* eslint-disable no-underscore-dangle */ /* eslint-disable @typescript-eslint/naming-convention */ import { ApolloClient, InMemoryCache } from '@apollo/client'; +import getUrls from 'config/urls'; +import { isServer } from 'tools/common'; /** @type {import('@apollo/client').ApolloClient} */ let apolloClient; -// prettier-ignore -const uri = typeof window === 'undefined' - ? process.env.NEXT_PUBLIC_URL_CRM_GRAPHQL_DIRECT - : process.env.NEXT_PUBLIC_URL_CRM_GRAPHQL_PROXY; +const { URL_CRM_GRAPHQL } = getUrls(); function createApolloClient() { return new ApolloClient({ - ssrMode: typeof window === 'undefined', - uri, + ssrMode: isServer(), + uri: URL_CRM_GRAPHQL, cache: new InMemoryCache(), }); } @@ -27,7 +26,7 @@ export default function initializeApollo(initialState = null) { _apolloClient.cache.restore(initialState); } // For SSG and SSR always create a new Apollo Client - if (typeof window === 'undefined') return _apolloClient; + if (isServer()) return _apolloClient; // Create the Apollo Client once in the client if (!apolloClient) apolloClient = _apolloClient; diff --git a/config/meta.jsx b/config/meta.jsx new file mode 100644 index 0000000..32297b2 --- /dev/null +++ b/config/meta.jsx @@ -0,0 +1,19 @@ +/* eslint-disable import/prefer-default-export */ +import getUrls from 'config/urls'; + +const { BASE_PATH } = getUrls(); + +function buildPath(filePath) { + return String.prototype.concat(BASE_PATH, filePath); +} + +export const metaFavicon = ( + <> + + + + + + + +); diff --git a/config/schema/env.js b/config/schema/env.js new file mode 100644 index 0000000..38775f6 --- /dev/null +++ b/config/schema/env.js @@ -0,0 +1,16 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ +const { z } = require('zod'); + +const envSchema = z.object({ + USE_DEV_COLORS: z + .unknown() + .optional() + .transform((val) => !!val), + PORT: z.string().optional(), + BASE_PATH: z.string().optional().default(''), + URL_CRM_GRAPHQL_DIRECT: z.string(), + URL_GET_USER_DIRECT: z.string(), + URL_CORE_FINGAP_DIRECT: z.string(), +}); + +module.exports = envSchema; diff --git a/config/schema/runtime-config.js b/config/schema/runtime-config.js new file mode 100644 index 0000000..0e77856 --- /dev/null +++ b/config/schema/runtime-config.js @@ -0,0 +1,20 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ +const envSchema = require('./env'); + +const publicRuntimeConfigSchema = envSchema.pick({ + BASE_PATH: true, + USE_DEV_COLORS: true, +}); + +const serverRuntimeConfigSchema = envSchema.pick({ + PORT: true, + BASE_PATH: true, + URL_CRM_GRAPHQL_DIRECT: true, + URL_GET_USER_DIRECT: true, + URL_CORE_FINGAP_DIRECT: true, +}); + +module.exports = { + publicRuntimeConfigSchema, + serverRuntimeConfigSchema, +}; diff --git a/config/urls.ts b/config/urls.ts new file mode 100644 index 0000000..2f4cf53 --- /dev/null +++ b/config/urls.ts @@ -0,0 +1,36 @@ +import urls from 'constants/urls'; +import getConfig from 'next/config'; +import { isServer } from 'tools/common'; +import { publicRuntimeConfigSchema, serverRuntimeConfigSchema } from './schema/runtime-config'; + +const { serverRuntimeConfig, publicRuntimeConfig } = getConfig(); + +function getUrls() { + if (isServer()) { + const { URL_CRM_GRAPHQL_DIRECT, URL_GET_USER_DIRECT, URL_CORE_FINGAP_DIRECT, BASE_PATH, PORT } = + serverRuntimeConfigSchema.parse(serverRuntimeConfig); + + return { + PORT, + BASE_PATH, + URL_CRM_GRAPHQL: URL_CRM_GRAPHQL_DIRECT, + URL_GET_USER: URL_GET_USER_DIRECT, + URL_CORE_FINGAP: URL_CORE_FINGAP_DIRECT, + }; + } + + const { BASE_PATH } = publicRuntimeConfigSchema.parse(publicRuntimeConfig); + + function withBasePath(url: string) { + return BASE_PATH + url; + } + + return { + 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), + }; +} + +export default getUrls; diff --git a/constants/colors.js b/constants/colors.js new file mode 100644 index 0000000..7e377c2 --- /dev/null +++ b/constants/colors.js @@ -0,0 +1,15 @@ +const COLORS_PROD = { + COLOR_PRIMARY: '#1C01A9', + COLOR_SECONDARY: '#3A0185', + COLOR_TERTIARTY: '#580161', +}; +const COLORS_DEV = { + COLOR_PRIMARY: '#BF3676', + COLOR_SECONDARY: '#FD4047', + COLOR_TERTIARTY: '#FF9112', +}; + +module.exports = { + COLORS_PROD, + COLORS_DEV, +}; diff --git a/constants/urls.js b/constants/urls.js new file mode 100644 index 0000000..ff1491a --- /dev/null +++ b/constants/urls.js @@ -0,0 +1,5 @@ +module.exports = { + URL_GET_USER_PROXY: '/api/auth/user', + URL_CRM_GRAPHQL_PROXY: '/api/graphql/crm', + URL_CORE_FINGAP_PROXY: '/api/core/fingap', +}; diff --git a/mocks/handlers.js b/mocks/handlers.js index 1940b91..15a2b83 100644 --- a/mocks/handlers.js +++ b/mocks/handlers.js @@ -1,3 +1,4 @@ +import getUrls from 'config/urls'; import { rest } from 'msw'; const _ = require('radash'); @@ -22,11 +23,13 @@ const users = { }, }; +const { URL_GET_USER, URL_CORE_FINGAP, URL_CRM_GRAPHQL } = getUrls(); + export const handlers = [ - rest.get(process.env.NEXT_PUBLIC_URL_GET_USER_DIRECT, (req, res, ctx) => { + rest.get(URL_GET_USER, (req, res, ctx) => { return res(ctx.json(users.vchikalkin)); }), - rest.post(process.env.NEXT_PUBLIC_URL_CORE_FINGAP_PROXY, (req, res, ctx) => { + rest.post(URL_CORE_FINGAP, (req, res, ctx) => { return res( ctx.json({ sum: _.random(100000, 200000), @@ -34,7 +37,7 @@ export const handlers = [ }) ); }), - // rest.post(process.env.NEXT_PUBLIC_URL_CRM_GRAPHQL_PROXY, (req, res, ctx) => { + // rest.post(URL_CRM_GRAPHQL, (req, res, ctx) => { // return res(ctx.status(503)); // }), ]; diff --git a/mocks/index.js b/mocks/index.js index 6f36a3f..b3d8372 100644 --- a/mocks/index.js +++ b/mocks/index.js @@ -1,4 +1,6 @@ -if (typeof window === 'undefined') { +const { isServer } = require('tools/common'); + +if (isServer()) { const { server } = require('./server'); server.listen(); } else { diff --git a/next.config.js b/next.config.js index 5285a2e..cca2a82 100644 --- a/next.config.js +++ b/next.config.js @@ -3,11 +3,28 @@ const { withPlugins } = require('next-composed-plugins'); const withLess = require('next-with-less'); const withGraphQL = require('next-plugin-graphql'); +const fs = require('fs'); +const { COLORS_DEV, COLORS_PROD } = require('./constants/colors'); +const envSchema = require('./config/schema/env'); +const urls = require('./constants/urls'); const { devices } = require('./@packages/ui/screens'); +const { publicRuntimeConfigSchema } = require('./config/schema/runtime-config'); +const { serverRuntimeConfigSchema } = require('./config/schema/runtime-config'); + +const env = envSchema.parse(process.env); + +const favicons = fs.readdirSync('./public/favicon/prod'); +const faviconSubPath = env.USE_DEV_COLORS ? '/favicon/dev' : '/favicon/prod'; +function buildFaviconRewrite(source) { + return { + source, + destination: String.prototype.concat(faviconSubPath, source), + }; +} /** @type {import('next').NextConfig} */ const nextConfig = { - basePath: process.env.NEXT_PUBLIC_BASE_PATH, + basePath: env.BASE_PATH, output: 'standalone', swcMinify: true, reactStrictMode: true, @@ -20,34 +37,37 @@ const nextConfig = { images: { deviceSizes: devices, }, - rewrites: - process.env.NODE_ENV === 'development' && - async function rewrites() { - return [ - { - source: process.env.NEXT_PUBLIC_URL_CRM_GRAPHQL_PROXY, - destination: process.env.NEXT_PUBLIC_URL_CRM_GRAPHQL_DIRECT, - }, - { - source: process.env.NEXT_PUBLIC_URL_GET_USER_PROXY, - destination: process.env.NEXT_PUBLIC_URL_GET_USER_DIRECT, - }, - { - source: process.env.NEXT_PUBLIC_URL_CORE_FINGAP_PROXY, - destination: process.env.NEXT_PUBLIC_URL_CORE_FINGAP_DIRECT, - }, - ]; - }, + async rewrites() { + return [ + { + source: urls.URL_CRM_GRAPHQL_PROXY, + destination: env.URL_CRM_GRAPHQL_DIRECT, + }, + { + source: urls.URL_GET_USER_PROXY, + destination: env.URL_GET_USER_DIRECT, + }, + { + source: urls.URL_CORE_FINGAP_PROXY, + destination: env.URL_CORE_FINGAP_DIRECT, + }, + ...favicons.map((fileName) => buildFaviconRewrite(`/${fileName}`)), + ]; + }, + publicRuntimeConfig: publicRuntimeConfigSchema.parse(env), + serverRuntimeConfig: serverRuntimeConfigSchema.parse(env), }; const plugins = [withLess, withGraphQL]; +const colorPrimary = env.USE_DEV_COLORS ? COLORS_DEV.COLOR_PRIMARY : COLORS_PROD.COLOR_PRIMARY; + const config = { ...nextConfig, lessLoaderOptions: { lessOptions: { modifyVars: { - 'primary-color': process.env.NEXT_PUBLIC_COLOR_PRIMARY, + 'primary-color': colorPrimary, }, }, }, diff --git a/pages/_app.jsx b/pages/_app.jsx index d10ecb1..382f82b 100644 --- a/pages/_app.jsx +++ b/pages/_app.jsx @@ -7,8 +7,8 @@ import 'normalize.css'; import { useMemo } from 'react'; import StoreProvider from 'stores/Provider'; import { ThemeProvider } from 'styled-components'; +import { GlobalStyle } from 'styles/global-style'; import { trpcClient } from 'trpc/client'; -import { GlobalStyle } from 'ui/colors'; import { ConfigProvider, ru_RU } from 'ui/elements/config'; import 'ui/elements/styles/antd.less'; import theme from 'ui/theme'; diff --git a/pages/_document.jsx b/pages/_document.jsx index ef528a8..dd8f382 100644 --- a/pages/_document.jsx +++ b/pages/_document.jsx @@ -1,3 +1,4 @@ +import { metaFavicon } from 'config/meta'; import Document, { Head, Html, Main, NextScript } from 'next/document'; import { ServerStyleSheet } from 'styled-components'; @@ -35,8 +36,7 @@ export default class MyDocument extends Document { - - + {metaFavicon}
diff --git a/public/assets/images/logo-100.png b/public/assets/images/logo-100.png deleted file mode 100644 index 4cdb1e1..0000000 Binary files a/public/assets/images/logo-100.png and /dev/null differ diff --git a/public/favicon.ico b/public/favicon.ico deleted file mode 100644 index 8bd844a..0000000 Binary files a/public/favicon.ico and /dev/null differ diff --git a/public/favicon.prod.ico b/public/favicon.prod.ico deleted file mode 100644 index 83f0252..0000000 Binary files a/public/favicon.prod.ico and /dev/null differ diff --git a/public/favicon/dev/android-chrome-192x192.png b/public/favicon/dev/android-chrome-192x192.png new file mode 100644 index 0000000..46a9c6a Binary files /dev/null and b/public/favicon/dev/android-chrome-192x192.png differ diff --git a/public/favicon/dev/android-chrome-512x512.png b/public/favicon/dev/android-chrome-512x512.png new file mode 100644 index 0000000..2d9ab30 Binary files /dev/null and b/public/favicon/dev/android-chrome-512x512.png differ diff --git a/public/favicon/dev/apple-touch-icon.png b/public/favicon/dev/apple-touch-icon.png new file mode 100644 index 0000000..ce0dac0 Binary files /dev/null and b/public/favicon/dev/apple-touch-icon.png differ diff --git a/public/favicon/dev/favicon-16x16.png b/public/favicon/dev/favicon-16x16.png new file mode 100644 index 0000000..5cdc6f1 Binary files /dev/null and b/public/favicon/dev/favicon-16x16.png differ diff --git a/public/favicon/dev/favicon-32x32.png b/public/favicon/dev/favicon-32x32.png new file mode 100644 index 0000000..ec8a1fb Binary files /dev/null and b/public/favicon/dev/favicon-32x32.png differ diff --git a/public/favicon/dev/favicon.ico b/public/favicon/dev/favicon.ico new file mode 100644 index 0000000..98c9d16 Binary files /dev/null and b/public/favicon/dev/favicon.ico differ diff --git a/public/favicon/dev/safari-pinned-tab.svg b/public/favicon/dev/safari-pinned-tab.svg new file mode 100644 index 0000000..8215a5a --- /dev/null +++ b/public/favicon/dev/safari-pinned-tab.svg @@ -0,0 +1,32 @@ + + + + +Created by potrace 1.14, written by Peter Selinger 2001-2017 + + + + + + diff --git a/public/favicon/prod/android-chrome-192x192.png b/public/favicon/prod/android-chrome-192x192.png new file mode 100644 index 0000000..2fab82b Binary files /dev/null and b/public/favicon/prod/android-chrome-192x192.png differ diff --git a/public/favicon/prod/android-chrome-512x512.png b/public/favicon/prod/android-chrome-512x512.png new file mode 100644 index 0000000..1c09769 Binary files /dev/null and b/public/favicon/prod/android-chrome-512x512.png differ diff --git a/public/favicon/prod/apple-touch-icon.png b/public/favicon/prod/apple-touch-icon.png new file mode 100644 index 0000000..1d6b1cb Binary files /dev/null and b/public/favicon/prod/apple-touch-icon.png differ diff --git a/public/favicon/prod/favicon-16x16.png b/public/favicon/prod/favicon-16x16.png new file mode 100644 index 0000000..7551e2c Binary files /dev/null and b/public/favicon/prod/favicon-16x16.png differ diff --git a/public/favicon/prod/favicon-32x32.png b/public/favicon/prod/favicon-32x32.png new file mode 100644 index 0000000..3d0a600 Binary files /dev/null and b/public/favicon/prod/favicon-32x32.png differ diff --git a/public/favicon/prod/favicon.ico b/public/favicon/prod/favicon.ico new file mode 100644 index 0000000..bd956e2 Binary files /dev/null and b/public/favicon/prod/favicon.ico differ diff --git a/public/favicon/prod/safari-pinned-tab.svg b/public/favicon/prod/safari-pinned-tab.svg new file mode 100644 index 0000000..20e305b --- /dev/null +++ b/public/favicon/prod/safari-pinned-tab.svg @@ -0,0 +1,48 @@ + + + + +Created by potrace 1.14, written by Peter Selinger 2001-2017 + + + + + + diff --git a/public/site.webmanifest b/public/site.webmanifest new file mode 100644 index 0000000..b20abb7 --- /dev/null +++ b/public/site.webmanifest @@ -0,0 +1,19 @@ +{ + "name": "", + "short_name": "", + "icons": [ + { + "src": "/android-chrome-192x192.png", + "sizes": "192x192", + "type": "image/png" + }, + { + "src": "/android-chrome-512x512.png", + "sizes": "512x512", + "type": "image/png" + } + ], + "theme_color": "#ffffff", + "background_color": "#ffffff", + "display": "standalone" +} diff --git a/stores/index.js b/stores/index.js index d732c6c..a795796 100644 --- a/stores/index.js +++ b/stores/index.js @@ -2,6 +2,7 @@ /* eslint-disable @typescript-eslint/naming-convention */ /* eslint-disable no-underscore-dangle */ import { createContext } from 'react'; +import { isServer } from 'tools/common'; import RootStore from './root'; /** @type{RootStore} */ @@ -27,7 +28,7 @@ export function initializeStore(initialData) { } } - if (typeof window === 'undefined') return _store; + if (isServer()) return _store; if (!store) store = _store; return _store; diff --git a/stores/root.ts b/stores/root.ts index a5f372e..0c49ab8 100644 --- a/stores/root.ts +++ b/stores/root.ts @@ -1,12 +1,13 @@ /* eslint-disable import/no-cycle */ import { enableStaticRendering } from 'mobx-react-lite'; +import { isServer } from 'tools/common'; import CalculationStore from './calculation'; import type { ProcessStore } from './process'; import createProcessStore from './process'; import ResultsStore from './results'; import TablesStore from './tables'; -enableStaticRendering(typeof window === 'undefined'); +enableStaticRendering(isServer()); export default class RootStore { $calculation: CalculationStore; diff --git a/styles/colors.js b/styles/colors.js new file mode 100644 index 0000000..0cef3bb --- /dev/null +++ b/styles/colors.js @@ -0,0 +1,14 @@ +import getConfig from 'next/config'; +import { publicRuntimeConfigSchema } from '../config/schema/runtime-config'; +import { COLORS_DEV, COLORS_PROD } from '../constants/colors'; + +const { publicRuntimeConfig } = getConfig(); +const { USE_DEV_COLORS } = publicRuntimeConfigSchema.parse(publicRuntimeConfig); + +export default function getColors() { + if (USE_DEV_COLORS) { + return COLORS_DEV; + } + + return COLORS_PROD; +} diff --git a/styles/global-style.js b/styles/global-style.js new file mode 100644 index 0000000..4c818fb --- /dev/null +++ b/styles/global-style.js @@ -0,0 +1,14 @@ +/* eslint-disable import/prefer-default-export */ +import { createGlobalStyle } from 'styled-components'; +import getColors from './colors'; + +const { COLOR_PRIMARY, COLOR_SECONDARY, COLOR_TERTIARTY } = getColors(); + +export const GlobalStyle = createGlobalStyle` +:root { + --color-background: rgb(240, 240, 240); + --color-primary: ${COLOR_PRIMARY}; + --color-secondary: ${COLOR_SECONDARY}; + --color-tertiarty: ${COLOR_TERTIARTY}; + } +`; diff --git a/trpc/client.ts b/trpc/client.ts index 574d56d..7c6eedb 100644 --- a/trpc/client.ts +++ b/trpc/client.ts @@ -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({ config() { return { links: [ httpBatchLink({ - url: `${getBaseUrl()}/api/trpc`, + url, }), ], transformer: SuperJSON, @@ -28,7 +32,7 @@ export const trpcClient = createTRPCNext({ export const trpcPureClient = createTRPCProxyClient({ links: [ httpBatchLink({ - url: `${getBaseUrl()}/api/trpc`, + url, }), ], transformer: SuperJSON,