@uityu/uityu-shared 1.0.4 → 1.0.5

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 +1,11 @@
1
+ export type LogLevel = "INFO" | "ERROR";
2
+ export type LogPayload = {
3
+ namespace: string;
4
+ message: string;
5
+ metadata?: Record<string, unknown>;
6
+ error?: unknown;
7
+ };
1
8
  export declare function serializeLogPayload(payload: unknown): Record<string, unknown>;
9
+ export declare function logInfo(payload: LogPayload): void;
10
+ export declare function logError(payload: LogPayload): void;
11
+ export declare function logFirestoreUsage(collection: string, operation: string, metadata?: Record<string, unknown>, hit?: boolean): void;
@@ -13,6 +13,48 @@ export function serializeLogPayload(payload) {
13
13
  return { value: "[unserializable]" };
14
14
  }
15
15
  }
16
+ export function logInfo(payload) {
17
+ log("INFO", payload);
18
+ }
19
+ export function logError(payload) {
20
+ log("ERROR", payload);
21
+ }
22
+ export function logFirestoreUsage(collection, operation, metadata = {}, hit) {
23
+ logInfo({
24
+ namespace: "FirestoreUsage",
25
+ message: `${collection}.${operation}`,
26
+ metadata: { ...metadata, hit },
27
+ });
28
+ }
29
+ function log(level, payload) {
30
+ const entry = {
31
+ level,
32
+ timestamp: new Date().toISOString(),
33
+ namespace: payload.namespace,
34
+ message: payload.message,
35
+ metadata: serializeLogPayload(payload.metadata ?? {}),
36
+ error: normalizeError(payload.error),
37
+ };
38
+ const serialized = JSON.stringify(entry);
39
+ if (level === "ERROR") {
40
+ console.error(serialized);
41
+ return;
42
+ }
43
+ console.log(serialized);
44
+ }
45
+ function normalizeError(error) {
46
+ if (!error) {
47
+ return undefined;
48
+ }
49
+ if (error instanceof Error) {
50
+ return {
51
+ name: error.name,
52
+ message: error.message,
53
+ stack: error.stack,
54
+ };
55
+ }
56
+ return serializeLogPayload(error);
57
+ }
16
58
  function normalizeValue(value) {
17
59
  if (value instanceof Date) {
18
60
  return value.toISOString();
@@ -31,7 +73,9 @@ function normalizeValue(value) {
31
73
  return value;
32
74
  }
33
75
  function hasToJSON(value) {
34
- return Boolean(value && typeof value === "object" && "toJSON" in value && typeof value.toJSON === "function");
76
+ return Boolean(value && typeof value === "object" &&
77
+ "toJSON" in value &&
78
+ typeof value.toJSON === "function");
35
79
  }
36
80
  function isSerializableObject(value) {
37
81
  return Boolean(value && typeof value === "object" && !Array.isArray(value));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uityu/uityu-shared",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -22,8 +22,37 @@
22
22
  "types": "./dist/common/index.d.ts",
23
23
  "import": "./dist/common/index.js"
24
24
  },
25
+ "./common/*": {
26
+ "types": "./dist/common/*/index.d.ts",
27
+ "import": "./dist/common/*/index.js"
28
+ },
25
29
  "./package.json": "./package.json"
26
30
  },
31
+ "typesVersions": {
32
+ "*": {
33
+ "common": [
34
+ "dist/common/index.d.ts"
35
+ ],
36
+ "common/*": [
37
+ "dist/common/*/index.d.ts"
38
+ ],
39
+ "errors": [
40
+ "dist/errors/index.d.ts"
41
+ ],
42
+ "errors/*": [
43
+ "dist/errors/*/index.d.ts"
44
+ ],
45
+ "search-index": [
46
+ "dist/search/search-index.d.ts"
47
+ ],
48
+ "search-index/*": [
49
+ "dist/search/*/index.d.ts"
50
+ ],
51
+ "*": [
52
+ "dist/index.d.ts"
53
+ ]
54
+ }
55
+ },
27
56
  "scripts": {
28
57
  "build": "tsc -p tsconfig.json"
29
58
  }
@@ -1,3 +1,12 @@
1
+ export type LogLevel = "INFO" | "ERROR";
2
+
3
+ export type LogPayload = {
4
+ namespace: string;
5
+ message: string;
6
+ metadata?: Record<string, unknown>;
7
+ error?: unknown;
8
+ };
9
+
1
10
  export function serializeLogPayload(payload: unknown): Record<string, unknown> {
2
11
  if (!isSerializableObject(payload)) {
3
12
  return {};
@@ -18,6 +27,62 @@ export function serializeLogPayload(payload: unknown): Record<string, unknown> {
18
27
  }
19
28
  }
20
29
 
30
+ export function logInfo(payload: LogPayload): void {
31
+ log("INFO", payload);
32
+ }
33
+
34
+ export function logError(payload: LogPayload): void {
35
+ log("ERROR", payload);
36
+ }
37
+
38
+ export function logFirestoreUsage(
39
+ collection: string,
40
+ operation: string,
41
+ metadata: Record<string, unknown> = {},
42
+ hit?: boolean,
43
+ ): void {
44
+ logInfo({
45
+ namespace: "FirestoreUsage",
46
+ message: `${collection}.${operation}`,
47
+ metadata: { ...metadata, hit },
48
+ });
49
+ }
50
+
51
+ function log(level: LogLevel, payload: LogPayload): void {
52
+ const entry = {
53
+ level,
54
+ timestamp: new Date().toISOString(),
55
+ namespace: payload.namespace,
56
+ message: payload.message,
57
+ metadata: serializeLogPayload(payload.metadata ?? {}),
58
+ error: normalizeError(payload.error),
59
+ };
60
+
61
+ const serialized = JSON.stringify(entry);
62
+ if (level === "ERROR") {
63
+ console.error(serialized);
64
+ return;
65
+ }
66
+
67
+ console.log(serialized);
68
+ }
69
+
70
+ function normalizeError(error: unknown): unknown {
71
+ if (!error) {
72
+ return undefined;
73
+ }
74
+
75
+ if (error instanceof Error) {
76
+ return {
77
+ name: error.name,
78
+ message: error.message,
79
+ stack: error.stack,
80
+ };
81
+ }
82
+
83
+ return serializeLogPayload(error);
84
+ }
85
+
21
86
  function normalizeValue(value: unknown): unknown {
22
87
  if (value instanceof Date) {
23
88
  return value.toISOString();
@@ -40,7 +105,9 @@ function normalizeValue(value: unknown): unknown {
40
105
 
41
106
  function hasToJSON(value: unknown): value is { toJSON: () => unknown } {
42
107
  return Boolean(
43
- value && typeof value === "object" && "toJSON" in value && typeof (value as { toJSON?: unknown }).toJSON === "function",
108
+ value && typeof value === "object" &&
109
+ "toJSON" in value &&
110
+ typeof (value as { toJSON?: unknown }).toJSON === "function",
44
111
  );
45
112
  }
46
113