@uniai-fe/util-functions 0.1.0 → 0.2.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.
@@ -1,5 +1,31 @@
1
1
  export { D as DateTimeFormatOptionsType } from '../type-CNV_n0-w.cjs';
2
2
 
3
+ /**
4
+ * 콘솔로그; Next.js API Route 디버깅용
5
+ * @util
6
+ * @param {string} method GET, POST, DELETE
7
+ * @param {string} routeUrl Next.js route API route URL
8
+ * @param {string} [queryUrl] DB API route URL
9
+ * @param {unknown[]} [messages] 로그 데이터
10
+ */
11
+ declare const nextAPILog: (method: string, routeUrl: string, queryUrl?: string, ...messages: unknown[]) => void;
12
+ /**
13
+ * API; 쿼리스트링 생성
14
+ * @util
15
+ * @param {unknown} [searchParams] 쿼리스트링으로 변환할 객체
16
+ * @return {string} 쿼리스트링
17
+ */
18
+ declare const getQueryString: (searchParams?: unknown) => string;
19
+ /**
20
+ * API; POST/DELETE method option
21
+ */
22
+ declare const getFetchOptions: ({ method, headers, body, }: {
23
+ method: string;
24
+ } & Partial<{
25
+ headers: HeadersInit;
26
+ body: BodyInit | null;
27
+ }>) => RequestInit;
28
+
3
29
  type InfraKey = "ai" | "db" | "uniai" | (string & {});
4
30
  type CommonPostResponseType = {
5
31
  is_ok: boolean;
@@ -8,6 +34,7 @@ type CommonPostResponseType = {
8
34
  error: string;
9
35
  };
10
36
  type DomainResolver<Infra extends InfraKey = InfraKey> = (infra: Infra) => string | undefined;
37
+ type EnvDomainMap<Infra extends InfraKey = InfraKey> = Partial<Record<Infra, string>>;
11
38
  type ApiLogger = (method: string, routeUrl: string, queryUrl?: string, ...messages: unknown[]) => void;
12
39
  type BackendApiConfig<Infra extends InfraKey = InfraKey> = {
13
40
  resolveDomain: DomainResolver<Infra>;
@@ -34,22 +61,182 @@ type FetchBackendQueryOptions<Infra extends InfraKey, FetchRequestType extends o
34
61
  };
35
62
 
36
63
  /**
37
- * 기본 API 로거.
38
- * 개발 환경에서만 동작하며 요청/응답 정보를 콘솔에 출력합니다.
64
+ * API 요청 url 생성; GET 타입
65
+ * @util
66
+ * @param {object} props
67
+ * @param {"ai" | "db" | string} props.domain API 인프라
68
+ * @param {string} props.routeUrl Next.js /app/api 라우트 주소
69
+ * @param {string} props.queryUrl 백엔드 API url
70
+ * @param {URLSearchParams} [props.searchParams] 쿼리 스트링 추출
71
+ * @param {object} [props.log] 디버깅용 서버 로그 정보
72
+ * @param {boolean} [props.logDisabled] 로그 비활성화
73
+ * @return {string} GET API 요청 full url
39
74
  */
40
- declare const defaultLogger: ApiLogger;
75
+ declare const generateBackendQueryUrl_GET: ({ domain, routeUrl, queryUrl, searchParams, log, logDisabled, }: {
76
+ /**
77
+ * API 도메인
78
+ */
79
+ domain: string;
80
+ /**
81
+ * Next.js /app/api 라우트 주소
82
+ */
83
+ routeUrl: string;
84
+ /**
85
+ * 백엔드 API url
86
+ */
87
+ queryUrl: string;
88
+ } & Partial<{
89
+ /**
90
+ * 쿼리 스트링
91
+ * @desc
92
+ * - URLSearchParams 객체로 가공된 파라미터
93
+ */
94
+ searchParams: URLSearchParams | object;
95
+ /**
96
+ * 디버깅용 서버 로그 정보
97
+ */
98
+ log: object;
99
+ /**
100
+ * 로그 비활성화
101
+ * @default false
102
+ */
103
+ logDisabled: boolean;
104
+ }>) => string;
41
105
  /**
42
- * 객체나 URLSearchParams를 쿼리스트링으로 직렬화합니다.
106
+ * API fetch 요청; POST/DELETE 타입
107
+ * @util
108
+ * @param {object} props
109
+ * @param {"ai" | "db" | string} props.infra API 인프라
110
+ * @param {"POST" | "DELETE"} props.method POST, DELETE
111
+ * @param {string} props.routeUrl Next.js /app/api 라우트 주소
112
+ * @param {string} props.queryUrl 백엔드 API url
113
+ * @param {HeadersInit} [props.headers] fetch Headers
114
+ * @param {BodyInit | null} [props.body] post/delete body (이대로 바로 전송됨)
115
+ * @param {object} [props.bodyData] body로 전송하기 위해 가공이 필요한 데이터 객체
116
+ * @param {object} [props.queryStringData] url에 쿼리스트링으로 요청하는 경우에 대한 데이터 객체
117
+ * @param {object} [props.log] 디버깅용 서버 로그 정보
118
+ * @param {boolean} [props.logDisabled] 로그 비활성화
119
+ * @param {boolean} [props.fetchDisabled] fetch 실행 비활성화
120
+ * @return {Promise<MutateAPICommonResponseType>} POST, DELETE 응답
43
121
  */
44
- declare const getQueryString: (searchParams?: unknown) => string;
122
+ declare const fetchBackendQuery: <FetchRequestType extends object, FetchResponseType extends CommonPostResponseType>({ domain, method, routeUrl, queryUrl, headers, body, bodyOriginData, queryStringData, log, logDisabled, fetchDisabled, }: {
123
+ /**
124
+ * API 도메인
125
+ */
126
+ domain: string;
127
+ /**
128
+ * 요청 방식
129
+ * POST, DELETE
130
+ */
131
+ method: "POST" | "DELETE";
132
+ /**
133
+ * 프론트 API URL
134
+ */
135
+ routeUrl: string;
136
+ /**
137
+ * 백엔드 API 요청 URL
138
+ */
139
+ queryUrl: string;
140
+ } & Partial<{
141
+ /**
142
+ * fetch Headers
143
+ */
144
+ headers: HeadersInit;
145
+ /**
146
+ * fetch Body
147
+ */
148
+ body: BodyInit | null;
149
+ /**
150
+ * fetch Body 를 SearchParams 전환할 데이터
151
+ */
152
+ bodyOriginData: FetchRequestType;
153
+ /**
154
+ * URL 쿼리 스트링
155
+ */
156
+ queryStringData: FetchRequestType;
157
+ /**
158
+ * 디버그용 로그 객체
159
+ */
160
+ log: object;
161
+ /**
162
+ * 로그 비활성화
163
+ */
164
+ logDisabled: boolean;
165
+ /**
166
+ * fetch 비활성화
167
+ */
168
+ fetchDisabled: boolean;
169
+ }>) => Promise<FetchResponseType>;
45
170
  /**
46
- * 인프라 도메인 해석기와 로거를 주입받아 API 헬퍼 집합을 생성합니다.
171
+ * API; POST/DELETE method fetch
172
+ * @util
173
+ * @param {object} props
174
+ * @param {"ai" | "db" | string} props.domain
175
+ * @param {"POST" | "DELETE"} props.method POST, DELETE
176
+ * @param {string} props.routeUrl Next.js /app/api 라우트 주소
177
+ * @param {string} props.queryUrl 백엔드 API url
178
+ * @param {HeadersInit} [props.headers] fetch Headers
179
+ * @param {BodyInit | null} [props.body] post/delete body (이대로 바로 전송됨)
180
+ * @param {object} [props.bodyData] body로 전송하기 위해 가공이 필요한 데이터 객체
181
+ * @param {object} [props.log] 디버깅용 서버 로그 정보
182
+ * @param {boolean} [props.logDisabled] 로그 비활성화
183
+ * @param {boolean} [props.fetchDisabled] fetch 실행 비활성화
184
+ * @param {object} [props.alternateResponse] fetch 실패 시 대체 응답
185
+ * @return {Promise<ResponseType>} POST, DELETE 응답
47
186
  */
48
- declare const createBackendApiClient: <Infra extends InfraKey = InfraKey>(config: BackendApiConfig<Infra>) => {
49
- getQueryString: (searchParams?: unknown) => string;
50
- generateBackendQueryUrl_GET: ({ infra, domain, routeUrl, queryUrl, searchParams, log, logDisabled, logger, }: GenerateBackendQueryUrlOptions<Infra>) => string;
51
- fetchBackendQuery: <FetchRequestType extends object, FetchResponseType extends CommonPostResponseType>({ infra, domain, routeUrl, queryUrl, method, headers, body, bodyOriginData, queryStringData, log, logDisabled, fetchDisabled, logger, }: FetchBackendQueryOptions<Infra, FetchRequestType>) => Promise<FetchResponseType>;
52
- logger: ApiLogger;
53
- };
187
+ declare const fetchWithBody: <BodyDataType = object, ResponseType extends object | Response = Response>({ domain, method, routeUrl, queryUrl, searchParams, headers, body, bodyData, isRawResponse, alternateResponse, debug, disabled, disabledLog, }: {
188
+ /**
189
+ * API 도메인
190
+ */
191
+ domain: string;
192
+ /**
193
+ * 요청 방식
194
+ * POST, DELETE
195
+ */
196
+ method: string;
197
+ /**
198
+ * 프론트 API URL
199
+ */
200
+ routeUrl: string;
201
+ /**
202
+ * 백엔드 API 요청 URL
203
+ */
204
+ queryUrl: string;
205
+ /**
206
+ * fetch 실패 시 대체 응답
207
+ */
208
+ alternateResponse: ResponseType;
209
+ } & Partial<{
210
+ searchParams: URLSearchParams | object;
211
+ /**
212
+ * fetch Headers
213
+ */
214
+ headers: HeadersInit;
215
+ /**
216
+ * fetch Body
217
+ */
218
+ body: BodyInit | null;
219
+ /**
220
+ * fetch Body Data
221
+ */
222
+ bodyData: BodyDataType;
223
+ /**
224
+ * 응답값 원본으로 return
225
+ * const responseRaw = await fetch(api);
226
+ */
227
+ isRawResponse: boolean;
228
+ /**
229
+ * 디버그용 로그 객체
230
+ */
231
+ debug: object;
232
+ /**
233
+ * fetch 비활성화
234
+ */
235
+ disabled: boolean;
236
+ /**
237
+ * 로그 비활성화
238
+ */
239
+ disabledLog: boolean;
240
+ }>) => Promise<ResponseType>;
54
241
 
55
- export { type ApiLogger, type BackendApiConfig, type CommonPostResponseType, type DomainResolver, type FetchBackendQueryOptions, type GenerateBackendQueryUrlOptions, type InfraKey, createBackendApiClient, defaultLogger as defaultApiLogger, getQueryString };
242
+ export { type ApiLogger, type BackendApiConfig, type CommonPostResponseType, type DomainResolver, type EnvDomainMap, type FetchBackendQueryOptions, type GenerateBackendQueryUrlOptions, type InfraKey, fetchBackendQuery, fetchWithBody, generateBackendQueryUrl_GET, getFetchOptions, getQueryString, nextAPILog };
@@ -1,5 +1,31 @@
1
1
  export { D as DateTimeFormatOptionsType } from '../type-CNV_n0-w.js';
2
2
 
3
+ /**
4
+ * 콘솔로그; Next.js API Route 디버깅용
5
+ * @util
6
+ * @param {string} method GET, POST, DELETE
7
+ * @param {string} routeUrl Next.js route API route URL
8
+ * @param {string} [queryUrl] DB API route URL
9
+ * @param {unknown[]} [messages] 로그 데이터
10
+ */
11
+ declare const nextAPILog: (method: string, routeUrl: string, queryUrl?: string, ...messages: unknown[]) => void;
12
+ /**
13
+ * API; 쿼리스트링 생성
14
+ * @util
15
+ * @param {unknown} [searchParams] 쿼리스트링으로 변환할 객체
16
+ * @return {string} 쿼리스트링
17
+ */
18
+ declare const getQueryString: (searchParams?: unknown) => string;
19
+ /**
20
+ * API; POST/DELETE method option
21
+ */
22
+ declare const getFetchOptions: ({ method, headers, body, }: {
23
+ method: string;
24
+ } & Partial<{
25
+ headers: HeadersInit;
26
+ body: BodyInit | null;
27
+ }>) => RequestInit;
28
+
3
29
  type InfraKey = "ai" | "db" | "uniai" | (string & {});
4
30
  type CommonPostResponseType = {
5
31
  is_ok: boolean;
@@ -8,6 +34,7 @@ type CommonPostResponseType = {
8
34
  error: string;
9
35
  };
10
36
  type DomainResolver<Infra extends InfraKey = InfraKey> = (infra: Infra) => string | undefined;
37
+ type EnvDomainMap<Infra extends InfraKey = InfraKey> = Partial<Record<Infra, string>>;
11
38
  type ApiLogger = (method: string, routeUrl: string, queryUrl?: string, ...messages: unknown[]) => void;
12
39
  type BackendApiConfig<Infra extends InfraKey = InfraKey> = {
13
40
  resolveDomain: DomainResolver<Infra>;
@@ -34,22 +61,182 @@ type FetchBackendQueryOptions<Infra extends InfraKey, FetchRequestType extends o
34
61
  };
35
62
 
36
63
  /**
37
- * 기본 API 로거.
38
- * 개발 환경에서만 동작하며 요청/응답 정보를 콘솔에 출력합니다.
64
+ * API 요청 url 생성; GET 타입
65
+ * @util
66
+ * @param {object} props
67
+ * @param {"ai" | "db" | string} props.domain API 인프라
68
+ * @param {string} props.routeUrl Next.js /app/api 라우트 주소
69
+ * @param {string} props.queryUrl 백엔드 API url
70
+ * @param {URLSearchParams} [props.searchParams] 쿼리 스트링 추출
71
+ * @param {object} [props.log] 디버깅용 서버 로그 정보
72
+ * @param {boolean} [props.logDisabled] 로그 비활성화
73
+ * @return {string} GET API 요청 full url
39
74
  */
40
- declare const defaultLogger: ApiLogger;
75
+ declare const generateBackendQueryUrl_GET: ({ domain, routeUrl, queryUrl, searchParams, log, logDisabled, }: {
76
+ /**
77
+ * API 도메인
78
+ */
79
+ domain: string;
80
+ /**
81
+ * Next.js /app/api 라우트 주소
82
+ */
83
+ routeUrl: string;
84
+ /**
85
+ * 백엔드 API url
86
+ */
87
+ queryUrl: string;
88
+ } & Partial<{
89
+ /**
90
+ * 쿼리 스트링
91
+ * @desc
92
+ * - URLSearchParams 객체로 가공된 파라미터
93
+ */
94
+ searchParams: URLSearchParams | object;
95
+ /**
96
+ * 디버깅용 서버 로그 정보
97
+ */
98
+ log: object;
99
+ /**
100
+ * 로그 비활성화
101
+ * @default false
102
+ */
103
+ logDisabled: boolean;
104
+ }>) => string;
41
105
  /**
42
- * 객체나 URLSearchParams를 쿼리스트링으로 직렬화합니다.
106
+ * API fetch 요청; POST/DELETE 타입
107
+ * @util
108
+ * @param {object} props
109
+ * @param {"ai" | "db" | string} props.infra API 인프라
110
+ * @param {"POST" | "DELETE"} props.method POST, DELETE
111
+ * @param {string} props.routeUrl Next.js /app/api 라우트 주소
112
+ * @param {string} props.queryUrl 백엔드 API url
113
+ * @param {HeadersInit} [props.headers] fetch Headers
114
+ * @param {BodyInit | null} [props.body] post/delete body (이대로 바로 전송됨)
115
+ * @param {object} [props.bodyData] body로 전송하기 위해 가공이 필요한 데이터 객체
116
+ * @param {object} [props.queryStringData] url에 쿼리스트링으로 요청하는 경우에 대한 데이터 객체
117
+ * @param {object} [props.log] 디버깅용 서버 로그 정보
118
+ * @param {boolean} [props.logDisabled] 로그 비활성화
119
+ * @param {boolean} [props.fetchDisabled] fetch 실행 비활성화
120
+ * @return {Promise<MutateAPICommonResponseType>} POST, DELETE 응답
43
121
  */
44
- declare const getQueryString: (searchParams?: unknown) => string;
122
+ declare const fetchBackendQuery: <FetchRequestType extends object, FetchResponseType extends CommonPostResponseType>({ domain, method, routeUrl, queryUrl, headers, body, bodyOriginData, queryStringData, log, logDisabled, fetchDisabled, }: {
123
+ /**
124
+ * API 도메인
125
+ */
126
+ domain: string;
127
+ /**
128
+ * 요청 방식
129
+ * POST, DELETE
130
+ */
131
+ method: "POST" | "DELETE";
132
+ /**
133
+ * 프론트 API URL
134
+ */
135
+ routeUrl: string;
136
+ /**
137
+ * 백엔드 API 요청 URL
138
+ */
139
+ queryUrl: string;
140
+ } & Partial<{
141
+ /**
142
+ * fetch Headers
143
+ */
144
+ headers: HeadersInit;
145
+ /**
146
+ * fetch Body
147
+ */
148
+ body: BodyInit | null;
149
+ /**
150
+ * fetch Body 를 SearchParams 전환할 데이터
151
+ */
152
+ bodyOriginData: FetchRequestType;
153
+ /**
154
+ * URL 쿼리 스트링
155
+ */
156
+ queryStringData: FetchRequestType;
157
+ /**
158
+ * 디버그용 로그 객체
159
+ */
160
+ log: object;
161
+ /**
162
+ * 로그 비활성화
163
+ */
164
+ logDisabled: boolean;
165
+ /**
166
+ * fetch 비활성화
167
+ */
168
+ fetchDisabled: boolean;
169
+ }>) => Promise<FetchResponseType>;
45
170
  /**
46
- * 인프라 도메인 해석기와 로거를 주입받아 API 헬퍼 집합을 생성합니다.
171
+ * API; POST/DELETE method fetch
172
+ * @util
173
+ * @param {object} props
174
+ * @param {"ai" | "db" | string} props.domain
175
+ * @param {"POST" | "DELETE"} props.method POST, DELETE
176
+ * @param {string} props.routeUrl Next.js /app/api 라우트 주소
177
+ * @param {string} props.queryUrl 백엔드 API url
178
+ * @param {HeadersInit} [props.headers] fetch Headers
179
+ * @param {BodyInit | null} [props.body] post/delete body (이대로 바로 전송됨)
180
+ * @param {object} [props.bodyData] body로 전송하기 위해 가공이 필요한 데이터 객체
181
+ * @param {object} [props.log] 디버깅용 서버 로그 정보
182
+ * @param {boolean} [props.logDisabled] 로그 비활성화
183
+ * @param {boolean} [props.fetchDisabled] fetch 실행 비활성화
184
+ * @param {object} [props.alternateResponse] fetch 실패 시 대체 응답
185
+ * @return {Promise<ResponseType>} POST, DELETE 응답
47
186
  */
48
- declare const createBackendApiClient: <Infra extends InfraKey = InfraKey>(config: BackendApiConfig<Infra>) => {
49
- getQueryString: (searchParams?: unknown) => string;
50
- generateBackendQueryUrl_GET: ({ infra, domain, routeUrl, queryUrl, searchParams, log, logDisabled, logger, }: GenerateBackendQueryUrlOptions<Infra>) => string;
51
- fetchBackendQuery: <FetchRequestType extends object, FetchResponseType extends CommonPostResponseType>({ infra, domain, routeUrl, queryUrl, method, headers, body, bodyOriginData, queryStringData, log, logDisabled, fetchDisabled, logger, }: FetchBackendQueryOptions<Infra, FetchRequestType>) => Promise<FetchResponseType>;
52
- logger: ApiLogger;
53
- };
187
+ declare const fetchWithBody: <BodyDataType = object, ResponseType extends object | Response = Response>({ domain, method, routeUrl, queryUrl, searchParams, headers, body, bodyData, isRawResponse, alternateResponse, debug, disabled, disabledLog, }: {
188
+ /**
189
+ * API 도메인
190
+ */
191
+ domain: string;
192
+ /**
193
+ * 요청 방식
194
+ * POST, DELETE
195
+ */
196
+ method: string;
197
+ /**
198
+ * 프론트 API URL
199
+ */
200
+ routeUrl: string;
201
+ /**
202
+ * 백엔드 API 요청 URL
203
+ */
204
+ queryUrl: string;
205
+ /**
206
+ * fetch 실패 시 대체 응답
207
+ */
208
+ alternateResponse: ResponseType;
209
+ } & Partial<{
210
+ searchParams: URLSearchParams | object;
211
+ /**
212
+ * fetch Headers
213
+ */
214
+ headers: HeadersInit;
215
+ /**
216
+ * fetch Body
217
+ */
218
+ body: BodyInit | null;
219
+ /**
220
+ * fetch Body Data
221
+ */
222
+ bodyData: BodyDataType;
223
+ /**
224
+ * 응답값 원본으로 return
225
+ * const responseRaw = await fetch(api);
226
+ */
227
+ isRawResponse: boolean;
228
+ /**
229
+ * 디버그용 로그 객체
230
+ */
231
+ debug: object;
232
+ /**
233
+ * fetch 비활성화
234
+ */
235
+ disabled: boolean;
236
+ /**
237
+ * 로그 비활성화
238
+ */
239
+ disabledLog: boolean;
240
+ }>) => Promise<ResponseType>;
54
241
 
55
- export { type ApiLogger, type BackendApiConfig, type CommonPostResponseType, type DomainResolver, type FetchBackendQueryOptions, type GenerateBackendQueryUrlOptions, type InfraKey, createBackendApiClient, defaultLogger as defaultApiLogger, getQueryString };
242
+ export { type ApiLogger, type BackendApiConfig, type CommonPostResponseType, type DomainResolver, type EnvDomainMap, type FetchBackendQueryOptions, type GenerateBackendQueryUrlOptions, type InfraKey, fetchBackendQuery, fetchWithBody, generateBackendQueryUrl_GET, getFetchOptions, getQueryString, nextAPILog };