From 5acb1366a466233ec7926f0bb90c0dabc3661081 Mon Sep 17 00:00:00 2001 From: notaphplover Date: Thu, 24 Oct 2024 16:37:44 +0200 Subject: [PATCH 1/4] chore: update workspace with foundation libraries --- .github/workflows/build.yaml | 2 ++ .github/workflows/collect-coverage.yaml | 1 + .vscode/settings.json | 1 + codecov.yml | 7 ++++++- pnpm-workspace.yaml | 1 + 5 files changed, 11 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 3befeeec..0b94dc00 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -45,6 +45,7 @@ jobs: with: path: | ./packages/container/libraries/* + ./packages/foundation/libraries/* ./packages/foundation/tools/* key: ts-build-${{ steps.get-git-commit-hash.outputs.gitCommitHash }} restore-keys: | @@ -77,6 +78,7 @@ jobs: with: path: | ./packages/container/libraries/* + ./packages/foundation/libraries/* ./packages/foundation/tools/* key: ts-build-${{ steps.get-git-commit-hash.outputs.gitCommitHash }} restore-keys: | diff --git a/.github/workflows/collect-coverage.yaml b/.github/workflows/collect-coverage.yaml index 6ba4fd7e..7eb95981 100644 --- a/.github/workflows/collect-coverage.yaml +++ b/.github/workflows/collect-coverage.yaml @@ -51,6 +51,7 @@ jobs: with: path: | ./packages/container/libraries/* + ./packages/foundation/libraries/* ./packages/foundation/tools/* key: ts-build-${{ steps.get-git-commit-hash.outputs.gitCommitHash }} restore-keys: | diff --git a/.vscode/settings.json b/.vscode/settings.json index 07e642d0..a5b8557c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,7 @@ { "eslint.workingDirectories": [ { "pattern": "./packages/container/libraries/*" }, + { "pattern": "./packages/foundation/libraries/*" }, { "pattern": "./packages/foundation/tools/*" } ] } diff --git a/codecov.yml b/codecov.yml index 4a123568..567e5542 100644 --- a/codecov.yml +++ b/codecov.yml @@ -5,8 +5,13 @@ coverage: '@inversifyjs/common': flags: - '@inversifyjs/common' - + '@inversifyjs/reflect-metadata-utils': + flags: + - '@inversifyjs/reflect-metadata-utils' flags: '@inversifyjs/common': paths: - packages/container/libraries/common/ + '@inversifyjs/reflect-metadata-utils': + paths: + - packages/foundation/libraries/reflect-metadata-utils diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 623cd0b6..9fdac710 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -1,3 +1,4 @@ packages: - "packages/container/libraries/*" + - "packages/foundation/libraries/*" - "packages/foundation/tools/*" From aa80b196e7ee2a02b51ac0875e716f1a7bc79a92 Mon Sep 17 00:00:00 2001 From: notaphplover Date: Thu, 24 Oct 2024 16:38:53 +0200 Subject: [PATCH 2/4] chore: upte turbo config with better inputs --- turbo.json | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/turbo.json b/turbo.json index a7222249..dceee68d 100644 --- a/turbo.json +++ b/turbo.json @@ -4,50 +4,47 @@ "build": { "dependsOn": ["^build"], "inputs": ["src/**/*.{cts,mts,ts}"], - "outputs": [ - "dist/**", - "lib/**" - ] + "outputs": ["dist/**", "lib/**"] }, "format": { - "inputs": ["src/**/*.ts"], - "outputs": ["src/**/*.ts"] + "inputs": ["src/**/*.{cts,mts,ts}"], + "outputs": ["src/**/*.{cts,mts,ts}"] }, "lint": { "dependsOn": ["^build"], - "inputs": ["src/**/*.ts"] + "inputs": ["src/**/*.{cts,mts,ts}"] }, "test": { - "inputs": ["src/**/*.ts"], + "inputs": ["src/**/*.{cts,mts,ts}"], "outputs": [] }, "test:integration:js": { "dependsOn": ["build"], - "inputs": ["dist/**", "lib/**"], + "inputs": ["{dist,lib}/**/*.{cjs,mjs,js}"], "outputs": [] }, "test:js": { "dependsOn": ["build"], - "inputs": ["dist/**", "lib/**"], + "inputs": ["{dist,lib}/**/*.{cjs,mjs,js}"], "outputs": [] }, "test:js:coverage": { "dependsOn": ["build"], - "inputs": ["dist/**", "lib/**"], + "inputs": ["{dist,lib}/**/*.{cjs,mjs,js}"], "outputs": ["coverage/**"] }, "test:mutation": { "dependsOn": ["^build"], - "inputs": ["src/**/*.ts"], + "inputs": ["src/**/*.{cts,mts,ts}"], "outputs": ["reports/**"] }, "test:uncommitted": { - "inputs": ["src/**/*.ts"], + "inputs": ["src/**/*.{cts,mts,ts}"], "outputs": [] }, "test:unit:js": { "dependsOn": ["build"], - "inputs": ["dist/**", "lib/**"], + "inputs": ["{dist,lib}/**/*.{cjs,mjs,js}"], "outputs": [] } }, From 7b9e0702cb624b0c844001ab4d85db3953600563 Mon Sep 17 00:00:00 2001 From: notaphplover Date: Thu, 24 Oct 2024 16:41:18 +0200 Subject: [PATCH 3/4] feat(reflect-metadata-utils): add reflect metadata utils package --- .../reflect-metadata-utils/.gitignore | 19 +++ .../reflect-metadata-utils/.lintstagedrc.json | 9 ++ .../reflect-metadata-utils/.npmignore | 18 +++ .../reflect-metadata-utils/README.md | 6 + .../reflect-metadata-utils/eslint.config.mjs | 3 + .../reflect-metadata-utils/jest.config.mjs | 3 + .../jest.config.stryker.mjs | 9 ++ .../reflect-metadata-utils/jest.js.config.mjs | 3 + .../reflect-metadata-utils/package.json | 81 ++++++++++++ .../prettier.config.mjs | 3 + .../reflect-metadata-utils/src/index.ts | 4 + .../utils/getReflectMetadata.spec.ts | 45 +++++++ .../utils/getReflectMetadata.ts | 7 + .../utils/updateReflectMetadata.spec.ts | 122 ++++++++++++++++++ .../utils/updateReflectMetadata.ts | 15 +++ .../src/types/Reflect.d.ts | 1 + .../reflect-metadata-utils/stryker.config.mjs | 3 + .../reflect-metadata-utils/tsconfig.cjs.json | 10 ++ .../reflect-metadata-utils/tsconfig.esm.json | 10 ++ .../reflect-metadata-utils/tsconfig.json | 4 + 20 files changed, 375 insertions(+) create mode 100644 packages/foundation/libraries/reflect-metadata-utils/.gitignore create mode 100644 packages/foundation/libraries/reflect-metadata-utils/.lintstagedrc.json create mode 100644 packages/foundation/libraries/reflect-metadata-utils/.npmignore create mode 100644 packages/foundation/libraries/reflect-metadata-utils/README.md create mode 100644 packages/foundation/libraries/reflect-metadata-utils/eslint.config.mjs create mode 100644 packages/foundation/libraries/reflect-metadata-utils/jest.config.mjs create mode 100644 packages/foundation/libraries/reflect-metadata-utils/jest.config.stryker.mjs create mode 100644 packages/foundation/libraries/reflect-metadata-utils/jest.js.config.mjs create mode 100644 packages/foundation/libraries/reflect-metadata-utils/package.json create mode 100644 packages/foundation/libraries/reflect-metadata-utils/prettier.config.mjs create mode 100644 packages/foundation/libraries/reflect-metadata-utils/src/index.ts create mode 100644 packages/foundation/libraries/reflect-metadata-utils/src/reflectMetadata/utils/getReflectMetadata.spec.ts create mode 100644 packages/foundation/libraries/reflect-metadata-utils/src/reflectMetadata/utils/getReflectMetadata.ts create mode 100644 packages/foundation/libraries/reflect-metadata-utils/src/reflectMetadata/utils/updateReflectMetadata.spec.ts create mode 100644 packages/foundation/libraries/reflect-metadata-utils/src/reflectMetadata/utils/updateReflectMetadata.ts create mode 100644 packages/foundation/libraries/reflect-metadata-utils/src/types/Reflect.d.ts create mode 100644 packages/foundation/libraries/reflect-metadata-utils/stryker.config.mjs create mode 100644 packages/foundation/libraries/reflect-metadata-utils/tsconfig.cjs.json create mode 100644 packages/foundation/libraries/reflect-metadata-utils/tsconfig.esm.json create mode 100644 packages/foundation/libraries/reflect-metadata-utils/tsconfig.json diff --git a/packages/foundation/libraries/reflect-metadata-utils/.gitignore b/packages/foundation/libraries/reflect-metadata-utils/.gitignore new file mode 100644 index 00000000..288feba2 --- /dev/null +++ b/packages/foundation/libraries/reflect-metadata-utils/.gitignore @@ -0,0 +1,19 @@ +# Typescript compiled files +/lib/** + +/tsconfig.tsbuildinfo +/tsconfig.cjs.tsbuildinfo +/tsconfig.esm.tsbuildinfo + +# Test coverage report +/coverage + +# Test mutation report +/reports + +# node modules +/node_modules/ + +# Turborepo files +.turbo/ + diff --git a/packages/foundation/libraries/reflect-metadata-utils/.lintstagedrc.json b/packages/foundation/libraries/reflect-metadata-utils/.lintstagedrc.json new file mode 100644 index 00000000..0061dfc2 --- /dev/null +++ b/packages/foundation/libraries/reflect-metadata-utils/.lintstagedrc.json @@ -0,0 +1,9 @@ +{ + "*.js": [ + "prettier --write" + ], + "*.ts": [ + "prettier --write", + "eslint" + ] +} diff --git a/packages/foundation/libraries/reflect-metadata-utils/.npmignore b/packages/foundation/libraries/reflect-metadata-utils/.npmignore new file mode 100644 index 00000000..8f883537 --- /dev/null +++ b/packages/foundation/libraries/reflect-metadata-utils/.npmignore @@ -0,0 +1,18 @@ +**/*.spec.js +**/*.spec.js.map +**/*.ts +!lib/**/*.d.ts +lib/**/*.spec.d.ts + +.lintstagedrc.json +eslint.config.mjs +jest.config.mjs +jest.config.stryker.mjs +jest.js.config.mjs +prettier.config.mjs +stryker.config.mjs +tsconfig.json +tsconfig.cjs.json +tsconfig.esm.json +tsconfig.cjs.tsbuildinfo +tsconfig.esm.tsbuildinfo diff --git a/packages/foundation/libraries/reflect-metadata-utils/README.md b/packages/foundation/libraries/reflect-metadata-utils/README.md new file mode 100644 index 00000000..44a90b52 --- /dev/null +++ b/packages/foundation/libraries/reflect-metadata-utils/README.md @@ -0,0 +1,6 @@ +[![Test coverage](https://codecov.io/gh/inversify/monorepo/branch/main/graph/badge.svg?flag=%40inversifyjs%2Freflect-metadata-utils)](https://codecov.io/gh/inversify/monorepo/branch/main/graph/badge.svg?flag=%40inversifyjs%2Freflect-metadata-utils) +[![npm version](https://img.shields.io/github/package-json/v/inversify/monorepo?filename=packages%2Ffoundation%2Flibraries%2Freflect-metadata-utils%2Fpackage.json&style=plastic)](https://www.npmjs.com/package/@inversifyjs/reflect-metadata-utils) + +# @inversifyjs/reflect-metadata-utils + +Inversify reflect metadata utils package. diff --git a/packages/foundation/libraries/reflect-metadata-utils/eslint.config.mjs b/packages/foundation/libraries/reflect-metadata-utils/eslint.config.mjs new file mode 100644 index 00000000..42002283 --- /dev/null +++ b/packages/foundation/libraries/reflect-metadata-utils/eslint.config.mjs @@ -0,0 +1,3 @@ +import myconfig from '@inversifyjs/foundation-eslint-config'; + +export default [...myconfig]; diff --git a/packages/foundation/libraries/reflect-metadata-utils/jest.config.mjs b/packages/foundation/libraries/reflect-metadata-utils/jest.config.mjs new file mode 100644 index 00000000..7425b9fa --- /dev/null +++ b/packages/foundation/libraries/reflect-metadata-utils/jest.config.mjs @@ -0,0 +1,3 @@ +import { tsGlobalConfig } from '@inversifyjs/foundation-jest-config'; + +export default tsGlobalConfig; diff --git a/packages/foundation/libraries/reflect-metadata-utils/jest.config.stryker.mjs b/packages/foundation/libraries/reflect-metadata-utils/jest.config.stryker.mjs new file mode 100644 index 00000000..ec0166c4 --- /dev/null +++ b/packages/foundation/libraries/reflect-metadata-utils/jest.config.stryker.mjs @@ -0,0 +1,9 @@ +import { getJestTsProjectConfig } from '@inversifyjs/foundation-jest-config'; + +const tsGlobalConfig = getJestTsProjectConfig( + 'All', + ['/node_modules', '.int.spec.ts'], + '.spec.ts', +); + +export default tsGlobalConfig; diff --git a/packages/foundation/libraries/reflect-metadata-utils/jest.js.config.mjs b/packages/foundation/libraries/reflect-metadata-utils/jest.js.config.mjs new file mode 100644 index 00000000..773dbb6f --- /dev/null +++ b/packages/foundation/libraries/reflect-metadata-utils/jest.js.config.mjs @@ -0,0 +1,3 @@ +import { jsGlobalConfig } from '@inversifyjs/foundation-jest-config'; + +export default jsGlobalConfig; diff --git a/packages/foundation/libraries/reflect-metadata-utils/package.json b/packages/foundation/libraries/reflect-metadata-utils/package.json new file mode 100644 index 00000000..b8e2cc0d --- /dev/null +++ b/packages/foundation/libraries/reflect-metadata-utils/package.json @@ -0,0 +1,81 @@ +{ + "name": "@inversifyjs/reflect-metadata-utils", + "version": "0.1.0", + "description": "Reflect metadata utils", + "main": "lib/cjs/index.js", + "module": "lib/esm/index.js", + "exports": { + ".": { + "import": "./lib/esm/index.js", + "require": "./lib/cjs/index.js" + } + }, + "repository": { + "type": "git", + "url": "git+https://github.com/inversify/monorepo.git" + }, + "keywords": [ + "dependency injection", + "dependency inversion", + "di", + "inversion of control container", + "ioc", + "javascript", + "node", + "reflect-metadata", + "typescript" + ], + "author": "Roberto Pintos López", + "license": "MIT", + "bugs": { + "url": "https://github.com/inversify/monorepo/issues" + }, + "devDependencies": { + "@eslint/js": "9.13.0", + "@jest/globals": "29.7.0", + "@stryker-mutator/core": "8.6.0", + "@stryker-mutator/jest-runner": "8.6.0", + "@stryker-mutator/typescript-checker": "8.6.0", + "@types/node": "20.17.0", + "@typescript-eslint/eslint-plugin": "8.11.0", + "@typescript-eslint/parser": "8.11.0", + "jest": "29.7.0", + "prettier": "3.3.3", + "rimraf": "6.0.1", + "ts-jest": "29.2.5", + "ts-node": "10.9.2", + "typescript": "5.6.3" + }, + "devEngines": { + "node": "^20.18.0", + "pnpm": "^9.12.1" + }, + "homepage": "https://inversify.io", + "os": [ + "darwin", + "linux" + ], + "peerDependencies": { + "reflect-metadata": "0.2.2" + }, + "publishConfig": { + "access": "public" + }, + "scripts": { + "build": "pnpm run build:cjs && pnpm run build:esm", + "build:cjs": "tsc --build tsconfig.cjs.json && pnpm exec foundation-ts-package-cjs ./lib/cjs", + "build:esm": "tsc --build tsconfig.esm.json && pnpm exec foundation-ts-package-esm ./lib/esm", + "build:clean": "rimraf lib", + "format": "prettier --write ./src/**/*.ts", + "lint": "eslint ./src", + "prebuild": "pnpm run build:clean", + "test": "jest --config=jest.config.mjs --runInBand", + "test:integration:js": "pnpm run test:js --selectProjects Integration", + "test:js": "jest --config=jest.js.config.mjs --runInBand", + "test:js:coverage": "pnpm run test:unit:js --coverage", + "test:mutation": "stryker run", + "test:uncommitted": "pnpm run test --changedSince=HEAD", + "test:unit:js": "pnpm run test:js --selectProjects Unit" + }, + "sideEffects": false +} diff --git a/packages/foundation/libraries/reflect-metadata-utils/prettier.config.mjs b/packages/foundation/libraries/reflect-metadata-utils/prettier.config.mjs new file mode 100644 index 00000000..70361db5 --- /dev/null +++ b/packages/foundation/libraries/reflect-metadata-utils/prettier.config.mjs @@ -0,0 +1,3 @@ +import config from '@inversifyjs/foundation-prettier-config'; + +export default config; diff --git a/packages/foundation/libraries/reflect-metadata-utils/src/index.ts b/packages/foundation/libraries/reflect-metadata-utils/src/index.ts new file mode 100644 index 00000000..ffa4a301 --- /dev/null +++ b/packages/foundation/libraries/reflect-metadata-utils/src/index.ts @@ -0,0 +1,4 @@ +import { getReflectMetadata } from './reflectMetadata/utils/getReflectMetadata'; +import { updateReflectMetadata } from './reflectMetadata/utils/updateReflectMetadata'; + +export { getReflectMetadata, updateReflectMetadata }; diff --git a/packages/foundation/libraries/reflect-metadata-utils/src/reflectMetadata/utils/getReflectMetadata.spec.ts b/packages/foundation/libraries/reflect-metadata-utils/src/reflectMetadata/utils/getReflectMetadata.spec.ts new file mode 100644 index 00000000..36332f18 --- /dev/null +++ b/packages/foundation/libraries/reflect-metadata-utils/src/reflectMetadata/utils/getReflectMetadata.spec.ts @@ -0,0 +1,45 @@ +import { beforeAll, describe, expect, it } from '@jest/globals'; + +import 'reflect-metadata'; + +import { getReflectMetadata } from './getReflectMetadata'; + +describe(getReflectMetadata.name, () => { + describe('when called, and no metadata is registered', () => { + let result: unknown; + + beforeAll(() => { + result = getReflectMetadata(class {}, 'sample-key'); + }); + + it('should return undefined', () => { + expect(result).toBeUndefined(); + }); + }); + + describe('when called, and metadata is registered', () => { + let result: unknown; + + let metadataFixture: unknown; + + beforeAll(() => { + metadataFixture = 'sample-metadata'; + + // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type + const targetFixture: Function = class {}; + const metadataKeyFixture: unknown = 'sample-key'; + + Reflect.defineMetadata( + metadataKeyFixture, + metadataFixture, + targetFixture, + ); + + result = getReflectMetadata(targetFixture, metadataKeyFixture); + }); + + it('should return metadata', () => { + expect(result).toBe(metadataFixture); + }); + }); +}); diff --git a/packages/foundation/libraries/reflect-metadata-utils/src/reflectMetadata/utils/getReflectMetadata.ts b/packages/foundation/libraries/reflect-metadata-utils/src/reflectMetadata/utils/getReflectMetadata.ts new file mode 100644 index 00000000..1951690f --- /dev/null +++ b/packages/foundation/libraries/reflect-metadata-utils/src/reflectMetadata/utils/getReflectMetadata.ts @@ -0,0 +1,7 @@ +// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters +export function getReflectMetadata( + target: object, + metadataKey: unknown, +): TMetadata | undefined { + return Reflect.getMetadata(metadataKey, target) as TMetadata | undefined; +} diff --git a/packages/foundation/libraries/reflect-metadata-utils/src/reflectMetadata/utils/updateReflectMetadata.spec.ts b/packages/foundation/libraries/reflect-metadata-utils/src/reflectMetadata/utils/updateReflectMetadata.spec.ts new file mode 100644 index 00000000..a5ce54b8 --- /dev/null +++ b/packages/foundation/libraries/reflect-metadata-utils/src/reflectMetadata/utils/updateReflectMetadata.spec.ts @@ -0,0 +1,122 @@ +import { afterAll, beforeAll, describe, expect, it, jest } from '@jest/globals'; + +import 'reflect-metadata'; + +jest.mock('./getReflectMetadata'); + +import { getReflectMetadata } from './getReflectMetadata'; +import { updateReflectMetadata } from './updateReflectMetadata'; + +describe(updateReflectMetadata.name, () => { + describe('when called, and getReflectMetadata returns undefined', () => { + // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type + let targetFixture: Function; + let metadataKeyFixture: unknown; + let defaultValueFixture: unknown; + let callbackMock: jest.Mock<(value: unknown) => unknown>; + let reflectMetadata: unknown; + + beforeAll(() => { + targetFixture = class {}; + metadataKeyFixture = 'sample-key'; + defaultValueFixture = 'default-value'; + callbackMock = jest + .fn<(value: unknown) => unknown>() + .mockImplementationOnce((value: unknown) => value); + + ( + getReflectMetadata as jest.Mock + ).mockReturnValueOnce(undefined); + + updateReflectMetadata( + targetFixture, + metadataKeyFixture, + defaultValueFixture, + callbackMock, + ); + + reflectMetadata = Reflect.getOwnMetadata( + metadataKeyFixture, + targetFixture, + ); + }); + + afterAll(() => { + jest.clearAllMocks(); + }); + + it('should call getReflectMetadata()', () => { + expect(getReflectMetadata).toHaveBeenCalledTimes(1); + expect(getReflectMetadata).toHaveBeenCalledWith( + targetFixture, + metadataKeyFixture, + ); + }); + + it('should call callback()', () => { + expect(callbackMock).toHaveBeenCalledTimes(1); + expect(callbackMock).toHaveBeenCalledWith(defaultValueFixture); + }); + + it('should define metadata', () => { + expect(reflectMetadata).toBe(defaultValueFixture); + }); + }); + + describe('when called, and getReflectMetadata returns metadata', () => { + // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type + let targetFixture: Function; + let metadataFixture: unknown; + let metadataKeyFixture: unknown; + let defaultValueFixture: unknown; + let callbackMock: jest.Mock<(value: unknown) => unknown>; + let reflectMetadata: unknown; + + beforeAll(() => { + targetFixture = class {}; + metadataFixture = 'metadata'; + metadataKeyFixture = 'sample-key'; + defaultValueFixture = 'default-value'; + callbackMock = jest + .fn<(value: unknown) => unknown>() + .mockImplementationOnce((value: unknown) => value); + + ( + getReflectMetadata as jest.Mock + ).mockReturnValueOnce(metadataFixture); + + updateReflectMetadata( + targetFixture, + metadataKeyFixture, + defaultValueFixture, + callbackMock, + ); + + reflectMetadata = Reflect.getOwnMetadata( + metadataKeyFixture, + targetFixture, + ); + }); + + afterAll(() => { + jest.clearAllMocks(); + }); + + it('should call getReflectMetadata()', () => { + expect(getReflectMetadata).toHaveBeenCalledTimes(1); + expect(getReflectMetadata).toHaveBeenCalledWith( + targetFixture, + metadataKeyFixture, + ); + }); + + it('should call callback()', () => { + expect(callbackMock).toHaveBeenCalledTimes(1); + expect(callbackMock).toHaveBeenCalledWith(metadataFixture); + }); + + it('should define metadata', () => { + expect(reflectMetadata).toBe(metadataFixture); + }); + }); +}); diff --git a/packages/foundation/libraries/reflect-metadata-utils/src/reflectMetadata/utils/updateReflectMetadata.ts b/packages/foundation/libraries/reflect-metadata-utils/src/reflectMetadata/utils/updateReflectMetadata.ts new file mode 100644 index 00000000..cf2dbeea --- /dev/null +++ b/packages/foundation/libraries/reflect-metadata-utils/src/reflectMetadata/utils/updateReflectMetadata.ts @@ -0,0 +1,15 @@ +import { getReflectMetadata } from './getReflectMetadata'; + +export function updateReflectMetadata( + target: object, + metadataKey: unknown, + defaultValue: TMetadata, + callback: (metadata: TMetadata) => TMetadata, +): void { + const metadata: TMetadata = + getReflectMetadata(target, metadataKey) ?? defaultValue; + + const updatedMetadata: TMetadata = callback(metadata); + + Reflect.defineMetadata(metadataKey, updatedMetadata, target); +} diff --git a/packages/foundation/libraries/reflect-metadata-utils/src/types/Reflect.d.ts b/packages/foundation/libraries/reflect-metadata-utils/src/types/Reflect.d.ts new file mode 100644 index 00000000..d2c9bc6e --- /dev/null +++ b/packages/foundation/libraries/reflect-metadata-utils/src/types/Reflect.d.ts @@ -0,0 +1 @@ +import 'reflect-metadata'; diff --git a/packages/foundation/libraries/reflect-metadata-utils/stryker.config.mjs b/packages/foundation/libraries/reflect-metadata-utils/stryker.config.mjs new file mode 100644 index 00000000..c2513393 --- /dev/null +++ b/packages/foundation/libraries/reflect-metadata-utils/stryker.config.mjs @@ -0,0 +1,3 @@ +import config from '@inversifyjs/foundation-stryker-config'; + +export default config; diff --git a/packages/foundation/libraries/reflect-metadata-utils/tsconfig.cjs.json b/packages/foundation/libraries/reflect-metadata-utils/tsconfig.cjs.json new file mode 100644 index 00000000..72630fb8 --- /dev/null +++ b/packages/foundation/libraries/reflect-metadata-utils/tsconfig.cjs.json @@ -0,0 +1,10 @@ +{ + "$schema": "http://json.schemastore.org/tsconfig", + "extends": "@inversifyjs/foundation-typescript-config/tsconfig.base.cjs.json", + "compilerOptions": { + "outDir": "./lib/cjs", + "rootDir": "./src", + "tsBuildInfoFile": "tsconfig.cjs.tsbuildinfo" + }, + "include": ["src"] +} diff --git a/packages/foundation/libraries/reflect-metadata-utils/tsconfig.esm.json b/packages/foundation/libraries/reflect-metadata-utils/tsconfig.esm.json new file mode 100644 index 00000000..e597177d --- /dev/null +++ b/packages/foundation/libraries/reflect-metadata-utils/tsconfig.esm.json @@ -0,0 +1,10 @@ +{ + "$schema": "http://json.schemastore.org/tsconfig", + "extends": "@inversifyjs/foundation-typescript-config/tsconfig.base.esm.json", + "compilerOptions": { + "outDir": "./lib/esm", + "rootDir": "./src", + "tsBuildInfoFile": "tsconfig.esm.tsbuildinfo" + }, + "include": ["src"] +} diff --git a/packages/foundation/libraries/reflect-metadata-utils/tsconfig.json b/packages/foundation/libraries/reflect-metadata-utils/tsconfig.json new file mode 100644 index 00000000..04a10c90 --- /dev/null +++ b/packages/foundation/libraries/reflect-metadata-utils/tsconfig.json @@ -0,0 +1,4 @@ +{ + "$schema": "http://json.schemastore.org/tsconfig", + "extends": "./tsconfig.esm.json" +} From eff2876f95e3f31922e9e16cdc68cd0758d93893 Mon Sep 17 00:00:00 2001 From: notaphplover Date: Thu, 24 Oct 2024 16:41:36 +0200 Subject: [PATCH 4/4] docs: add changesets --- .changeset/eight-carrots-hope.md | 5 +++++ .changeset/perfect-geese-happen.md | 5 +++++ 2 files changed, 10 insertions(+) create mode 100644 .changeset/eight-carrots-hope.md create mode 100644 .changeset/perfect-geese-happen.md diff --git a/.changeset/eight-carrots-hope.md b/.changeset/eight-carrots-hope.md new file mode 100644 index 00000000..065cb1c4 --- /dev/null +++ b/.changeset/eight-carrots-hope.md @@ -0,0 +1,5 @@ +--- +"@inversifyjs/reflect-metadata-utils": minor +--- + +Added `getReflectMetadata`. diff --git a/.changeset/perfect-geese-happen.md b/.changeset/perfect-geese-happen.md new file mode 100644 index 00000000..648d470d --- /dev/null +++ b/.changeset/perfect-geese-happen.md @@ -0,0 +1,5 @@ +--- +"@inversifyjs/reflect-metadata-utils": minor +--- + +Added `updateReflectMetadata`.