Skip to content

Commit

Permalink
Merge pull request #44 from inversify/feat/add-reflect-metadata-utils…
Browse files Browse the repository at this point in the history
…-package

Add reflect metadata utils package
  • Loading branch information
notaphplover authored Oct 24, 2024
2 parents 6178c14 + eff2876 commit b75a11c
Show file tree
Hide file tree
Showing 28 changed files with 407 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-carrots-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@inversifyjs/reflect-metadata-utils": minor
---

Added `getReflectMetadata`.
5 changes: 5 additions & 0 deletions .changeset/perfect-geese-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@inversifyjs/reflect-metadata-utils": minor
---

Added `updateReflectMetadata`.
2 changes: 2 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down Expand Up @@ -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: |
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/collect-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"eslint.workingDirectories": [
{ "pattern": "./packages/container/libraries/*" },
{ "pattern": "./packages/foundation/libraries/*" },
{ "pattern": "./packages/foundation/tools/*" }
]
}
7 changes: 6 additions & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 19 additions & 0 deletions packages/foundation/libraries/reflect-metadata-utils/.gitignore
Original file line number Diff line number Diff line change
@@ -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/

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"*.js": [
"prettier --write"
],
"*.ts": [
"prettier --write",
"eslint"
]
}
18 changes: 18 additions & 0 deletions packages/foundation/libraries/reflect-metadata-utils/.npmignore
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import myconfig from '@inversifyjs/foundation-eslint-config';

export default [...myconfig];
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { tsGlobalConfig } from '@inversifyjs/foundation-jest-config';

export default tsGlobalConfig;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { jsGlobalConfig } from '@inversifyjs/foundation-jest-config';

export default jsGlobalConfig;
81 changes: 81 additions & 0 deletions packages/foundation/libraries/reflect-metadata-utils/package.json
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import config from '@inversifyjs/foundation-prettier-config';

export default config;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { getReflectMetadata } from './reflectMetadata/utils/getReflectMetadata';
import { updateReflectMetadata } from './reflectMetadata/utils/updateReflectMetadata';

export { getReflectMetadata, updateReflectMetadata };
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters
export function getReflectMetadata<TMetadata>(
target: object,
metadataKey: unknown,
): TMetadata | undefined {
return Reflect.getMetadata(metadataKey, target) as TMetadata | undefined;
}
Loading

0 comments on commit b75a11c

Please sign in to comment.