topsyde-utils 1.0.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.
Files changed (48) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +192 -0
  3. package/dist/app.d.ts +11 -0
  4. package/dist/app.js +32 -0
  5. package/dist/app.js.map +1 -0
  6. package/dist/consts.d.ts +30 -0
  7. package/dist/consts.js +34 -0
  8. package/dist/consts.js.map +1 -0
  9. package/dist/enums.d.ts +13 -0
  10. package/dist/enums.js +19 -0
  11. package/dist/enums.js.map +1 -0
  12. package/dist/errors.d.ts +37 -0
  13. package/dist/errors.js +46 -0
  14. package/dist/errors.js.map +1 -0
  15. package/dist/guards.d.ts +19 -0
  16. package/dist/guards.js +42 -0
  17. package/dist/guards.js.map +1 -0
  18. package/dist/index.d.ts +13 -0
  19. package/dist/index.js +75 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/initializable.d.ts +134 -0
  22. package/dist/initializable.js +311 -0
  23. package/dist/initializable.js.map +1 -0
  24. package/dist/lib.d.ts +67 -0
  25. package/dist/lib.js +488 -0
  26. package/dist/lib.js.map +1 -0
  27. package/dist/router/index.d.ts +5 -0
  28. package/dist/router/index.js +30 -0
  29. package/dist/router/index.js.map +1 -0
  30. package/dist/router/middleware.d.ts +21 -0
  31. package/dist/router/middleware.js +47 -0
  32. package/dist/router/middleware.js.map +1 -0
  33. package/dist/router/route.d.ts +14 -0
  34. package/dist/router/route.js +22 -0
  35. package/dist/router/route.js.map +1 -0
  36. package/dist/router/router.d.ts +20 -0
  37. package/dist/router/router.js +35 -0
  38. package/dist/router/router.js.map +1 -0
  39. package/dist/singleton.d.ts +97 -0
  40. package/dist/singleton.js +142 -0
  41. package/dist/singleton.js.map +1 -0
  42. package/dist/throwable.d.ts +42 -0
  43. package/dist/throwable.js +74 -0
  44. package/dist/throwable.js.map +1 -0
  45. package/dist/types.d.ts +9 -0
  46. package/dist/types.js +3 -0
  47. package/dist/types.js.map +1 -0
  48. package/package.json +55 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"route.js","sourceRoot":"","sources":["../../src/router/route.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACH,MAAa,KAAK;IAChB,YACkB,IAAY,EACZ,MAAc,EACd,OAA6C;QAF7C,SAAI,GAAJ,IAAI,CAAQ;QACZ,WAAM,GAAN,MAAM,CAAQ;QACd,YAAO,GAAP,OAAO,CAAsC;IAC5D,CAAC;IAEJ;;OAEG;IACI,OAAO,CAAC,IAAY,EAAE,MAAc;QACzC,OAAO,IAAI,CAAC,IAAI,KAAK,IAAI,IAAI,IAAI,CAAC,MAAM,KAAK,MAAM,CAAC;IACtD,CAAC;CACF;AAbD,sBAaC;AAED,kBAAe,KAAK,CAAC"}
@@ -0,0 +1,20 @@
1
+ import { Route } from './route';
2
+ /**
3
+ * Simple router for handling HTTP requests
4
+ */
5
+ declare class Router {
6
+ private routes;
7
+ /**
8
+ * Add a new route to the router
9
+ */
10
+ addRoute(route: Route): Router;
11
+ /**
12
+ * Find a route that matches the given path and method
13
+ */
14
+ findRoute(path: string, method: string): Route | undefined;
15
+ /**
16
+ * Handle a request by finding and executing the matching route
17
+ */
18
+ handleRequest(req: any, res: any): Promise<any>;
19
+ }
20
+ export default Router;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * Simple router for handling HTTP requests
5
+ */
6
+ class Router {
7
+ constructor() {
8
+ this.routes = [];
9
+ }
10
+ /**
11
+ * Add a new route to the router
12
+ */
13
+ addRoute(route) {
14
+ this.routes.push(route);
15
+ return this;
16
+ }
17
+ /**
18
+ * Find a route that matches the given path and method
19
+ */
20
+ findRoute(path, method) {
21
+ return this.routes.find(route => route.matches(path, method));
22
+ }
23
+ /**
24
+ * Handle a request by finding and executing the matching route
25
+ */
26
+ async handleRequest(req, res) {
27
+ const route = this.findRoute(req.path, req.method);
28
+ if (!route) {
29
+ throw new Error(`No route found for ${req.method} ${req.path}`);
30
+ }
31
+ return route.handler(req, res);
32
+ }
33
+ }
34
+ exports.default = Router;
35
+ //# sourceMappingURL=router.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"router.js","sourceRoot":"","sources":["../../src/router/router.ts"],"names":[],"mappings":";;AAEA;;GAEG;AACH,MAAM,MAAM;IAAZ;QACU,WAAM,GAAY,EAAE,CAAC;IA6B/B,CAAC;IA3BC;;OAEG;IACI,QAAQ,CAAC,KAAY;QAC1B,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACxB,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;OAEG;IACI,SAAS,CAAC,IAAY,EAAE,MAAc;QAC3C,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC;IAChE,CAAC;IAED;;OAEG;IACI,KAAK,CAAC,aAAa,CAAC,GAAQ,EAAE,GAAQ;QAC3C,MAAM,KAAK,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;QAEnD,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,MAAM,IAAI,KAAK,CAAC,sBAAsB,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC;QAClE,CAAC;QAED,OAAO,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IACjC,CAAC;CACF;AAED,kBAAe,MAAM,CAAC"}
@@ -0,0 +1,97 @@
1
+ /**
2
+ * Type representing a constructor function or class type
3
+ */
4
+ export type Constructor<T> = new (...args: any[]) => T;
5
+ /**
6
+ * Type for a singleton constructor with static methods
7
+ */
8
+ export type SingletonConstructor<T extends Singleton> = Constructor<T> & typeof Singleton;
9
+ /**
10
+ * Enhanced interface for singleton classes, providing type-safe instance management
11
+ */
12
+ export interface SingletonClass<T extends Singleton> {
13
+ new (...args: any[]): T;
14
+ GetInstance<U extends T>(...args: any[]): U;
15
+ HasInstance(): boolean;
16
+ ClearInstance(): boolean;
17
+ CreateFactory<U extends T>(...args: any[]): () => U;
18
+ }
19
+ /**
20
+ * Base class for implementing the singleton pattern with type-safe instance management.
21
+ * Supports constructors with any number of arguments.
22
+ *
23
+ * @example
24
+ * // No constructor arguments
25
+ * class DatabaseExample extends Singleton {
26
+ * private constructor() { super(); }
27
+ * public static connect() {
28
+ * return this.getInstance();
29
+ * }
30
+ * }
31
+ *
32
+ * @example
33
+ * // With constructor arguments
34
+ * class EncryptionServiceExample extends Singleton {
35
+ * private constructor(key: string, algorithm: string) { super(); }
36
+ * public static create(key: string, algorithm: string) {
37
+ * return this.getInstance(key, algorithm);
38
+ * }
39
+ * }
40
+ *
41
+ * @example
42
+ * // Lazy initialization with factory function
43
+ * class ConfigService extends Singleton {
44
+ * private constructor(configPath: string) {
45
+ * super();
46
+ * // Heavy initialization work
47
+ * }
48
+ *
49
+ * public static createLazy(configPath: string) {
50
+ * return this.createFactory(configPath);
51
+ * }
52
+ * }
53
+ */
54
+ declare abstract class Singleton {
55
+ private static readonly instances;
56
+ private static readonly initializationLocks;
57
+ private static readonly activeInstances;
58
+ /**
59
+ * Protected constructor to prevent direct instantiation
60
+ */
61
+ protected constructor();
62
+ /**
63
+ * Gets the singleton instance of the class
64
+ * Creates a new instance if one doesn't exist
65
+ * @throws Error if concurrent initialization is detected
66
+ */
67
+ static GetInstance<T extends Singleton>(this: Constructor<T>, ...args: any[]): T;
68
+ /**
69
+ * Checks if an instance already exists
70
+ */
71
+ static HasInstance<T extends Singleton>(this: Constructor<T>): boolean;
72
+ /**
73
+ * Clears the instance (useful for testing or resource cleanup)
74
+ * @returns true if an instance was cleared, false if no instance existed
75
+ */
76
+ static ClearInstance<T extends Singleton>(this: Constructor<T>): boolean;
77
+ /**
78
+ * Creates a factory function for lazy initialization
79
+ */
80
+ static CreateFactory<T extends Singleton>(this: SingletonConstructor<T>, ...args: any[]): () => T;
81
+ /**
82
+ * Clears all singleton instances
83
+ * Primarily used for testing or application shutdown
84
+ */
85
+ static ClearAllInstances(): void;
86
+ /**
87
+ * Gets the count of active singleton instances
88
+ * Useful for debugging and testing
89
+ */
90
+ static GetInstanceCount(): number;
91
+ /**
92
+ * Gets the list of active singleton class names
93
+ * Useful for debugging and testing
94
+ */
95
+ static GetActiveInstanceNames(): string[];
96
+ }
97
+ export default Singleton;
@@ -0,0 +1,142 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * Base class for implementing the singleton pattern with type-safe instance management.
5
+ * Supports constructors with any number of arguments.
6
+ *
7
+ * @example
8
+ * // No constructor arguments
9
+ * class DatabaseExample extends Singleton {
10
+ * private constructor() { super(); }
11
+ * public static connect() {
12
+ * return this.getInstance();
13
+ * }
14
+ * }
15
+ *
16
+ * @example
17
+ * // With constructor arguments
18
+ * class EncryptionServiceExample extends Singleton {
19
+ * private constructor(key: string, algorithm: string) { super(); }
20
+ * public static create(key: string, algorithm: string) {
21
+ * return this.getInstance(key, algorithm);
22
+ * }
23
+ * }
24
+ *
25
+ * @example
26
+ * // Lazy initialization with factory function
27
+ * class ConfigService extends Singleton {
28
+ * private constructor(configPath: string) {
29
+ * super();
30
+ * // Heavy initialization work
31
+ * }
32
+ *
33
+ * public static createLazy(configPath: string) {
34
+ * return this.createFactory(configPath);
35
+ * }
36
+ * }
37
+ */
38
+ class Singleton {
39
+ /**
40
+ * Protected constructor to prevent direct instantiation
41
+ */
42
+ constructor() {
43
+ // Ensure the constructor is only called from getInstance
44
+ const constructorName = this.constructor.name;
45
+ const callerName = new Error().stack?.split("\n")[2]?.trim() || "";
46
+ if (!callerName.includes("getInstance")) {
47
+ console.warn(`${constructorName} is a singleton and should be accessed via ${constructorName}.getInstance()`);
48
+ }
49
+ }
50
+ /**
51
+ * Gets the singleton instance of the class
52
+ * Creates a new instance if one doesn't exist
53
+ * @throws Error if concurrent initialization is detected
54
+ */
55
+ static GetInstance(...args) {
56
+ const classReference = this;
57
+ // Fast path: return existing instance if available
58
+ if (Singleton.instances.has(classReference)) {
59
+ return Singleton.instances.get(classReference);
60
+ }
61
+ // Protection for worker thread environments
62
+ if (Singleton.initializationLocks.get(classReference)) {
63
+ throw new Error(`Concurrent initialization of ${classReference.name} singleton detected`);
64
+ }
65
+ try {
66
+ Singleton.initializationLocks.set(classReference, true);
67
+ // Double-check pattern to handle race conditions
68
+ if (!Singleton.instances.has(classReference)) {
69
+ const instance = new classReference(...args);
70
+ Singleton.instances.set(classReference, instance);
71
+ // Track the instance for debugging
72
+ Singleton.activeInstances.set(classReference.name, classReference);
73
+ }
74
+ return Singleton.instances.get(classReference);
75
+ }
76
+ catch (error) {
77
+ throw new Error(`Failed to initialize ${classReference.name} singleton: ${error instanceof Error ? error.message : String(error)}`);
78
+ }
79
+ finally {
80
+ Singleton.initializationLocks.delete(classReference);
81
+ }
82
+ }
83
+ /**
84
+ * Checks if an instance already exists
85
+ */
86
+ static HasInstance() {
87
+ return Singleton.instances.has(this);
88
+ }
89
+ /**
90
+ * Clears the instance (useful for testing or resource cleanup)
91
+ * @returns true if an instance was cleared, false if no instance existed
92
+ */
93
+ static ClearInstance() {
94
+ const hadInstance = Singleton.instances.has(this);
95
+ Singleton.instances.delete(this);
96
+ Singleton.activeInstances.delete(this.name);
97
+ return hadInstance;
98
+ }
99
+ /**
100
+ * Creates a factory function for lazy initialization
101
+ */
102
+ static CreateFactory(...args) {
103
+ const classReference = this;
104
+ return () => classReference.GetInstance(...args);
105
+ }
106
+ /**
107
+ * Clears all singleton instances
108
+ * Primarily used for testing or application shutdown
109
+ */
110
+ static ClearAllInstances() {
111
+ const instanceCount = Singleton.GetInstanceCount();
112
+ // Clear the WeakMap by removing all references
113
+ Singleton.activeInstances.forEach((constructor) => {
114
+ Singleton.instances.delete(constructor);
115
+ });
116
+ // Clear the tracking map
117
+ Singleton.activeInstances.clear();
118
+ console.log(`Cleared ${instanceCount} singleton instances`);
119
+ }
120
+ /**
121
+ * Gets the count of active singleton instances
122
+ * Useful for debugging and testing
123
+ */
124
+ static GetInstanceCount() {
125
+ return Singleton.activeInstances.size;
126
+ }
127
+ /**
128
+ * Gets the list of active singleton class names
129
+ * Useful for debugging and testing
130
+ */
131
+ static GetActiveInstanceNames() {
132
+ return Array.from(Singleton.activeInstances.keys());
133
+ }
134
+ }
135
+ // Using WeakMap allows garbage collection when no references remain to the class
136
+ Singleton.instances = new WeakMap();
137
+ // Lock to prevent concurrent initialization in worker environments
138
+ Singleton.initializationLocks = new WeakMap();
139
+ // Track active instances for debugging and testing
140
+ Singleton.activeInstances = new Map();
141
+ exports.default = Singleton;
142
+ //# sourceMappingURL=singleton.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"singleton.js","sourceRoot":"","sources":["../src/singleton.ts"],"names":[],"mappings":";;AAsBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAe,SAAS;IAiBtB;;OAEG;IACH;QACE,yDAAyD;QACzD,MAAM,eAAe,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC;QAC9C,MAAM,UAAU,GAAG,IAAI,KAAK,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;QAEnE,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,aAAa,CAAC,EAAE,CAAC;YACxC,OAAO,CAAC,IAAI,CACV,GAAG,eAAe,8CAA8C,eAAe,gBAAgB,CAChG,CAAC;QACJ,CAAC;IACH,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,WAAW,CAEvB,GAAG,IAAW;QAEd,MAAM,cAAc,GAAG,IAAI,CAAC;QAE5B,mDAAmD;QACnD,IAAI,SAAS,CAAC,SAAS,CAAC,GAAG,CAAC,cAAc,CAAC,EAAE,CAAC;YAC5C,OAA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AG,EAAE,CAAC;AAmId,kBAAe,SAAS,CAAC"}
@@ -0,0 +1,42 @@
1
+ /**
2
+ * @description Custom error class for errors that are expected to be thrown
3
+ * and should not trigger retry mechanisms or be reported to error monitoring services.
4
+ *
5
+ * Use this class when you want to throw an error that:
6
+ * 1. Is an expected part of the application flow
7
+ * 2. Should not be retried by retry handlers
8
+ * 3. Should not be reported to error monitoring services like Sentry
9
+ */
10
+ declare class Throwable extends Error {
11
+ /** Flag indicating this is a deliberate error that shouldn't be retried */
12
+ readonly isDeliberate: boolean;
13
+ /** Original error if this Throwable wraps another error */
14
+ readonly originalError?: Error;
15
+ /** Additional context that might be helpful for debugging */
16
+ readonly context?: Record<string, unknown>;
17
+ /**
18
+ * Create a new Throwable error
19
+ * @param message Error message or existing Error object to wrap
20
+ * @param options Configuration options
21
+ */
22
+ constructor(message: unknown, options?: {
23
+ /** Whether to log this error to console (default: true) */
24
+ logError?: boolean;
25
+ /** Additional context data to attach to the error */
26
+ context?: Record<string, unknown>;
27
+ });
28
+ /**
29
+ * Check if an error is a Throwable
30
+ * @param e Any error to check
31
+ * @returns True if the error is a Throwable
32
+ */
33
+ static IsThrowable(e: any): e is Throwable;
34
+ /**
35
+ * Create a Throwable from any error or message
36
+ * @param error Error or message to convert
37
+ * @param context Additional context to attach
38
+ * @returns A new Throwable instance
39
+ */
40
+ static from(error: unknown, context?: Record<string, unknown>): Throwable;
41
+ }
42
+ export default Throwable;
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const guards_1 = __importDefault(require("./guards"));
7
+ const lib_1 = __importDefault(require("./lib"));
8
+ /**
9
+ * @description Custom error class for errors that are expected to be thrown
10
+ * and should not trigger retry mechanisms or be reported to error monitoring services.
11
+ *
12
+ * Use this class when you want to throw an error that:
13
+ * 1. Is an expected part of the application flow
14
+ * 2. Should not be retried by retry handlers
15
+ * 3. Should not be reported to error monitoring services like Sentry
16
+ */
17
+ class Throwable extends Error {
18
+ /**
19
+ * Create a new Throwable error
20
+ * @param message Error message or existing Error object to wrap
21
+ * @param options Configuration options
22
+ */
23
+ constructor(message, options = {}) {
24
+ const { logError = true, context } = options;
25
+ // Format the message appropriately based on type
26
+ const _message = guards_1.default.IsString(message)
27
+ ? message
28
+ : message instanceof Error
29
+ ? message.message
30
+ : JSON.stringify(message);
31
+ super(_message);
32
+ /** Flag indicating this is a deliberate error that shouldn't be retried */
33
+ this.isDeliberate = true;
34
+ this.name = "Throwable";
35
+ this.context = context;
36
+ // Log the error if requested
37
+ if (logError) {
38
+ if (context) {
39
+ lib_1.default.$Log("Throwable: ", _message, "Context:", context);
40
+ }
41
+ else {
42
+ lib_1.default.$Log("Throwable: ", message);
43
+ }
44
+ }
45
+ // Capture stack trace and cause if wrapping an existing error
46
+ if (message instanceof Error) {
47
+ this.stack = message.stack;
48
+ this.cause = message.cause;
49
+ this.originalError = message;
50
+ }
51
+ }
52
+ /**
53
+ * Check if an error is a Throwable
54
+ * @param e Any error to check
55
+ * @returns True if the error is a Throwable
56
+ */
57
+ static IsThrowable(e) {
58
+ return e instanceof Throwable;
59
+ }
60
+ /**
61
+ * Create a Throwable from any error or message
62
+ * @param error Error or message to convert
63
+ * @param context Additional context to attach
64
+ * @returns A new Throwable instance
65
+ */
66
+ static from(error, context) {
67
+ if (error instanceof Throwable) {
68
+ return error;
69
+ }
70
+ return new Throwable(error, { context });
71
+ }
72
+ }
73
+ exports.default = Throwable;
74
+ //# sourceMappingURL=throwable.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"throwable.js","sourceRoot":"","sources":["../src/throwable.ts"],"names":[],"mappings":";;;;;AAAA,sDAA8B;AAC9B,gDAAwB;AAExB;;;;;;;;GAQG;AACH,MAAM,SAAU,SAAQ,KAAK;IAU3B;;;;OAIG;IACH,YACE,OAAgB,EAChB,UAKI,EAAE;QAE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OAAO,IAAI,SAAS,CAAC,KAAK,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC;IAC3C,CAAC;CACF;AAED,kBAAe,SAAS,CAAC"}
@@ -0,0 +1,9 @@
1
+ export type ClassConstructor<T> = new (...args: any[]) => T;
2
+ export type NonNullableType<T> = Exclude<T, null | undefined>;
3
+ type ObjectKeysValues = string | number | string[] | number[] | null;
4
+ export type ObjectKeys<T, U = ObjectKeysValues> = Partial<Record<keyof T, U>>;
5
+ export type KVObj<T> = {
6
+ key: string;
7
+ value: T;
8
+ };
9
+ export {};
package/dist/types.js ADDED
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "topsyde-utils",
3
+ "version": "1.0.0",
4
+ "description": "A bundle of TypeScript utility classes and functions",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "scripts": {
11
+ "build": "bun run tsc",
12
+ "clean": "rimraf dist",
13
+ "prebuild": "bun run clean",
14
+ "prepublishOnly": "bun run build",
15
+ "test": "bun test",
16
+ "lint": "eslint . --ext .ts",
17
+ "format": "prettier --write \"**/*.ts\"",
18
+ "generate-indexes": "./scripts/generate-indexes.sh",
19
+ "prebuild:indexes": "bun run generate-indexes",
20
+ "build:indexes": "bun run build"
21
+ },
22
+ "keywords": [
23
+ "typescript",
24
+ "utilities",
25
+ "helpers",
26
+ "functions",
27
+ "error-handling"
28
+ ],
29
+ "author": "",
30
+ "license": "MIT",
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "git+https://github.com/topsyde/topsyde-utils.git"
34
+ },
35
+ "bugs": {
36
+ "url": "https://github.com/topsyde/topsyde-utils/issues"
37
+ },
38
+ "homepage": "https://github.com/topsyde/topsyde-utils#readme",
39
+ "devDependencies": {
40
+ "@types/jest": "^29.5.0",
41
+ "@types/node": "^18.15.11",
42
+ "@types/bun": "^1.2.4",
43
+ "@typescript-eslint/eslint-plugin": "^5.57.1",
44
+ "@typescript-eslint/parser": "^5.57.1",
45
+ "eslint": "^8.37.0",
46
+ "jest": "^29.5.0",
47
+ "prettier": "^2.8.7",
48
+ "rimraf": "^4.4.1",
49
+ "ts-jest": "^29.1.0",
50
+ "typescript": "^5.0.3"
51
+ },
52
+ "engines": {
53
+ "node": ">=14.0.0"
54
+ }
55
+ }