@yimingliao/cms 0.0.20 → 0.0.21

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.
@@ -0,0 +1,30 @@
1
+ import { R as Result } from '../types-DHlRoJwv.js';
2
+ import { Logger } from 'logry';
3
+
4
+ interface FetchContext {
5
+ input: string;
6
+ init: RequestInit;
7
+ meta: {
8
+ baseUrl?: string;
9
+ url?: string;
10
+ startTime?: number;
11
+ };
12
+ }
13
+ type RequestInterceptor = (ctx: FetchContext) => Promise<FetchContext> | FetchContext;
14
+ type ResponseInterceptor = <T>(response: Response, ctx: FetchContext) => Promise<Result<T>>;
15
+
16
+ declare function createSmartFetch({ requestInterceptor, responseInterceptor, logger, }: {
17
+ requestInterceptor: RequestInterceptor;
18
+ responseInterceptor: ResponseInterceptor;
19
+ logger: Logger;
20
+ }): <T>(input: string, init?: RequestInit) => Promise<Result<T>>;
21
+
22
+ declare function createRequestInterceptor({ baseUrl }: {
23
+ baseUrl: string;
24
+ }): RequestInterceptor;
25
+
26
+ declare function createResponseInterceptor({ logger }: {
27
+ logger: Logger;
28
+ }): ResponseInterceptor;
29
+
30
+ export { createRequestInterceptor, createResponseInterceptor, createSmartFetch };
@@ -0,0 +1,68 @@
1
+ // src/client/infrastructure/smart-fetch/smart-fetch.ts
2
+ function createSmartFetch({
3
+ requestInterceptor,
4
+ responseInterceptor,
5
+ logger
6
+ }) {
7
+ const smartFetch = async (input, init = {}) => {
8
+ try {
9
+ let ctx = {
10
+ input,
11
+ init,
12
+ meta: {}
13
+ };
14
+ ctx = await requestInterceptor(ctx);
15
+ const response = await fetch(ctx.input, ctx.init);
16
+ return await responseInterceptor(response, ctx);
17
+ } catch (error) {
18
+ logger.error("smartFetch error", { error });
19
+ throw error instanceof Error ? error : new Error("Network Error");
20
+ }
21
+ };
22
+ return smartFetch;
23
+ }
24
+
25
+ // src/client/infrastructure/smart-fetch/request-interceptor.ts
26
+ function createRequestInterceptor({ baseUrl }) {
27
+ const requestInterceptor = (ctx) => {
28
+ const normalizedBase = baseUrl.replace(/\/+$/, "");
29
+ const normalizedPath = ctx.input.replace(/^\/+/, "");
30
+ const isAbsolute = /^[a-zA-Z]+:\/\//.test(ctx.input);
31
+ const input = isAbsolute ? ctx.input : `${normalizedBase}/${normalizedPath}`;
32
+ return {
33
+ ...ctx,
34
+ input,
35
+ meta: {
36
+ ...ctx.meta,
37
+ baseUrl,
38
+ url: ctx.input,
39
+ startTime: Date.now()
40
+ }
41
+ };
42
+ };
43
+ return requestInterceptor;
44
+ }
45
+
46
+ // src/client/infrastructure/smart-fetch/response-interceptor.ts
47
+ function createResponseInterceptor({ logger }) {
48
+ const responseInterceptor = async (response, ctx) => {
49
+ const duration = Date.now() - (ctx.meta.startTime ?? 0);
50
+ if (!response.ok) {
51
+ logger.warn("HTTP error", {
52
+ url: ctx.meta.url,
53
+ status: response.status,
54
+ duration
55
+ });
56
+ throw new Error(`HTTP Error ${response.status}`);
57
+ }
58
+ const data = await response.json();
59
+ logger.debug("Fetch success", {
60
+ url: ctx.meta.url,
61
+ duration
62
+ });
63
+ return data;
64
+ };
65
+ return responseInterceptor;
66
+ }
67
+
68
+ export { createRequestInterceptor, createResponseInterceptor, createSmartFetch };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  import { F as FolderFull } from './base-DbGnfZr6.js';
2
2
  export { A as ADMIN_ROLES, a as Admin, b as AdminCard, c as AdminFull, d as AdminRefreshToken, e as AdminRole, f as AdminSafe, g as AdminTranslation, h as Alternate, B as BaseTranslation, D as DeviceInfo, E as ExternalLink, i as FILE_TYPES, j as Faq, k as File, l as FileCard, m as FileFull, n as FileTranslation, o as FileType, p as Folder, M as MultiItems, P as POST_TYPES, q as Post, r as PostFull, s as PostListCard, t as PostTranslation, u as PostType, S as SeoMetadata, v as SingleItem, T as TocItem, w as Translation } from './base-DbGnfZr6.js';
3
3
  export { B as BlobFile } from './types-0oS1A2K5.js';
4
+ import { E as ErrorDetail, S as SuccessResult, a as ErrorResult } from './types-DHlRoJwv.js';
5
+ export { R as Result } from './types-DHlRoJwv.js';
4
6
 
5
7
  declare const ROOT_FOLDER_ID = "01ARZ3NDEKTSV4RRFFQ69G5FAV";
6
8
  declare const ROOT_FOLDER_NAME = "ROOT";
@@ -21,25 +23,6 @@ declare const getMediaInfo: (file: Blob) => Promise<MediaInfo>;
21
23
 
22
24
  declare const formatFileSize: (size: number, decimals?: number) => string;
23
25
 
24
- interface SuccessResult<D = unknown> {
25
- success: true;
26
- message?: string;
27
- data?: D;
28
- meta?: Record<string, unknown>;
29
- }
30
- interface ErrorResult {
31
- success: false;
32
- message?: string;
33
- errors?: ErrorDetail[];
34
- code?: string;
35
- }
36
- interface ErrorDetail {
37
- field?: string;
38
- message?: string;
39
- code?: string;
40
- }
41
- type Result<D = unknown> = SuccessResult<D> | ErrorResult;
42
-
43
26
  interface SuccessResultParams<D = unknown> {
44
27
  message?: string;
45
28
  data?: D;
@@ -61,4 +44,4 @@ declare const result: {
61
44
  error: typeof error;
62
45
  };
63
46
 
64
- export { type ErrorDetail, type ErrorResult, type ErrorResultParams, FolderFull, ROOT_FOLDER, ROOT_FOLDER_ID, ROOT_FOLDER_NAME, type Result, SIMPLE_UPLOAD_FOLDER_KEY, SIMPLE_UPLOAD_FOLDER_NAME, type SuccessResult, type SuccessResultParams, classifyFileType, formatFileSize, getMediaInfo, mimeToExtension, result };
47
+ export { ErrorDetail, ErrorResult, type ErrorResultParams, FolderFull, ROOT_FOLDER, ROOT_FOLDER_ID, ROOT_FOLDER_NAME, SIMPLE_UPLOAD_FOLDER_KEY, SIMPLE_UPLOAD_FOLDER_NAME, SuccessResult, type SuccessResultParams, classifyFileType, formatFileSize, getMediaInfo, mimeToExtension, result };
@@ -0,0 +1,20 @@
1
+ interface SuccessResult<D = unknown> {
2
+ success: true;
3
+ message?: string;
4
+ data?: D;
5
+ meta?: Record<string, unknown>;
6
+ }
7
+ interface ErrorResult {
8
+ success: false;
9
+ message?: string;
10
+ errors?: ErrorDetail[];
11
+ code?: string;
12
+ }
13
+ interface ErrorDetail {
14
+ field?: string;
15
+ message?: string;
16
+ code?: string;
17
+ }
18
+ type Result<D = unknown> = SuccessResult<D> | ErrorResult;
19
+
20
+ export type { ErrorDetail as E, Result as R, SuccessResult as S, ErrorResult as a };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yimingliao/cms",
3
- "version": "0.0.20",
3
+ "version": "0.0.21",
4
4
  "author": "Yiming Liao",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -10,6 +10,11 @@
10
10
  "import": "./dist/index.js",
11
11
  "require": "./dist/index.js"
12
12
  },
13
+ "./client": {
14
+ "types": "./dist/client/index.d.ts",
15
+ "import": "./dist/client/index.js",
16
+ "require": "./dist/client/index.js"
17
+ },
13
18
  "./server": {
14
19
  "types": "./dist/server/index.d.ts",
15
20
  "import": "./dist/server/index.js",