-
Notifications
You must be signed in to change notification settings - Fork 38
feat: support optional inject #254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8a45b8f
feat: support optional inject
gxkl 45320f0
feat: support optional inject in constructor
gxkl 088cc68
test: unit test for optioanl inject
gxkl 7f318b8
fix: type
gxkl 753be08
docs: add optional inject
gxkl facba7e
f
gxkl 22f59d4
f
gxkl e44fe71
f
gxkl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
core/metadata/test/fixtures/modules/optional-inject-module/OptionalInjectService.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { Inject, InjectOptional, SingletonProto } from '@eggjs/core-decorator'; | ||
|
|
||
| interface PersistenceService { | ||
| } | ||
|
|
||
| @SingletonProto() | ||
| export default class OptionalInjectService { | ||
| @Inject({ optional: true }) | ||
| persistenceService: PersistenceService; | ||
|
|
||
| @InjectOptional() | ||
| persistenceService2: PersistenceService; | ||
| } |
6 changes: 6 additions & 0 deletions
6
core/metadata/test/fixtures/modules/optional-inject-module/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "name": "optional-inject-service", | ||
| "eggModule": { | ||
| "name": "optionalInjectService" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| export interface InjectParams { | ||
| // obj instance name, default is property name | ||
| name?: string; | ||
| // optional inject, default is false which means it will throw error when there is no relative object | ||
| optional?: boolean; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import mm from 'egg-mock'; | ||
| import path from 'path'; | ||
| import assert from 'assert'; | ||
| import { BarService } from './fixtures/apps/optional-inject/app/modules/module-a/BarService'; | ||
| import { FooService } from './fixtures/apps/optional-inject/app/modules/module-a/FooService'; | ||
|
|
||
| describe('plugin/tegg/test/Inject.test.ts', () => { | ||
| let app; | ||
|
|
||
| beforeEach(async () => { | ||
| mm(process.env, 'EGG_TYPESCRIPT', true); | ||
| mm(process, 'cwd', () => { | ||
| return path.join(__dirname, '..'); | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await app.close(); | ||
| mm.restore(); | ||
| }); | ||
|
|
||
| describe('optional', () => { | ||
| beforeEach(async () => { | ||
| app = mm.app({ | ||
| baseDir: path.join(__dirname, 'fixtures/apps/optional-inject'), | ||
| framework: require.resolve('egg'), | ||
| }); | ||
| await app.ready(); | ||
| }); | ||
|
|
||
| it('should work with property', async () => { | ||
| const barService: BarService = await app.getEggObject(BarService); | ||
| const res = barService.bar(); | ||
| assert.deepStrictEqual(res, { | ||
| nil1: 'Y', | ||
| nil2: 'Y', | ||
| }); | ||
| }); | ||
|
|
||
| it('should work with constructor', async () => { | ||
| const fooService: FooService = await app.getEggObject(FooService); | ||
| const res = fooService.foo(); | ||
| assert.deepStrictEqual(res, { | ||
| nil1: 'Y', | ||
| nil2: 'Y', | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| it('should throw error if no proto found', async () => { | ||
| app = mm.app({ | ||
| baseDir: path.join(__dirname, 'fixtures/apps/invalid-inject'), | ||
| framework: require.resolve('egg'), | ||
| }); | ||
| await assert.rejects( | ||
| app.ready(), | ||
| /EggPrototypeNotFound: Object doesNotExist not found in LOAD_UNIT:a/, | ||
| ); | ||
| }); | ||
| }); |
11 changes: 11 additions & 0 deletions
11
plugin/tegg/test/fixtures/apps/invalid-inject/app/modules/module-a/BarService.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { SingletonProto, Inject } from '@eggjs/tegg'; | ||
|
|
||
| @SingletonProto() | ||
| export class BarService { | ||
| @Inject() | ||
| doesNotExist: object; | ||
|
|
||
| bar() { | ||
| console.log(this.doesNotExist); | ||
| } | ||
| } |
6 changes: 6 additions & 0 deletions
6
plugin/tegg/test/fixtures/apps/invalid-inject/app/modules/module-a/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "name": "module-a", | ||
| "eggModule": { | ||
| "name": "a" | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
plugin/tegg/test/fixtures/apps/invalid-inject/config/config.default.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| 'use strict'; | ||
|
|
||
| const path = require('path'); | ||
|
|
||
| module.exports = function(appInfo) { | ||
| const config = { | ||
| keys: 'test key', | ||
| customLogger: { | ||
| xxLogger: { | ||
| file: path.join(appInfo.root, 'logs/xx.log'), | ||
| }, | ||
| }, | ||
| security: { | ||
| csrf: { | ||
| ignoreJSON: false, | ||
| }, | ||
| }, | ||
| }; | ||
| return config; | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.