Skip to content

Commit be915d0

Browse files
committed
refactor(app): use new request way to fetch system config
1 parent 5b006c9 commit be915d0

File tree

8 files changed

+53
-29
lines changed

8 files changed

+53
-29
lines changed

src/app/layout.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ import { SpeedInsights } from '@vercel/speed-insights/next';
2828
import { GoogleAnalytics } from '@/components/GoogleAnalytics';
2929
import { getAppConfig } from '@/utils/app';
3030

31+
import { fetchConfig } from '#/domain/system/repository';
3132
import { nunito_sans } from '#/lib/font';
3233
import { siteConfig } from '#/lib/site';
33-
import { getConfigs } from '#/services/common';
3434

3535
import ClientEntry from '../entry';
3636
import DefaultLayout from '../entry/layouts/default';
@@ -101,12 +101,12 @@ export const metadata = {
101101
};
102102

103103
export default async function RootLayout({ children }: { children: React.ReactNode }) {
104-
const configRes = await getConfigs();
104+
const config = await fetchConfig();
105105

106106
return (
107107
<html lang="en" data-theme="light" className={`${nunito_sans.className} light`} suppressHydrationWarning>
108108
<body>
109-
<ClientEntry config={{ static: getAppConfig(), dynamic: configRes }}>
109+
<ClientEntry config={{ static: getAppConfig(), dynamic: config }}>
110110
<DefaultLayout>{children}</DefaultLayout>
111111
</ClientEntry>
112112
<GoogleAnalytics />

src/domain/system/repository.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright 2024 OpenBuild
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import httpClient from '@/utils/http';
18+
19+
function fetchConfig() {
20+
return httpClient.get('/config', { cache: 'no-store' });
21+
}
22+
23+
export { fetchConfig };

src/services/common.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,3 @@ export async function ownedNFTs(address) {
3232
const res = await get(`ts/v1/nft/general/tools/address/owned?address=${address}`);
3333
return res;
3434
}
35-
36-
export async function getConfigs() {
37-
const res = await fetch(`${process.env.NEXT_PUBLIC_API_BASE_URL}ts/v1/config`, { cache: 'no-store' });
38-
if (!res.ok) {
39-
return null;
40-
// throw console.error('Failed to fetch data')
41-
}
42-
return res.json();
43-
}

src/shared/types/http.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,13 @@ type ResponseResult<VT extends DataValue = DataValue> = {
2525
extra?: Record<string, DataValue>;
2626
};
2727

28-
export type { ResponseResult };
28+
type RequestConfig = RequestInit & {
29+
baseUrl?: string;
30+
params?: Record<string, DataValue>;
31+
isServer?: boolean;
32+
type?: 'upload';
33+
};
34+
35+
type ResponseInterceptor = (res: ResponseResult) => ResponseResult;
36+
37+
export type { ResponseResult, RequestConfig, ResponseInterceptor };

src/shared/utils/http.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,10 @@
1717
import { isBoolean, isFunction, isPlainObject } from 'lodash';
1818
import { stringify } from 'qs';
1919

20-
import type { DataValue, ResponseResult } from '../types';
20+
import type { DataValue, ResponseResult, RequestConfig, ResponseInterceptor } from '../types';
2121

2222
import { isLogicalSuccess, request } from './request';
2323

24-
type RequestConfig = {
25-
params?: Record<string, DataValue>;
26-
isServer?: boolean;
27-
};
28-
29-
type ResponseInterceptor = (res: ResponseResult) => ResponseResult;
30-
3124
interface IHttpClient {
3225
new (options: { baseUrl?: string }): {
3326
_setInterceptor: (interceptor: ResponseInterceptor) => void;

src/shared/utils/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,11 @@ export function HTMLDecode(text) {
9494
// return output;
9595
}
9696

97-
export { isEmpty, isNumber, isInteger, isString, isFunction, isPlainObject, noop, get, cloneDeep, merge, capitalize, chunk, range } from 'lodash';
97+
export {
98+
isEmpty, isNumber, isInteger, isString, isFunction, isPlainObject,
99+
noop,
100+
get, cloneDeep, merge, omit,
101+
capitalize,
102+
chunk, range,
103+
} from 'lodash';
98104
export { nanoid as generateRandomId } from 'nanoid';

src/shared/utils/request.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { getServerSession } from 'next-auth';
1818
import { getSession } from 'next-auth/react';
1919

2020
import { authOptions } from '../../lib/auth';
21+
import { omit } from './index';
2122

2223
const DEFAULT_PARAMS = {
2324
mode: 'cors',
@@ -49,10 +50,12 @@ export function isLogicalSuccess(code) {
4950
return code >= 200 && code < 300;
5051
}
5152

52-
export async function request(url, method, data, config) {
53+
export async function request(url, method, data, config = {}) {
54+
const { isServer, type, baseUrl, ...others } = config;
55+
5356
let session;
5457

55-
if (config?.isServer) {
58+
if (isServer) {
5659
session = await getServerSession(authOptions);
5760
} else {
5861
session = await getSession();
@@ -66,16 +69,15 @@ export async function request(url, method, data, config) {
6669
if (method === 'POST') {
6770
sendBody = JSON.stringify(data);
6871
}
69-
if (config && config.type === 'upload' && method === 'POST') {
72+
if (type === 'upload' && method === 'POST') {
7073
sendBody = data;
7174
} else {
7275
headers['Content-Type'] = 'application/json';
7376
}
7477

75-
// console.log(process.env.NEXT_PUBLIC_API_BASE_URL, 'NEXT_PUBLIC_API_BASE_URL')
76-
77-
return await fetch(resolveRequestUrl(config && config.baseUrl, url), {
78+
return await fetch(resolveRequestUrl(baseUrl, url), {
7879
...DEFAULT_PARAMS,
80+
...omit(others, ['params']),
7981
headers,
8082
method,
8183
body: sendBody,

src/state/application/updater.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default function Updater({ datas }) {
3030
const isMobile = !useMediaQuery('(min-width: 768px)');
3131
const isNotLg = useMediaQuery('(min-width: 1024px)');
3232
const gc = useCallback(async () => {
33-
if (datas?.configs?.code === 200) {
33+
if (datas?.configs?.success) {
3434
dispatch(updateConfig(datas.configs.data.list));
3535
}
3636
}, [dispatch, datas]);

0 commit comments

Comments
 (0)