topsyde-utils 1.0.7 → 1.0.9
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/README.md +29 -48
- package/dist/application.js +41 -0
- package/dist/application.js.map +1 -0
- package/dist/consts.js +34 -0
- package/dist/consts.js.map +1 -0
- package/dist/enums.js +19 -0
- package/dist/enums.js.map +1 -0
- package/dist/errors.js +46 -0
- package/dist/errors.js.map +1 -0
- package/dist/guards.js +42 -0
- package/dist/guards.js.map +1 -0
- package/dist/index.js +79 -1035
- package/dist/index.js.map +1 -22
- package/dist/initializable.js +309 -0
- package/dist/initializable.js.map +1 -0
- package/dist/lib.js +460 -0
- package/dist/lib.js.map +1 -0
- package/dist/router/index.d.ts +5 -5
- package/dist/router/index.js +30 -0
- package/dist/router/index.js.map +1 -0
- package/dist/router/router.internal.js +47 -0
- package/dist/router/router.internal.js.map +1 -0
- package/dist/router/router.js +43 -0
- package/dist/router/router.js.map +1 -0
- package/dist/router/routes.js +5 -0
- package/dist/router/routes.js.map +1 -0
- package/dist/server/controller.d.ts +1 -1
- package/dist/server/controller.js +55 -0
- package/dist/server/controller.js.map +1 -0
- package/dist/server/index.d.ts +2 -2
- package/dist/server/index.js +26 -0
- package/dist/server/index.js.map +1 -0
- package/dist/singleton.js +142 -0
- package/dist/singleton.js.map +1 -0
- package/dist/throwable.js +70 -0
- package/dist/throwable.js.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +10 -10
@@ -0,0 +1,55 @@
|
|
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 errors_1 = require("../errors");
|
7
|
+
const guards_1 = __importDefault(require("../guards"));
|
8
|
+
const initializable_1 = __importDefault(require("../initializable"));
|
9
|
+
class Controller extends initializable_1.default {
|
10
|
+
constructor(options) {
|
11
|
+
super(options);
|
12
|
+
this.post = new Map();
|
13
|
+
this.get = new Map();
|
14
|
+
this.controllerMap = new Map();
|
15
|
+
this.POST();
|
16
|
+
this.GET();
|
17
|
+
this.setControllerMap();
|
18
|
+
}
|
19
|
+
static Create() {
|
20
|
+
return new this();
|
21
|
+
}
|
22
|
+
static async AsyncCreate() {
|
23
|
+
return new this();
|
24
|
+
}
|
25
|
+
async call(request) {
|
26
|
+
await this.isInitialized();
|
27
|
+
const action = this.resolve(request);
|
28
|
+
return await action(request);
|
29
|
+
}
|
30
|
+
setControllerMap() {
|
31
|
+
this.controllerMap.set("GET", this.get);
|
32
|
+
this.controllerMap.set("POST", this.post);
|
33
|
+
}
|
34
|
+
resolve(req) {
|
35
|
+
const url = new URL(req.url);
|
36
|
+
const endpointArray = url.pathname.split("/");
|
37
|
+
const actionName = endpointArray.slice(2).join("/");
|
38
|
+
if (!guards_1.default.IsString(actionName, true))
|
39
|
+
throw errors_1.ERROR_CODE.INVALID_ACTION;
|
40
|
+
const methodMap = this.controllerMap.get(req.method);
|
41
|
+
if (guards_1.default.IsNil(methodMap)) {
|
42
|
+
throw errors_1.ERROR_CODE.INVALID_METHOD;
|
43
|
+
}
|
44
|
+
return this.resolveAction(methodMap, actionName);
|
45
|
+
}
|
46
|
+
resolveAction(methodMap, actionName) {
|
47
|
+
const action = methodMap.get(actionName);
|
48
|
+
if (!guards_1.default.IsFunction(action, true)) {
|
49
|
+
throw errors_1.ERROR_CODE.NO_ACTION_IN_MAP;
|
50
|
+
}
|
51
|
+
return action;
|
52
|
+
}
|
53
|
+
}
|
54
|
+
exports.default = Controller;
|
55
|
+
//# sourceMappingURL=controller.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"controller.js","sourceRoot":"","sources":["../../src/server/controller.ts"],"names":[],"mappings":";;;;;AAAA,sCAAuC;AACvC,uDAA+B;AAC/B,qEAAuE;AAavE,MAA8B,UAAW,SAAQ,uBAAa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mBAAU,CAAC,cAAc,CAAC;QAExE,MAAM,SAAS,GAAG,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QAErD,IAAI,gBAAM,CAAC,KAAK,CAAC,SAAS,CAAC,EAAE,CAAC;YAC7B,MAAM,mBAAU,CAAC,cAAc,CAAC;QACjC,CAAC;QAED,OAAO,IAAI,CAAC,aAAa,CAAC,SAAS,EAAE,UAAU,CAAC,CAAC;IAClD,CAAC;IAEO,aAAa,CAAC,SAAwB,EAAE,UAAkB;QACjE,MAAM,MAAM,GAAG,SAAS,CAAC,GAAG,CAAC,UAAU,CAAa,CAAC;QAErD,IAAI,CAAC,gBAAM,CAAC,UAAU,CAAC,MAAM,EAAE,IAAI,CAAC,EAAE,CAAC;YACtC,MAAM,mBAAU,CAAC,gBAAgB,CAAC;QACnC,CAAC;QAED,OAAO,MAAM,CAAC;IACf,CAAC;CAuCD;AA/FD,6BA+FC"}
|
package/dist/server/index.d.ts
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
export * from
|
2
|
-
export { default as Controller } from
|
1
|
+
export * from './controller';
|
2
|
+
export { default as Controller } from './controller';
|
@@ -0,0 +1,26 @@
|
|
1
|
+
"use strict";
|
2
|
+
// This file is auto-generated by scripts/generate-indexes.sh
|
3
|
+
// Do not edit this file directly
|
4
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
5
|
+
if (k2 === undefined) k2 = k;
|
6
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
7
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
8
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
9
|
+
}
|
10
|
+
Object.defineProperty(o, k2, desc);
|
11
|
+
}) : (function(o, m, k, k2) {
|
12
|
+
if (k2 === undefined) k2 = k;
|
13
|
+
o[k2] = m[k];
|
14
|
+
}));
|
15
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
16
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
17
|
+
};
|
18
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
19
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
20
|
+
};
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
22
|
+
exports.Controller = void 0;
|
23
|
+
__exportStar(require("./controller"), exports);
|
24
|
+
var controller_1 = require("./controller");
|
25
|
+
Object.defineProperty(exports, "Controller", { enumerable: true, get: function () { return __importDefault(controller_1).default; } });
|
26
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":";AAAA,6DAA6D;AAC7D,iCAAiC;;;;;;;;;;;;;;;;;;;;AAEjC,+CAA6B;AAC7B,2CAAqD;AAA5C,yHAAA,OAAO,OAAc"}
|
@@ -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":";;AAqBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAe,SAAS;IAUvB;;OAEG;IACH;QACC,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;YACzC,OAAO,CAAC,IAAI,CAAC,GAAG,eAAe,8CAA8C,eAAe,gBAAgB,CAAC,CAAC;QAC/G,CAAC;IACF,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,WAAW,CAA4C,GAAG,IAAW;QAClF,MAAM,cAAc,GAAG,IAAI,CAAC;QAE5B,mDAAmD;QACnD,IAAI,SAAS,CAAC,SAAS,CAAC,GAAG,CAAC,cAAc,CAAC,EAAE,CAAC;YAC7C,OAAO,SAAS,CAAC,SAAS,CAAC,GAAG,CAAC,cAAc,CAAM,CAAC;QACrD,CAAC;QAED,4CAA4C;QAC5C,IAAI,SAAS,CAAC,mBAAmB,CAAC,GAAG,CAAC,cAAc,CAAC,EAAE,CAAC;YACvD,MAAM,IAAI,KAAK,CAAC,gCAAgC,cAAc,CAAC,IAAI,qBAAqB,CA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,WAAW,CAAC;IACpB,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,aAAa,CAAqD,GAAG,IAAW;QAC7F,MAAM,cAAc,GAAG,IAAI,CAAC;QAC5B,OAAO,GAAG,EAAE,CAAC,cAAc,CAAC,WAAW,CAAC,GAAG,IAAI,CAAC,CAAC;IAClD,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,iBAAiB;QAC9B,MAAM,aAAa,GAAG,SAAS,CAAC,gBAAgB,EAAE,CAAC;QAEnD,+CAA+C;QAC/C,SAAS,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC,WAAW,EAAE,EAAE;YACjD,SAAS,CAAC,SAAS,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;QACzC,CAAC,CAAC,CAAC;QAEH,yBAAyB;QACzB,SAAS,CAAC,eAAe,CAAC,KAAK,EAAE,CAAC;QAElC,OAAO,CAAC,GAAG,CAAC,WAAW,aAAa,sBAAsB,CAAC,CAAC;IAC7D,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,gBAAgB;QAC7B,OAAO,SAAS,CAAC,eAAe,CAAC,IAAI,CAAC;IACvC,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,sBAAsB;QACnC,OAAO,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,eAAe,CAAC,IAAI,EAAE,CAAC,CAAC;IACrD,CAAC;;AArHD,iFAAiF;AACzD,mBAAS,GAAG,IAAI,OAAO,EAA+B,CAAC;AAE/E,mEAAmE;AAC3C,6BAAmB,GAAG,IAAI,OAAO,EAA6B,CAAC;AAEvF,mDAAmD;AAC3B,yBAAe,GAAkC,IAAI,GAAG,EAAE,CAAC;AAiHpF,kBAAe,SAAS,CAAC"}
|
@@ -0,0 +1,70 @@
|
|
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) ? message : message instanceof Error ? message.message : JSON.stringify(message);
|
27
|
+
super(_message);
|
28
|
+
/** Flag indicating this is a deliberate error that shouldn't be retried */
|
29
|
+
this.isDeliberate = true;
|
30
|
+
this.name = "Throwable";
|
31
|
+
this.context = context;
|
32
|
+
// Log the error if requested
|
33
|
+
if (logError) {
|
34
|
+
if (context) {
|
35
|
+
lib_1.default.$Log("Throwable: ", _message, "Context:", context);
|
36
|
+
}
|
37
|
+
else {
|
38
|
+
lib_1.default.$Log("Throwable: ", message);
|
39
|
+
}
|
40
|
+
}
|
41
|
+
// Capture stack trace and cause if wrapping an existing error
|
42
|
+
if (message instanceof Error) {
|
43
|
+
this.stack = message.stack;
|
44
|
+
this.cause = message.cause;
|
45
|
+
this.originalError = message;
|
46
|
+
}
|
47
|
+
}
|
48
|
+
/**
|
49
|
+
* Check if an error is a Throwable
|
50
|
+
* @param e Any error to check
|
51
|
+
* @returns True if the error is a Throwable
|
52
|
+
*/
|
53
|
+
static IsThrowable(e) {
|
54
|
+
return e instanceof Throwable;
|
55
|
+
}
|
56
|
+
/**
|
57
|
+
* Create a Throwable from any error or message
|
58
|
+
* @param error Error or message to convert
|
59
|
+
* @param context Additional context to attach
|
60
|
+
* @returns A new Throwable instance
|
61
|
+
*/
|
62
|
+
static from(error, context) {
|
63
|
+
if (error instanceof Throwable) {
|
64
|
+
return error;
|
65
|
+
}
|
66
|
+
return new Throwable(error, { context });
|
67
|
+
}
|
68
|
+
}
|
69
|
+
exports.default = Throwable;
|
70
|
+
//# 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;IAU5B;;;;OAIG;IACH,YACC,OAAgB,EAChB,UAKI,EAAE;QAEN,MAAM,EAAE,QAAQ,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;QAE7C,iDAAiD;QACjD,MAAM,QAAQ,GAAG,gBAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,YAAY,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;QAE3H,KAAK,CAAC,QAAQ,CAAC,CAAC;QA5BjB,2EAA2E;QAC3D,iBAAY,GAAY,IAAI,CAAC;QA4B5C,IAAI,CAAC,IAAI,GAAG,WAAW,CAAC;QACxB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QAEvB,6BAA6B;QAC7B,IAAI,QAAQ,EAAE,CAAC;YACd,IAAI,OAAO,EAAE,CAAC;gBACb,aAAG,CAAC,IAAI,CAAC,aAAa,EAAE,QAAQ,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC;YACxD,CAAC;iBAAM,CAAC;gBACP,aAAG,CAAC,IAAI,CAAC,aAAa,EAAE,OAAO,CAAC,CAAC;YAClC,CAAC;QACF,CAAC;QAED,8DAA8D;QAC9D,IAAI,OAAO,YAAY,KAAK,EAAE,CAAC;YAC9B,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;YAC3B,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;YAC3B,IAAI,CAAC,aAAa,GAAG,OAAO,CAAC;QAC9B,CAAC;IACF,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,WAAW,CAAC,CAAM;QACxB,OAAO,CAAC,YAAY,SAAS,CAAC;IAC/B,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,IAAI,CAAC,KAAc,EAAE,OAAiC;QAC5D,IAAI,KAAK,YAAY,SAAS,EAAE,CAAC;YAChC,OAAO,KAAK,CAAC;QACd,CAAC;QAED,OAAO,IAAI,SAAS,CAAC,KAAK,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC;IAC1C,CAAC;CACD;AAED,kBAAe,SAAS,CAAC"}
|
package/dist/types.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "topsyde-utils",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.9",
|
4
4
|
"description": "A bundle of TypeScript utility classes and functions",
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"types": "dist/index.d.ts",
|
@@ -10,16 +10,19 @@
|
|
10
10
|
"scripts": {
|
11
11
|
"clean": "rimraf dist",
|
12
12
|
"format": "prettier --cache --write \"**/*.ts\"",
|
13
|
-
"format:generated": "prettier --cache --write \"src/index.ts\"
|
14
|
-
"lint": "eslint . --ext .ts --cache",
|
13
|
+
"format:generated": "prettier --cache --write \"src/index.ts\"",
|
15
14
|
"generate-indexes": "./scripts/generate-indexes.sh",
|
16
|
-
"
|
17
|
-
"build": "
|
18
|
-
"build:
|
15
|
+
"build:ts": "tsc",
|
16
|
+
"build:types": "tsc --emitDeclarationOnly --declaration --outDir ./dist",
|
17
|
+
"build:prepare": "bun run clean && bun run generate-indexes && bun run format:generated",
|
18
|
+
"build": "bun run build:prepare && bun run build:ts && bun run build:types",
|
19
19
|
"test": "bun test",
|
20
20
|
"prepublishOnly": "echo Skipping prepublishOnly during release",
|
21
21
|
"prepare": "echo Skipping prepare during release",
|
22
|
-
"release": "./scripts/release.sh"
|
22
|
+
"release": "./scripts/release.sh",
|
23
|
+
"release:dry-run": "./scripts/release.sh --dry-run",
|
24
|
+
"release:test": "./scripts/release.sh --test-publish",
|
25
|
+
"version:bump": "npm version --no-git-tag-version"
|
23
26
|
},
|
24
27
|
"keywords": [
|
25
28
|
"typescript",
|
@@ -42,9 +45,6 @@
|
|
42
45
|
"@types/jest": "^29.5.0",
|
43
46
|
"@types/node": "^18.15.11",
|
44
47
|
"@types/bun": "^1.2.4",
|
45
|
-
"@typescript-eslint/eslint-plugin": "^5.57.1",
|
46
|
-
"@typescript-eslint/parser": "^5.57.1",
|
47
|
-
"eslint": "^8.37.0",
|
48
48
|
"jest": "^29.5.0",
|
49
49
|
"prettier": "^2.8.7",
|
50
50
|
"rimraf": "^4.4.1",
|