sh-ui-cli 0.24.0 → 0.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/data/changelog/versions.json +13 -0
- package/package.json +1 -1
- package/templates/monorepo/README.md +3 -3
- package/templates/nextjs-app/app/api/proxy/[...path]/route.ts +85 -0
- package/templates/nextjs-app/src/shared/api/apiTypes.ts +21 -0
- package/templates/nextjs-app/src/shared/api/error.ts +12 -0
- package/templates/nextjs-app/src/shared/api/http.ts +56 -0
- package/templates/nextjs-standalone/app/api/proxy/[...path]/route.ts +85 -0
- package/templates/nextjs-standalone/src/shared/api/apiTypes.ts +21 -0
- package/templates/nextjs-standalone/src/shared/api/error.ts +12 -0
- package/templates/nextjs-standalone/src/shared/api/http.ts +56 -0
|
@@ -2,6 +2,19 @@
|
|
|
2
2
|
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
3
|
"$description": "sh-ui 릴리즈 노트 단일 소스. docs(React)와 showcase(Flutter)가 함께 읽는다. 새 릴리즈마다 맨 앞에 추가.",
|
|
4
4
|
"versions": [
|
|
5
|
+
{
|
|
6
|
+
"version": "0.25.0",
|
|
7
|
+
"date": "2026-04-28",
|
|
8
|
+
"title": "Next.js 템플릿에 http 골격 + BFF proxy 추가 — IS_SERVER 분기 제거",
|
|
9
|
+
"type": "minor",
|
|
10
|
+
"highlights": [
|
|
11
|
+
"nextjs-app · nextjs-standalone 템플릿에 axios 단일 인스턴스(src/shared/api/http.ts)와 BFF 라우트 핸들러(app/api/proxy/[...path]/route.ts) 추가 — 클라/서버 모두 /api/proxy 경유, IS_SERVER 분기 없음",
|
|
12
|
+
"인증 · 토큰 refresh · Sentry · 로그인 쿠키 특례 처리는 의도적으로 제외해 인증 비종속 골격으로 유지 — 프로젝트별 추가는 apps/docs/recipes 가이드로 분리",
|
|
13
|
+
"레시피 9종 추가 (apps/docs/app/recipes/): api-layer, auth, file-upload, tanstack-query, async-boundary, i18n, sentry, testing, deployment",
|
|
14
|
+
"사이드바에 '레시피' 진입점 추가"
|
|
15
|
+
],
|
|
16
|
+
"url": "https://github.com/sanghyeonKim0201/sh-ui/releases/tag/v0.25.0"
|
|
17
|
+
},
|
|
5
18
|
{
|
|
6
19
|
"version": "0.24.0",
|
|
7
20
|
"date": "2026-04-28",
|
package/package.json
CHANGED
|
@@ -77,7 +77,7 @@ pnpm dev # 모든 앱 동시 실행
|
|
|
77
77
|
## 앱 추가
|
|
78
78
|
|
|
79
79
|
```bash
|
|
80
|
-
npx sh-ui-create add-app
|
|
80
|
+
npx sh-ui-cli create add-app
|
|
81
81
|
```
|
|
82
82
|
|
|
83
83
|
`apps/{name}/` 과 `packages/ui/ui-apps/ui-{name}/` 을 함께 생성합니다.
|
|
@@ -93,10 +93,10 @@ pnpm --filter admin build
|
|
|
93
93
|
|
|
94
94
|
```bash
|
|
95
95
|
# 모든 ui 패키지에 추가 (대화형)
|
|
96
|
-
npx sh-ui-create add-component button
|
|
96
|
+
npx sh-ui-cli create add-component button
|
|
97
97
|
|
|
98
98
|
# 특정 앱에만 추가
|
|
99
|
-
npx sh-ui-create add-component button --app web
|
|
99
|
+
npx sh-ui-cli create add-component button --app web
|
|
100
100
|
```
|
|
101
101
|
|
|
102
102
|
내부적으로 `packages/ui/ui-apps/ui-{app}/` 디렉토리에서 `npx sh-ui add button` 이 실행되며,
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { cookies } from 'next/headers';
|
|
2
|
+
import { NextResponse, type NextRequest } from 'next/server';
|
|
3
|
+
|
|
4
|
+
const API_URL = process.env.API_URL ?? 'http://localhost:8080/api';
|
|
5
|
+
const ACCESS_TOKEN_COOKIE = 'accessToken';
|
|
6
|
+
const LOCALE_COOKIE = 'NEXT_LOCALE';
|
|
7
|
+
|
|
8
|
+
const proxyRequest = async (
|
|
9
|
+
request: NextRequest,
|
|
10
|
+
ctx: { params: Promise<{ path: string[] }> },
|
|
11
|
+
method: string,
|
|
12
|
+
) => {
|
|
13
|
+
const { path } = await ctx.params;
|
|
14
|
+
const url = new URL(`${API_URL}/${path.join('/')}`);
|
|
15
|
+
|
|
16
|
+
request.nextUrl.searchParams.forEach((value, key) => {
|
|
17
|
+
url.searchParams.set(key, value);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const cookieStore = await cookies();
|
|
21
|
+
const accessToken = cookieStore.get(ACCESS_TOKEN_COOKIE)?.value;
|
|
22
|
+
const locale =
|
|
23
|
+
cookieStore.get(LOCALE_COOKIE)?.value ??
|
|
24
|
+
request.headers.get('Accept-Language') ??
|
|
25
|
+
undefined;
|
|
26
|
+
|
|
27
|
+
const headers: Record<string, string> = {};
|
|
28
|
+
if (accessToken) headers.Authorization = `Bearer ${accessToken}`;
|
|
29
|
+
if (locale) headers['Accept-Language'] = locale;
|
|
30
|
+
|
|
31
|
+
let body: BodyInit | undefined;
|
|
32
|
+
if (method !== 'GET' && method !== 'HEAD') {
|
|
33
|
+
const contentType = request.headers.get('Content-Type');
|
|
34
|
+
if (contentType?.includes('multipart/form-data')) {
|
|
35
|
+
body = await request.formData();
|
|
36
|
+
} else {
|
|
37
|
+
headers['Content-Type'] = 'application/json';
|
|
38
|
+
body = await request.text();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
const response = await fetch(url.toString(), { method, headers, body });
|
|
44
|
+
const data = await response.json();
|
|
45
|
+
return NextResponse.json(data, { status: response.status });
|
|
46
|
+
} catch (error) {
|
|
47
|
+
console.error(`[PROXY] ${method} ${url.toString()} —`, error);
|
|
48
|
+
return NextResponse.json(
|
|
49
|
+
{
|
|
50
|
+
result: 'ERROR',
|
|
51
|
+
data: null,
|
|
52
|
+
error: {
|
|
53
|
+
code: 'NETWORK_ERROR',
|
|
54
|
+
message: '서버에 연결할 수 없습니다.',
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
{ status: 502 },
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export const GET = (
|
|
63
|
+
req: NextRequest,
|
|
64
|
+
ctx: { params: Promise<{ path: string[] }> },
|
|
65
|
+
) => proxyRequest(req, ctx, 'GET');
|
|
66
|
+
|
|
67
|
+
export const POST = (
|
|
68
|
+
req: NextRequest,
|
|
69
|
+
ctx: { params: Promise<{ path: string[] }> },
|
|
70
|
+
) => proxyRequest(req, ctx, 'POST');
|
|
71
|
+
|
|
72
|
+
export const PUT = (
|
|
73
|
+
req: NextRequest,
|
|
74
|
+
ctx: { params: Promise<{ path: string[] }> },
|
|
75
|
+
) => proxyRequest(req, ctx, 'PUT');
|
|
76
|
+
|
|
77
|
+
export const PATCH = (
|
|
78
|
+
req: NextRequest,
|
|
79
|
+
ctx: { params: Promise<{ path: string[] }> },
|
|
80
|
+
) => proxyRequest(req, ctx, 'PATCH');
|
|
81
|
+
|
|
82
|
+
export const DELETE = (
|
|
83
|
+
req: NextRequest,
|
|
84
|
+
ctx: { params: Promise<{ path: string[] }> },
|
|
85
|
+
) => proxyRequest(req, ctx, 'DELETE');
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** 공통 API 에러 형식 */
|
|
2
|
+
export type ApiErrorBody = {
|
|
3
|
+
message: string;
|
|
4
|
+
code: string;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
/** 백엔드 공통 응답 래퍼 */
|
|
8
|
+
export type ApiResponse<TData = unknown, TError = ApiErrorBody> = {
|
|
9
|
+
result: 'SUCCESS' | 'ERROR';
|
|
10
|
+
data: TData;
|
|
11
|
+
error: TError | null;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/** 페이지네이션 응답 래퍼 */
|
|
15
|
+
export type PaginatedData<T> = {
|
|
16
|
+
content: T[];
|
|
17
|
+
totalItems: number;
|
|
18
|
+
offset: number;
|
|
19
|
+
limit: number;
|
|
20
|
+
hasNext: boolean;
|
|
21
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ApiErrorBody } from './apiTypes';
|
|
2
|
+
|
|
3
|
+
export class ApiError extends Error {
|
|
4
|
+
constructor(
|
|
5
|
+
public readonly status: number,
|
|
6
|
+
public readonly code: string,
|
|
7
|
+
public readonly data: ApiErrorBody | null,
|
|
8
|
+
) {
|
|
9
|
+
super(data?.message ?? `API 요청 실패 (${status})`);
|
|
10
|
+
this.name = 'ApiError';
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
|
|
3
|
+
import { ApiError } from './error';
|
|
4
|
+
import type { ApiResponse } from './apiTypes';
|
|
5
|
+
|
|
6
|
+
const HTTP_TIMEOUT = 10_000;
|
|
7
|
+
|
|
8
|
+
const http = axios.create({
|
|
9
|
+
baseURL: '/api/proxy',
|
|
10
|
+
timeout: HTTP_TIMEOUT,
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
// 서버 컴포넌트에서 호출 시 axios 는 절대 URL 이 필요하다.
|
|
14
|
+
// 호스트만 프리픽스하고 그 외 분기는 두지 않는다.
|
|
15
|
+
http.interceptors.request.use(async (config) => {
|
|
16
|
+
if (typeof window !== 'undefined') return config;
|
|
17
|
+
|
|
18
|
+
const { headers: getHeaders } = await import('next/headers');
|
|
19
|
+
const hdrs = await getHeaders();
|
|
20
|
+
const host = hdrs.get('host') ?? 'localhost:3000';
|
|
21
|
+
const protocol = process.env.NODE_ENV === 'production' ? 'https' : 'http';
|
|
22
|
+
config.baseURL = `${protocol}://${host}/api/proxy`;
|
|
23
|
+
return config;
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
http.interceptors.response.use(
|
|
27
|
+
(response) => {
|
|
28
|
+
const body = response.data as ApiResponse;
|
|
29
|
+
if (body && typeof body === 'object' && 'result' in body) {
|
|
30
|
+
if (body.result === 'ERROR') {
|
|
31
|
+
return Promise.reject(
|
|
32
|
+
new ApiError(response.status, body.error?.code ?? '', body.error),
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
response.data = body.data;
|
|
36
|
+
}
|
|
37
|
+
return response;
|
|
38
|
+
},
|
|
39
|
+
(error) => {
|
|
40
|
+
if (axios.isAxiosError(error)) {
|
|
41
|
+
const { response } = error;
|
|
42
|
+
const body = response?.data as ApiResponse | undefined;
|
|
43
|
+
const errorBody = body?.error ?? null;
|
|
44
|
+
return Promise.reject(
|
|
45
|
+
new ApiError(
|
|
46
|
+
response?.status ?? 0,
|
|
47
|
+
errorBody?.code ?? '',
|
|
48
|
+
errorBody,
|
|
49
|
+
),
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
return Promise.reject(error);
|
|
53
|
+
},
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
export { http };
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { cookies } from 'next/headers';
|
|
2
|
+
import { NextResponse, type NextRequest } from 'next/server';
|
|
3
|
+
|
|
4
|
+
const API_URL = process.env.API_URL ?? 'http://localhost:8080/api';
|
|
5
|
+
const ACCESS_TOKEN_COOKIE = 'accessToken';
|
|
6
|
+
const LOCALE_COOKIE = 'NEXT_LOCALE';
|
|
7
|
+
|
|
8
|
+
const proxyRequest = async (
|
|
9
|
+
request: NextRequest,
|
|
10
|
+
ctx: { params: Promise<{ path: string[] }> },
|
|
11
|
+
method: string,
|
|
12
|
+
) => {
|
|
13
|
+
const { path } = await ctx.params;
|
|
14
|
+
const url = new URL(`${API_URL}/${path.join('/')}`);
|
|
15
|
+
|
|
16
|
+
request.nextUrl.searchParams.forEach((value, key) => {
|
|
17
|
+
url.searchParams.set(key, value);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const cookieStore = await cookies();
|
|
21
|
+
const accessToken = cookieStore.get(ACCESS_TOKEN_COOKIE)?.value;
|
|
22
|
+
const locale =
|
|
23
|
+
cookieStore.get(LOCALE_COOKIE)?.value ??
|
|
24
|
+
request.headers.get('Accept-Language') ??
|
|
25
|
+
undefined;
|
|
26
|
+
|
|
27
|
+
const headers: Record<string, string> = {};
|
|
28
|
+
if (accessToken) headers.Authorization = `Bearer ${accessToken}`;
|
|
29
|
+
if (locale) headers['Accept-Language'] = locale;
|
|
30
|
+
|
|
31
|
+
let body: BodyInit | undefined;
|
|
32
|
+
if (method !== 'GET' && method !== 'HEAD') {
|
|
33
|
+
const contentType = request.headers.get('Content-Type');
|
|
34
|
+
if (contentType?.includes('multipart/form-data')) {
|
|
35
|
+
body = await request.formData();
|
|
36
|
+
} else {
|
|
37
|
+
headers['Content-Type'] = 'application/json';
|
|
38
|
+
body = await request.text();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
const response = await fetch(url.toString(), { method, headers, body });
|
|
44
|
+
const data = await response.json();
|
|
45
|
+
return NextResponse.json(data, { status: response.status });
|
|
46
|
+
} catch (error) {
|
|
47
|
+
console.error(`[PROXY] ${method} ${url.toString()} —`, error);
|
|
48
|
+
return NextResponse.json(
|
|
49
|
+
{
|
|
50
|
+
result: 'ERROR',
|
|
51
|
+
data: null,
|
|
52
|
+
error: {
|
|
53
|
+
code: 'NETWORK_ERROR',
|
|
54
|
+
message: '서버에 연결할 수 없습니다.',
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
{ status: 502 },
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export const GET = (
|
|
63
|
+
req: NextRequest,
|
|
64
|
+
ctx: { params: Promise<{ path: string[] }> },
|
|
65
|
+
) => proxyRequest(req, ctx, 'GET');
|
|
66
|
+
|
|
67
|
+
export const POST = (
|
|
68
|
+
req: NextRequest,
|
|
69
|
+
ctx: { params: Promise<{ path: string[] }> },
|
|
70
|
+
) => proxyRequest(req, ctx, 'POST');
|
|
71
|
+
|
|
72
|
+
export const PUT = (
|
|
73
|
+
req: NextRequest,
|
|
74
|
+
ctx: { params: Promise<{ path: string[] }> },
|
|
75
|
+
) => proxyRequest(req, ctx, 'PUT');
|
|
76
|
+
|
|
77
|
+
export const PATCH = (
|
|
78
|
+
req: NextRequest,
|
|
79
|
+
ctx: { params: Promise<{ path: string[] }> },
|
|
80
|
+
) => proxyRequest(req, ctx, 'PATCH');
|
|
81
|
+
|
|
82
|
+
export const DELETE = (
|
|
83
|
+
req: NextRequest,
|
|
84
|
+
ctx: { params: Promise<{ path: string[] }> },
|
|
85
|
+
) => proxyRequest(req, ctx, 'DELETE');
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** 공통 API 에러 형식 */
|
|
2
|
+
export type ApiErrorBody = {
|
|
3
|
+
message: string;
|
|
4
|
+
code: string;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
/** 백엔드 공통 응답 래퍼 */
|
|
8
|
+
export type ApiResponse<TData = unknown, TError = ApiErrorBody> = {
|
|
9
|
+
result: 'SUCCESS' | 'ERROR';
|
|
10
|
+
data: TData;
|
|
11
|
+
error: TError | null;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/** 페이지네이션 응답 래퍼 */
|
|
15
|
+
export type PaginatedData<T> = {
|
|
16
|
+
content: T[];
|
|
17
|
+
totalItems: number;
|
|
18
|
+
offset: number;
|
|
19
|
+
limit: number;
|
|
20
|
+
hasNext: boolean;
|
|
21
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ApiErrorBody } from './apiTypes';
|
|
2
|
+
|
|
3
|
+
export class ApiError extends Error {
|
|
4
|
+
constructor(
|
|
5
|
+
public readonly status: number,
|
|
6
|
+
public readonly code: string,
|
|
7
|
+
public readonly data: ApiErrorBody | null,
|
|
8
|
+
) {
|
|
9
|
+
super(data?.message ?? `API 요청 실패 (${status})`);
|
|
10
|
+
this.name = 'ApiError';
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
|
|
3
|
+
import { ApiError } from './error';
|
|
4
|
+
import type { ApiResponse } from './apiTypes';
|
|
5
|
+
|
|
6
|
+
const HTTP_TIMEOUT = 10_000;
|
|
7
|
+
|
|
8
|
+
const http = axios.create({
|
|
9
|
+
baseURL: '/api/proxy',
|
|
10
|
+
timeout: HTTP_TIMEOUT,
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
// 서버 컴포넌트에서 호출 시 axios 는 절대 URL 이 필요하다.
|
|
14
|
+
// 호스트만 프리픽스하고 그 외 분기는 두지 않는다.
|
|
15
|
+
http.interceptors.request.use(async (config) => {
|
|
16
|
+
if (typeof window !== 'undefined') return config;
|
|
17
|
+
|
|
18
|
+
const { headers: getHeaders } = await import('next/headers');
|
|
19
|
+
const hdrs = await getHeaders();
|
|
20
|
+
const host = hdrs.get('host') ?? 'localhost:3000';
|
|
21
|
+
const protocol = process.env.NODE_ENV === 'production' ? 'https' : 'http';
|
|
22
|
+
config.baseURL = `${protocol}://${host}/api/proxy`;
|
|
23
|
+
return config;
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
http.interceptors.response.use(
|
|
27
|
+
(response) => {
|
|
28
|
+
const body = response.data as ApiResponse;
|
|
29
|
+
if (body && typeof body === 'object' && 'result' in body) {
|
|
30
|
+
if (body.result === 'ERROR') {
|
|
31
|
+
return Promise.reject(
|
|
32
|
+
new ApiError(response.status, body.error?.code ?? '', body.error),
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
response.data = body.data;
|
|
36
|
+
}
|
|
37
|
+
return response;
|
|
38
|
+
},
|
|
39
|
+
(error) => {
|
|
40
|
+
if (axios.isAxiosError(error)) {
|
|
41
|
+
const { response } = error;
|
|
42
|
+
const body = response?.data as ApiResponse | undefined;
|
|
43
|
+
const errorBody = body?.error ?? null;
|
|
44
|
+
return Promise.reject(
|
|
45
|
+
new ApiError(
|
|
46
|
+
response?.status ?? 0,
|
|
47
|
+
errorBody?.code ?? '',
|
|
48
|
+
errorBody,
|
|
49
|
+
),
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
return Promise.reject(error);
|
|
53
|
+
},
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
export { http };
|