decorator-dependency-injection 1.0.2 → 1.0.4
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 +124 -26
- package/eslint.config.js +73 -0
- package/index.d.ts +146 -0
- package/index.js +103 -107
- package/package.json +46 -7
- package/src/Container.js +184 -0
- package/src/proxy.js +42 -0
- package/.github/workflows/release.yml +0 -129
- package/babel.config.json +0 -6
- package/test/injection.test.js +0 -152
- package/test/mock.test.js +0 -77
- package/test/proxy.test.js +0 -73
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
name: Create Release
|
|
2
|
-
|
|
3
|
-
on:
|
|
4
|
-
push:
|
|
5
|
-
branches: [ "main" ]
|
|
6
|
-
pull_request:
|
|
7
|
-
branches: [ "main" ]
|
|
8
|
-
|
|
9
|
-
jobs:
|
|
10
|
-
build:
|
|
11
|
-
runs-on: ubuntu-latest
|
|
12
|
-
strategy:
|
|
13
|
-
matrix:
|
|
14
|
-
node-version: [22.x, 23.x]
|
|
15
|
-
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
|
|
16
|
-
steps:
|
|
17
|
-
- uses: actions/checkout@v4
|
|
18
|
-
- name: Use Node.js ${{ matrix.node-version }}
|
|
19
|
-
uses: actions/setup-node@v4
|
|
20
|
-
with:
|
|
21
|
-
node-version: ${{ matrix.node-version }}
|
|
22
|
-
cache: 'npm'
|
|
23
|
-
- run: npm ci
|
|
24
|
-
- run: npm run build --if-present
|
|
25
|
-
- run: npm test
|
|
26
|
-
|
|
27
|
-
release:
|
|
28
|
-
needs: build
|
|
29
|
-
runs-on: ubuntu-latest
|
|
30
|
-
permissions:
|
|
31
|
-
contents: write
|
|
32
|
-
steps:
|
|
33
|
-
- name: Checkout Repository
|
|
34
|
-
uses: actions/checkout@v4
|
|
35
|
-
|
|
36
|
-
- name: Install Node.js
|
|
37
|
-
uses: actions/setup-node@v4
|
|
38
|
-
with:
|
|
39
|
-
node-version: '22'
|
|
40
|
-
|
|
41
|
-
- name: Get package.json version
|
|
42
|
-
id: package_version
|
|
43
|
-
run: echo "VERSION=$(node -p "require('./package.json').version")" >> $GITHUB_ENV
|
|
44
|
-
|
|
45
|
-
- name: Check if tag exists
|
|
46
|
-
id: check_tag
|
|
47
|
-
run: |
|
|
48
|
-
TAG_EXISTS=$(git ls-remote --tags origin "v${VERSION}" | wc -l)
|
|
49
|
-
echo "TAG_EXISTS=${TAG_EXISTS}" >> $GITHUB_ENV
|
|
50
|
-
|
|
51
|
-
- name: Get commit messages
|
|
52
|
-
id: commit_messages
|
|
53
|
-
run: |
|
|
54
|
-
COMMITS=$(git log --pretty=format:"%h - %s" $(git describe --tags --abbrev=0 @^)..@ | while read -r hash msg; do echo "[${hash}](https://github.com/${{ github.repository }}/commit/${hash}) - ${msg}"; done)
|
|
55
|
-
echo "COMMITS=${COMMITS}" >> $GITHUB_ENV
|
|
56
|
-
|
|
57
|
-
- name: Create Tag and Release
|
|
58
|
-
uses: actions/github-script@v7
|
|
59
|
-
with:
|
|
60
|
-
script: |
|
|
61
|
-
const version = process.env.VERSION
|
|
62
|
-
const tag = `v${version}`
|
|
63
|
-
const commits = process.env.COMMITS
|
|
64
|
-
const { data: tags } = await github.rest.repos.listTags({
|
|
65
|
-
owner: context.repo.owner,
|
|
66
|
-
repo: context.repo.repo,
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
if (!tags.find(t => t.name === tag)) {
|
|
70
|
-
await github.rest.git.createRef({
|
|
71
|
-
owner: context.repo.owner,
|
|
72
|
-
repo: context.repo.repo,
|
|
73
|
-
ref: `refs/tags/${tag}`,
|
|
74
|
-
sha: context.sha,
|
|
75
|
-
})
|
|
76
|
-
|
|
77
|
-
await github.rest.repos.createRelease({
|
|
78
|
-
owner: context.repo.owner,
|
|
79
|
-
repo: context.repo.repo,
|
|
80
|
-
tag_name: tag,
|
|
81
|
-
name: `Release ${tag}`,
|
|
82
|
-
body: `Automated release of version ${tag}\n\nCommits:\n${commits}`,
|
|
83
|
-
draft: false,
|
|
84
|
-
prerelease: false,
|
|
85
|
-
})
|
|
86
|
-
core.notice(`Release ${tag} created.`)
|
|
87
|
-
} else {
|
|
88
|
-
core.notice(`Release ${tag} already exists. No new release created.`)
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
publish:
|
|
92
|
-
needs: release
|
|
93
|
-
runs-on: ubuntu-latest
|
|
94
|
-
steps:
|
|
95
|
-
- name: Checkout Repository
|
|
96
|
-
uses: actions/checkout@v4
|
|
97
|
-
|
|
98
|
-
- name: Install Node.js
|
|
99
|
-
uses: actions/setup-node@v4
|
|
100
|
-
with:
|
|
101
|
-
node-version: '22'
|
|
102
|
-
registry-url: 'https://registry.npmjs.org/'
|
|
103
|
-
|
|
104
|
-
- name: Get package.json version
|
|
105
|
-
id: package_version
|
|
106
|
-
run: echo "VERSION=$(node -p "require('./package.json').version")" >> $GITHUB_ENV
|
|
107
|
-
|
|
108
|
-
- name: Check if version exists on npm
|
|
109
|
-
id: check_npm_version
|
|
110
|
-
run: |
|
|
111
|
-
if npm show decorator-dependency-injection@${{ env.VERSION }} > /dev/null 2>&1; then
|
|
112
|
-
echo "VERSION_EXISTS=true" >> $GITHUB_ENV
|
|
113
|
-
else
|
|
114
|
-
echo "VERSION_EXISTS=false" >> $GITHUB_ENV
|
|
115
|
-
fi
|
|
116
|
-
|
|
117
|
-
- name: Publish to npm
|
|
118
|
-
if: env.VERSION_EXISTS == 'false'
|
|
119
|
-
run: |
|
|
120
|
-
npm publish --access public
|
|
121
|
-
echo "::notice::Published new version ${{ env.VERSION }} to npm."
|
|
122
|
-
env:
|
|
123
|
-
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
124
|
-
|
|
125
|
-
- name: Notice Publish Status
|
|
126
|
-
if: env.VERSION_EXISTS == 'true'
|
|
127
|
-
run: echo "::notice::Version ${{ env.VERSION }} already exists on npm. No new version published."
|
|
128
|
-
|
|
129
|
-
|
package/babel.config.json
DELETED
package/test/injection.test.js
DELETED
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
import {Factory, Inject, Singleton} from '../index.js'
|
|
2
|
-
|
|
3
|
-
describe('Injection via fields', () => {
|
|
4
|
-
@Singleton()
|
|
5
|
-
class TestSingleton {
|
|
6
|
-
static calls = 0
|
|
7
|
-
|
|
8
|
-
constructor() {
|
|
9
|
-
TestSingleton.calls++
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
it('should inject singleton', () => {
|
|
14
|
-
class TestInjection {
|
|
15
|
-
@Inject(TestSingleton) testSingleton
|
|
16
|
-
|
|
17
|
-
constructor() {
|
|
18
|
-
expect(this.testSingleton).toBeInstanceOf(TestSingleton)
|
|
19
|
-
expect(TestSingleton.calls).toBe(1)
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
class TestInjection2 {
|
|
24
|
-
@Inject(TestSingleton) testSingleton
|
|
25
|
-
|
|
26
|
-
constructor() {
|
|
27
|
-
expect(this.testSingleton).toBeInstanceOf(TestSingleton)
|
|
28
|
-
expect(TestSingleton.calls).toBe(1)
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
new TestInjection()
|
|
33
|
-
new TestInjection2()
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
@Factory()
|
|
37
|
-
class TestFactory {
|
|
38
|
-
static calls = 0
|
|
39
|
-
params
|
|
40
|
-
|
|
41
|
-
@Inject(TestSingleton) testSingleton
|
|
42
|
-
|
|
43
|
-
constructor(...params) {
|
|
44
|
-
TestFactory.calls++
|
|
45
|
-
this.params = params
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
it('should inject factory', () => {
|
|
50
|
-
class TestInjectionFactory {
|
|
51
|
-
@Inject(TestFactory) testFactory
|
|
52
|
-
|
|
53
|
-
constructor() {
|
|
54
|
-
expect(this.testFactory).toBeInstanceOf(TestFactory)
|
|
55
|
-
expect(TestFactory.calls).toBe(1)
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
class TestInjectionFactory2 {
|
|
60
|
-
@Inject(TestFactory) testFactory
|
|
61
|
-
|
|
62
|
-
constructor() {
|
|
63
|
-
expect(this.testFactory).toBeInstanceOf(TestFactory)
|
|
64
|
-
expect(TestFactory.calls).toBe(2)
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const result = new TestInjectionFactory()
|
|
69
|
-
new TestInjectionFactory2()
|
|
70
|
-
expect(result.testFactory.testSingleton).toBeInstanceOf(TestSingleton)
|
|
71
|
-
})
|
|
72
|
-
|
|
73
|
-
it('should inject factory with parameters', () => {
|
|
74
|
-
class TestInjectionFactoryParams {
|
|
75
|
-
@Inject(TestFactory, 'param1', 'param2') testFactory
|
|
76
|
-
|
|
77
|
-
constructor() {
|
|
78
|
-
expect(this.testFactory).toBeInstanceOf(TestFactory)
|
|
79
|
-
expect(this.testFactory.params).toEqual(['param1', 'param2'])
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
new TestInjectionFactoryParams()
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
@Singleton("named")
|
|
87
|
-
class NamedSingleton {
|
|
88
|
-
static calls = 0
|
|
89
|
-
|
|
90
|
-
constructor() {
|
|
91
|
-
NamedSingleton.calls++
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
it('should inject named singleton', () => {
|
|
96
|
-
class TestInjectionNamedSingleton {
|
|
97
|
-
@Inject("named") namedSingleton
|
|
98
|
-
|
|
99
|
-
constructor() {
|
|
100
|
-
expect(this.namedSingleton).toBeInstanceOf(NamedSingleton)
|
|
101
|
-
expect(NamedSingleton.calls).toBe(1)
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
class TestInjectionNamedSingleton2 {
|
|
106
|
-
@Inject("named") namedSingleton
|
|
107
|
-
|
|
108
|
-
constructor() {
|
|
109
|
-
expect(this.namedSingleton).toBeInstanceOf(NamedSingleton)
|
|
110
|
-
expect(NamedSingleton.calls).toBe(1)
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
new TestInjectionNamedSingleton()
|
|
115
|
-
new TestInjectionNamedSingleton2()
|
|
116
|
-
})
|
|
117
|
-
|
|
118
|
-
@Factory("named2")
|
|
119
|
-
class NamedFactory {
|
|
120
|
-
static calls = 0
|
|
121
|
-
params
|
|
122
|
-
|
|
123
|
-
constructor(...params) {
|
|
124
|
-
NamedFactory.calls++
|
|
125
|
-
this.params = params
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
it('should inject named factory', () => {
|
|
130
|
-
class TestInjectionNamedFactory {
|
|
131
|
-
@Inject("named2") namedFactory
|
|
132
|
-
|
|
133
|
-
constructor() {
|
|
134
|
-
expect(this.namedFactory).toBeInstanceOf(NamedFactory)
|
|
135
|
-
expect(NamedFactory.calls).toBe(1)
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
class TestInjectionNamedFactory2 {
|
|
140
|
-
@Inject("named2") namedFactory
|
|
141
|
-
|
|
142
|
-
constructor() {
|
|
143
|
-
expect(this.namedFactory).toBeInstanceOf(NamedFactory)
|
|
144
|
-
expect(NamedFactory.calls).toBe(2)
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const result = new TestInjectionNamedFactory()
|
|
149
|
-
new TestInjectionNamedFactory2()
|
|
150
|
-
expect(result.namedFactory.params).toEqual([])
|
|
151
|
-
})
|
|
152
|
-
})
|
package/test/mock.test.js
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import {resetMocks, Inject, Mock, Singleton, Factory} from '../index.js'
|
|
2
|
-
|
|
3
|
-
describe('Mocking', () => {
|
|
4
|
-
@Singleton()
|
|
5
|
-
class ToBeMockedSingleton {
|
|
6
|
-
op() {
|
|
7
|
-
return 'original'
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
op2() {
|
|
11
|
-
return 'original2'
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
class TestInjection {
|
|
16
|
-
@Inject(ToBeMockedSingleton) toBeMockedSingleton
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
afterEach(() => {
|
|
20
|
-
resetMocks()
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
it('should inject a mock singleton', () => {
|
|
24
|
-
@Mock(ToBeMockedSingleton)
|
|
25
|
-
class MockedSingleton {
|
|
26
|
-
op() {
|
|
27
|
-
return 'mocked'
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const result = new TestInjection()
|
|
32
|
-
expect(result.toBeMockedSingleton.op()).toBe('mocked')
|
|
33
|
-
expect(result.toBeMockedSingleton.op2).toBe.undefined
|
|
34
|
-
|
|
35
|
-
resetMocks()
|
|
36
|
-
const result2 = new TestInjection()
|
|
37
|
-
expect(result2.toBeMockedSingleton.op()).toBe('original')
|
|
38
|
-
expect(result2.toBeMockedSingleton.op2()).toBe('original2')
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
@Factory()
|
|
42
|
-
class ToBeMockedFactory {
|
|
43
|
-
op() {
|
|
44
|
-
return 'original'
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
class TestInjectionFactory {
|
|
49
|
-
@Inject(ToBeMockedFactory) toBeMockedFactory
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
it('should inject a mock factory', () => {
|
|
53
|
-
@Mock(ToBeMockedFactory)
|
|
54
|
-
class MockedFactory {
|
|
55
|
-
op() {
|
|
56
|
-
return 'mocked'
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const result = new TestInjectionFactory()
|
|
61
|
-
expect(result.toBeMockedFactory.op()).toBe('mocked')
|
|
62
|
-
|
|
63
|
-
resetMocks()
|
|
64
|
-
const result2 = new TestInjectionFactory()
|
|
65
|
-
expect(result2.toBeMockedFactory.op()).toBe('original')
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
it('should throw an error if a mock is not a singleton or factory', () => {
|
|
69
|
-
expect(() => {
|
|
70
|
-
@Mock(ToBeMockedFactory)
|
|
71
|
-
class Mocked1 {}
|
|
72
|
-
|
|
73
|
-
@Mock(ToBeMockedFactory)
|
|
74
|
-
class Mocked2 {}
|
|
75
|
-
}).toThrow('Mock already defined, reset before mocking again')
|
|
76
|
-
})
|
|
77
|
-
})
|
package/test/proxy.test.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import {resetMocks, Inject, Mock, Singleton, Factory} from '../index.js'
|
|
2
|
-
|
|
3
|
-
describe('Proxy Mocking', () => {
|
|
4
|
-
@Singleton()
|
|
5
|
-
class ToBeProxiedSingleton {
|
|
6
|
-
op() {
|
|
7
|
-
return 'original'
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
op2() {
|
|
11
|
-
return 'original2'
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
class TestInjection {
|
|
16
|
-
@Inject(ToBeProxiedSingleton) toBeProxiedSingleton
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
afterEach(() => {
|
|
20
|
-
resetMocks()
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
it('should inject a proxy singleton', () => {
|
|
24
|
-
@Mock(ToBeProxiedSingleton, true)
|
|
25
|
-
class ProxiedSingleton {
|
|
26
|
-
op() {
|
|
27
|
-
return 'mocked'
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const result = new TestInjection()
|
|
32
|
-
expect(result.toBeProxiedSingleton.op()).toBe('mocked')
|
|
33
|
-
expect(result.toBeProxiedSingleton.op2()).toBe('original2')
|
|
34
|
-
|
|
35
|
-
resetMocks()
|
|
36
|
-
const result2 = new TestInjection()
|
|
37
|
-
expect(result2.toBeProxiedSingleton.op()).toBe('original')
|
|
38
|
-
expect(result2.toBeProxiedSingleton.op2()).toBe('original2')
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
@Factory()
|
|
42
|
-
class ToBeProxiedFactory {
|
|
43
|
-
op() {
|
|
44
|
-
return 'original'
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
op2() {
|
|
48
|
-
return 'original2'
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
class TestInjectionFactory {
|
|
53
|
-
@Inject(ToBeProxiedFactory) toBeProxiedFactory
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
it('should inject a proxy factory', () => {
|
|
57
|
-
@Mock(ToBeProxiedFactory, true)
|
|
58
|
-
class ProxiedFactory {
|
|
59
|
-
op() {
|
|
60
|
-
return 'mocked'
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const result = new TestInjectionFactory()
|
|
65
|
-
expect(result.toBeProxiedFactory.op()).toBe('mocked')
|
|
66
|
-
expect(result.toBeProxiedFactory.op2()).toBe('original2')
|
|
67
|
-
|
|
68
|
-
resetMocks()
|
|
69
|
-
const result2 = new TestInjectionFactory()
|
|
70
|
-
expect(result2.toBeProxiedFactory.op()).toBe('original')
|
|
71
|
-
expect(result2.toBeProxiedFactory.op2()).toBe('original2')
|
|
72
|
-
})
|
|
73
|
-
})
|