@medusajs/utils 0.0.1 → 0.0.2-next-20230322094312
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/dist/bundles.d.ts +2 -0
- package/dist/bundles.js +29 -0
- package/dist/bundles.js.map +1 -0
- package/dist/decorators/context-parameter.js +4 -4
- package/dist/decorators/context-parameter.js.map +1 -1
- package/dist/decorators/inject-entity-manager.js +2 -2
- package/dist/decorators/inject-entity-manager.js.map +1 -1
- package/dist/event-bus/index.d.ts +9 -0
- package/dist/event-bus/index.js +52 -0
- package/dist/event-bus/index.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/package.json +4 -3
package/dist/bundles.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.EventBusUtils = exports.DecoratorUtils = void 0;
|
|
27
|
+
exports.DecoratorUtils = __importStar(require("./decorators"));
|
|
28
|
+
exports.EventBusUtils = __importStar(require("./event-bus"));
|
|
29
|
+
//# sourceMappingURL=bundles.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bundles.js","sourceRoot":"","sources":["../src/bundles.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+DAA+C;AAC/C,6DAA6C"}
|
|
@@ -3,13 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.MedusaContext = void 0;
|
|
4
4
|
function MedusaContext() {
|
|
5
5
|
return function (target, propertyKey, parameterIndex) {
|
|
6
|
-
if (!target.
|
|
7
|
-
target.
|
|
6
|
+
if (!target.MedusaContextIndex_) {
|
|
7
|
+
target.MedusaContextIndex_ = {};
|
|
8
8
|
}
|
|
9
|
-
if (propertyKey in target.
|
|
9
|
+
if (propertyKey in target.MedusaContextIndex_) {
|
|
10
10
|
throw new Error(`Only one MedusaContext is allowed on method "${String(propertyKey)}".`);
|
|
11
11
|
}
|
|
12
|
-
target.
|
|
12
|
+
target.MedusaContextIndex_[propertyKey] = parameterIndex;
|
|
13
13
|
};
|
|
14
14
|
}
|
|
15
15
|
exports.MedusaContext = MedusaContext;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context-parameter.js","sourceRoot":"","sources":["../../src/decorators/context-parameter.ts"],"names":[],"mappings":";;;AAAA,SAAgB,aAAa;IAC3B,OAAO,UACL,MAAW,EACX,WAA4B,EAC5B,cAAsB;QAEtB,IAAI,CAAC,MAAM,CAAC,
|
|
1
|
+
{"version":3,"file":"context-parameter.js","sourceRoot":"","sources":["../../src/decorators/context-parameter.ts"],"names":[],"mappings":";;;AAAA,SAAgB,aAAa;IAC3B,OAAO,UACL,MAAW,EACX,WAA4B,EAC5B,cAAsB;QAEtB,IAAI,CAAC,MAAM,CAAC,mBAAmB,EAAE;YAC/B,MAAM,CAAC,mBAAmB,GAAG,EAAE,CAAA;SAChC;QAED,IAAI,WAAW,IAAI,MAAM,CAAC,mBAAmB,EAAE;YAC7C,MAAM,IAAI,KAAK,CACb,gDAAgD,MAAM,CAAC,WAAW,CAAC,IAAI,CACxE,CAAA;SACF;QAED,MAAM,CAAC,mBAAmB,CAAC,WAAW,CAAC,GAAG,cAAc,CAAA;IAC1D,CAAC,CAAA;AACH,CAAC;AAlBD,sCAkBC"}
|
|
@@ -3,11 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.InjectEntityManager = void 0;
|
|
4
4
|
function InjectEntityManager(managerProperty = "manager_") {
|
|
5
5
|
return function (target, propertyKey, descriptor) {
|
|
6
|
-
if (!target.
|
|
6
|
+
if (!target.MedusaContextIndex_) {
|
|
7
7
|
throw new Error(`To apply @InjectEntityManager you have to flag a parameter using @MedusaContext`);
|
|
8
8
|
}
|
|
9
9
|
const originalMethod = descriptor.value;
|
|
10
|
-
const argIndex = target.
|
|
10
|
+
const argIndex = target.MedusaContextIndex_[propertyKey];
|
|
11
11
|
descriptor.value = async function (...args) {
|
|
12
12
|
const context = args[argIndex] ?? {};
|
|
13
13
|
if (context?.transactionManager) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inject-entity-manager.js","sourceRoot":"","sources":["../../src/decorators/inject-entity-manager.ts"],"names":[],"mappings":";;;AAEA,SAAgB,mBAAmB,CACjC,eAAe,GAAG,UAAU;IAE5B,OAAO,UACL,MAAW,EACX,WAA4B,EAC5B,UAAe;QAEf,IAAI,CAAC,MAAM,CAAC,
|
|
1
|
+
{"version":3,"file":"inject-entity-manager.js","sourceRoot":"","sources":["../../src/decorators/inject-entity-manager.ts"],"names":[],"mappings":";;;AAEA,SAAgB,mBAAmB,CACjC,eAAe,GAAG,UAAU;IAE5B,OAAO,UACL,MAAW,EACX,WAA4B,EAC5B,UAAe;QAEf,IAAI,CAAC,MAAM,CAAC,mBAAmB,EAAE;YAC/B,MAAM,IAAI,KAAK,CACb,iFAAiF,CAClF,CAAA;SACF;QAED,MAAM,cAAc,GAAG,UAAU,CAAC,KAAK,CAAA;QAEvC,MAAM,QAAQ,GAAG,MAAM,CAAC,mBAAmB,CAAC,WAAW,CAAC,CAAA;QACxD,UAAU,CAAC,KAAK,GAAG,KAAK,WAAW,GAAG,IAAW;YAC/C,MAAM,OAAO,GAAkB,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAA;YAEnD,IAAI,OAAO,EAAE,kBAAkB,EAAE;gBAC/B,OAAO,MAAM,cAAc,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAA;aAC9C;YAED,OAAO,MAAM,IAAI,CAAC,eAAe,CAAC,CAAC,WAAW,CAC5C,KAAK,EAAE,kBAAkB,EAAE,EAAE;gBAC3B,IAAI,CAAC,QAAQ,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAA;gBACrC,IAAI,CAAC,QAAQ,CAAC,CAAC,kBAAkB,GAAG,kBAAkB,CAAA;gBAEtD,OAAO,MAAM,cAAc,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAA;YAC/C,CAAC,CACF,CAAA;QACH,CAAC,CAAA;IACH,CAAC,CAAA;AACH,CAAC;AAlCD,kDAkCC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { EventBusTypes } from "@medusajs/types";
|
|
2
|
+
export declare abstract class AbstractEventBusModuleService implements EventBusTypes.IEventBusModuleService {
|
|
3
|
+
protected eventToSubscribersMap_: Map<string | symbol, EventBusTypes.SubscriberDescriptor[]>;
|
|
4
|
+
get eventToSubscribersMap(): Map<string | symbol, EventBusTypes.SubscriberDescriptor[]>;
|
|
5
|
+
abstract emit<T>(eventName: string, data: T, options: Record<string, unknown>): Promise<void>;
|
|
6
|
+
abstract emit<T>(data: EventBusTypes.EmitData<T>[]): Promise<void>;
|
|
7
|
+
subscribe(eventName: string | symbol, subscriber: EventBusTypes.Subscriber, context?: EventBusTypes.SubscriberContext): this;
|
|
8
|
+
unsubscribe(eventName: string | symbol, subscriber: EventBusTypes.Subscriber, context: EventBusTypes.SubscriberContext): this;
|
|
9
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AbstractEventBusModuleService = void 0;
|
|
4
|
+
const ulid_1 = require("ulid");
|
|
5
|
+
class AbstractEventBusModuleService {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.eventToSubscribersMap_ = new Map();
|
|
8
|
+
}
|
|
9
|
+
get eventToSubscribersMap() {
|
|
10
|
+
return this.eventToSubscribersMap_;
|
|
11
|
+
}
|
|
12
|
+
subscribe(eventName, subscriber, context) {
|
|
13
|
+
if (typeof subscriber !== `function`) {
|
|
14
|
+
throw new Error("Subscriber must be a function");
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* If context is provided, we use the subscriberId from it
|
|
18
|
+
* otherwise we generate a random using a ulid
|
|
19
|
+
*/
|
|
20
|
+
const randId = (0, ulid_1.ulid)();
|
|
21
|
+
const event = eventName.toString();
|
|
22
|
+
const subscriberId = context?.subscriberId ?? `${event}-${randId}`;
|
|
23
|
+
const newSubscriberDescriptor = { subscriber, id: subscriberId };
|
|
24
|
+
const existingSubscribers = this.eventToSubscribersMap_.get(event) ?? [];
|
|
25
|
+
const subscriberAlreadyExists = existingSubscribers.find((sub) => sub.id === subscriberId);
|
|
26
|
+
if (subscriberAlreadyExists) {
|
|
27
|
+
throw Error(`Subscriber with id ${subscriberId} already exists`);
|
|
28
|
+
}
|
|
29
|
+
this.eventToSubscribersMap_.set(event, [
|
|
30
|
+
...existingSubscribers,
|
|
31
|
+
newSubscriberDescriptor,
|
|
32
|
+
]);
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
unsubscribe(eventName, subscriber, context) {
|
|
36
|
+
if (typeof subscriber !== `function`) {
|
|
37
|
+
throw new Error("Subscriber must be a function");
|
|
38
|
+
}
|
|
39
|
+
const existingSubscribers = this.eventToSubscribersMap_.get(eventName);
|
|
40
|
+
if (existingSubscribers?.length) {
|
|
41
|
+
const subIndex = existingSubscribers?.findIndex((sub) => sub.id === context?.subscriberId);
|
|
42
|
+
if (subIndex !== -1) {
|
|
43
|
+
this.eventToSubscribersMap_
|
|
44
|
+
.get(eventName)
|
|
45
|
+
?.splice(subIndex, 1);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.AbstractEventBusModuleService = AbstractEventBusModuleService;
|
|
52
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/event-bus/index.ts"],"names":[],"mappings":";;;AACA,+BAA2B;AAE3B,MAAsB,6BAA6B;IAAnD;QAGY,2BAAsB,GAG5B,IAAI,GAAG,EAAE,CAAA;IA+Ef,CAAC;IA7EC,IAAW,qBAAqB;QAI9B,OAAO,IAAI,CAAC,sBAAsB,CAAA;IACpC,CAAC;IASM,SAAS,CACd,SAA0B,EAC1B,UAAoC,EACpC,OAAyC;QAEzC,IAAI,OAAO,UAAU,KAAK,UAAU,EAAE;YACpC,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAA;SACjD;QACD;;;WAGG;QAEH,MAAM,MAAM,GAAG,IAAA,WAAI,GAAE,CAAA;QACrB,MAAM,KAAK,GAAG,SAAS,CAAC,QAAQ,EAAE,CAAA;QAElC,MAAM,YAAY,GAAG,OAAO,EAAE,YAAY,IAAI,GAAG,KAAK,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@@ -14,5 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./bundles"), exports);
|
|
17
18
|
__exportStar(require("./decorators"), exports);
|
|
19
|
+
__exportStar(require("./event-bus"), exports);
|
|
18
20
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+CAA4B"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4CAAyB;AACzB,+CAA4B;AAC5B,8CAA2B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@medusajs/utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2-next-20230322094312",
|
|
4
4
|
"description": "Medusa utilities functions shared by Medusa core and Modules",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"repository": {
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"author": "Medusa",
|
|
18
18
|
"license": "MIT",
|
|
19
19
|
"devDependencies": {
|
|
20
|
-
"@medusajs/types": "
|
|
20
|
+
"@medusajs/types": "0.0.2-next-20230322094312",
|
|
21
21
|
"cross-env": "^5.2.1",
|
|
22
22
|
"jest": "^25.5.4",
|
|
23
23
|
"ts-jest": "^25.5.1",
|
|
@@ -25,11 +25,12 @@
|
|
|
25
25
|
"typescript": "^4.4.4"
|
|
26
26
|
},
|
|
27
27
|
"peerDependencies": {
|
|
28
|
-
"@medusajs/types": "
|
|
28
|
+
"@medusajs/types": "0.0.2-next-20230322094312"
|
|
29
29
|
},
|
|
30
30
|
"scripts": {
|
|
31
31
|
"prepare": "cross-env NODE_ENV=production yarn run build",
|
|
32
32
|
"build": "tsc --build",
|
|
33
|
+
"watch": "tsc --build --watch",
|
|
33
34
|
"test": "jest --passWithNoTests src"
|
|
34
35
|
}
|
|
35
36
|
}
|