@dexyn/common-library 1.0.5 → 1.0.7
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/date/dateUtils.d.ts +5 -1
- package/date/dateUtils.js +5 -1
- package/index.d.ts +6 -0
- package/index.js +11 -0
- package/package.json +1 -1
- package/result/httpResultUtils.d.ts +11 -8
- package/result/httpResultUtils.js +13 -16
- package/result/resultUtils.d.ts +6 -1
- package/result/resultUtils.js +9 -1
- package/validation/validationUtils.d.ts +19 -0
- package/validation/validationUtils.js +18 -0
- package/validation/numberIdSchema.d.ts +0 -11
- package/validation/numberIdSchema.js +0 -15
package/date/dateUtils.d.ts
CHANGED
@@ -8,4 +8,8 @@
|
|
8
8
|
* DateFormatter.formatTime(new Date("2024-12-03T10:30:00"), "en-US", { hour: "2-digit", minute: "2-digit" });
|
9
9
|
* // Output: "10:30 AM"
|
10
10
|
*/
|
11
|
-
|
11
|
+
declare function formatDateTime(date: Date, locale?: string, options?: Intl.DateTimeFormatOptions): string;
|
12
|
+
declare const DateUtils: {
|
13
|
+
formatDateTime: typeof formatDateTime;
|
14
|
+
};
|
15
|
+
export { DateUtils };
|
package/date/dateUtils.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
3
|
+
exports.DateUtils = void 0;
|
4
4
|
/**
|
5
5
|
* Formats a date as a time string.
|
6
6
|
* @param date The `Date` object to format.
|
@@ -14,3 +14,7 @@ exports.formatDateTime = formatDateTime;
|
|
14
14
|
function formatDateTime(date, locale = "en-US", options = { hour: "numeric", minute: "numeric", second: "numeric" }) {
|
15
15
|
return new Intl.DateTimeFormat(locale, options).format(date);
|
16
16
|
}
|
17
|
+
const DateUtils = {
|
18
|
+
formatDateTime
|
19
|
+
};
|
20
|
+
exports.DateUtils = DateUtils;
|
package/index.d.ts
ADDED
@@ -0,0 +1,6 @@
|
|
1
|
+
import { ErrorResult, StandardError, Result, ErrorName } from "./result/resultUtils";
|
2
|
+
export { ResultUtils } from "./result/resultUtils";
|
3
|
+
export { HttpResultUtils } from "./result/httpResultUtils";
|
4
|
+
export { DateUtils } from "./date/dateUtils";
|
5
|
+
export { ValidationUtils } from "./validation/validationUtils";
|
6
|
+
export type { ErrorResult, StandardError, Result, ErrorName };
|
package/index.js
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.ValidationUtils = exports.DateUtils = exports.HttpResultUtils = exports.ResultUtils = void 0;
|
4
|
+
var resultUtils_1 = require("./result/resultUtils");
|
5
|
+
Object.defineProperty(exports, "ResultUtils", { enumerable: true, get: function () { return resultUtils_1.ResultUtils; } });
|
6
|
+
var httpResultUtils_1 = require("./result/httpResultUtils");
|
7
|
+
Object.defineProperty(exports, "HttpResultUtils", { enumerable: true, get: function () { return httpResultUtils_1.HttpResultUtils; } });
|
8
|
+
var dateUtils_1 = require("./date/dateUtils");
|
9
|
+
Object.defineProperty(exports, "DateUtils", { enumerable: true, get: function () { return dateUtils_1.DateUtils; } });
|
10
|
+
var validationUtils_1 = require("./validation/validationUtils");
|
11
|
+
Object.defineProperty(exports, "ValidationUtils", { enumerable: true, get: function () { return validationUtils_1.ValidationUtils; } });
|
package/package.json
CHANGED
@@ -1,10 +1,13 @@
|
|
1
1
|
import { ErrorResult } from "./resultUtils";
|
2
|
-
|
3
|
-
|
4
|
-
status: number;
|
5
|
-
};
|
6
|
-
BadRequest: (error: ErrorResult) => {
|
7
|
-
status: number;
|
8
|
-
jsonBody: string;
|
9
|
-
};
|
2
|
+
declare function Accepted(): {
|
3
|
+
status: number;
|
10
4
|
};
|
5
|
+
declare function BadRequest(error: ErrorResult): {
|
6
|
+
status: number;
|
7
|
+
jsonBody: string;
|
8
|
+
};
|
9
|
+
export declare const HttpResultUtils: {
|
10
|
+
Accepted: typeof Accepted;
|
11
|
+
BadRequest: typeof BadRequest;
|
12
|
+
};
|
13
|
+
export {};
|
@@ -1,18 +1,15 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
return { Accepted, BadRequest };
|
17
|
-
};
|
18
|
-
exports.HttpResult = createHTTPResult();
|
3
|
+
exports.HttpResultUtils = void 0;
|
4
|
+
function Accepted() {
|
5
|
+
return {
|
6
|
+
status: 202,
|
7
|
+
};
|
8
|
+
}
|
9
|
+
function BadRequest(error) {
|
10
|
+
return {
|
11
|
+
status: 400,
|
12
|
+
jsonBody: JSON.stringify(error)
|
13
|
+
};
|
14
|
+
}
|
15
|
+
exports.HttpResultUtils = { Accepted, BadRequest };
|
package/result/resultUtils.d.ts
CHANGED
@@ -26,4 +26,9 @@ export declare function toResult<T>(data: T): Result<T>;
|
|
26
26
|
export declare function errorToErrorResult(error: Error): ErrorResult;
|
27
27
|
export type ErrorName = "UnhandledError" | "Duplicate" | "NotFound" | "ApiError" | "ValidationError";
|
28
28
|
export declare function toErrorResult(name: ErrorName, message: string, details?: Record<string, unknown>, code?: string, statusCode?: number): ErrorResult;
|
29
|
-
|
29
|
+
declare const ResultUtils: {
|
30
|
+
toResult: typeof toResult;
|
31
|
+
errorToErrorResult: typeof errorToErrorResult;
|
32
|
+
toErrorResult: typeof toErrorResult;
|
33
|
+
};
|
34
|
+
export { ResultUtils };
|
package/result/resultUtils.js
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
"use strict";
|
2
2
|
// ResultUtils type for consistent responses
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
4
|
+
exports.ResultUtils = void 0;
|
4
5
|
exports.toResult = toResult;
|
5
6
|
exports.errorToErrorResult = errorToErrorResult;
|
6
7
|
exports.toErrorResult = toErrorResult;
|
@@ -24,6 +25,13 @@ function toErrorResult(name, message, details, code, statusCode) {
|
|
24
25
|
code: code,
|
25
26
|
details: details,
|
26
27
|
statusCode: statusCode,
|
27
|
-
timestamp:
|
28
|
+
timestamp: dateUtils_1.DateUtils.formatDateTime(new Date())
|
28
29
|
} };
|
29
30
|
}
|
31
|
+
// Export everything as a single constant object
|
32
|
+
const ResultUtils = {
|
33
|
+
toResult,
|
34
|
+
errorToErrorResult,
|
35
|
+
toErrorResult,
|
36
|
+
};
|
37
|
+
exports.ResultUtils = ResultUtils;
|
@@ -1,4 +1,23 @@
|
|
1
|
+
import { z } from "zod";
|
2
|
+
export declare const numberIdSchema: z.ZodObject<{
|
3
|
+
id: z.ZodEffects<z.ZodNumber, number, unknown>;
|
4
|
+
}, "strip", z.ZodTypeAny, {
|
5
|
+
id: number;
|
6
|
+
}, {
|
7
|
+
id?: unknown;
|
8
|
+
}>;
|
1
9
|
export declare function formatZodErrorsToString(errors: {
|
2
10
|
path: (string | number)[];
|
3
11
|
message: string;
|
4
12
|
}[]): string;
|
13
|
+
declare const ValidationUtils: {
|
14
|
+
numberIdSchema: z.ZodObject<{
|
15
|
+
id: z.ZodEffects<z.ZodNumber, number, unknown>;
|
16
|
+
}, "strip", z.ZodTypeAny, {
|
17
|
+
id: number;
|
18
|
+
}, {
|
19
|
+
id?: unknown;
|
20
|
+
}>;
|
21
|
+
formatZodErrorsToString: typeof formatZodErrorsToString;
|
22
|
+
};
|
23
|
+
export { ValidationUtils };
|
@@ -1,8 +1,26 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.ValidationUtils = exports.numberIdSchema = void 0;
|
3
4
|
exports.formatZodErrorsToString = formatZodErrorsToString;
|
5
|
+
const zod_1 = require("zod");
|
6
|
+
exports.numberIdSchema = zod_1.z.object({
|
7
|
+
id: zod_1.z.preprocess((val) => {
|
8
|
+
// Convert strings and numbers to a number
|
9
|
+
if (typeof val === 'string' || typeof val === 'number') {
|
10
|
+
return Number(val);
|
11
|
+
}
|
12
|
+
throw new Error("Unsupported type for id"); // Explicitly throw for unsupported types
|
13
|
+
}, zod_1.z.number().positive({
|
14
|
+
message: 'must be a positive number',
|
15
|
+
})),
|
16
|
+
});
|
4
17
|
function formatZodErrorsToString(errors) {
|
5
18
|
return errors
|
6
19
|
.map((err) => `${err.path.join(".")}: ${err.message}`)
|
7
20
|
.join("\n");
|
8
21
|
}
|
22
|
+
const ValidationUtils = {
|
23
|
+
numberIdSchema: exports.numberIdSchema,
|
24
|
+
formatZodErrorsToString,
|
25
|
+
};
|
26
|
+
exports.ValidationUtils = ValidationUtils;
|
@@ -1,15 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.idSchema = void 0;
|
4
|
-
const zod_1 = require("zod");
|
5
|
-
exports.idSchema = zod_1.z.object({
|
6
|
-
id: zod_1.z.preprocess((val) => {
|
7
|
-
// Convert strings and numbers to a number
|
8
|
-
if (typeof val === 'string' || typeof val === 'number') {
|
9
|
-
return Number(val);
|
10
|
-
}
|
11
|
-
throw new Error("Unsupported type for id"); // Explicitly throw for unsupported types
|
12
|
-
}, zod_1.z.number().positive({
|
13
|
-
message: 'must be a positive number',
|
14
|
-
})),
|
15
|
-
});
|