-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Implement injectable lifecycle hooks for strategies
Relates to #303
- Loading branch information
1 parent
65a113b
commit 451caf1
Showing
14 changed files
with
200 additions
and
48 deletions.
There are no files selected for viewing
This file contains 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,52 @@ | ||
import { Injector } from '@vendure/core'; | ||
import { createTestEnvironment } from '@vendure/testing'; | ||
import path from 'path'; | ||
|
||
import { initialData } from '../../../e2e-common/e2e-initial-data'; | ||
import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config'; | ||
import { AutoIncrementIdStrategy } from '../src/config/entity-id-strategy/auto-increment-id-strategy'; | ||
import { ProductService } from '../src/service/services/product.service'; | ||
|
||
const initSpy = jest.fn(); | ||
const destroySpy = jest.fn(); | ||
|
||
class TestIdStrategy extends AutoIncrementIdStrategy { | ||
async init(injector: Injector) { | ||
const productService = injector.get(ProductService); | ||
const connection = injector.getConnection(); | ||
await new Promise(resolve => setTimeout(resolve, 100)); | ||
initSpy(productService.constructor.name, connection.name); | ||
} | ||
|
||
async destroy() { | ||
await new Promise(resolve => setTimeout(resolve, 100)); | ||
destroySpy(); | ||
} | ||
} | ||
|
||
describe('lifecycle hooks for configurable objects', () => { | ||
const { server, adminClient } = createTestEnvironment({ | ||
...testConfig, | ||
entityIdStrategy: new TestIdStrategy(), | ||
}); | ||
|
||
beforeAll(async () => { | ||
await server.init({ | ||
initialData, | ||
productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-empty.csv'), | ||
customerCount: 1, | ||
}); | ||
await adminClient.asSuperAdmin(); | ||
}, TEST_SETUP_TIMEOUT_MS); | ||
|
||
it('runs init with Injector', () => { | ||
expect(initSpy).toHaveBeenCalled(); | ||
expect(initSpy.mock.calls[0][0]).toEqual('ProductService'); | ||
expect(initSpy.mock.calls[0][1]).toBe('default'); | ||
}); | ||
|
||
it('runs destroy', async () => { | ||
await server.destroy(); | ||
expect(destroySpy).toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains 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 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,3 +1,4 @@ | ||
export * from './async-queue'; | ||
export * from './error/errors'; | ||
export * from './injector'; | ||
export * from './utils'; | ||
export * from './async-queue'; |
This file contains 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,44 @@ | ||
import { Type } from '@nestjs/common'; | ||
import { ContextId, ModuleRef } from '@nestjs/core'; | ||
import { getConnectionToken } from '@nestjs/typeorm'; | ||
import { Connection } from 'typeorm'; | ||
|
||
/** | ||
* @description | ||
* The Injector wraps the underlying Nestjs `ModuleRef`, allowing injection of providers | ||
* known to the application's dependency injection container. This is intended to enable the injection | ||
* of services into objects which exist outside of the Nestjs module system, e.g. the various | ||
* Strategies which can be supplied in the VendureConfig. | ||
* | ||
* @docsCategory common | ||
*/ | ||
export class Injector { | ||
constructor(private moduleRef: ModuleRef) {} | ||
|
||
/** | ||
* @description | ||
* Retrieve an instance of the given type from the app's dependency injection container. | ||
* Wraps the Nestjs `ModuleRef.get()` method. | ||
*/ | ||
get<T, R = T>(typeOrToken: Type<T> | string | symbol): R { | ||
return this.moduleRef.get(typeOrToken, { strict: false }); | ||
} | ||
|
||
/** | ||
* @description | ||
* Retrieve the TypeORM `Connection` instance. | ||
*/ | ||
getConnection(): Connection { | ||
return this.moduleRef.get(getConnectionToken() as any, { strict: false }); | ||
} | ||
|
||
/** | ||
* @description | ||
* Retrieve an instance of the given scoped provider (transient or request-scoped) from the | ||
* app's dependency injection container. | ||
* Wraps the Nestjs `ModuleRef.resolve()` method. | ||
*/ | ||
resolve<T, R = T>(typeOrToken: Type<T> | string | symbol, contextId?: ContextId): Promise<R> { | ||
return this.moduleRef.resolve(typeOrToken, contextId, { strict: false }); | ||
} | ||
} |
This file contains 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,32 @@ | ||
import { Injector } from '../injector'; | ||
|
||
/** | ||
* @description | ||
* This interface defines the setup and teardown hooks available to the | ||
* various strategies used to configure Vendure. | ||
* | ||
* @docsCategory common | ||
*/ | ||
export interface InjectableStrategy { | ||
/** | ||
* @description | ||
* Defines setup logic to be run during application bootstrap. Receives | ||
* the {@link Injector} as an argument, which allows application providers | ||
* to be used as part of the setup. | ||
* | ||
* @example | ||
* ```TypeScript | ||
* async init(injector: Injector) { | ||
* const myService = injector.get(MyService); | ||
* await myService.doSomething(); | ||
* } | ||
* ``` | ||
*/ | ||
init?: (injector: Injector) => void | Promise<void>; | ||
|
||
/** | ||
* @description | ||
* Defines teardown logic to be run before application shutdown. | ||
*/ | ||
destroy?: () => void | Promise<void>; | ||
} |
4 changes: 3 additions & 1 deletion
4
packages/core/src/config/asset-naming-strategy/asset-naming-strategy.ts
This file contains 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 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 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 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 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 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 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 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 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