chore: Cleanup utils package (#9238)

Fixes: FRMW-2712

Old PR: https://github.com/medusajs/medusa/pull/9234 Closed because of incorrect branch naming

Co-authored-by: Adrien de Peretti <25098370+adrien2p@users.noreply.github.com>
This commit is contained in:
Harminder VirkandAdrien de Peretti authored and GitHub committed 2024-09-23 10:37:58 +00:00
1 parent 79e3578932
commit 97e003ef4f
17 files changed
+138 -75

No files matched your search

+15 -4
View File
@@ -1,13 +1,24 @@
module.exports = { module.exports = {
transform: { transform: {
"^.+\\.[jt]s?$": [ "^.+\\.[jt]s$": [
"ts-jest", "@swc/jest",
{ {
tsconfig: "tsconfig.spec.json", jsc: {
isolatedModules: true, parser: {
syntax: "typescript",
decorators: true,
},
transform: {
useDefineForClassFields: false,
legacyDecorator: true,
decoratorMetadata: true,
},
target: "ES2021",
},
}, },
], ],
}, },
testPathIgnorePatterns: [`dist/`, `node_modules/`],
testEnvironment: `node`, testEnvironment: `node`,
moduleFileExtensions: [`js`, `ts`], moduleFileExtensions: [`js`, `ts`],
} }
+16 -11
View File
@@ -3,32 +3,37 @@
"version": "1.11.9", "version": "1.11.9",
"description": "Medusa utilities functions shared by Medusa core and Modules", "description": "Medusa utilities functions shared by Medusa core and Modules",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "exports": {
".": "./dist/index.js"
},
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/medusajs/medusa", "url": "https://github.com/medusajs/medusa",
"directory": "packages/utils" "directory": "packages/utils"
}, },
"engines": { "engines": {
"node": ">=16" "node": ">=20.0.0"
}, },
"publishConfig": { "publishConfig": {
"access": "public" "access": "public"
}, },
"files": [ "files": [
"dist" "dist",
"!dist/**/__tests__",
"!dist/**/__mocks__",
"!dist/**/__fixtures__"
], ],
"author": "Medusa", "author": "Medusa",
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"@types/express": "^4.17.17", "@swc/jest": "^0.2.36",
"@types/express": "^4.17.21",
"cross-env": "^5.2.1", "cross-env": "^5.2.1",
"expect-type": "^0.19.0", "expect-type": "^0.20.0",
"express": "^4.18.2", "express": "^4.21.0",
"jest": "^29.7.0", "jest": "^29.7.0",
"rimraf": "^5.0.1", "rimraf": "^5.0.1",
"ts-jest": "^29.1.1", "typescript": "^5.6.2"
"typescript": "^5.1.6"
}, },
"dependencies": { "dependencies": {
"@medusajs/types": "^1.11.16", "@medusajs/types": "^1.11.16",
@@ -40,15 +45,15 @@
"dotenv": "^16.4.5", "dotenv": "^16.4.5",
"dotenv-expand": "^11.0.6", "dotenv-expand": "^11.0.6",
"jsonwebtoken": "^9.0.2", "jsonwebtoken": "^9.0.2",
"pg": "^8.12.0", "pg": "^8.13.0",
"pg-connection-string": "^2.6.4", "pg-connection-string": "^2.7.0",
"pluralize": "^8.0.0", "pluralize": "^8.0.0",
"ulid": "^2.3.0" "ulid": "^2.3.0"
}, },
"scripts": { "scripts": {
"build": "rimraf dist && tsc --build", "build": "rimraf dist && tsc --build",
"watch": "tsc --build --watch", "watch": "tsc --build --watch",
"test": "jest --silent --bail --maxWorkers=50% --forceExit --testPathIgnorePatterns='/integration-tests/' -- src/**/__tests__/**/*.ts", "test": "jest --silent=false --bail --maxWorkers=50% --forceExit --testPathIgnorePatterns='/integration-tests/' -- src/**/__tests__/**/*.ts",
"test:integration": "jest --silent --bail --runInBand --forceExit -- src/**/integration-tests/__tests__/**/*.ts" "test:integration": "jest --silent --bail --runInBand --forceExit -- src/**/integration-tests/__tests__/**/*.ts"
} }
} }
@@ -1,4 +1,4 @@
import { partitionArray } from "../../../dist" import { partitionArray } from "../../index"
describe("partitionArray", function () { describe("partitionArray", function () {
it("should split array according to predicate", function () { it("should split array according to predicate", function () {
@@ -2,7 +2,6 @@
import { objectFromStringPath } from "./object-from-string-path" import { objectFromStringPath } from "./object-from-string-path"
type Order = { type Order = {
[key: string]: "ASC" | "DESC" | Order [key: string]: "ASC" | "DESC" | Order
} }
@@ -42,7 +41,7 @@ function buildRelationsOrSelect(collection: string[]): Selects | Relations {
* } * }
* @param orderBy * @param orderBy
*/ */
export function buildOrder<T>(orderBy: { [k: string]: "ASC" | "DESC" }): Order { export function buildOrder(orderBy: { [k: string]: "ASC" | "DESC" }): Order {
const output: Order = {} const output: Order = {}
const orderKeys = Object.keys(orderBy) const orderKeys = Object.keys(orderBy)
@@ -3,5 +3,5 @@ export function toCamelCase(str: string): string {
? str ? str
: str : str
.toLowerCase() .toLowerCase()
.replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.toUpperCase()) .replace(/[^a-zA-Z0-9]+(.)/g, (_, chr) => chr.toUpperCase())
} }
@@ -180,8 +180,7 @@ describe("mikroOrmRepository", () => {
it("should successfully create a flat entity", async () => { it("should successfully create a flat entity", async () => {
const entity1 = { id: "1", title: "en1", amount: 100 } const entity1 = { id: "1", title: "en1", amount: 100 }
const { entities: resp, performedActions } = const { performedActions } = await manager1().upsertWithReplace([entity1])
await manager1().upsertWithReplace([entity1])
expect(performedActions).toEqual({ expect(performedActions).toEqual({
created: { created: {
@@ -5,7 +5,7 @@ describe("Array property", () => {
test("should create an array property type", () => { test("should create an array property type", () => {
const property = new ArrayProperty() const property = new ArrayProperty()
expectTypeOf(property["$dataType"]).toEqualTypeOf<[]>() expectTypeOf(property["$dataType"]).toEqualTypeOf<string[]>()
expect(property.parse("codes")).toEqual({ expect(property.parse("codes")).toEqual({
fieldName: "codes", fieldName: "codes",
dataType: { dataType: {
@@ -11,10 +11,6 @@ import { HasOne } from "../../relations/has-one"
import { ManyToMany as DmlManyToMany } from "../../relations/many-to-many" import { ManyToMany as DmlManyToMany } from "../../relations/many-to-many"
import { parseEntityName } from "../entity-builder/parse-entity-name" import { parseEntityName } from "../entity-builder/parse-entity-name"
type Context = {
MANY_TO_MANY_TRACKED_RELATIONS: Record<string, boolean>
}
function defineRelationships( function defineRelationships(
modelName: string, modelName: string,
relationship: RelationshipMetadata, relationship: RelationshipMetadata,
@@ -8,7 +8,7 @@ describe("MessageAggregator", function () {
it("should group messages by any given group of keys", function () { it("should group messages by any given group of keys", function () {
const aggregator = new MessageAggregator() const aggregator = new MessageAggregator()
aggregator.save({ aggregator.save({
eventName: "ProductVariant.created", name: "ProductVariant.created",
metadata: { metadata: {
source: "Product", source: "Product",
action: "created", action: "created",
@@ -18,7 +18,7 @@ describe("MessageAggregator", function () {
data: { id: 999 }, data: { id: 999 },
}) })
aggregator.save({ aggregator.save({
eventName: "Product.created", name: "Product.created",
metadata: { metadata: {
source: "Product", source: "Product",
action: "created", action: "created",
@@ -28,7 +28,7 @@ describe("MessageAggregator", function () {
data: { id: 1 }, data: { id: 1 },
}) })
aggregator.save({ aggregator.save({
eventName: "ProductVariant.created", name: "ProductVariant.created",
metadata: { metadata: {
source: "Product", source: "Product",
action: "created", action: "created",
@@ -38,7 +38,7 @@ describe("MessageAggregator", function () {
data: { id: 222 }, data: { id: 222 },
}) })
aggregator.save({ aggregator.save({
eventName: "ProductType.detached", name: "ProductType.detached",
metadata: { metadata: {
source: "Product", source: "Product",
action: "detached", action: "detached",
@@ -48,7 +48,7 @@ describe("MessageAggregator", function () {
data: { id: 333 }, data: { id: 333 },
}) })
aggregator.save({ aggregator.save({
eventName: "ProductVariant.updated", name: "ProductVariant.updated",
metadata: { metadata: {
source: "Product", source: "Product",
action: "updated", action: "updated",
@@ -59,7 +59,7 @@ describe("MessageAggregator", function () {
}) })
const format = { const format = {
groupBy: ["eventName", "metadata.object", "metadata.action"], groupBy: ["name", "metadata.object", "metadata.action"],
sortBy: { sortBy: {
"metadata.object": ["ProductType", "ProductVariant", "Product"], "metadata.object": ["ProductType", "ProductVariant", "Product"],
"data.id": "asc", "data.id": "asc",
@@ -74,7 +74,7 @@ describe("MessageAggregator", function () {
expect(allGroups[0]).toEqual([ expect(allGroups[0]).toEqual([
{ {
eventName: "ProductType.detached", name: "ProductType.detached",
metadata: { metadata: {
source: "Product", source: "Product",
action: "detached", action: "detached",
@@ -87,7 +87,7 @@ describe("MessageAggregator", function () {
expect(allGroups[1]).toEqual([ expect(allGroups[1]).toEqual([
{ {
eventName: "ProductVariant.updated", name: "ProductVariant.updated",
metadata: { metadata: {
source: "Product", source: "Product",
action: "updated", action: "updated",
@@ -100,7 +100,7 @@ describe("MessageAggregator", function () {
expect(allGroups[2]).toEqual([ expect(allGroups[2]).toEqual([
{ {
eventName: "ProductVariant.created", name: "ProductVariant.created",
metadata: { metadata: {
source: "Product", source: "Product",
action: "created", action: "created",
@@ -110,7 +110,7 @@ describe("MessageAggregator", function () {
data: { id: 222 }, data: { id: 222 },
}, },
{ {
eventName: "ProductVariant.created", name: "ProductVariant.created",
metadata: { metadata: {
source: "Product", source: "Product",
action: "created", action: "created",
@@ -123,7 +123,7 @@ describe("MessageAggregator", function () {
expect(allGroups[3]).toEqual([ expect(allGroups[3]).toEqual([
{ {
eventName: "Product.created", name: "Product.created",
metadata: { metadata: {
source: "Product", source: "Product",
action: "created", action: "created",
@@ -70,7 +70,7 @@ describe.skip("Revert migrations", () => {
const results = await migrations.revert() const results = await migrations.revert()
const orm = await MikroORM.init(config) const orm = await MikroORM.init(config)
const usersTableExists = await orm.em.getKnex().schema.hasTable("user") const usersTableExists = await orm.em["getKnex"]().schema.hasTable("user")
await orm.close() await orm.close()
expect(results).toHaveLength(1) expect(results).toHaveLength(1)
@@ -171,7 +171,7 @@ describe.skip("Revert migrations", () => {
expect(migrations.revert()).rejects.toThrow(/.*Migration.*/) expect(migrations.revert()).rejects.toThrow(/.*Migration.*/)
const orm = await MikroORM.init(config) const orm = await MikroORM.init(config)
const usersTableExists = await orm.em.getKnex().schema.hasTable("user") const usersTableExists = await orm.em["getKnex"]().schema.hasTable("user")
await orm.close() await orm.close()
expect(usersTableExists).toEqual(true) expect(usersTableExists).toEqual(true)
@@ -70,7 +70,7 @@ describe.skip("Run migrations", () => {
const results = await migrations.run() const results = await migrations.run()
const orm = await MikroORM.init(config) const orm = await MikroORM.init(config)
const usersTableExists = await orm.em.getKnex().schema.hasTable("user") const usersTableExists = await orm.em["getKnex"]().schema.hasTable("user")
await orm.close() await orm.close()
expect(results).toHaveLength(1) expect(results).toHaveLength(1)
@@ -168,7 +168,7 @@ describe.skip("Run migrations", () => {
expect(migrations.run()).rejects.toThrow(/.*Migration.*/) expect(migrations.run()).rejects.toThrow(/.*Migration.*/)
const orm = await MikroORM.init(config) const orm = await MikroORM.init(config)
const usersTableExists = await orm.em.getKnex().schema.hasTable("user") const usersTableExists = await orm.em["getKnex"]().schema.hasTable("user")
await orm.close() await orm.close()
@@ -67,6 +67,7 @@ describe("Internal Module Service Factory", () => {
const instance = new compositeIMedusaInternalService(containerMock) const instance = new compositeIMedusaInternalService(containerMock)
// @ts-expect-error
const err = await instance.retrieve().catch((e) => e) const err = await instance.retrieve().catch((e) => e)
expect(err.message).toBe("compositeModel - id, name must be defined") expect(err.message).toBe("compositeModel - id, name must be defined")
}) })
@@ -525,7 +525,7 @@ export function MedusaService<
buildMethodNamesFromModel(name, config as TModels[keyof TModels]), buildMethodNamesFromModel(name, config as TModels[keyof TModels]),
]) ])
for (let [modelName, model, modelMethods] of modelsMethods) { for (let [modelName, , modelMethods] of modelsMethods) {
Object.entries(modelMethods).forEach(([method, methodName]) => { Object.entries(modelMethods).forEach(([method, methodName]) => {
buildAndAssignMethodImpl( buildAndAssignMethodImpl(
AbstractModuleService_.prototype, AbstractModuleService_.prototype,
@@ -34,10 +34,6 @@ type UnionToArray<T, A extends unknown[] = []> = IsUnion<T> extends true
? UnionToArray<Exclude<T, PopUnion<T>>, [PopUnion<T>, ...A]> ? UnionToArray<Exclude<T, PopUnion<T>>, [PopUnion<T>, ...A]>
: [T, ...A] : [T, ...A]
type Reverse<T extends unknown[], R extends unknown[] = []> = ReturnType<
T extends [infer F, ...infer L] ? () => Reverse<L, [F, ...R]> : () => R
>
/** /**
* End of utils * End of utils
*/ */
+3 -5
View File
@@ -5,6 +5,7 @@
"outDir": "./dist", "outDir": "./dist",
"esModuleInterop": true, "esModuleInterop": true,
"declaration": true, "declaration": true,
"noUnusedLocals": true,
"declarationMap": true, "declarationMap": true,
"module": "commonjs", "module": "commonjs",
"moduleResolution": "node", "moduleResolution": "node",
@@ -12,19 +13,16 @@
"experimentalDecorators": true, "experimentalDecorators": true,
"sourceMap": true, "sourceMap": true,
"noImplicitReturns": true, "noImplicitReturns": true,
"forceConsistentCasingInFileNames": true,
"strictNullChecks": true, "strictNullChecks": true,
"strictFunctionTypes": true, "strictFunctionTypes": true,
"noImplicitThis": true, "noImplicitThis": true,
"allowJs": true, "allowJs": true,
"skipLibCheck": true, "skipLibCheck": true
"downlevelIteration": true
}, },
"include": ["src"], "include": ["src"],
"exclude": [ "exclude": [
"dist", "dist",
"./src/**/__tests__",
"./src/**/__mocks__",
"./src/**/__fixtures__",
"node_modules" "node_modules"
] ]
} }
-8
View File
@@ -1,8 +0,0 @@
{
"extends": "./tsconfig.json",
"include": ["src"],
"exclude": ["node_modules", "dist"],
"compilerOptions": {
"sourceMap": true
}
}
+82 -16
View File
@@ -6744,22 +6744,22 @@ __metadata:
"@mikro-orm/core": 5.9.7 "@mikro-orm/core": 5.9.7
"@mikro-orm/migrations": 5.9.7 "@mikro-orm/migrations": 5.9.7
"@mikro-orm/postgresql": 5.9.7 "@mikro-orm/postgresql": 5.9.7
"@types/express": ^4.17.17 "@swc/jest": ^0.2.36
"@types/express": ^4.17.21
awilix: ^8.0.1 awilix: ^8.0.1
bignumber.js: ^9.1.2 bignumber.js: ^9.1.2
cross-env: ^5.2.1 cross-env: ^5.2.1
dotenv: ^16.4.5 dotenv: ^16.4.5
dotenv-expand: ^11.0.6 dotenv-expand: ^11.0.6
expect-type: ^0.19.0 expect-type: ^0.20.0
express: ^4.18.2 express: ^4.21.0
jest: ^29.7.0 jest: ^29.7.0
jsonwebtoken: ^9.0.2 jsonwebtoken: ^9.0.2
pg: ^8.12.0 pg: ^8.13.0
pg-connection-string: ^2.6.4 pg-connection-string: ^2.7.0
pluralize: ^8.0.0 pluralize: ^8.0.0
rimraf: ^5.0.1 rimraf: ^5.0.1
ts-jest: ^29.1.1 typescript: ^5.6.2
typescript: ^5.1.6
ulid: ^2.3.0 ulid: ^2.3.0
languageName: unknown languageName: unknown
linkType: soft linkType: soft
@@ -13643,7 +13643,7 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"@types/express@npm:*, @types/express@npm:^4.17.17, @types/express@npm:^4.7.0": "@types/express@npm:*, @types/express@npm:^4.17.17, @types/express@npm:^4.17.21, @types/express@npm:^4.7.0":
version: 4.17.21 version: 4.17.21
resolution: "@types/express@npm:4.17.21" resolution: "@types/express@npm:4.17.21"
dependencies: dependencies:
@@ -20003,13 +20003,6 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"expect-type@npm:^0.19.0":
version: 0.19.0
resolution: "expect-type@npm:0.19.0"
checksum: 0a7305021c3e37bf024ce01f0a141de109435ac4225457c35fbd649a0d8cdc19b9078185fd9aaa9ebb136a88c5065d27a60883ab0f961ddc281c11851ed18097
languageName: node
linkType: hard
"expect-type@npm:^0.20.0": "expect-type@npm:^0.20.0":
version: 0.20.0 version: 0.20.0
resolution: "expect-type@npm:0.20.0" resolution: "expect-type@npm:0.20.0"
@@ -20169,6 +20162,45 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"express@npm:^4.21.0":
version: 4.21.0
resolution: "express@npm:4.21.0"
dependencies:
accepts: ~1.3.8
array-flatten: 1.1.1
body-parser: 1.20.3
content-disposition: 0.5.4
content-type: ~1.0.4
cookie: 0.6.0
cookie-signature: 1.0.6
debug: 2.6.9
depd: 2.0.0
encodeurl: ~2.0.0
escape-html: ~1.0.3
etag: ~1.8.1
finalhandler: 1.3.1
fresh: 0.5.2
http-errors: 2.0.0
merge-descriptors: 1.0.3
methods: ~1.1.2
on-finished: 2.4.1
parseurl: ~1.3.3
path-to-regexp: 0.1.10
proxy-addr: ~2.0.7
qs: 6.13.0
range-parser: ~1.2.1
safe-buffer: 5.2.1
send: 0.19.0
serve-static: 1.16.2
setprototypeof: 1.2.0
statuses: 2.0.1
type-is: ~1.6.18
utils-merge: 1.0.1
vary: ~1.1.2
checksum: 4cf7ca328f3fdeb720f30ccb2ea7708bfa7d345f9cc460b64a82bf1b2c91e5b5852ba15a9a11b2a165d6089acf83457fc477dc904d59cd71ed34c7a91762c6cc
languageName: node
linkType: hard
"extend@npm:^3.0.0, extend@npm:~3.0.2": "extend@npm:^3.0.0, extend@npm:~3.0.2":
version: 3.0.2 version: 3.0.2
resolution: "extend@npm:3.0.2" resolution: "extend@npm:3.0.2"
@@ -20526,6 +20558,21 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"finalhandler@npm:1.3.1":
version: 1.3.1
resolution: "finalhandler@npm:1.3.1"
dependencies:
debug: 2.6.9
encodeurl: ~2.0.0
escape-html: ~1.0.3
on-finished: 2.4.1
parseurl: ~1.3.3
statuses: 2.0.1
unpipe: ~1.0.0
checksum: d38035831865a49b5610206a3a9a9aae4e8523cbbcd01175d0480ffbf1278c47f11d89be3ca7f617ae6d94f29cf797546a4619cd84dd109009ef33f12f69019f
languageName: node
linkType: hard
"finalhandler@npm:~1.1.2": "finalhandler@npm:~1.1.2":
version: 1.1.2 version: 1.1.2
resolution: "finalhandler@npm:1.1.2" resolution: "finalhandler@npm:1.1.2"
@@ -27057,13 +27104,20 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"pg-connection-string@npm:^2.6.2, pg-connection-string@npm:^2.6.4": "pg-connection-string@npm:^2.6.2":
version: 2.6.4 version: 2.6.4
resolution: "pg-connection-string@npm:2.6.4" resolution: "pg-connection-string@npm:2.6.4"
checksum: 0d0b617df0fc6507bf6a94bdcd56c7a305788a1402d69bff9773350947c8f525d6d8136128065370749a3325e99658ae40fbdcce620fb8e60126181f0591a6a6 checksum: 0d0b617df0fc6507bf6a94bdcd56c7a305788a1402d69bff9773350947c8f525d6d8136128065370749a3325e99658ae40fbdcce620fb8e60126181f0591a6a6
languageName: node languageName: node
linkType: hard linkType: hard
"pg-connection-string@npm:^2.7.0":
version: 2.7.0
resolution: "pg-connection-string@npm:2.7.0"
checksum: 50a1496a1c858f9495d78a2c7a66d93ef3602e718aff2953bb5738f3ea616d7f727f32fc20513c9bed127650cd14c1ddc7b458396f4000e689d4b64c65c5c51e
languageName: node
linkType: hard
"pg-god@npm:^1.0.12": "pg-god@npm:^1.0.12":
version: 1.0.12 version: 1.0.12
resolution: "pg-god@npm:1.0.12" resolution: "pg-god@npm:1.0.12"
@@ -30278,6 +30332,18 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"serve-static@npm:1.16.2":
version: 1.16.2
resolution: "serve-static@npm:1.16.2"
dependencies:
encodeurl: ~2.0.0
escape-html: ~1.0.3
parseurl: ~1.3.3
send: 0.19.0
checksum: 528fff6f5e12d0c5a391229ad893910709bc51b5705962b09404a1d813857578149b8815f35d3ee5752f44cd378d0f31669d4b1d7e2d11f41e08283d5134bd1f
languageName: node
linkType: hard
"set-blocking@npm:^2.0.0": "set-blocking@npm:^2.0.0":
version: 2.0.0 version: 2.0.0
resolution: "set-blocking@npm:2.0.0" resolution: "set-blocking@npm:2.0.0"